This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
package xml
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type testStruct struct {
|
||||
XMLName struct{} `xml:"root"`
|
||||
Name string `xml:"name"`
|
||||
Value int `xml:"value"`
|
||||
}
|
||||
|
||||
func TestEncoder(t *testing.T) {
|
||||
t.Run("successful encode", func(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
data := testStruct{Name: "test", Value: 42}
|
||||
|
||||
err := Encoder(&buf, data)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
|
||||
result := buf.String()
|
||||
if !strings.Contains(result, "<name>test</name>") {
|
||||
t.Errorf("expected result to contain '<name>test</name>', got %q", result)
|
||||
}
|
||||
if !strings.Contains(result, "<value>42</value>") {
|
||||
t.Errorf("expected result to contain '<value>42</value>', got %q", result)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("encode slice", func(t *testing.T) {
|
||||
type Item struct {
|
||||
Value int `xml:"value"`
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
data := []Item{{Value: 1}, {Value: 2}}
|
||||
|
||||
err := Encoder(&buf, data)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
|
||||
result := buf.String()
|
||||
if !strings.Contains(result, "<value>1</value>") {
|
||||
t.Error("expected result to contain first item")
|
||||
}
|
||||
if !strings.Contains(result, "<value>2</value>") {
|
||||
t.Error("expected result to contain second item")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestDecoder(t *testing.T) {
|
||||
t.Run("successful decode", func(t *testing.T) {
|
||||
input := `<root><name>test</name><value>42</value></root>`
|
||||
reader := strings.NewReader(input)
|
||||
|
||||
var result testStruct
|
||||
err := Decoder(reader, &result)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
|
||||
if result.Name != "test" {
|
||||
t.Errorf("expected name 'test', got %q", result.Name)
|
||||
}
|
||||
if result.Value != 42 {
|
||||
t.Errorf("expected value 42, got %d", result.Value)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("decode empty XML", func(t *testing.T) {
|
||||
input := `<root></root>`
|
||||
reader := strings.NewReader(input)
|
||||
|
||||
var result testStruct
|
||||
err := Decoder(reader, &result)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
|
||||
if result.Name != "" {
|
||||
t.Errorf("expected empty name, got %q", result.Name)
|
||||
}
|
||||
if result.Value != 0 {
|
||||
t.Errorf("expected zero value, got %d", result.Value)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("decode invalid XML", func(t *testing.T) {
|
||||
input := `<invalid><unclosed>`
|
||||
reader := strings.NewReader(input)
|
||||
|
||||
var result testStruct
|
||||
err := Decoder(reader, &result)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for invalid XML, got nil")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("decode empty input", func(t *testing.T) {
|
||||
reader := strings.NewReader("")
|
||||
|
||||
var result testStruct
|
||||
err := Decoder(reader, &result)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for empty input, got nil")
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user