diff options
Diffstat (limited to 'common_test.go')
-rw-r--r-- | common_test.go | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/common_test.go b/common_test.go new file mode 100644 index 0000000..8e8c673 --- /dev/null +++ b/common_test.go @@ -0,0 +1,76 @@ +package cnp + +import ( + "bytes" + "io" + "io/ioutil" + "reflect" + "strings" +) + +func headerEqual(a, b Header) bool { + return a.Intent == b.Intent && a.VersionMajor == b.VersionMajor && a.VersionMinor == b.VersionMinor && paramEqual(a.Parameters, b.Parameters) +} + +func paramEqual(a, b Parameters) bool { + if len(a) != len(b) { + return false + } + for k := range a { + if a[k] != b[k] { + return false + } + } + return true +} + +func errorEqual(a, b error) bool { + if a == nil && b == nil { + return true + } + return reflect.TypeOf(a) == reflect.TypeOf(b) +} + +func msgEqual(a, b *Message) bool { + return headerEqual(a.Header, b.Header) && bodyEqual(a.Body, b.Body) +} + +func bodyEqual(a, b io.Reader) bool { + if a == nil && b == nil { + return true + } + if a == nil { + a = strings.NewReader("") + } + if b == nil { + b = strings.NewReader("") + } + ba, err := ioutil.ReadAll(a) + if err != nil { + panic(err) + } + if s, ok := a.(io.Seeker); ok { + _, _ = s.Seek(0, io.SeekStart) + } + bb, err := ioutil.ReadAll(b) + if err != nil { + panic(err) + } + if s, ok := b.(io.Seeker); ok { + _, _ = s.Seek(0, io.SeekStart) + } + return bytes.Equal(ba, bb) +} + +type testStringReader struct { + s string +} + +func (r *testStringReader) Read(b []byte) (n int, err error) { + if r.s == "" { + return 0, io.EOF + } + n = copy(b, r.s) + r.s = r.s[n:] + return +} |