-
Notifications
You must be signed in to change notification settings - Fork 29
/
testutils.go
126 lines (114 loc) · 2.24 KB
/
testutils.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
package main
import (
"fmt"
"log"
"os"
"sort"
"strings"
"unicode"
)
type testDataType int
const (
// GENTESTEXPDATA to generate expected test data
GENTESTEXPDATA = iota
// GENTESTRESDATA to generate result test data
GENTESTRESDATA
)
func testSetup(jctx *JCtx) error {
file := jctx.file
if *genTestData {
var err error
if jctx.testMeta, err = os.Create(file + ".testmeta"); err != nil {
return err
}
if jctx.testBytes, err = os.Create(file + ".testbytes"); err != nil {
return err
}
if jctx.testExp, err = os.Create(file + ".testexp"); err != nil {
return err
}
}
return nil
}
func testTearDown(jctx *JCtx) {
if *genTestData {
if jctx.testMeta != nil {
jctx.testMeta.Close()
jctx.testMeta = nil
}
if jctx.testBytes != nil {
jctx.testBytes.Close()
jctx.testBytes = nil
}
if jctx.testExp != nil {
jctx.testExp.Close()
jctx.testExp = nil
}
}
}
func generateTestData(jctx *JCtx, data []byte) {
log.Printf("data len = %d", len(data))
if jctx.testMeta != nil {
d := fmt.Sprintf("%d:", len(data))
jctx.testMeta.WriteString(d)
}
if jctx.testBytes != nil {
jctx.testBytes.Write(data)
}
}
func testDataPoints(jctx *JCtx, testType testDataType, tags map[string]string, fields map[string]interface{}) {
var f *os.File
switch testType {
case GENTESTEXPDATA:
if jctx.testExp == nil {
return
}
f = jctx.testExp
case GENTESTRESDATA:
if jctx.testRes == nil {
return
}
f = jctx.testRes
default:
return
}
f.WriteString("TAGS: [")
var keys []string
for k := range tags {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v := tags[k]
f.WriteString(fmt.Sprintf(" %s=%s ", k, v))
}
f.WriteString("]\n")
f.WriteString("FIELDS: [")
keys = nil
for k := range fields {
// Skip the fields that need not be compared
if k == gDeviceTs {
continue
}
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v := fields[k]
f.WriteString(fmt.Sprintf("%s=%s", k, v))
}
f.WriteString("]\n")
f.Sync()
}
func compareString(a string, b string) bool {
filter := func(r rune) rune {
if unicode.IsSpace(r) {
return -1
}
return r
}
if strings.Compare(strings.Map(filter, a), strings.Map(filter, b)) == 0 {
return true
}
return false
}