-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalue_test.go
More file actions
292 lines (271 loc) · 8.2 KB
/
Copy pathvalue_test.go
File metadata and controls
292 lines (271 loc) · 8.2 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package forms
import (
"errors"
"strings"
"testing"
"github.com/go-pdfkit/reader"
)
// choiceDoc is a choice field with two rows and whatever flags the test wants.
func choiceDoc(t *testing.T, flags int64) *Form {
t.Helper()
_, f := textFieldDoc(t, reader.Dict{
"FT": reader.Name("Ch"), "Ff": reader.Integer(flags),
"Opt": reader.Array{
reader.Array{str("FR"), str("France")},
reader.Array{str("BE"), str("Belgique")},
},
})
return f
}
// buttonDoc is a checkbox whose widget names Yes as its chosen state.
func buttonDoc(t *testing.T, flags int64) *Form {
t.Helper()
d := formDoc(t, func(w *reader.Writer, page reader.Ref) (reader.Dict, reader.Array) {
blank := w.Add(&reader.Stream{Dict: reader.Dict{"BBox": nums(0, 0, 10, 10)}, Raw: []byte("")})
ref := w.Add(reader.Dict{"T": str("tick"), "FT": reader.Name("Btn"),
"Ff": reader.Integer(flags), "Subtype": reader.Name("Widget"),
"Rect": nums(0, 0, 12, 12), "P": page,
"AP": reader.Dict{"N": reader.Dict{"Off": blank, "Yes": blank}}})
return reader.Dict{"Fields": reader.Array{ref}}, reader.Array{ref}
})
f, ok := Read(d)
if !ok {
t.Fatal("no form")
}
return f
}
func TestPuttingAValueInATextField(t *testing.T) {
_, f := textFieldDoc(t, nil)
fld := f.Fields()[0]
if err := fld.SetText("Mozart"); err != nil {
t.Fatal(err)
}
if fld.Value != "Mozart" || !fld.Changed() {
t.Errorf("holds %q, changed %v", fld.Value, fld.Changed())
}
if len(f.Changed()) != 1 {
t.Errorf("the form lists %d changed fields", len(f.Changed()))
}
}
func TestATextFieldKeepsToItsLength(t *testing.T) {
_, f := textFieldDoc(t, reader.Dict{"MaxLen": reader.Integer(4)})
fld := f.Fields()[0]
if err := fld.SetText("Mozart"); err != nil {
t.Fatal(err)
}
if fld.Value != "Moza" {
t.Errorf("holds %q, wanted it cut to four", fld.Value)
}
}
func TestASingleLineFieldHoldsOneLine(t *testing.T) {
// A newline in a box that shows one line would run off the end of it
// rather than wrap, so it becomes a space.
_, f := textFieldDoc(t, nil)
fld := f.Fields()[0]
if err := fld.SetText("one\r\ntwo\nthree\rfour"); err != nil {
t.Fatal(err)
}
if strings.ContainsAny(fld.Value, "\r\n") {
t.Errorf("holds %q", fld.Value)
}
_, g := textFieldDoc(t, reader.Dict{"Ff": reader.Integer(flagMultiline)})
multi := g.Fields()[0]
if err := multi.SetText("one\ntwo"); err != nil {
t.Fatal(err)
}
if multi.Value != "one\ntwo" {
t.Errorf("a field that takes several lines holds %q", multi.Value)
}
}
func TestAReadOnlyFieldIsNotFilledIn(t *testing.T) {
_, f := textFieldDoc(t, reader.Dict{"Ff": reader.Integer(flagReadOnly)})
fld := f.Fields()[0]
for _, err := range []error{
fld.SetText("x"), fld.SetChecked(true), fld.Press("Yes"), fld.Choose("x"),
} {
if !errors.Is(err, ErrReadOnly) {
t.Errorf("a read-only field gave %v", err)
}
}
if fld.Changed() {
t.Error("a read-only field was marked as changed")
}
}
func TestAFieldRefusesTheWrongSortOfValue(t *testing.T) {
_, text := textFieldDoc(t, nil)
if err := text.Fields()[0].SetChecked(true); !errors.Is(err, ErrKind) {
t.Errorf("ticking a text field gave %v", err)
}
if err := text.Fields()[0].Press("Yes"); !errors.Is(err, ErrKind) {
t.Errorf("pressing a text field gave %v", err)
}
if err := text.Fields()[0].Choose("a"); !errors.Is(err, ErrKind) {
t.Errorf("choosing in a text field gave %v", err)
}
tick := buttonDoc(t, 0)
if err := tick.Fields()[0].SetText("x"); !errors.Is(err, ErrKind) {
t.Errorf("typing into a checkbox gave %v", err)
}
}
func TestTickingABox(t *testing.T) {
f := buttonDoc(t, 0)
fld := f.Fields()[0]
if err := fld.SetChecked(true); err != nil {
t.Fatal(err)
}
if fld.Value != "Yes" || !fld.Checked() {
t.Errorf("holds %q, checked %v", fld.Value, fld.Checked())
}
if err := fld.SetChecked(false); err != nil {
t.Fatal(err)
}
if fld.Value != "Off" || fld.Checked() {
t.Errorf("unticked, holds %q, checked %v", fld.Value, fld.Checked())
}
}
func TestTickingABoxThatNamesNoState(t *testing.T) {
// A widget with no appearance dictionary has no name for chosen, and the
// format's own default is used.
_, f := textFieldDoc(t, reader.Dict{"FT": reader.Name("Btn")})
fld := f.Fields()[0]
if err := fld.SetChecked(true); err != nil {
t.Fatal(err)
}
if fld.Value != "Yes" {
t.Errorf("holds %q, wanted Yes", fld.Value)
}
}
func TestPressingOneButtonOfAGroup(t *testing.T) {
f := buttonDoc(t, flagRadio)
fld := f.Fields()[0]
if err := fld.Press("Yes"); err != nil {
t.Fatal(err)
}
if fld.Value != "Yes" {
t.Errorf("holds %q", fld.Value)
}
if err := fld.Press("Off"); err != nil {
t.Fatal(err)
}
if err := fld.Press("Nowhere"); !errors.Is(err, ErrKind) {
t.Errorf("pressing a button that does not exist gave %v", err)
}
}
func TestChoosingRows(t *testing.T) {
f := choiceDoc(t, flagCombo)
fld := f.Fields()[0]
if err := fld.Choose("FR"); err != nil {
t.Fatal(err)
}
if fld.Value != "FR" || fld.Text() != "France" {
t.Errorf("holds %q and shows %q", fld.Value, fld.Text())
}
if err := fld.Choose(); err != nil {
t.Fatal(err)
}
if fld.Value != "" {
t.Errorf("after choosing nothing it holds %q", fld.Value)
}
if err := fld.Choose("XX"); !errors.Is(err, ErrKind) {
t.Errorf("choosing a row that does not exist gave %v", err)
}
if err := fld.Choose("FR", "BE"); !errors.Is(err, ErrKind) {
t.Errorf("choosing two rows of a field that takes one gave %v", err)
}
}
func TestChoosingSeveralRows(t *testing.T) {
f := choiceDoc(t, flagMultiSelect)
fld := f.Fields()[0]
if err := fld.Choose("FR", "BE"); err != nil {
t.Fatal(err)
}
if fld.Value != "FR" || len(fld.Values) != 2 {
t.Errorf("holds %q and %v", fld.Value, fld.Values)
}
if got := fld.selected(); len(got) != 2 {
t.Errorf("chose %v", got)
}
}
func TestAComboBoxThatTakesSomethingOfItsOwn(t *testing.T) {
f := choiceDoc(t, flagCombo|flagEdit)
fld := f.Fields()[0]
if err := fld.Choose("Andorre"); err != nil {
t.Errorf("an editable combo box refused something not on its list: %v", err)
}
if err := fld.SetText("Monaco"); err != nil {
t.Errorf("an editable combo box refused typing: %v", err)
}
fixed := choiceDoc(t, flagCombo)
if err := fixed.Fields()[0].SetText("Monaco"); !errors.Is(err, ErrKind) {
t.Errorf("a fixed combo box took typing: %v", err)
}
if err := fixed.Fields()[0].SetText("France"); err != nil {
t.Errorf("a fixed combo box refused one of its own rows: %v", err)
}
}
func TestFillingAFieldByName(t *testing.T) {
_, f := textFieldDoc(t, nil)
if err := f.Fill("name", "Mozart"); err != nil {
t.Fatal(err)
}
if f.Fields()[0].Value != "Mozart" {
t.Errorf("holds %q", f.Fields()[0].Value)
}
if err := f.Fill("nothing", "x"); err == nil {
t.Error("filling a field that does not exist was allowed")
}
}
func TestFillingABoxByName(t *testing.T) {
for _, c := range []struct {
given string
want string
}{
{"yes", "Yes"}, {"true", "Yes"}, {"1", "Yes"}, {"on", "Yes"},
{"no", "Off"}, {"false", "Off"}, {"0", "Off"}, {"", "Off"}, {"off", "Off"},
{"Yes", "Yes"},
} {
f := buttonDoc(t, 0)
if err := f.Fill("tick", c.given); err != nil {
t.Fatalf("%q: %v", c.given, err)
}
if got := f.Fields()[0].Value; got != c.want {
t.Errorf("%q left it holding %q, wanted %q", c.given, got, c.want)
}
}
}
func TestFillingAChoiceByName(t *testing.T) {
f := choiceDoc(t, flagCombo)
if err := f.Fill("name", "FR"); err != nil {
t.Fatal(err)
}
if f.Fields()[0].Value != "FR" {
t.Errorf("holds %q", f.Fields()[0].Value)
}
}
func TestSomeFieldsAreNotThingsToFillIn(t *testing.T) {
for _, kind := range []reader.Name{"Btn", "Sig"} {
flags := int64(0)
if kind == "Btn" {
flags = flagPushButton
}
_, f := textFieldDoc(t, reader.Dict{"FT": kind, "Ff": reader.Integer(flags)})
if err := f.Fill("name", "x"); !errors.Is(err, ErrKind) {
t.Errorf("%s: filling it gave %v", kind, err)
}
}
}
func TestWhatAFieldShows(t *testing.T) {
// A choice field shows the label its value stands for; everything else
// shows its value.
_, f := textFieldDoc(t, reader.Dict{"V": str("plain")})
if got := f.Fields()[0].Text(); got != "plain" {
t.Errorf("a text field shows %q", got)
}
c := choiceDoc(t, flagCombo)
if err := c.Fields()[0].Choose("BE"); err != nil {
t.Fatal(err)
}
if got := c.Fields()[0].Text(); got != "Belgique" {
t.Errorf("a chosen row shows %q", got)
}
}