-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser_test.go
170 lines (141 loc) · 3.79 KB
/
parser_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
DTDParser "github.com/blefort/DTDParser/parser"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const dirTest = "tests/"
var overwrite bool
var log *zap.SugaredLogger
// TestMain Test Initialization
func TestMain(m *testing.M) {
var level zap.AtomicLevel
verbosity := flag.String("verbose", "v", "Verbose v, vv or vvv")
overwriteF := flag.Bool("overwrite", false, "Overwrite output file")
if *verbosity == "v" {
level = zap.NewAtomicLevelAt(zap.WarnLevel)
} else if *verbosity == "vv" {
level = zap.NewAtomicLevelAt(zap.InfoLevel)
} else if *verbosity == "vvv" {
level = zap.NewAtomicLevelAt(zap.DebugLevel)
} else {
level = zap.NewAtomicLevelAt(zap.FatalLevel)
}
level = zap.NewAtomicLevelAt(zap.DebugLevel)
flag.Parse()
cfg := zap.Config{
Level: level,
Development: false,
DisableCaller: true,
DisableStacktrace: true,
Sampling: nil,
Encoding: "json",
EncoderConfig: zapcore.EncoderConfig{
MessageKey: "m",
LevelKey: "",
TimeKey: "",
NameKey: "",
CallerKey: "",
StacktraceKey: "stack",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
EncodeName: zapcore.FullNameEncoder,
},
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stdout"},
}
cfg.EncoderConfig.TimeKey = zapcore.OmitKey
logger, err := cfg.Build()
if err != nil {
panic(err)
}
defer logger.Sync() // flushes buffer, if any
log = logger.Sugar()
if *overwriteF {
overwrite = true
}
os.Exit(m.Run())
}
// loadJSON load a json file and return the result in v
func loadJSON(file string, v interface{}) {
filebuffer, err := ioutil.ReadFile(file)
if err != nil {
panic(err)
}
// parse file
errj := json.Unmarshal(filebuffer, v)
if errj != nil {
panic(errj)
}
}
// newParser() Instantiate parser and configure it
func newParser(dir string) *DTDParser.Parser {
fmt.Sprintf("new parser %p", log)
// New parser
p := DTDParser.NewDTDParser(log)
// Configure parser
p.WithComments = true
p.IgnoreExtRefIssue = true
p.SetFormatter("DTD")
p.SetOutputPath(dir)
if overwrite {
p.Overwrite = overwrite
}
return p
}
// Render
func render(p *DTDParser.Parser) func(*testing.T) {
return func(t *testing.T) {
fmt.Printf("pointer: %p", log)
p.Render("")
}
}
/**
* Helpers
*/
// checkStrValue Check if the block found from the parser has the expected value
func checkStrValue(a string, b string, i interface{}, f interface{}) func(*testing.T) {
return func(t *testing.T) {
if a != b {
ac := strings.ReplaceAll(a, "\n", "\\n")
bc := strings.ReplaceAll(b, "\n", "\\n")
t.Errorf("Received wrong value, '%s' instead of '%s' - %+v - %+v", ac, bc, i, f)
}
}
}
// checkBoolValue Check if the block found from the parser has the expected value
func checkBoolValue(a bool, b bool, i interface{}, f interface{}) func(*testing.T) {
return func(t *testing.T) {
if a != b {
t.Errorf("Received wrong bool value, '%t' instead of '%t' - %+v - %+v", a, b, i, f)
}
}
}
// checkIntValue Check if the block found from the parser has the expected value
func checkIntValue(a int, b int, i interface{}, f interface{}) func(*testing.T) {
return func(t *testing.T) {
if a != b {
t.Errorf("Received wrong int value, '%d' instead of '%d'- %+v- %+v", a, b, i, f)
}
}
}
// assertPanic Helper to test panic
// @see https://stackoverflow.com/questions/31595791/how-to-test-panics
func assertPanic(t *testing.T, f func()) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
f()
}