Mercurial > yakumo_izuru > aya
comparison zs_test.go @ 33:e3c902a7380d draft
rewritten using zs templates, allowing go templates using <% %> delimiters
author | zaitsev.serge |
---|---|
date | Wed, 02 Sep 2015 17:05:09 +0000 |
parents | 40f55059fbfa |
children | ed40ca93db1e |
comparison
equal
deleted
inserted
replaced
32:75822e38c3e0 | 33:e3c902a7380d |
---|---|
1 package main | 1 package main |
2 | 2 |
3 import ( | 3 import ( |
4 "bytes" | |
5 "fmt" | |
6 "io/ioutil" | 4 "io/ioutil" |
7 "log" | |
8 "os" | 5 "os" |
9 "strings" | 6 "path/filepath" |
10 "testing" | 7 "testing" |
11 ) | 8 ) |
12 | 9 |
13 func TestSplit2(t *testing.T) { | 10 func TestRenameExt(t *testing.T) { |
14 if a, b := split2("a:b", ":"); a != "a" || b != "b" { | 11 if s := renameExt("foo.amber", ".amber", ".html"); s != "foo.html" { |
15 t.Fail() | 12 t.Error(s) |
16 } | 13 } |
17 if a, b := split2(":b", ":"); a != "" || b != "b" { | 14 if s := renameExt("foo.amber", "", ".html"); s != "foo.html" { |
18 t.Fail() | 15 t.Error(s) |
19 } | 16 } |
20 if a, b := split2("a:", ":"); a != "a" || b != "" { | 17 if s := renameExt("foo.amber", ".md", ".html"); s != "foo.amber" { |
21 t.Fail() | 18 t.Error(s) |
22 } | 19 } |
23 if a, b := split2(":", ":"); a != "" || b != "" { | 20 if s := renameExt("foo", ".amber", ".html"); s != "foo" { |
24 t.Fail() | 21 t.Error(s) |
25 } | 22 } |
26 if a, b := split2("a", ":"); a != "a" || b != "" { | 23 if s := renameExt("foo", "", ".html"); s != "foo.html" { |
27 t.Fail() | 24 t.Error(s) |
28 } | |
29 if a, b := split2("", ":"); a != "" || b != "" { | |
30 t.Fail() | |
31 } | 25 } |
32 } | 26 } |
33 | 27 |
34 func tmpfile(path, s string) string { | 28 func TestRun(t *testing.T) { |
35 ioutil.WriteFile(path, []byte(s), 0644) | 29 // external command |
36 return path | 30 if s, err := run(Vars{}, "echo", "hello"); err != nil || s != "hello\n" { |
31 t.Error(s, err) | |
32 } | |
33 // passing variables to plugins | |
34 if s, err := run(Vars{"foo": "bar"}, "sh", "-c", "echo $ZS_FOO"); err != nil || s != "bar\n" { | |
35 t.Error(s, err) | |
36 } | |
37 | |
38 // custom plugin overriding external command | |
39 os.Mkdir(ZSDIR, 0755) | |
40 script := `#!/bin/sh | |
41 echo foo | |
42 ` | |
43 ioutil.WriteFile(filepath.Join(ZSDIR, "echo"), []byte(script), 0755) | |
44 if s, err := run(Vars{}, "echo", "hello"); err != nil || s != "foo\n" { | |
45 t.Error(s, err) | |
46 } | |
47 os.Remove(filepath.Join(ZSDIR, "echo")) | |
48 os.Remove(ZSDIR) | |
37 } | 49 } |
38 | 50 |
39 func TestMD(t *testing.T) { | 51 func TestVars(t *testing.T) { |
40 defer os.Remove("foo.md") | 52 tests := map[string]Vars{ |
41 v, body, _ := md(tmpfile("foo.md", ` | 53 ` |
42 title: Hello, world! | 54 foo: bar |
43 keywords: foo, bar, baz | 55 title: Hello, world! |
44 empty: | |
45 bayan: [:|||:] | |
46 | 56 |
47 this: is a content`), Vars{}) | 57 Some content in markdown |
48 if v["title"] != "Hello, world!" { | 58 `: Vars{ |
49 t.Error() | 59 "foo": "bar", |
50 } | 60 "title": "Hello, world!", |
51 if v["keywords"] != "foo, bar, baz" { | 61 "url": "test.html", |
52 t.Error() | 62 "file": "test.md", |
53 } | 63 "output": filepath.Join(PUBDIR, "test.html"), |
54 if s, ok := v["empty"]; !ok || len(s) != 0 { | 64 "__content": "Some content in markdown\n", |
55 t.Error() | 65 }, |
56 } | 66 `url: "example.com/foo.html" |
57 if v["bayan"] != "[:|||:]" { | 67 |
58 t.Error() | 68 Hello |
59 } | 69 `: Vars{ |
60 if body != "this: is a content" { | 70 "url": "example.com/foo.html", |
61 t.Error(body) | 71 "__content": "Hello\n", |
72 }, | |
62 } | 73 } |
63 | 74 |
64 // Test empty md | 75 for script, vars := range tests { |
65 v, body, _ = md(tmpfile("foo.md", ""), Vars{}) | 76 ioutil.WriteFile("test.md", []byte(script), 0644) |
66 if v["url"] != "foo.html" || len(body) != 0 { | 77 if v, s, err := getVars("test.md", Vars{"baz": "123"}); err != nil { |
67 t.Error(v, body) | 78 t.Error(err) |
68 } | 79 } else if s != vars["__content"] { |
69 | 80 t.Error(s, vars["__content"]) |
70 // Test empty header | 81 } else { |
71 v, body, _ = md(tmpfile("foo.md", "Hello"), Vars{}) | 82 for key, value := range vars { |
72 if v["url"] != "foo.html" || body != "Hello" { | 83 if key != "__content" && v[key] != value { |
73 t.Error(v, body) | 84 t.Error(key, v[key], value) |
85 } | |
86 } | |
87 } | |
74 } | 88 } |
75 } | 89 } |
76 | 90 |
77 func TestRender(t *testing.T) { | 91 func TestRender(t *testing.T) { |
78 vars := map[string]string{"foo": "bar"} | 92 vars := map[string]string{"foo": "bar"} |
79 funcs := Funcs{ | 93 |
80 "greet": func(s ...string) string { | 94 if s, _ := render("foo bar", vars); s != "foo bar" { |
81 if len(s) == 0 { | 95 t.Error(s) |
82 return "hello" | |
83 } else { | |
84 return "hello " + strings.Join(s, " ") | |
85 } | |
86 }, | |
87 } | 96 } |
88 | 97 if s, _ := render("a {{printf short}} text", vars); s != "a short text" { |
89 if s, err := render("plain text", funcs, vars); err != nil || s != "plain text" { | 98 t.Error(s) |
90 t.Error(s, err) | |
91 } | 99 } |
92 if s, err := render("a {{greet}} text", funcs, vars); err != nil || s != "a hello text" { | 100 if s, _ := render("{{printf Hello}} x{{foo}}z", vars); s != "Hello xbarz" { |
93 t.Error(s, err) | 101 t.Error(s) |
94 } | |
95 if s, err := render("{{greet}} x{{foo}}z", funcs, vars); err != nil || s != "hello xbarz" { | |
96 t.Error(s, err) | |
97 } | 102 } |
98 // Test error case | 103 // Test error case |
99 if s, err := render("a {{greet text ", funcs, vars); err == nil || len(s) != 0 { | 104 if _, err := render("a {{greet text ", vars); err == nil { |
100 t.Error(s, err) | 105 t.Error("error expected") |
101 } | 106 } |
102 } | 107 } |
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 } |