1 | package main
|
---|
2 |
|
---|
3 | import (
|
---|
4 | "io/ioutil"
|
---|
5 | "os"
|
---|
6 | "path/filepath"
|
---|
7 | "testing"
|
---|
8 | )
|
---|
9 |
|
---|
10 | func TestRenameExt(t *testing.T) {
|
---|
11 | if s := renameExt("foo.amber", ".amber", ".html"); s != "foo.html" {
|
---|
12 | t.Error(s)
|
---|
13 | }
|
---|
14 | if s := renameExt("foo.amber", "", ".html"); s != "foo.html" {
|
---|
15 | t.Error(s)
|
---|
16 | }
|
---|
17 | if s := renameExt("foo.amber", ".md", ".html"); s != "foo.amber" {
|
---|
18 | t.Error(s)
|
---|
19 | }
|
---|
20 | if s := renameExt("foo", ".amber", ".html"); s != "foo" {
|
---|
21 | t.Error(s)
|
---|
22 | }
|
---|
23 | if s := renameExt("foo", "", ".html"); s != "foo.html" {
|
---|
24 | t.Error(s)
|
---|
25 | }
|
---|
26 | }
|
---|
27 |
|
---|
28 | func TestRun(t *testing.T) {
|
---|
29 | // external command
|
---|
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)
|
---|
49 | }
|
---|
50 |
|
---|
51 | func TestVars(t *testing.T) {
|
---|
52 | tests := map[string]Vars{
|
---|
53 | `
|
---|
54 | foo: bar
|
---|
55 | title: Hello, world!
|
---|
56 |
|
---|
57 | Some content in markdown
|
---|
58 | `: Vars{
|
---|
59 | "foo": "bar",
|
---|
60 | "title": "Hello, world!",
|
---|
61 | "url": "test.html",
|
---|
62 | "file": "test.md",
|
---|
63 | "output": filepath.Join(PUBDIR, "test.html"),
|
---|
64 | "__content": "Some content in markdown\n",
|
---|
65 | },
|
---|
66 | `url: "example.com/foo.html"
|
---|
67 |
|
---|
68 | Hello
|
---|
69 | `: Vars{
|
---|
70 | "url": "example.com/foo.html",
|
---|
71 | "__content": "Hello\n",
|
---|
72 | },
|
---|
73 | }
|
---|
74 |
|
---|
75 | for script, vars := range tests {
|
---|
76 | ioutil.WriteFile("test.md", []byte(script), 0644)
|
---|
77 | if v, s, err := getVars("test.md", Vars{"baz": "123"}); err != nil {
|
---|
78 | t.Error(err)
|
---|
79 | } else if s != vars["__content"] {
|
---|
80 | t.Error(s, vars["__content"])
|
---|
81 | } else {
|
---|
82 | for key, value := range vars {
|
---|
83 | if key != "__content" && v[key] != value {
|
---|
84 | t.Error(key, v[key], value)
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | func TestRender(t *testing.T) {
|
---|
92 | vars := map[string]string{"foo": "bar"}
|
---|
93 |
|
---|
94 | if s, _ := render("foo bar", vars); s != "foo bar" {
|
---|
95 | t.Error(s)
|
---|
96 | }
|
---|
97 | if s, _ := render("a {{printf short}} text", vars); s != "a short text" {
|
---|
98 | t.Error(s)
|
---|
99 | }
|
---|
100 | if s, _ := render("{{printf Hello}} x{{foo}}z", vars); s != "Hello xbarz" {
|
---|
101 | t.Error(s)
|
---|
102 | }
|
---|
103 | // Test error case
|
---|
104 | if _, err := render("a {{greet text ", vars); err == nil {
|
---|
105 | t.Error("error expected")
|
---|
106 | }
|
---|
107 | }
|
---|