source: code/trunk/zs_test.go@ 35

Last change on this file since 35 was 35, checked in by zaitsev.serge, 10 years ago

added explicit yaml separator, fixed amber compilation sequence

File size: 2.4 KB
RevLine 
[1]1package main
2
[5]3import (
[18]4 "io/ioutil"
[5]5 "os"
[34]6 "path/filepath"
[5]7 "testing"
8)
[1]9
[34]10func 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]28func 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
41echo 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]51func TestVars(t *testing.T) {
52 tests := map[string]Vars{
53 `
54foo: bar
55title: Hello, world!
[35]56---
[34]57Some 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 },
[35]66 `
67url: "example.com/foo.html"
68---
[34]69Hello
70`: Vars{
71 "url": "example.com/foo.html",
72 "__content": "Hello\n",
73 },
[1]74 }
[5]75
[34]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 }
[5]87 }
88 }
89 }
90}
91
[34]92func TestRender(t *testing.T) {
93 vars := map[string]string{"foo": "bar"}
[5]94
[34]95 if s, _ := render("foo bar", vars); s != "foo bar" {
96 t.Error(s)
[5]97 }
[34]98 if s, _ := render("a {{printf short}} text", vars); s != "a short text" {
99 t.Error(s)
[5]100 }
[34]101 if s, _ := render("{{printf Hello}} x{{foo}}z", vars); s != "Hello xbarz" {
102 t.Error(s)
[5]103 }
[34]104 // Test error case
105 if _, err := render("a {{greet text ", vars); err == nil {
106 t.Error("error expected")
107 }
[5]108}
Note: See TracBrowser for help on using the repository browser.