diff options
Diffstat (limited to 'lib/parse/parse_test.go')
-rw-r--r-- | lib/parse/parse_test.go | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/lib/parse/parse_test.go b/lib/parse/parse_test.go new file mode 100644 index 0000000..3bc837b --- /dev/null +++ b/lib/parse/parse_test.go @@ -0,0 +1,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) + } + } +} |