This repository was archived by the owner on May 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
150 lines (135 loc) · 2.39 KB
/
main_test.go
File metadata and controls
150 lines (135 loc) · 2.39 KB
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
package main
import (
"bytes"
"io/ioutil"
"strings"
"testing"
codestyle "github.com/Konstantin8105/cs"
)
func Test(t *testing.T) {
*inputFile = "./testdata/test.gotmpl"
t.Run("list", func(t *testing.T) {
*listFlag = true
defer func() {
*listFlag = false
}()
f, err := ioutil.TempFile("", "list")
if err != nil {
t.Fatal(err)
}
tmp := osStdout
osStdout = f
defer func() {
osStdout = tmp
}()
err = list()
if err != nil {
t.Fatal(err)
}
filename := f.Name()
err = f.Close()
if err != nil {
t.Fatal(err)
}
b, err := ioutil.ReadFile(filename)
if err != nil {
t.Fatal(err)
}
s1 := string(b)
s2 := "Preprocessor names :\n* Float32\n* Float64\n* pre1\n* pre2\n"
if strings.Compare(s1, s2) != 0 {
t.Fatalf("Not equal:\n%s\n%s", s1, s2)
}
})
t.Run("preprocessors", func(t *testing.T) {
tcs := []struct {
outputFile string
pres string
}{
{
outputFile: "./testdata/pre1.go",
pres: "pre1",
},
{
outputFile: "./testdata/pre2.go",
pres: "pre2",
},
{
outputFile: "./testdata/f32.go",
pres: "Float32",
},
{
outputFile: "./testdata/f64.go",
pres: "Float64",
},
}
defer func() {
*outputFile = ""
*pres = ""
}()
for _, tc := range tcs {
t.Run(tc.pres, func(t *testing.T) {
*outputFile = tc.outputFile
*pres = tc.pres
err := change()
if err != nil {
t.Fatal(err)
}
// compare results
out, err := ioutil.ReadFile(tc.outputFile)
if err != nil {
t.Fatal(err)
}
exp, err := ioutil.ReadFile(tc.outputFile + ".expect")
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(out, exp) {
t.Fatalf("result is not same")
}
})
}
})
}
func TestIfDef(t *testing.T) {
tcs := []struct {
line, ps string
has, found bool
}{
{
line: "// #ifdef FL",
ps: "FL",
has: true,
found: true,
},
{
line: "// #ifdef FL FL2 DL",
ps: "FL",
has: true,
found: true,
},
{
line: "// #ifdef FL3 FL2 DL1",
ps: "FL",
has: true,
found: false,
},
{
line: "// #ifdef FL3 FL DL",
ps: "FL",
has: true,
found: true,
},
}
for _, tc := range tcs {
t.Run(tc.line, func(t *testing.T) {
has, _ := hasIfdef(tc.line, tc.ps)
if has != tc.has {
t.Errorf("not same")
}
})
}
}
func TestCodeStyle(t *testing.T) {
codestyle.All(t)
}