-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors_test.go
145 lines (130 loc) · 4.2 KB
/
errors_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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package seltabl
import (
"reflect"
"strings"
"testing"
)
// TestErrNoDataFound_Error tests the Error method of ErrNoDataFound with a valid HTML document.
// It checks if the error message contains all the necessary information including the selector,
// field name, types, and the HTML content.
func TestErrNoDataFound_Error(t *testing.T) {
field := reflect.StructField{
Name: "TestField",
Type: reflect.TypeOf(""),
}
cfg := &SelectorConfig{
QuerySelector: "test-selector",
}
err := &ErrNoDataFound{
Typ: reflect.TypeOf(struct{}{}),
Field: field,
Cfg: cfg,
}
errorMsg := err.Error()
expectedParts := []string{
"no data found",
"struct",
}
for _, part := range expectedParts {
if !strings.Contains(errorMsg, part) {
t.Errorf("Expected error message to contain '%s', but it doesn't it is '%s'", part, errorMsg)
}
}
}
// TestErrNoDataFound_Error_FailedHtml tests the Error method of ErrNoDataFound when HTML generation fails.
// It verifies that the error message indicates a failure in getting the HTML content.
func TestErrNoDataFound_Error_FailedHtml(t *testing.T) {
field := reflect.StructField{
Name: "TestField",
Type: reflect.TypeOf(""),
}
cfg := &SelectorConfig{
QuerySelector: "test-selector",
}
err := &ErrNoDataFound{
Typ: reflect.TypeOf(struct{}{}),
Field: field,
Cfg: cfg,
}
errorMsg := err.Error()
expected := "no data found"
if !strings.Contains(errorMsg, expected) {
t.Errorf("Expected error message to contain '%s', but it doesn't it is '%s'", expected, errorMsg)
}
}
// TestErrSelectorNotFound_Error tests the Error method of ErrSelectorNotFound with a valid HTML document.
// It ensures the error message includes the selector, field name, types, and HTML content.
func TestErrSelectorNotFound_Error(t *testing.T) {
field := reflect.StructField{
Name: "TestField",
Type: reflect.TypeOf(""),
}
cfg := &SelectorConfig{
QuerySelector: "test-selector",
}
err := &ErrSelectorNotFound{
Typ: reflect.TypeOf(struct{}{}),
Field: field,
Cfg: cfg,
}
errorMsg := err.Error()
expectedParts := []string{
"selector test-selector",
"with type struct {",
"not found for field TestField",
"with type string",
}
for _, part := range expectedParts {
if !strings.Contains(errorMsg, part) {
t.Errorf("Expected error message to contain '%s', but it doesn't", part)
}
}
}
// TestErrSelectorNotFound_Error_FailedHtml tests the Error method of ErrSelectorNotFound when HTML generation fails.
// It checks if the error message indicates a failure in getting the HTML content.
func TestErrSelectorNotFound_Error_FailedHtml(t *testing.T) {
field := reflect.StructField{
Name: "TestField",
Type: reflect.TypeOf(""),
}
cfg := &SelectorConfig{
QuerySelector: "test-selector",
}
err := &ErrSelectorNotFound{
Typ: reflect.TypeOf(struct{}{}),
Field: field,
Cfg: cfg,
}
errorMsg := err.Error()
expected := "not found"
if !strings.Contains(errorMsg, expected) {
t.Errorf("Expected error message to contain '%s', but it doesn't it is '%s'", expected, errorMsg)
}
}
func TestErrNoDataFound_Error2(t *testing.T) {
errNoDataFound := &ErrNoDataFound{
Typ: reflect.TypeOf("string"),
Field: reflect.StructField{Name: "TestField", Type: reflect.TypeOf("string")},
Cfg: &SelectorConfig{QuerySelector: "div.invalid"},
}
expected := "failed to get data rows html: EOF"
if strings.Contains(errNoDataFound.Error(), expected) {
t.Errorf("ErrNoDataFound.Error() should not contain '%s', but it does", expected)
}
}
// TestErrSelectorNotFound_Error2 tests the Error method of ErrSelectorNotFound
// with a valid HTML document.
//
// It ensures the error message includes the selector, field name, types, and
// HTML content.
func TestErrSelectorNotFound_Error2(t *testing.T) {
errSelectorNotFound := &ErrSelectorNotFound{
Typ: reflect.TypeOf("string"),
Field: reflect.StructField{Name: "TestField", Type: reflect.TypeOf("string")},
Cfg: &SelectorConfig{QuerySelector: "div.invalid"},
}
expected := "selector div.invalid with type string not found for field TestField with type string\n html: EOF"
if strings.Contains(errSelectorNotFound.Error(), expected) {
t.Errorf("ErrSelectorNotFound.Error() should not contain '%s', but it does", expected)
}
}