structs_test.go
changeset 3 6121aa2f21b1
parent 2 4dabfef08c8c
child 6 8e425e340ca1
equal deleted inserted replaced
2:4dabfef08c8c 3:6121aa2f21b1
     7 import (
     7 import (
     8 	"bytes"
     8 	"bytes"
     9 	"testing"
     9 	"testing"
    10 	"xml"
    10 	"xml"
    11 )
    11 )
       
    12 
       
    13 func assertEquals(t *testing.T, expected, observed string) {
       
    14 	if expected != observed {
       
    15 		t.Errorf("Expected:\n%s\nObserved:\n%s\n", expected,
       
    16 			observed)
       
    17 	}
       
    18 }
       
    19 
       
    20 func TestJid(t *testing.T) {
       
    21 	str := "user@domain/res"
       
    22 	jid := &JID{}
       
    23 	if !jid.Set(str) {
       
    24 		t.Errorf("Set(%s) failed\n", str)
       
    25 	}
       
    26 	assertEquals(t, "user", *jid.Node)
       
    27 	assertEquals(t, "domain", jid.Domain)
       
    28 	assertEquals(t, "res", *jid.Resource)
       
    29 	assertEquals(t, str, jid.String())
       
    30 
       
    31 	str = "domain.tld"
       
    32 	if !jid.Set(str) {
       
    33 		t.Errorf("Set(%s) failed\n", str)
       
    34 	}
       
    35 	if jid.Node != nil {
       
    36 		t.Errorf("Node: %v\n", *jid.Node)
       
    37 	}
       
    38 	assertEquals(t, "domain.tld", jid.Domain)
       
    39 	if jid.Resource != nil {
       
    40 		t.Errorf("Resource: %v\n", *jid.Resource)
       
    41 	}
       
    42 	assertEquals(t, str, jid.String())
       
    43 }
       
    44 
       
    45 func assertMarshal(t *testing.T, expected string, marshal interface{}) {
       
    46 	buf := bytes.NewBuffer(nil)
       
    47 	xml.Marshal(buf, marshal)
       
    48 	observed := string(buf.Bytes())
       
    49 	assertEquals(t, expected, observed)
       
    50 }
    12 
    51 
    13 func TestStreamMarshal(t *testing.T) {
    52 func TestStreamMarshal(t *testing.T) {
    14 	s := &Stream{to: "bob"}
    53 	s := &Stream{to: "bob"}
    15 	exp := `<stream:stream to="bob">`
    54 	exp := `<stream:stream to="bob">`
    16 	assertMarshal(t, exp, s)
    55 	assertMarshal(t, exp, s)
    36 	exp = `<stream:error><ack xmlns="` + nsStreams +
    75 	exp = `<stream:error><ack xmlns="` + nsStreams +
    37 		`"></ack><text xmlns="` + nsStreams +
    76 		`"></ack><text xmlns="` + nsStreams +
    38 		`" xml:lang="pt">things happen</text></stream:error>`
    77 		`" xml:lang="pt">things happen</text></stream:error>`
    39 	assertMarshal(t, exp, e)
    78 	assertMarshal(t, exp, e)
    40 }
    79 }
    41 
       
    42 func assertMarshal(t *testing.T, expected string, marshal interface{}) {
       
    43 	buf := bytes.NewBuffer(nil)
       
    44 	xml.Marshal(buf, marshal)
       
    45 	observed := string(buf.Bytes())
       
    46 	if expected != observed {
       
    47 		t.Errorf("Expected:\n%s\nObserved:\n%s\n", expected,
       
    48 			observed)
       
    49 	}
       
    50 }