Repository navigation
Expand file tree
/
Copy pathinputs_test.go
More file actions
221 lines (203 loc) · 6 KB
/
Copy pathinputs_test.go
File metadata and controls
221 lines (203 loc) · 6 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package input
// This file contains the shared registry and helpers used by all test files.
// To add a new input:
// 1. Add a case in buildInput()
// 2. Add validation cases in validation_test.go
// 3. Add render cases in render_test.go (if it has custom String())
import (
"testing"
"webtyp.com/fmt"
)
// tc is a compact validation test case.
type tc struct {
t string // input type name (must match a case in buildInput)
name string // subtest name
val string // input value
err string // expected error substring (empty = no error expected)
opts []fmt.KeyValue
req bool
}
// rc is a compact render test case.
type rc struct {
t string // input type name
name string // subtest name
values []string
opts []fmt.KeyValue
contain string // expected substring in HTML output
}
// Shared option sets used across test files.
var opts12 = []fmt.KeyValue{{Key: "1", Value: "Admin"}, {Key: "2", Value: "Editor"}}
var optsGender = []fmt.KeyValue{{Key: "m", Value: "Male"}, {Key: "f", Value: "Female"}}
// buildInput creates a fresh input instance by kind. Add new inputs here.
func buildInput(t *testing.T, kind string, opts []fmt.KeyValue) Input {
t.Helper()
id, name := "tid", "tfield"
var inp Input
switch kind {
case "Address":
inp = Address()
case "Checkbox":
inp = Checkbox()
case "Datalist":
dl := Datalist()
if len(opts) > 0 {
dl.(interface{ SetOptions(...fmt.KeyValue) }).SetOptions(opts...)
}
inp = dl
case "Date":
inp = Date()
case "Decimal":
inp = Decimal()
case "Email":
inp = Email()
case "Filepath":
inp = Filepath()
case "Gender":
g := Gender()
if len(opts) > 0 {
g.(interface{ SetOptions(...fmt.KeyValue) }).SetOptions(opts...)
}
inp = g
case "Hour":
inp = Hour()
case "IP":
inp = IP()
case "Number":
inp = Number()
case "Password":
inp = Password()
case "Phone":
inp = Phone()
case "Radio":
r := Radio()
if len(opts) > 0 {
r.(interface{ SetOptions(...fmt.KeyValue) }).SetOptions(opts...)
}
inp = r
case "Rut":
inp = Rut()
case "Search":
inp = Search()
case "Select":
s := Select()
if len(opts) > 0 {
s.(interface{ SetOptions(...fmt.KeyValue) }).SetOptions(opts...)
}
inp = s
case "Text":
inp = Text()
case "Textarea":
inp = Textarea()
default:
t.Fatalf("unknown input type: %q — add it to buildInput()", kind)
return nil
}
return inp.Clone(id, name)
}
// checkErr asserts the error matches the expected substring (case-insensitive).
func checkErr(t *testing.T, err error, expected string) {
t.Helper()
if expected == "" {
if err != nil {
t.Errorf("expected no error, got %q", err.Error())
}
return
}
if err == nil {
t.Errorf("expected error containing %q, got nil", expected)
return
}
got := fmt.Convert(err.Error()).ToLower().String()
exp := fmt.Convert(expected).ToLower().String()
if !fmt.Contains(got, exp) {
t.Errorf("expected error containing %q, got %q", expected, err.Error())
}
}
func TestIP_Placeholder(t *testing.T) {
// PLAN v0.2.0 item 7: IP accepts IPv4 AND IPv6, so the field name cannot
// teach the shape — the format hint belongs to the type that defines the
// format (the precedent rut.go set). A placeholder mirroring the LABEL
// would be noise; one teaching the format is not.
i := IP()
getter, ok := i.(interface{ GetPlaceholder() string })
if !ok {
t.Fatal("IP() must expose GetPlaceholder")
}
if got := getter.GetPlaceholder(); got != "example: 192.168.1.1" {
t.Errorf("expected IP placeholder 'example: 192.168.1.1', got %q", got)
}
// The translated case lives in tests/placeholder_translate_test.go (WASM):
// the dictionary only exists in the browser page.
}
func TestCanonicalIP(t *testing.T) {
// The same machine reaches a server as ::1 or 127.0.0.1 depending on the
// client (Chrome vs curl on one "localhost"), so an IP stored at setup and
// the one seen at login must be written the same way before they are
// compared — or a trusted device is rejected for spelling, not location.
cases := []struct{ in, want string }{
{"::1", "127.0.0.1"},
{"0:0:0:0:0:0:0:1", "127.0.0.1"},
{"127.0.0.1", "127.0.0.1"},
{"127.1.2.3", "127.0.0.1"},
{"::ffff:127.0.0.1", "127.0.0.1"},
{"::ffff:192.168.1.10", "192.168.1.10"},
{"::FFFF:192.168.1.10", "192.168.1.10"},
{" 192.168.1.10 ", "192.168.1.10"},
{"FE80::1", "fe80::1"},
{"2001:db8::1", "2001:db8::1"},
{"192.168.1.1", "192.168.1.1"},
{"", ""},
}
for _, c := range cases {
if got := CanonicalIP(c.in); got != c.want {
t.Errorf("CanonicalIP(%q) = %q, want %q", c.in, got, c.want)
}
}
}
func TestClone_Preservation(t *testing.T) {
// Create a prototype with custom configuration
proto := Text()
if setter, ok := proto.(interface{ SetPlaceholder(...string) }); ok {
setter.SetPlaceholder("Custom Placeholder")
}
if setter, ok := proto.(interface{ SetTitle(string) }); ok {
setter.SetTitle("Custom Title")
}
proto.AddAttribute("data-test", "value")
proto.SetRequired(true)
// Clone it
cloned := proto.Clone("parent", "field")
// Verify ID and name are updated
if cloned.GetID() != "parent.field" {
t.Errorf("Expected ID 'parent.field', got %q", cloned.GetID())
}
if cloned.FieldName() != "field" {
t.Errorf("Expected name 'field', got %q", cloned.FieldName())
}
// Verify custom configuration is preserved
if getter, ok := cloned.(interface{ GetPlaceholder() string }); ok {
if getter.GetPlaceholder() != "Custom Placeholder" {
t.Errorf("Expected placeholder 'Custom Placeholder', got %q", getter.GetPlaceholder())
}
}
if getter, ok := cloned.(interface{ GetTitle() string }); ok {
if getter.GetTitle() != "Custom Title" {
t.Errorf("Expected title 'Custom Title', got %q", getter.GetTitle())
}
}
// Verify attributes are preserved
attrs := cloned.GetAttributes()
found := false
for _, attr := range attrs {
if attr.Key == "data-test" && attr.Value == "value" {
found = true
break
}
}
if !found {
t.Errorf("Expected attribute data-test=\"value\" to be preserved")
}
if !cloned.IsRequired() {
t.Errorf("Expected required attribute to be preserved")
}
}