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