-
Notifications
You must be signed in to change notification settings - Fork 3
/
template_test.go
117 lines (107 loc) · 1.75 KB
/
template_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
package fauna
import (
"testing"
"github.com/stretchr/testify/assert"
)
type TemplateSuccessCase struct {
given string
wants *[]templatePart
}
type TemplateErrorCase struct {
given string
error string
}
func TestTemplate_ParseSuccess(t *testing.T) {
testCases := []TemplateSuccessCase{
{
"let x = ${my_var}",
&[]templatePart{
{
"let x = ",
templateLiteral,
},
{
"my_var",
templateVariable,
},
},
},
{
"let x = ${my_var}\nlet y = ${my_var}\nx * y",
&[]templatePart{
{
"let x = ",
templateLiteral,
},
{
"my_var",
templateVariable,
},
{
"\nlet y = ",
templateLiteral,
},
{
"my_var",
templateVariable,
},
{
"\nx * y",
templateLiteral,
},
},
},
{
"${my_var} { .name }",
&[]templatePart{
{
"my_var",
templateVariable,
},
{
" { .name }",
templateLiteral,
},
},
},
{
"let x = '$${not_a_var}'",
&[]templatePart{
{
"let x = '$",
templateLiteral,
},
{
"{not_a_var}'",
templateLiteral,
},
},
},
}
for _, tc := range testCases {
parsed, err := parseTemplate(tc.given)
if !assert.NoError(t, err) {
return
}
assert.Equal(t, len(*tc.wants), len(parsed))
for i, tp := range parsed {
expected := (*tc.wants)[i]
assert.Equal(t, expected.Text, tp.Text)
assert.Equal(t, expected.Category, tp.Category)
}
}
}
func TestTemplate_ParseFail(t *testing.T) {
testCases := []TemplateErrorCase{
{
"let x = ${かわいい}",
"invalid placeholder in template: position 9",
},
}
for _, tc := range testCases {
_, err := parseTemplate(tc.given)
if assert.Error(t, err) {
assert.EqualError(t, err, tc.error)
}
}
}