1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
package parser
import (
"runtime"
"testing"
)
func TestIsInclude(t *testing.T) {
tests := []struct {
data string
file string
addr string
read int
}{
{
"{{foo}}",
"foo", "", 7,
},
{
"{{foo}} ",
"foo", "", 7,
},
{
"{{foo}}[a]",
"foo", "a", 10,
},
{
"{{foo}}[a ] ",
"foo", "a ", 12,
},
{
"{{foo}}a]",
"foo", "", 7,
},
{
" {{foo}}",
"foo", "", 10,
},
// fails
{
"{foo}}",
"", "", 0,
},
{
"{foo}",
"", "", 0,
},
{
"{{foo}}[a",
"", "", 0,
},
}
p := New()
for i, test := range tests {
file, addr, read := p.isInclude([]byte(test.data))
if file != test.file {
t.Errorf("test %d, want %s, got %s", i, test.file, file)
}
if string(addr) != test.addr {
t.Errorf("test %d, want %s, got %s", i, test.addr, addr)
}
if read != test.read {
t.Errorf("test %d, want %d, got %d", i, test.read, read)
}
}
}
func TestIsCodeInclude(t *testing.T) {
tests := []struct {
data []byte
file string
addr string
read int
}{
{
[]byte("<{{foo}}"),
"foo", "", 8,
},
{
[]byte("<{{foo}} "),
"foo", "", 8,
},
{
[]byte(" <{{foo}} "),
"foo", "", 11,
},
}
p := New()
for i, test := range tests {
file, addr, read := p.isCodeInclude(test.data)
if file != test.file {
t.Errorf("test %d, want %s, got %s", i, test.file, file)
}
if string(addr) != test.addr {
t.Errorf("test %d, want %s, got %s", i, test.addr, addr)
}
if read != test.read {
t.Errorf("test %d, want %d, got %d", i, test.read, read)
}
}
}
func isWindows() bool {
return runtime.GOOS == "windows"
}
func TestPush(t *testing.T) {
if isWindows() {
return
}
i := newIncStack()
if i.Push("/new/foo"); i.stack[0] != "/new" {
t.Errorf("want %s, got %s", "/new", i.stack[0])
}
if i.Push("new/new"); i.stack[1] != "/new/new" {
t.Errorf("want %s, got %s", "/new/new", i.stack[1])
}
}
func TestPop(t *testing.T) {
i := newIncStack()
if i.Push("/new/foo"); i.stack[0] != "/new" {
t.Errorf("want %s, got %s", "/new", i.stack[0])
}
i.Pop()
if len(i.stack) != 0 {
t.Errorf("after pop, want %d, got %d", 0, len(i.stack))
}
}
|