-
Notifications
You must be signed in to change notification settings - Fork 2
/
form_test.go
114 lines (104 loc) · 2.8 KB
/
form_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
package gosubmit_test
import (
"bytes"
"fmt"
"reflect"
"testing"
. "github.com/jeremija/gosubmit"
)
type errReader struct {
*bytes.Reader
}
func (r *errReader) Read(b []byte) (n int, err error) {
return 0, fmt.Errorf("A test error")
}
func TestParse_error(t *testing.T) {
r := &errReader{Reader: bytes.NewReader([]byte("</dflakugk>"))}
doc := Parse(r)
if doc.Err() == nil {
t.Error("Expected parsing error, but got nil")
}
}
func TestParse_Find(t *testing.T) {
r := bytes.NewReader([]byte("<!DOCTYPE html><html></html>"))
doc := Parse(r)
if err := doc.Err(); err != nil {
t.Fatalf("Unexpected Parse error: %s", err)
}
form := doc.FindForm("name", "test")
expected := "No form with attributes name='test' found"
if err := form.Err(); err == nil || err.Error() != expected {
t.Fatalf("Expected no error '%s' but got %s", expected, err)
}
}
func TestFindFormsByClass(t *testing.T) {
r := bytes.NewReader([]byte(`<!DOCTYPE html>
<html>
<body>
<form class="a b">
<input type="checkbox" name="one-chk" value="two" required>
</form>
<form class="b c">
<input type="checkbox" name="two-chk" value="one">
</form>
</html>
`))
doc := Parse(r)
if err := doc.Err(); err != nil {
t.Fatalf("Unexpected Parse error: %s", err)
}
forms := doc.FindFormsByClass("a")
if size := len(forms); size != 1 {
t.Fatalf("Expected to find one form with class a, but got: %d", size)
}
if !reflect.DeepEqual(forms.First(), forms.Last()) {
t.Fatalf("Expected first and last to be same")
}
forms = doc.FindFormsByClass("b")
if size := len(forms); size != 2 {
t.Fatalf("Expected to find two forms with class b, but got: %d", size)
}
if reflect.DeepEqual(forms.First(), forms.Last()) {
t.Fatalf("Expected forms to be different")
}
forms = doc.FindFormsByClass("c")
if size := len(forms); size != 1 {
t.Fatalf("Expected to find two forms with class c, but got: %d", size)
}
if !reflect.DeepEqual(forms.First(), forms.Last()) {
t.Fatalf("Expected first and last to be same")
}
}
func TestParse_GetOptionsFor(t *testing.T) {
r := bytes.NewReader([]byte(`<!DOCTYPE html>
<html>
<body>
<form>
<input type="checkbox" name="chk" value="one">
<input type="checkbox" name="chk" value="two" required>
</form>
</html>
`))
doc := Parse(r)
if err := doc.Err(); err != nil {
t.Fatalf("Unexpected Parse error: %s", err)
}
form := doc.Forms()[0]
opts := form.GetOptionsFor("chk")
if len(opts) != 2 || opts[0] != "one" || opts[1] != "two" {
t.Errorf("Expected to find two options")
}
opts = form.GetOptionsFor("something-else")
if len(opts) != 0 {
t.Errorf("Expected to find no options")
}
}
func TestFirstForm(t *testing.T) {
var doc Document
_, err := doc.FirstForm().NewTestRequest(
Set("a", "b"),
)
if err == nil || err.Error() != "No forms found" {
t.Errorf("Expected an error 'No forms found', but got %s", err)
}
}