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
|
package parse
import (
"testing"
"strings"
)
func TestReadUntil(t *testing.T) {
s := "until="
want := "until"
msg, _, err := ReadUntil(strings.NewReader(s), []byte{'='})
if err != nil {
t.Fatal(err)
}
if want != msg {
t.Errorf(`ReadUntil(strings.NewReader(s), []byte{'='}) = %q, %v want "", error`, msg, err)
}
}
func TestReadTag(t *testing.T) {
tag := "<hello>"
want := new(Tag)
want.name = "hello"
r := strings.NewReader(tag)
b := make([]byte, 1)
// Consume '<'
_, _ = r.Read(b)
msg, err := ReadTag(r)
if err != nil {
t.Fatal(err)
}
if want.name != msg.name {
t.Errorf(`ReadTag(strings.NewReader("<hello>")) = %q, %v, want "", error`, msg.name, err)
}
}
func TestReadTagAttributes(t *testing.T) {
tag := `<hello attribute=value and="another one">`
want := new(Tag)
want.attributes = make(map[string]string)
want.attributes["attribute"] = "value"
want.attributes["and"] = "another one"
r := strings.NewReader(tag)
b := make([]byte, 1)
// Consume '<'
_, _ = r.Read(b)
msg, err := ReadTag(r)
if err != nil {
t.Fatal(err)
}
if want.attributes["attribute"] != msg.attributes["attribute"] {
t.Errorf(`ReadTag(strings.NewReader(<hello attribute=value and="another one">)) = %q, %v, want "", error`, msg.attributes["attribute"], err)
}
}
func TestParseTagContents(t *testing.T) {
elementString := `<p>Contents</p>`
want := make([]any, 3)
{
e := new(Tag)
e.name = "p"
want[0] = e
}
want[1] = "Contents"
{
e := new(Tag)
e.name = "/p"
want[2] = e
}
msg, err := Parse(strings.NewReader(elementString))
if err != nil {
t.Fatal(err)
}
for e := range msg {
same := true
switch msg[e].(type) {
case Tag:
same = msg[e].(Tag).name == want[e].(Tag).name
for k := range msg[e].(Tag).attributes {
if msg[e].(Tag).attributes[k] != want[e].(Tag).attributes[k] {
same = false
break
}
}
case string:
same = msg[e] == want[e]
}
if !same {
t.Errorf(`Parse(strings.NewReader(elementString)) = %q, %v, want "", error`, msg[e], err)
}
}
}
|