-
Notifications
You must be signed in to change notification settings - Fork 37
/
jade_test.go
153 lines (127 loc) · 3.27 KB
/
jade_test.go
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package jade
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/Joker/hpp"
)
var wdir string
func init() {
wdir, _ = os.Getwd()
}
func TestStrFilter(t *testing.T) {
a := `"aaa'a" + 'b\"bb"b' + 'c' | +"dddd" + "eeee"+"ffff" +` + " `iiii ${jjjj} kkkk`"
b := "\"aaa'ab\\\"bb\\\"bc\" | +\"ddddeeeeffff\" + `iiii `+jjjj+` kkkk`"
if filterString(a) != b {
t.Fail()
}
}
func examination(test func(fpath string, dat []byte) (string, error), ext, path string, t *testing.T) {
os.Chdir(path)
files, err := ioutil.ReadDir(path)
if err != nil {
fmt.Printf("--- FAIL: ReadDir error: %v\n\n", err)
t.Fail()
}
var name, fext string
for _, file := range files {
name = file.Name()
fext = filepath.Ext(name)
if fext != ".jade" && fext != ".pug" {
continue
}
dat, err := ioutil.ReadFile(path + name)
if err != nil {
fmt.Println("_________" + name)
fmt.Printf("--- FAIL: ReadFile error: %v\n\n", err)
t.Fail()
continue
}
tpl, err := test(path+name, dat)
if err != nil {
fmt.Println("_________" + name)
fmt.Printf("--- FAIL: test run() error: \n%s\n\n", err)
t.Fail()
continue
}
tmpl := bufio.NewScanner(strings.NewReader(tpl))
tmpl.Split(bufio.ScanLines)
inFile, err := os.Open(path + strings.TrimSuffix(name, fext) + ext)
if err != nil {
// make files
ioutil.WriteFile(path+strings.TrimSuffix(name, fext)+ext, []byte(tpl), 0644)
fmt.Println("```", tpl, "\n\n```")
continue
}
html := bufio.NewScanner(inFile)
html.Split(bufio.ScanLines)
nilerr, line := 0, 0
for tmpl.Scan() {
html.Scan()
a := tmpl.Text()
b := html.Text()
line += 1
if strings.Compare(a, b) != 0 && nilerr < 4 {
if nilerr == 0 {
fmt.Println("_________" + name + "\n")
}
fmt.Printf("%s\n%s\n%d^___________________________\n", a, b, line)
nilerr += 1
t.Fail()
}
}
inFile.Close()
if nilerr != 0 {
fmt.Print("--- FAIL\n\n\n\n")
}
}
}
func lexerTest(fpath string, dat []byte) (string, error) {
var buf bytes.Buffer
l := lex(fpath, dat)
for i := range l.items {
switch {
case i.typ == itemError:
buf.WriteString("\n\n\nError:\n\t")
buf.WriteString(fmt.Sprintf("%s line: %d", i.val, i.line))
buf.WriteString("\n\n\n")
return "", fmt.Errorf("%s", buf.String())
case i.typ == itemEOF:
buf.WriteString("\nEOF")
case i.typ == itemEndL:
buf.WriteByte('\n')
case i.typ == itemEmptyLine:
buf.WriteString(i.val)
case i.typ == itemIdent:
buf.WriteString(i.val)
default:
buf.WriteString(fmt.Sprintf("[%d %s \"%s\"]\t", i.depth, i.typ, i.val))
}
}
return buf.String(), nil
}
func xTestJadeLex(t *testing.T) {
examination(lexerTest, ".lex", wdir+"/testdata/v1/", t)
examination(lexerTest, ".lex", wdir+"/testdata/v2/", t)
}
//
func parserTest(fpath string, text []byte) (string, error) {
outTpl, err := New(fpath).Parse(text)
if err != nil {
return "", err
}
b := new(bytes.Buffer)
outTpl.WriteIn(b)
return string(hpp.Print(b)), nil
}
func TestJadeParse(t *testing.T) {
examination(parserTest, ".tpl", wdir+"/testdata/v1/", t)
examination(parserTest, ".tpl", wdir+"/testdata/v2/", t)
examination(parserTest, ".tpl", wdir+"/testdata/v2/includes/", t)
examination(parserTest, ".tpl", wdir+"/testdata/v2/inheritance/", t)
}