[48] | 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
|
---|
[53] | 58 | `: {
|
---|
[48] | 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 | `
|
---|
| 67 | url: "example.com/foo.html"
|
---|
| 68 | ---
|
---|
| 69 | Hello
|
---|
[53] | 70 | `: {
|
---|
[48] | 71 | "url": "example.com/foo.html",
|
---|
| 72 | "__content": "Hello\n",
|
---|
| 73 | },
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | for script, vars := range tests {
|
---|
| 77 | ioutil.WriteFile("test.md", []byte(script), 0644)
|
---|
| 78 | if v, s, err := getVars("test.md", Vars{"baz": "123"}); err != nil {
|
---|
| 79 | t.Error(err)
|
---|
| 80 | } else if s != vars["__content"] {
|
---|
| 81 | t.Error(s, vars["__content"])
|
---|
| 82 | } else {
|
---|
| 83 | for key, value := range vars {
|
---|
| 84 | if key != "__content" && v[key] != value {
|
---|
| 85 | t.Error(key, v[key], value)
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | func TestRender(t *testing.T) {
|
---|
| 93 | vars := map[string]string{"foo": "bar"}
|
---|
| 94 |
|
---|
| 95 | if s, _ := render("foo bar", vars); s != "foo bar" {
|
---|
| 96 | t.Error(s)
|
---|
| 97 | }
|
---|
| 98 | if s, _ := render("a {{printf short}} text", vars); s != "a short text" {
|
---|
| 99 | t.Error(s)
|
---|
| 100 | }
|
---|
| 101 | if s, _ := render("{{printf Hello}} x{{foo}}z", vars); s != "Hello xbarz" {
|
---|
| 102 | t.Error(s)
|
---|
| 103 | }
|
---|
| 104 | // Test error case
|
---|
| 105 | if _, err := render("a {{greet text ", vars); err == nil {
|
---|
| 106 | t.Error("error expected")
|
---|
| 107 | }
|
---|
| 108 | }
|
---|