source: code/trunk/zs_test.go@ 20

Last change on this file since 20 was 20, checked in by zaitsev.serge, 10 years ago

added global variables, added default date for markdown

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