-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions_test.go
62 lines (56 loc) · 1.66 KB
/
options_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
// Copyright (c) 2023 William Dode. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
package webo
import (
"html/template"
"strings"
"testing"
)
type O struct {
v int
label string
grp string
attr string
}
func (o *O) OptionValue() int {
return o.v
}
func (o *O) OptionLabel() string {
return o.label
}
func (o *O) OptionAttrs() template.HTMLAttr {
return template.HTMLAttr(o.attr)
}
func (o *O) OptionGroup() string {
return o.grp
}
func equal(s, t string) bool {
s = strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(s, " ", ""), "\n", ""), "\t", "")
t = strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(t, " ", ""), "\n", ""), "\t", "")
// log.Println("S=", s)
// log.Println("T=", t)
return s == t
}
func TestOptions(t *testing.T) {
os := []*O{&O{1, "un", "", "class='cls' style='color:red'"}, &O{2, "deux", "", ""}}
res := string(FmtOptions(os, 2))
if !equal(res, "<option value='1' class='cls' style='color:red'>un</option><option value='2' selected>deux</option>") {
t.Fatalf("Option : %s", res)
}
}
func TestOptionsGroup(t *testing.T) {
os := []*O{&O{1, "un", "g1", ""}, &O{2, "deux", "g1", "class='cls' style='color:red'"}, &O{3, "trois", "g2", ""}, &O{4, "quatre", "g2", ""}}
res := string(FmtOptionsGroup(os, 2))
if !equal(res, `
<optgroup label='g1'>
<option value='1' >un</option>
<option value='2' class='cls' style='color:red' selected>deux</option>
</optgroup>
<optgroup label='g2'>
<option value='3' >trois</option>
<option value='4' >quatre</option>
</optgroup>
`) {
t.Fatalf("OptionGroup : %s", res)
}
}