Skip to content

Commit 6c12dd0

Browse files
author
xander
committed
Added a test to have enough coverage
1 parent a36b9c2 commit 6c12dd0

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed

global_arg_test.go

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"io/ioutil"
7+
"os"
8+
"path/filepath"
9+
"strings"
10+
"testing"
11+
)
12+
13+
type buildConstraintTest struct {
14+
Name string
15+
Args []string
16+
ExpectedLines []string
17+
}
18+
19+
var buildConstraintTests = []buildConstraintTest{
20+
{
21+
Name: "single build constraint",
22+
Args: []string{"-buildconstraint", "integrationtest"},
23+
ExpectedLines: []string{
24+
"// Code generated by \"enumer %s\"; DO NOT EDIT.",
25+
"//go:build integrationtest",
26+
"",
27+
"package main",
28+
},
29+
},
30+
{
31+
Name: "or build constraint",
32+
Args: []string{"-buildconstraint", "unittest || integrationtest"},
33+
ExpectedLines: []string{
34+
"// Code generated by \"enumer %s\"; DO NOT EDIT.",
35+
"//go:build unittest || integrationtest",
36+
"",
37+
"package main",
38+
},
39+
},
40+
{
41+
Name: "multi build constraint",
42+
Args: []string{"-buildconstraint", "integrationtest", "-buildconstraint", "unittest"},
43+
ExpectedLines: []string{
44+
"// Code generated by \"enumer %s\"; DO NOT EDIT.",
45+
"//go:build integrationtest",
46+
"//go:build unittest",
47+
"",
48+
"package main",
49+
},
50+
},
51+
{
52+
Name: "single comment",
53+
Args: []string{"-comment", "Some long comment explaining what this enum is all about..."},
54+
ExpectedLines: []string{
55+
"// Code generated by \"enumer %s\"; DO NOT EDIT.",
56+
"",
57+
"// Some long comment explaining what this enum is all about...",
58+
"package main",
59+
},
60+
},
61+
{
62+
Name: "multi comment",
63+
Args: []string{"-comment", "Some long comment", "-comment", "explaining what", "-comment", "", "-comment", " this enum is all about..."},
64+
ExpectedLines: []string{
65+
"// Code generated by \"enumer %s\"; DO NOT EDIT.",
66+
"",
67+
"// Some long commentexplaining what this enum is all about...",
68+
"package main",
69+
},
70+
},
71+
}
72+
73+
func TestGlobalArgs(t *testing.T) {
74+
const TypeName = "SnakeCaseValue"
75+
const InputFilename = "transform_snake.go"
76+
const TransformNameMethod = "SnakeCaseValue"
77+
78+
dir, err := ioutil.TempDir("", "stringer")
79+
if err != nil {
80+
t.Fatal(err)
81+
}
82+
defer os.RemoveAll(dir)
83+
84+
// Create stringer in temporary directory.
85+
stringer := filepath.Join(dir, fmt.Sprintf("stringer%s", GOEXE))
86+
err = run("go", "build", "-o", stringer)
87+
if err != nil {
88+
t.Fatalf("building stringer: %s", err)
89+
}
90+
91+
for _, test := range buildConstraintTests {
92+
stringSource := filepath.Join(dir, InputFilename)
93+
94+
// combine arguments
95+
var args []string
96+
args = append(args, "-type", TypeName, "-output", stringSource, "-transform", TransformNameMethod)
97+
args = append(args, test.Args...)
98+
args = append(args, filepath.Join("testdata/"+InputFilename))
99+
100+
// Run stringer in temporary directory.
101+
t.Logf("run: %s %v\n", stringer, args)
102+
err = run(stringer, args...)
103+
if err != nil {
104+
t.Fatal(err)
105+
}
106+
107+
actual := loadResult(t, stringSource, len(test.ExpectedLines))
108+
109+
if len(actual) != len(test.ExpectedLines) {
110+
t.Errorf("'%s': expected at least %d line in the output but found only %d",
111+
test.Name,
112+
len(test.ExpectedLines),
113+
len(actual),
114+
)
115+
}
116+
117+
for idx := 0; idx < len(test.ExpectedLines); idx++ {
118+
expected := test.ExpectedLines[idx]
119+
120+
// The first line is a special case as the contents is dynamic
121+
if idx == 0 {
122+
expected = fmt.Sprintf(expected, strings.Join(args, " "))
123+
}
124+
125+
if actual[idx] != expected {
126+
t.Errorf("'%s': expected line %d \n\tto be : %s\n\tbut was: %s",
127+
test.Name,
128+
idx,
129+
expected,
130+
actual[idx])
131+
}
132+
}
133+
}
134+
}
135+
136+
func loadResult(t *testing.T, filename string, lineCount int) []string {
137+
fh, err := os.Open(filename)
138+
if err != nil {
139+
t.Fatal(err)
140+
return nil
141+
}
142+
defer fh.Close()
143+
144+
lines := make([]string, 0)
145+
scanner := bufio.NewScanner(fh)
146+
for scanner.Scan() {
147+
lines = append(lines, scanner.Text())
148+
lineCount--
149+
if lineCount <= 0 {
150+
break
151+
}
152+
}
153+
154+
return lines
155+
}

0 commit comments

Comments
 (0)