forked from gobuffalo/plush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct_test.go
80 lines (69 loc) · 1.54 KB
/
struct_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
package plush
import (
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func Test_Render_Struct_Attribute(t *testing.T) {
r := require.New(t)
input := `<%= f.Name %>`
ctx := NewContext()
f := struct {
Name string
}{"Mark"}
ctx.Set("f", f)
s, err := Render(input, ctx)
r.NoError(err)
r.Equal("Mark", s)
}
func Test_Render_UnknownAttribute_on_Callee(t *testing.T) {
r := require.New(t)
ctx := NewContext()
ctx.Set("m", struct{}{})
input := `<%= m.Foo %>`
_, err := Render(input, ctx)
r.Error(err)
r.Contains(err.Error(), "'m' does not have a field or method named 'Foo' (m.Foo)")
}
type Robot struct {
Avatar Avatar
name string
}
type Avatar string
func (a Avatar) URL() string {
return strings.ToUpper(string(a))
}
func (r *Robot) Name() string {
return r.name
}
func Test_Render_Function_on_sub_Struct(t *testing.T) {
r := require.New(t)
ctx := NewContext()
bender := Robot{
Avatar: Avatar("bender.jpg"),
}
ctx.Set("robot", bender)
input := `<%= robot.Avatar.URL() %>`
s, err := Render(input, ctx)
r.NoError(err)
r.Equal("BENDER.JPG", s)
}
func Test_Render_Struct_PointerMethod(t *testing.T) {
r := require.New(t)
ctx := NewContext()
robot := Robot{name: "robot"}
t.Run("ByValue", func(t *testing.T) {
ctx.Set("robot", robot)
input := `<%= robot.Name() %>`
s, err := Render(input, ctx)
r.NoError(err)
r.Equal("robot", s)
})
t.Run("ByPointer", func(t *testing.T) {
ctx.Set("robot", &robot)
input := `<%= robot.Name() %>`
s, err := Render(input, ctx)
r.NoError(err)
r.Equal("robot", s)
})
}