|
| 1 | +package xml |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestMarshalPerson(t *testing.T) { |
| 9 | + person := Person{ |
| 10 | + Name: "Alice", |
| 11 | + Age: 30, |
| 12 | + Email: "alice@example.com", |
| 13 | + } |
| 14 | + |
| 15 | + data, err := MarshalPerson(person) |
| 16 | + if err != nil { |
| 17 | + t.Fatalf("MarshalPerson() error = %v", err) |
| 18 | + } |
| 19 | + |
| 20 | + xmlStr := string(data) |
| 21 | + if !strings.Contains(xmlStr, "<name>Alice</name>") { |
| 22 | + t.Errorf("Expected XML to contain <name>Alice</name>, got %s", xmlStr) |
| 23 | + } |
| 24 | + if !strings.Contains(xmlStr, "<age>30</age>") { |
| 25 | + t.Errorf("Expected XML to contain <age>30</age>, got %s", xmlStr) |
| 26 | + } |
| 27 | + if !strings.Contains(xmlStr, "<email>alice@example.com</email>") { |
| 28 | + t.Errorf("Expected XML to contain <email>alice@example.com</email>, got %s", xmlStr) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func TestUnmarshalPerson(t *testing.T) { |
| 33 | + xmlData := []byte(`<Person><name>Bob</name><age>25</age><email>bob@example.com</email></Person>`) |
| 34 | + |
| 35 | + person, err := UnmarshalPerson(xmlData) |
| 36 | + if err != nil { |
| 37 | + t.Fatalf("UnmarshalPerson() error = %v", err) |
| 38 | + } |
| 39 | + |
| 40 | + if person.Name != "Bob" { |
| 41 | + t.Errorf("Expected Name to be Bob, got %s", person.Name) |
| 42 | + } |
| 43 | + if person.Age != 25 { |
| 44 | + t.Errorf("Expected Age to be 25, got %d", person.Age) |
| 45 | + } |
| 46 | + if person.Email != "bob@example.com" { |
| 47 | + t.Errorf("Expected Email to be bob@example.com, got %s", person.Email) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestMarshalBook(t *testing.T) { |
| 52 | + book := Book{ |
| 53 | + Title: "Go Programming", |
| 54 | + Author: "John Doe", |
| 55 | + Year: 2023, |
| 56 | + } |
| 57 | + |
| 58 | + data, err := MarshalBook(book) |
| 59 | + if err != nil { |
| 60 | + t.Fatalf("MarshalBook() error = %v", err) |
| 61 | + } |
| 62 | + |
| 63 | + xmlStr := string(data) |
| 64 | + if !strings.Contains(xmlStr, "<title>Go Programming</title>") { |
| 65 | + t.Errorf("Expected XML to contain <title>Go Programming</title>, got %s", xmlStr) |
| 66 | + } |
| 67 | + if !strings.Contains(xmlStr, "<author>John Doe</author>") { |
| 68 | + t.Errorf("Expected XML to contain <author>John Doe</author>, got %s", xmlStr) |
| 69 | + } |
| 70 | + if !strings.Contains(xmlStr, "<year>2023</year>") { |
| 71 | + t.Errorf("Expected XML to contain <year>2023</year>, got %s", xmlStr) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestUnmarshalBook(t *testing.T) { |
| 76 | + xmlData := []byte(`<Book><title>Learning XML</title><author>Jane Smith</author><year>2024</year></Book>`) |
| 77 | + |
| 78 | + book, err := UnmarshalBook(xmlData) |
| 79 | + if err != nil { |
| 80 | + t.Fatalf("UnmarshalBook() error = %v", err) |
| 81 | + } |
| 82 | + |
| 83 | + if book.Title != "Learning XML" { |
| 84 | + t.Errorf("Expected Title to be Learning XML, got %s", book.Title) |
| 85 | + } |
| 86 | + if book.Author != "Jane Smith" { |
| 87 | + t.Errorf("Expected Author to be Jane Smith, got %s", book.Author) |
| 88 | + } |
| 89 | + if book.Year != 2024 { |
| 90 | + t.Errorf("Expected Year to be 2024, got %d", book.Year) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestMarshalUnmarshalRoundTrip(t *testing.T) { |
| 95 | + originalPerson := Person{ |
| 96 | + Name: "Charlie", |
| 97 | + Age: 35, |
| 98 | + Email: "charlie@example.com", |
| 99 | + } |
| 100 | + |
| 101 | + // Marshal |
| 102 | + data, err := MarshalPerson(originalPerson) |
| 103 | + if err != nil { |
| 104 | + t.Fatalf("MarshalPerson() error = %v", err) |
| 105 | + } |
| 106 | + |
| 107 | + // Unmarshal |
| 108 | + parsedPerson, err := UnmarshalPerson(data) |
| 109 | + if err != nil { |
| 110 | + t.Fatalf("UnmarshalPerson() error = %v", err) |
| 111 | + } |
| 112 | + |
| 113 | + // Compare |
| 114 | + if parsedPerson.Name != originalPerson.Name { |
| 115 | + t.Errorf("Name mismatch: got %s, want %s", parsedPerson.Name, originalPerson.Name) |
| 116 | + } |
| 117 | + if parsedPerson.Age != originalPerson.Age { |
| 118 | + t.Errorf("Age mismatch: got %d, want %d", parsedPerson.Age, originalPerson.Age) |
| 119 | + } |
| 120 | + if parsedPerson.Email != originalPerson.Email { |
| 121 | + t.Errorf("Email mismatch: got %s, want %s", parsedPerson.Email, originalPerson.Email) |
| 122 | + } |
| 123 | +} |
0 commit comments