diff -r ce9f9f7108c8 -r 74686b8c9146 xmpp_test.go --- a/xmpp_test.go Thu Jan 05 14:32:15 2012 -0700 +++ b/xmpp_test.go Thu Jan 05 19:53:37 2012 -0700 @@ -1,105 +1,105 @@ -// Copyright 2011 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package xmpp - -import ( - "bytes" - "reflect" - "strings" - "sync" - "testing" - "xml" -) - -func TestReadError(t *testing.T) { - r := strings.NewReader(``) - ch := make(chan interface{}) - go readXml(r, ch, make(map[string] func(*xml.Name) interface{})) - x := <- ch - se, ok := x.(*streamError) - if !ok { - t.Fatalf("not StreamError: %v", reflect.TypeOf(x)) - } - assertEquals(t, "bad-foo", se.Any.XMLName.Local) - assertEquals(t, "", se.Any.XMLName.Space) - if se.Text != nil { - t.Errorf("text not nil: %v", se.Text) - } - - r = strings.NewReader(`` + - `Error text`) - ch = make(chan interface{}) - go readXml(r, ch, make(map[string] func(*xml.Name) interface{})) - x = <- ch - se, ok = x.(*streamError) - if !ok { - t.Fatalf("not StreamError: %v", reflect.TypeOf(x)) - } - assertEquals(t, "bad-foo", se.Any.XMLName.Local) - assertEquals(t, "", se.Any.XMLName.Space) - assertEquals(t, "Error text", se.Text.Text) - assertEquals(t, "en", se.Text.Lang) -} - -func TestReadStream(t *testing.T) { - r := strings.NewReader(``) - ch := make(chan interface{}) - go readXml(r, ch, make(map[string] func(*xml.Name) interface{})) - x := <- ch - ss, ok := x.(*stream) - if !ok { - t.Fatalf("not stream: %v", reflect.TypeOf(x)) - } - assertEquals(t, "foo.com", ss.To) - assertEquals(t, "bar.org", ss.From) - assertEquals(t, "42", ss.Id) - assertEquals(t, "1.0", ss.Version) -} - -func testWrite(obj interface{}) string { - w := bytes.NewBuffer(nil) - ch := make(chan interface{}) - var wg sync.WaitGroup - wg.Add(1) - go func() { - defer wg.Done() - writeXml(w, ch) - }() - ch <- obj - close(ch) - wg.Wait() - return w.String() -} - -func TestWriteError(t *testing.T) { - se := &streamError{Any: Generic{XMLName: xml.Name{Local: - "blah"}}} - str := testWrite(se) - exp := `` - assertEquals(t, exp, str) - - se = &streamError{Any: Generic{XMLName: xml.Name{Space: - NsStreams, Local: "foo"}}, Text: - &errText{Lang: "ru", Text: "Пошёл ты"}} - str = testWrite(se) - exp = `Пошёл ты` - assertEquals(t, exp, str) -} - -func TestWriteStream(t *testing.T) { - ss := &stream{To: "foo.org", From: "bar.com", Id: "42", Lang: - "en", Version: "1.0"} - str := testWrite(ss) - exp := `` - assertEquals(t, exp, str) -} +// Copyright 2011 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package xmpp + +import ( + "bytes" + "reflect" + "strings" + "sync" + "testing" + "xml" +) + +func TestReadError(t *testing.T) { + r := strings.NewReader(``) + ch := make(chan interface{}) + go readXml(r, ch, make(map[string] func(*xml.Name) interface{})) + x := <- ch + se, ok := x.(*streamError) + if !ok { + t.Fatalf("not StreamError: %v", reflect.TypeOf(x)) + } + assertEquals(t, "bad-foo", se.Any.XMLName.Local) + assertEquals(t, "", se.Any.XMLName.Space) + if se.Text != nil { + t.Errorf("text not nil: %v", se.Text) + } + + r = strings.NewReader(`` + + `Error text`) + ch = make(chan interface{}) + go readXml(r, ch, make(map[string] func(*xml.Name) interface{})) + x = <- ch + se, ok = x.(*streamError) + if !ok { + t.Fatalf("not StreamError: %v", reflect.TypeOf(x)) + } + assertEquals(t, "bad-foo", se.Any.XMLName.Local) + assertEquals(t, "", se.Any.XMLName.Space) + assertEquals(t, "Error text", se.Text.Text) + assertEquals(t, "en", se.Text.Lang) +} + +func TestReadStream(t *testing.T) { + r := strings.NewReader(``) + ch := make(chan interface{}) + go readXml(r, ch, make(map[string] func(*xml.Name) interface{})) + x := <- ch + ss, ok := x.(*stream) + if !ok { + t.Fatalf("not stream: %v", reflect.TypeOf(x)) + } + assertEquals(t, "foo.com", ss.To) + assertEquals(t, "bar.org", ss.From) + assertEquals(t, "42", ss.Id) + assertEquals(t, "1.0", ss.Version) +} + +func testWrite(obj interface{}) string { + w := bytes.NewBuffer(nil) + ch := make(chan interface{}) + var wg sync.WaitGroup + wg.Add(1) + go func() { + defer wg.Done() + writeXml(w, ch) + }() + ch <- obj + close(ch) + wg.Wait() + return w.String() +} + +func TestWriteError(t *testing.T) { + se := &streamError{Any: Generic{XMLName: xml.Name{Local: + "blah"}}} + str := testWrite(se) + exp := `` + assertEquals(t, exp, str) + + se = &streamError{Any: Generic{XMLName: xml.Name{Space: + NsStreams, Local: "foo"}}, Text: + &errText{Lang: "ru", Text: "Пошёл ты"}} + str = testWrite(se) + exp = `Пошёл ты` + assertEquals(t, exp, str) +} + +func TestWriteStream(t *testing.T) { + ss := &stream{To: "foo.org", From: "bar.com", Id: "42", Lang: + "en", Version: "1.0"} + str := testWrite(ss) + exp := `` + assertEquals(t, exp, str) +}