forked from gobuffalo/plush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
return_exit_test.go
111 lines (102 loc) · 2.22 KB
/
return_exit_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
package plush_test
import (
"strings"
"testing"
"github.com/gobuffalo/plush/v5"
"github.com/stretchr/testify/require"
)
func Test_Return_Exit_With__InfixExpression(t *testing.T) {
tests := []struct {
name string
success bool
expected string
input string
}{
{"infix_expression", true, "2", `<%
let numberify = fn(arg) {
if (arg == "one") {
return 1+1;
}
if (arg == "two") {
return 44;
}
if (arg == "three") {
return 2;
}
return "unsupported"
} %>
<%= numberify("one") %>`},
{"simple_return", true, "445", `<%
let numberify = fn(arg) {
if (arg == "one") {
return 1;
}
if (arg == "two") {
return 445;
}
if (arg == "three") {
return 3;
}
return "unsupported"
} %>
<%= numberify("two") %>`},
{"default_return", true, "default value", `<%
let numberify = fn(arg) {
if (arg == "one") {
return 1;
}
if (arg == "two") {
return 445;
}
if (arg == "three") {
return 3;
}
return "default value"
} %>
<%= numberify("six") %>`},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
r := require.New(t)
s, err := plush.Render(tc.input, plush.NewContext())
if tc.success {
r.NoError(err)
} else {
r.Error(err)
}
r.Equal(tc.expected, strings.TrimSpace(s))
})
}
}
func Test_User_Function_Return(t *testing.T) {
r := require.New(t)
ctx := plush.NewContext()
in := `<%
let print = fn(obj) {
if (obj.Secret) {
if (obj.GiveHint) {
return truncate(obj.String, {size: 12, trail: "****"})
}
return "**********"
}
return obj.String
}
%>You are: <%= print(data) %>.`
type obj struct {
Secret bool
GiveHint bool
String string
}
ctx.Set("data", obj{Secret: true, String: "your royal highness"})
out, err := plush.Render(in, ctx)
r.NoError(err, "Render")
r.Equal(`You are: **********.`, out)
ctx.Set("data", obj{Secret: true, GiveHint: true, String: "your royal highness"})
out, err = plush.Render(in, ctx)
r.NoError(err, "Render")
r.Equal(`You are: your roy****.`, out)
ctx.Set("data", obj{Secret: false, String: "your royal highness"})
out, err = plush.Render(in, ctx)
r.NoError(err, "Render")
r.Equal(`You are: your royal highness.`, out)
}