zs/main_test.go

109 lines
2.4 KiB
Go
Raw Normal View History

2014-12-05 11:59:56 -05:00
package main
import (
2015-08-29 11:47:16 -04:00
"io/ioutil"
"os"
"path/filepath"
"testing"
)
2014-12-05 11:59:56 -05: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
}
if s := renameExt("foo.amber", "", ".html"); s != "foo.html" {
t.Error(s)
2014-12-05 11:59:56 -05:00
}
if s := renameExt("foo.amber", ".md", ".html"); s != "foo.amber" {
t.Error(s)
2014-12-05 11:59:56 -05:00
}
if s := renameExt("foo", ".amber", ".html"); s != "foo" {
t.Error(s)
2014-12-05 11:59:56 -05: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
}
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
}
// 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
}
// 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
}
os.Remove(filepath.Join(ZSDIR, "echo"))
os.Remove(ZSDIR)
2014-12-05 11:59:56 -05:00
}
func TestVars(t *testing.T) {
tests := map[string]Vars{
`
foo: bar
title: Hello, world!
---
Some content in markdown
2021-10-28 12:10:15 -04:00
`: {
"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
},
`
url: "example.com/foo.html"
---
Hello
2021-10-28 12:10:15 -04:00
`: {
"url": "example.com/foo.html",
"__content": "Hello\n",
},
}
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)
}
}
}
}
}
func TestRender(t *testing.T) {
vars := map[string]string{"foo": "bar"}
if s, _ := render("foo bar", vars); s != "foo bar" {
t.Error(s)
}
if s, _ := render("a {{printf short}} text", vars); s != "a short text" {
t.Error(s)
}
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 11:59:56 -05:00
}