source: code/trunk/zs_test.go@ 10

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

rewritted default variables assignment

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