/
damsel_test.go
148 lines (125 loc) · 2.78 KB
/
damsel_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
package damsel
import (
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"testing"
"dasa.cc/damsel/parse"
)
var TestsDir = "tests"
func execute(t *Template, data interface{}) (string, error) {
tpl := NewHtmlTemplate(t)
r, err := tpl.Execute(data)
if err != nil {
return "", err
}
return r, nil
}
func get_html(t *testing.T, s string) string {
b, err := ioutil.ReadFile(filepath.Join(TestsDir, s+".html"))
if err != nil {
t.Fatal(err)
}
return strings.TrimSpace(string(b))
}
func test(t *testing.T, s string, data interface{}) {
fmt.Println("testing:", s)
TemplateDir = TestsDir
html := get_html(t, s)
tpl, _ := ParseFile(s + ".dmsl")
r, err := execute(tpl, data)
r = strings.TrimSpace(r)
if r != html {
fmt.Println("\nExpected\n========\n", html, "\nReceived\n========\n", r, "\n\n")
t.Fatal("parse failed:", s, "\n", err)
}
}
func Test_html(t *testing.T) {
test(t, "html", nil)
}
func Test_indent(t *testing.T) {
test(t, "indent", nil)
}
func Test_variable_indent(t *testing.T) {
test(t, "variable_indent", nil)
}
func Test_inline(t *testing.T) {
data := []string{"a", "b", "c", "d"}
test(t, "inline", data)
}
func Test_multiline_text(t *testing.T) {
test(t, "multiline_text", nil)
}
func Test_ul_range_header(t *testing.T) {
data := []string{"a", "b", "c", "d"}
test(t, "ul_range_header", data)
}
func Test_tabs(t *testing.T) {
test(t, "tabs", nil)
}
func Test_tag_hashes(t *testing.T) {
test(t, "tag_hashes", nil)
}
func Test_html_comment(t *testing.T) {
test(t, "hcomment", nil)
}
func Test_extends(t *testing.T) {
test(t, "extends", nil)
}
func Test_extends_super(t *testing.T) {
test(t, "extends_super", nil)
}
func Test_big_table(t *testing.T) {
table := [2][10]int{}
test(t, "bigtable", table)
}
func Benchmark_parser(b *testing.B) {
b.StopTimer()
bytes, err := ioutil.ReadFile(filepath.Join(TestsDir, "bigtable2.dmsl"))
if err != nil {
b.Fatal(err)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
parse.DocParse(bytes)
}
}
func Benchmark_bigtable_go_stdlib(b *testing.B) {
b.StopTimer()
bytes, err := ioutil.ReadFile(filepath.Join(TestsDir, "bigtable.dmsl"))
if err != nil {
b.Fatal(err)
}
table := [1000][10]int{}
tmpl, _ := Parse(bytes)
b.StartTimer()
for i := 0; i < b.N; i++ {
execute(tmpl, table)
}
}
func Benchmark_bigtable(b *testing.B) {
b.StopTimer()
bytes, err := ioutil.ReadFile(filepath.Join(TestsDir, "bigtable.dmsl"))
if err != nil {
b.Fatal(err)
}
table := [1000][10]int{}
b.StartTimer()
for i := 0; i < b.N; i++ {
tpl, _ := Parse(bytes)
execute(tpl, table)
}
}
func Benchmark_bigtable2(b *testing.B) {
b.StopTimer()
bytes, err := ioutil.ReadFile(filepath.Join(TestsDir, "bigtable2.dmsl"))
if err != nil {
b.Fatal(err)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
tpl, _ := Parse(bytes)
execute(tpl, nil)
}
}