forked from gravityblast/plate
/
main_test.go
84 lines (67 loc) · 1.8 KB
/
main_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
package main
import (
"io/ioutil"
logPkg "log"
"os"
"testing"
assert "github.com/pilu/miniassert"
)
func init() {
log.verbose = false
}
func TestNewPlate(t *testing.T) {
p := newPlate("foo", "bar")
assert.Equal(t, "foo", p.srcPath)
assert.Equal(t, "bar", p.outPath)
}
func TestPlate_BuildTemplatePath(t *testing.T) {
p := newPlate("foo", "bar")
assert.Equal(t, "foo/go.plate", p.buildTemplatePath("go"))
}
func TestPlate_BuildOutPath(t *testing.T) {
p := newPlate("foo", "bar")
assert.Equal(t, "bar/tmp/file.go", p.buildOutPath("tmp/file.go"))
}
func TestPlate_OpenTemplate(t *testing.T) {
p := newPlate("__test-fixtures__", "tmp")
mainTpl, err := p.openTemplate("go")
assert.NotNil(t, mainTpl)
assert.Nil(t, err)
assert.Equal(t, 6, len(mainTpl.Templates()))
names := []string{"main.go", "main_test.go", "config/app.config", "bin/test_command.sh"}
for _, name := range names {
tpl := mainTpl.Lookup(name)
assert.NotNil(t, tpl)
}
}
func TestPlate_OpenTemplate_template_not_found(t *testing.T) {
p := newPlate("__test-fixtures__", "tmp")
_, err := p.openTemplate("random-name")
assert.NotNil(t, err)
}
func TestPlate_Execute(t *testing.T) {
defer os.RemoveAll("__test-data__")
p := newPlate("__test-fixtures__", "__test-data__")
p.execute("go", []string{"program-name", "outpath", "World"}...)
paths := map[string]string{
"__test-data__/main.go": `package main
import "fmt"
func main() {
fmt.Println("Hello World")
}`,
"__test-data__/main_test.go": `package main
import "testing"
func TestFoo(t *testing.T) {
}`,
"__test-data__/config/app.config": `config file`,
}
for path, expectedContent := range paths {
f, err := os.Open(path)
assert.Nil(t, err)
content, err := ioutil.ReadAll(f)
if err != nil {
logPkg.Fatal(err)
}
assert.Equal(t, expectedContent, string(content))
}
}