1 | package main
|
---|
2 |
|
---|
3 | import (
|
---|
4 | "bytes"
|
---|
5 | "fmt"
|
---|
6 | "io/ioutil"
|
---|
7 | "log"
|
---|
8 | "os"
|
---|
9 | "strings"
|
---|
10 | "testing"
|
---|
11 | )
|
---|
12 |
|
---|
13 | func TestSplit2(t *testing.T) {
|
---|
14 | if a, b := split2("a:b", ":"); a != "a" || b != "b" {
|
---|
15 | t.Fail()
|
---|
16 | }
|
---|
17 | if a, b := split2(":b", ":"); a != "" || b != "b" {
|
---|
18 | t.Fail()
|
---|
19 | }
|
---|
20 | if a, b := split2("a:", ":"); a != "a" || b != "" {
|
---|
21 | t.Fail()
|
---|
22 | }
|
---|
23 | if a, b := split2(":", ":"); a != "" || b != "" {
|
---|
24 | t.Fail()
|
---|
25 | }
|
---|
26 | if a, b := split2("a", ":"); a != "a" || b != "" {
|
---|
27 | t.Fail()
|
---|
28 | }
|
---|
29 | if a, b := split2("", ":"); a != "" || b != "" {
|
---|
30 | t.Fail()
|
---|
31 | }
|
---|
32 | }
|
---|
33 |
|
---|
34 | func tmpfile(path, s string) string {
|
---|
35 | ioutil.WriteFile(path, []byte(s), 0644)
|
---|
36 | return path
|
---|
37 | }
|
---|
38 |
|
---|
39 | func TestMD(t *testing.T) {
|
---|
40 | defer os.Remove("foo.md")
|
---|
41 | v, body, _ := md(tmpfile("foo.md", `
|
---|
42 | title: Hello, world!
|
---|
43 | keywords: foo, bar, baz
|
---|
44 | empty:
|
---|
45 | bayan: [:|||:]
|
---|
46 |
|
---|
47 | this: is a content`), Vars{})
|
---|
48 | if v["title"] != "Hello, world!" {
|
---|
49 | t.Error()
|
---|
50 | }
|
---|
51 | if v["keywords"] != "foo, bar, baz" {
|
---|
52 | t.Error()
|
---|
53 | }
|
---|
54 | if s, ok := v["empty"]; !ok || len(s) != 0 {
|
---|
55 | t.Error()
|
---|
56 | }
|
---|
57 | if v["bayan"] != "[:|||:]" {
|
---|
58 | t.Error()
|
---|
59 | }
|
---|
60 | if body != "this: is a content" {
|
---|
61 | t.Error(body)
|
---|
62 | }
|
---|
63 |
|
---|
64 | // Test empty md
|
---|
65 | v, body, _ = md(tmpfile("foo.md", ""), Vars{})
|
---|
66 | if v["url"] != "foo.html" || len(body) != 0 {
|
---|
67 | t.Error(v, body)
|
---|
68 | }
|
---|
69 |
|
---|
70 | // Test empty header
|
---|
71 | v, body, _ = md(tmpfile("foo.md", "Hello"), Vars{})
|
---|
72 | if v["url"] != "foo.html" || body != "Hello" {
|
---|
73 | t.Error(v, body)
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | func TestRender(t *testing.T) {
|
---|
78 | vars := map[string]string{"foo": "bar"}
|
---|
79 | funcs := Funcs{
|
---|
80 | "greet": func(s ...string) string {
|
---|
81 | if len(s) == 0 {
|
---|
82 | return "hello"
|
---|
83 | } else {
|
---|
84 | return "hello " + strings.Join(s, " ")
|
---|
85 | }
|
---|
86 | },
|
---|
87 | }
|
---|
88 |
|
---|
89 | if s, err := render("plain text", funcs, vars); err != nil || s != "plain text" {
|
---|
90 | t.Error(s, err)
|
---|
91 | }
|
---|
92 | if s, err := render("a {{greet}} text", funcs, vars); err != nil || s != "a hello text" {
|
---|
93 | t.Error(s, err)
|
---|
94 | }
|
---|
95 | if s, err := render("{{greet}} x{{foo}}z", funcs, vars); err != nil || s != "hello xbarz" {
|
---|
96 | t.Error(s, err)
|
---|
97 | }
|
---|
98 | // Test error case
|
---|
99 | if s, err := render("a {{greet text ", funcs, vars); err == nil || len(s) != 0 {
|
---|
100 | t.Error(s, err)
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | func TestEnv(t *testing.T) {
|
---|
105 | e := env(map[string]string{"foo": "bar", "baz": "hello world"})
|
---|
106 | mustHave := []string{"ZS=" + os.Args[0], "ZS_FOO=bar", "ZS_BAZ=hello world", "PATH="}
|
---|
107 | for _, s := range mustHave {
|
---|
108 | found := false
|
---|
109 | for _, v := range e {
|
---|
110 | if strings.HasPrefix(v, s) {
|
---|
111 | found = true
|
---|
112 | break
|
---|
113 | }
|
---|
114 | }
|
---|
115 | if !found {
|
---|
116 | t.Error("Missing", s)
|
---|
117 | }
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | func TestRun(t *testing.T) {
|
---|
122 | out := bytes.NewBuffer(nil)
|
---|
123 | err := run("some_unbelievable_command_name", []string{}, map[string]string{}, out)
|
---|
124 | if err == nil {
|
---|
125 | t.Error()
|
---|
126 | }
|
---|
127 |
|
---|
128 | out = bytes.NewBuffer(nil)
|
---|
129 | err = run(os.Args[0], []string{"-test.run=TestHelperProcess"},
|
---|
130 | map[string]string{"helper": "1", "out": "foo", "err": "bar"}, out)
|
---|
131 | if err != nil {
|
---|
132 | t.Error(err)
|
---|
133 | }
|
---|
134 | if out.String() != "foo\n" {
|
---|
135 | t.Error(out.String())
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | func TestHelperProcess(*testing.T) {
|
---|
140 | if os.Getenv("ZS_HELPER") != "1" {
|
---|
141 | return
|
---|
142 | }
|
---|
143 | defer os.Exit(0) // TODO check exit code
|
---|
144 | log.Println(os.Getenv("ZS_ERR")) // stderr
|
---|
145 | fmt.Println(os.Getenv("ZS_OUT")) // stdout
|
---|
146 | }
|
---|