2014-12-05 11:59:56 -05:00
|
|
|
package main
|
|
|
|
|
2014-12-05 13:09:03 -05:00
|
|
|
import (
|
2015-08-29 11:47:16 -04:00
|
|
|
"io/ioutil"
|
2014-12-05 13:09:03 -05:00
|
|
|
"os"
|
2015-09-02 13:05:09 -04:00
|
|
|
"path/filepath"
|
2014-12-05 13:09:03 -05:00
|
|
|
"testing"
|
|
|
|
)
|
2014-12-05 11:59:56 -05:00
|
|
|
|
2015-09-02 13:05:09 -04:00
|
|
|
func TestRenameExt(t *testing.T) {
|
|
|
|
if s := renameExt("foo.amber", ".amber", ".html"); s != "foo.html" {
|
|
|
|
t.Error(s)
|
2014-12-05 11:59:56 -05:00
|
|
|
}
|
2015-09-02 13:05:09 -04:00
|
|
|
if s := renameExt("foo.amber", "", ".html"); s != "foo.html" {
|
|
|
|
t.Error(s)
|
2014-12-05 11:59:56 -05:00
|
|
|
}
|
2015-09-02 13:05:09 -04:00
|
|
|
if s := renameExt("foo.amber", ".md", ".html"); s != "foo.amber" {
|
|
|
|
t.Error(s)
|
2014-12-05 11:59:56 -05:00
|
|
|
}
|
2015-09-02 13:05:09 -04:00
|
|
|
if s := renameExt("foo", ".amber", ".html"); s != "foo" {
|
|
|
|
t.Error(s)
|
2014-12-05 11:59:56 -05:00
|
|
|
}
|
2015-09-02 13:05:09 -04:00
|
|
|
if s := renameExt("foo", "", ".html"); s != "foo.html" {
|
|
|
|
t.Error(s)
|
2014-12-05 11:59:56 -05:00
|
|
|
}
|
2015-08-29 11:47:16 -04:00
|
|
|
}
|
|
|
|
|
2015-09-02 13:05:09 -04:00
|
|
|
func TestRun(t *testing.T) {
|
|
|
|
// external command
|
|
|
|
if s, err := run(Vars{}, "echo", "hello"); err != nil || s != "hello\n" {
|
|
|
|
t.Error(s, err)
|
2014-12-05 11:59:56 -05:00
|
|
|
}
|
2015-09-02 13:05:09 -04:00
|
|
|
// passing variables to plugins
|
|
|
|
if s, err := run(Vars{"foo": "bar"}, "sh", "-c", "echo $ZS_FOO"); err != nil || s != "bar\n" {
|
|
|
|
t.Error(s, err)
|
2014-12-05 12:09:10 -05:00
|
|
|
}
|
|
|
|
|
2015-09-02 13:05:09 -04:00
|
|
|
// custom plugin overriding external command
|
|
|
|
os.Mkdir(ZSDIR, 0755)
|
|
|
|
script := `#!/bin/sh
|
|
|
|
echo foo
|
|
|
|
`
|
|
|
|
ioutil.WriteFile(filepath.Join(ZSDIR, "echo"), []byte(script), 0755)
|
|
|
|
if s, err := run(Vars{}, "echo", "hello"); err != nil || s != "foo\n" {
|
|
|
|
t.Error(s, err)
|
2014-12-05 12:09:10 -05:00
|
|
|
}
|
2015-09-02 13:05:09 -04:00
|
|
|
os.Remove(filepath.Join(ZSDIR, "echo"))
|
|
|
|
os.Remove(ZSDIR)
|
2014-12-05 11:59:56 -05:00
|
|
|
}
|
|
|
|
|
2015-09-02 13:05:09 -04:00
|
|
|
func TestVars(t *testing.T) {
|
|
|
|
tests := map[string]Vars{
|
|
|
|
`
|
|
|
|
foo: bar
|
|
|
|
title: Hello, world!
|
2015-09-02 13:35:26 -04:00
|
|
|
---
|
2015-09-02 13:05:09 -04:00
|
|
|
Some content in markdown
|
|
|
|
`: Vars{
|
|
|
|
"foo": "bar",
|
|
|
|
"title": "Hello, world!",
|
|
|
|
"url": "test.html",
|
|
|
|
"file": "test.md",
|
|
|
|
"output": filepath.Join(PUBDIR, "test.html"),
|
|
|
|
"__content": "Some content in markdown\n",
|
2015-08-29 12:46:05 -04:00
|
|
|
},
|
2015-09-02 13:35:26 -04:00
|
|
|
`
|
|
|
|
url: "example.com/foo.html"
|
|
|
|
---
|
2015-09-02 13:05:09 -04:00
|
|
|
Hello
|
|
|
|
`: Vars{
|
|
|
|
"url": "example.com/foo.html",
|
|
|
|
"__content": "Hello\n",
|
|
|
|
},
|
2014-12-05 13:09:03 -05:00
|
|
|
}
|
|
|
|
|
2015-09-02 13:05:09 -04:00
|
|
|
for script, vars := range tests {
|
|
|
|
ioutil.WriteFile("test.md", []byte(script), 0644)
|
|
|
|
if v, s, err := getVars("test.md", Vars{"baz": "123"}); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else if s != vars["__content"] {
|
|
|
|
t.Error(s, vars["__content"])
|
|
|
|
} else {
|
|
|
|
for key, value := range vars {
|
|
|
|
if key != "__content" && v[key] != value {
|
|
|
|
t.Error(key, v[key], value)
|
|
|
|
}
|
2014-12-05 13:09:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-02 13:05:09 -04:00
|
|
|
func TestRender(t *testing.T) {
|
|
|
|
vars := map[string]string{"foo": "bar"}
|
2014-12-05 13:09:03 -05:00
|
|
|
|
2015-09-02 13:05:09 -04:00
|
|
|
if s, _ := render("foo bar", vars); s != "foo bar" {
|
|
|
|
t.Error(s)
|
2014-12-05 13:09:03 -05:00
|
|
|
}
|
2015-09-02 13:05:09 -04:00
|
|
|
if s, _ := render("a {{printf short}} text", vars); s != "a short text" {
|
|
|
|
t.Error(s)
|
2014-12-05 13:09:03 -05:00
|
|
|
}
|
2015-09-02 13:05:09 -04:00
|
|
|
if s, _ := render("{{printf Hello}} x{{foo}}z", vars); s != "Hello xbarz" {
|
|
|
|
t.Error(s)
|
|
|
|
}
|
|
|
|
// Test error case
|
|
|
|
if _, err := render("a {{greet text ", vars); err == nil {
|
|
|
|
t.Error("error expected")
|
2014-12-05 13:09:03 -05:00
|
|
|
}
|
2014-12-05 11:59:56 -05:00
|
|
|
}
|