-
Notifications
You must be signed in to change notification settings - Fork 0
/
render_container_as_view_test.go
113 lines (100 loc) · 2.42 KB
/
render_container_as_view_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
package veun_test
import (
"context"
"html/template"
"testing"
"github.com/alecthomas/assert/v2"
. "github.com/stanistan/veun"
t "github.com/stanistan/veun/template"
)
type ContainerView2 struct {
Heading, Body AsView
}
func (v ContainerView2) View(ctx context.Context) (*View, error) {
return V(t.Template{
Tpl: containerViewTpl,
Slots: t.Slots{"heading": v.Heading, "body": v.Body},
}), nil
}
func TestRenderContainerAsView(t *testing.T) {
html, err := Render(context.Background(), ContainerView2{
Heading: ChildView1{},
Body: ChildView2{},
})
assert.NoError(t, err)
assert.Equal(t, template.HTML(`<div>
<div class="heading">HEADING</div>
<div class="body">BODY</div>
</div>`), html)
}
func BenchmarkRenderContainer(b *testing.B) {
ctx := context.Background()
b.Run("empty_container", func(b *testing.B) {
container := ContainerView2{}
for i := 0; i < b.N; i++ {
_, _ = Render(ctx, container)
}
})
b.Run("container_one_child", func(b *testing.B) {
container := ContainerView2{Heading: ChildView1{}}
for i := 0; i < b.N; i++ {
_, _ = Render(ctx, container)
}
})
b.Run("container_two_children", func(b *testing.B) {
container := ContainerView2{Heading: ChildView1{}, Body: ChildView2{}}
for i := 0; i < b.N; i++ {
_, _ = Render(ctx, container)
}
})
b.Run("container_nested", func(b *testing.B) {
container := ContainerView2{
Heading: ContainerView2{
Heading: ChildView1{},
Body: ChildView2{},
},
Body: ChildView2{},
}
for i := 0; i < b.N; i++ {
_, _ = Render(ctx, container)
}
})
b.Run("container_nested_double", func(b *testing.B) {
container := ContainerView2{
Heading: ContainerView2{
Heading: ChildView1{},
Body: ChildView2{},
},
Body: ContainerView2{
Heading: ChildView1{},
Body: PersonView(Person{Name: "hi"}),
},
}
for i := 0; i < b.N; i++ {
_, _ = Render(ctx, container)
}
})
b.Run("container_nested_more", func(b *testing.B) {
container := ContainerView2{
Heading: ContainerView2{
Heading: ContainerView2{
Heading: ContainerView2{
Heading: ContainerView2{
Heading: ChildView1{},
Body: PersonView(Person{Name: "hi"}),
},
},
Body: ChildView2{},
},
Body: ChildView2{},
},
Body: ContainerView2{
Heading: ChildView1{},
Body: PersonView(Person{Name: "hi"}),
},
}
for i := 0; i < b.N; i++ {
_, _ = Render(ctx, container)
}
})
}