summaryrefslogtreecommitdiffstats
path: root/common_test.go
diff options
context:
space:
mode:
authorclsr <clsr@clsr.net>2017-08-18 13:46:10 +0200
committerclsr <clsr@clsr.net>2017-08-18 13:46:10 +0200
commitda77deba78c8a7447b4a38324d2422a5df293b26 (patch)
treebb94b8e18eaf27a86e9fc21c4342d9e0a605baf7 /common_test.go
downloadcnp-go-da77deba78c8a7447b4a38324d2422a5df293b26.tar.gz
cnp-go-da77deba78c8a7447b4a38324d2422a5df293b26.zip
Initial commitv0.1.0
Diffstat (limited to 'common_test.go')
-rw-r--r--common_test.go76
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
+}