-
Notifications
You must be signed in to change notification settings - Fork 0
/
go_ml_test.go
128 lines (123 loc) · 3.7 KB
/
go_ml_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
package go_ml
import (
"log/slog"
"os"
"strings"
"testing"
)
func TestBasicDOM(t *testing.T) {
testSuite := []struct {
name string
givenDOM HTMLContent
buildOpts []buildOpt
expectedHtml string
}{
{
name: "build empty html document",
expectedHtml: `<!DOCTYPE html><html lang="en"></html>`,
givenDOM: Html(Lang("en"))(),
},
{
name: "build bare div with class names",
expectedHtml: `<div class="main container"><div></div></div>`,
givenDOM: Div(ClassNames("main", "container"))(Div()()),
},
{
name: "build script tag with non key-valued attributes",
expectedHtml: `<script defer></script>`,
givenDOM: Script(Defer())(),
},
{
name: "build script tag with multiple attributes",
expectedHtml: `<script defer src="./index.js"></script>`,
givenDOM: Script(Defer(), Src("./index.js"))(),
},
{
name: "build input with checked attribute",
expectedHtml: `<input checked/>`,
givenDOM: Input(Checked()),
},
{
name: "build script tag with source files",
expectedHtml: `<script src="index.js"></script>`,
givenDOM: Script(Src("index.js"))(),
},
{
name: "build input regular tag",
expectedHtml: `<input type="text"/>`,
givenDOM: Input(Type("text")),
},
{
name: "build non-void tag with inner text",
expectedHtml: `<div>olá mundo</div>`,
givenDOM: Div()(RawText("olá mundo")),
},
{
name: "build non-void tag with inner text",
expectedHtml: `<div>olá mundo<div></div>test<div></div></div>`,
givenDOM: Div()(RawText("olá mundo"), Div()(), RawText("test"), Div()()),
},
{
name: "build input with mutiple class attributes",
expectedHtml: `<input name="task" class="text name editable"/>`,
givenDOM: Input(Name("task"), ClassNames("text"), ClassNames("name"), ClassNames("editable")),
},
{
name: "build bare div with class names with default indentation",
expectedHtml: `<div class="main container">
<div></div>
</div>`,
givenDOM: Div(ClassNames("main", "container"))(Div()()),
buildOpts: []buildOpt{WithDefaultIndentation()},
},
{
name: "build shallow divs with default indentation",
expectedHtml: `<div class="main container">
<div>
<div>
<div>
<div></div>
</div>
</div>
</div>
</div>`,
givenDOM: Div(ClassNames("main", "container"))(Div()(
Div()(
Div()(
Div()(),
),
),
)),
buildOpts: []buildOpt{WithDefaultIndentation()},
},
/* HTMX attributes tests */
{
name: "build input with htmx attributes",
expectedHtml: `<input checked hx-on:click="console.log('hello')"/>`,
givenDOM: Input(Checked(), HxOn("click", "console.log('hello')")),
},
// TODO: fix wrong attribute spaces sort
// i.g.: <input type="checkbox"required required="required"/>
// {
// name: "build all boolean attributes possibilities",
// expectedHtml: `<input type="text" required required="required"/>`,
// givenDOM: Input(Type("checkbox"), Required(), attr("required", "required")),
// },
}
debugLogger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{}))
for _, tc := range testSuite {
t.Run(tc.name, func(t *testing.T) {
st := new(strings.Builder)
opts := []buildOpt{WithLogger(debugLogger), WithWriter(st)}
opts = append(opts, tc.buildOpts...)
err := tc.givenDOM.BuildDOM(opts...)
if err != nil {
t.Error(err)
}
parsedDoc := st.String()
if strings.Compare(parsedDoc, tc.expectedHtml) != 0 {
t.Errorf("result not match: given: [%s], expected: [%s]", parsedDoc, tc.expectedHtml)
}
})
}
}