forked from clipperhouse/gen
/
list_test.go
67 lines (53 loc) · 1.42 KB
/
list_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
package main
import (
"bytes"
"os"
"testing"
)
func TestList(t *testing.T) {
// use buffer instead of Stdout so we can inspect the results
var b bytes.Buffer
setOutput(&b)
defer revertOutput()
// use custom name so test won't interfere with a real _gen.go
setCustomName("_gen_test.go")
defer revertCustomName()
// remove existing files, start fresh
os.Remove(customName)
// standard
if err := runMain([]string{"gen", "list"}); err != nil {
t.Error(err)
}
// one line for title, 2 standard typewriters
if lines := bytes.Count(b.Bytes(), []byte("\n")); lines != 3 {
t.Errorf("standard list should output 3 lines, got %v", lines)
}
// clear out the output buffer
b.Reset()
// create a custom typewriter import file
w, err := os.Create(customName)
if err != nil {
t.Error(err)
}
defer os.Remove(customName)
p := pkg{
Name: "main",
Imports: []string{
// non-standard typewriter
`_ "github.com/clipperhouse/gen/typewriters/foowriter"`,
`_ "github.com/clipperhouse/gen/typewriters/genwriter"`,
`_ "github.com/clipperhouse/gen/typewriters/container"`,
},
}
if err := tmpl.Execute(w, p); err != nil {
t.Error(err)
}
// custom file now exists
if err := runMain([]string{"gen", "list"}); err != nil {
t.Error(err)
}
// one line for title, 3 custom typewriters
if lines := bytes.Count(b.Bytes(), []byte("\n")); lines != 4 {
t.Errorf("standard list should output 4 lines, got %v", lines)
}
}