source: code/trunk/zs_test.go@ 18

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

added amber and gcss compilers

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