This repository has been archived by the owner on Sep 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
encode_test.go
executable file
·113 lines (104 loc) · 3.27 KB
/
encode_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2015 Alex Browne. All rights reserved.
// Use of this source code is governed by the MIT
// license, which can be found in the LICENSE file.
package jobs
import (
"reflect"
"testing"
)
// NOTE:
// I know this code isn't very dry. Unfortunately, this is due to a restriction
// in reflect and gob packages. We can't have a general test case or test function
// that treats different types as interfaces, because then gob doesn't know what type
// to attempt to decode into. So we have to test the different types manually.
func TestConvertInt(t *testing.T) {
v := int(7)
// Encode v to a slice of bytes
reply, err := encode(v)
if err != nil {
t.Errorf("Unexpected error in encode: %s", err.Error())
}
// Decode reply and write results to the holder
holder := int(0)
if err := decode(reply, &holder); err != nil {
t.Errorf("Unexpected error in decode: %s", err.Error())
}
// Now the holder and the original should be equal. If they're not,
// there was a problem
expectEncodeDecodeEquals(t, v, holder)
}
func TestConvertString(t *testing.T) {
v := "test"
// Encode v to a slice of bytes
reply, err := encode(v)
if err != nil {
t.Errorf("Unexpected error in encode: %s", err.Error())
}
// Decode reply and write results to the holder
holder := ""
if err := decode(reply, &holder); err != nil {
t.Errorf("Unexpected error in decode: %s", err.Error())
}
// Now the holder and the original should be equal. If they're not,
// there was a problem
expectEncodeDecodeEquals(t, v, holder)
}
func TestConvertBool(t *testing.T) {
v := true
// Encode v to a slice of bytes
reply, err := encode(v)
if err != nil {
t.Errorf("Unexpected error in encode: %s", err.Error())
}
// Decode reply and write results to the holder
holder := false
if err := decode(reply, &holder); err != nil {
t.Errorf("Unexpected error in decode: %s", err.Error())
}
// Now the holder and the original should be equal. If they're not,
// there was a problem
expectEncodeDecodeEquals(t, v, holder)
}
func TestConvertStruct(t *testing.T) {
v := struct {
Name string
Age int
}{"test person", 23}
// Encode v to a slice of bytes
reply, err := encode(v)
if err != nil {
t.Errorf("Unexpected error in encode: %s", err.Error())
}
// Decode reply and write results to the holder
holder := struct {
Name string
Age int
}{}
if err := decode(reply, &holder); err != nil {
t.Errorf("Unexpected error in decode: %s", err.Error())
}
// Now the holder and the original should be equal. If they're not,
// there was a problem
expectEncodeDecodeEquals(t, v, holder)
}
func TestConvertSlice(t *testing.T) {
v := []string{"a", "b", "c"}
// Encode v to a slice of bytes
reply, err := encode(v)
if err != nil {
t.Errorf("Unexpected error in encode: %s", err.Error())
}
// Decode reply and write results to the holder
holder := []string{}
if err := decode(reply, &holder); err != nil {
t.Errorf("Unexpected error in decode: %s", err.Error())
}
// Now the holder and the original should be equal. If they're not,
// there was a problem
expectEncodeDecodeEquals(t, v, holder)
}
func expectEncodeDecodeEquals(t *testing.T, expected, got interface{}) {
if !reflect.DeepEqual(expected, got) {
t.Errorf("Error encoding/decoding type %T. Expected %v but got %v.", expected, expected, got)
}
}