x

Programs, configuration and documentation that don't fit anywhere else
Log | Files | Refs | README | LICENSE

atom_test.go (1378B)


      1 package atom
      2 
      3 import (
      4 	"bytes"
      5 	"encoding/xml"
      6 	"fmt"
      7 	"os"
      8 	"path/filepath"
      9 	"testing"
     10 	"time"
     11 )
     12 
     13 func TestMarshal(t *testing.T) {
     14 	f := &Feed{
     15 		Title: "Example Feed",
     16 		Link: []Link{
     17 			{HRef: "http://example.org/"},
     18 		},
     19 		Updated: time.Date(2003, time.Month(12), 13, 18, 30, 2, 0, time.UTC),
     20 		Author:  &Author{Name: "John Doe"},
     21 		ID:      "urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6",
     22 		Entries: []Entry{
     23 			{
     24 				Title: "Atom-Powered Robots Run Amok",
     25 				ID:    "urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a",
     26 				Links: []Link{
     27 					{HRef: "http://example.org/2003/12/13/atom03"},
     28 				},
     29 				Updated: time.Date(2003, time.Month(12), 13, 18, 30, 2, 0, time.UTC),
     30 				Summary: "Some text.",
     31 			},
     32 		},
     33 	}
     34 	got, err := xml.MarshalIndent(f, "", "  ")
     35 	if err != nil {
     36 		t.Fatal(err)
     37 	}
     38 	got = bytes.ReplaceAll(got, []byte("></link>"), []byte("/>"))
     39 
     40 	want, err := os.ReadFile("testdata/1.xml")
     41 	if err != nil {
     42 		t.Fatal(err)
     43 	}
     44 
     45 	if !bytes.Equal(got, want) {
     46 		t.Errorf("oops")
     47 		fmt.Fprintln(os.Stderr, string(got))
     48 	}
     49 }
     50 
     51 func TestDecode(t *testing.T) {
     52 	names, err := filepath.Glob("testdata/*.xml")
     53 	if err != nil {
     54 		t.Fatal(err)
     55 	}
     56 	for _, name := range names {
     57 		f, err := os.Open(name)
     58 		if err != nil {
     59 			t.Fatal(err)
     60 		}
     61 		var feed Feed
     62 		if err := xml.NewDecoder(f).Decode(&feed); err != nil {
     63 			t.Errorf("decode %s: %v", name, err)
     64 		}
     65 		f.Close()
     66 	}
     67 }