[1] | 1 | package main
|
---|
| 2 |
|
---|
[5] | 3 | import (
|
---|
[18] | 4 | "io/ioutil"
|
---|
[5] | 5 | "os"
|
---|
[34] | 6 | "path/filepath"
|
---|
[5] | 7 | "testing"
|
---|
| 8 | )
|
---|
[1] | 9 |
|
---|
[34] | 10 | func TestRenameExt(t *testing.T) {
|
---|
| 11 | if s := renameExt("foo.amber", ".amber", ".html"); s != "foo.html" {
|
---|
| 12 | t.Error(s)
|
---|
[1] | 13 | }
|
---|
[34] | 14 | if s := renameExt("foo.amber", "", ".html"); s != "foo.html" {
|
---|
| 15 | t.Error(s)
|
---|
[1] | 16 | }
|
---|
[34] | 17 | if s := renameExt("foo.amber", ".md", ".html"); s != "foo.amber" {
|
---|
| 18 | t.Error(s)
|
---|
[1] | 19 | }
|
---|
[34] | 20 | if s := renameExt("foo", ".amber", ".html"); s != "foo" {
|
---|
| 21 | t.Error(s)
|
---|
[1] | 22 | }
|
---|
[34] | 23 | if s := renameExt("foo", "", ".html"); s != "foo.html" {
|
---|
| 24 | t.Error(s)
|
---|
[1] | 25 | }
|
---|
| 26 | }
|
---|
| 27 |
|
---|
[34] | 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)
|
---|
[1] | 32 | }
|
---|
[34] | 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)
|
---|
[1] | 36 | }
|
---|
[3] | 37 |
|
---|
[34] | 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)
|
---|
[3] | 46 | }
|
---|
[34] | 47 | os.Remove(filepath.Join(ZSDIR, "echo"))
|
---|
| 48 | os.Remove(ZSDIR)
|
---|
[1] | 49 | }
|
---|
| 50 |
|
---|
[34] | 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",
|
---|
[19] | 65 | },
|
---|
[34] | 66 | `url: "example.com/foo.html"
|
---|
[1] | 67 |
|
---|
[34] | 68 | Hello
|
---|
| 69 | `: Vars{
|
---|
| 70 | "url": "example.com/foo.html",
|
---|
| 71 | "__content": "Hello\n",
|
---|
| 72 | },
|
---|
[1] | 73 | }
|
---|
[5] | 74 |
|
---|
[34] | 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 | }
|
---|
[5] | 86 | }
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[34] | 91 | func TestRender(t *testing.T) {
|
---|
| 92 | vars := map[string]string{"foo": "bar"}
|
---|
[5] | 93 |
|
---|
[34] | 94 | if s, _ := render("foo bar", vars); s != "foo bar" {
|
---|
| 95 | t.Error(s)
|
---|
[5] | 96 | }
|
---|
[34] | 97 | if s, _ := render("a {{printf short}} text", vars); s != "a short text" {
|
---|
| 98 | t.Error(s)
|
---|
[5] | 99 | }
|
---|
[34] | 100 | if s, _ := render("{{printf Hello}} x{{foo}}z", vars); s != "Hello xbarz" {
|
---|
| 101 | t.Error(s)
|
---|
[5] | 102 | }
|
---|
[34] | 103 | // Test error case
|
---|
| 104 | if _, err := render("a {{greet text ", vars); err == nil {
|
---|
| 105 | t.Error("error expected")
|
---|
| 106 | }
|
---|
[5] | 107 | }
|
---|