-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecoder_test.go
82 lines (73 loc) · 1.8 KB
/
decoder_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
package seltabl
import (
"io"
"strings"
"testing"
)
// DecodeExStruct is a test struct
type DecodeExStruct struct {
A int `json:"a" seltabl:"a" hSel:"tr:nth-child(1)" dSel:"table tr:not(:first-child) td:nth-child(1)" cSel:"$text"`
B int `json:"b" seltabl:"b" hSel:"tr:nth-child(1)" dSel:"table tr:not(:first-child) td:nth-child(2)" cSel:"$text"`
}
// TestDecoder_Decode tests the Decoder.Decode function
func TestDecoder_Decode(t *testing.T) {
testCases := []struct {
name string
input string
expected []DecodeExStruct
hasError bool
}{
{
name: "Valid input",
input: `
<table>
<tr>
<td>a</td>
<td>b</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
</table>
`,
expected: []DecodeExStruct{
{A: 1, B: 2},
{A: 3, B: 4},
},
hasError: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
r := io.NopCloser(strings.NewReader(tc.input))
decoder := NewDecoder[DecodeExStruct](r)
result, err := decoder.Decode()
if tc.hasError {
if err == nil {
t.Errorf("Expected an error, but got none")
}
return
}
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if len(result) != len(tc.expected) {
t.Errorf(
"Expected %d results, but got %d",
len(tc.expected),
len(result),
)
}
for i, expected := range tc.expected {
if result[i].A != expected.A || result[i].B != expected.B {
t.Errorf("Expected %+v, but got %+v", expected, result[i])
}
}
})
}
}