-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage_test.go
91 lines (84 loc) · 1.94 KB
/
message_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
package gometeor
import (
"encoding/json"
"fmt"
"reflect"
"regexp"
"strings"
"testing"
)
type RMStruct1 struct {
IntField int
StringField string
FloatField float64
SliceField []string
}
func TestRawMessage(t *testing.T) {
js := `{"intfield" : 42, "stringfield" : "ohai!", "floatfield":137, "slicefield" : ["a", "x", "q"]}`
var rawmsg RawMessage
err := json.Unmarshal([]byte(js), &rawmsg)
if err != nil {
t.Errorf("Could not unmarshal to RawMessage: %v", err)
}
var rms RMStruct1
err = rawmsg.Decode(&rms)
if err != nil {
t.Errorf("Could not unmarshal RawMessage to object %v", err)
} else {
if rms.IntField != 42 ||
rms.StringField != "ohai!" ||
rms.FloatField != 137.0 ||
!reflect.DeepEqual(rms.SliceField, []string{"a", "x", "q"}) {
t.Errorf("Decoded object was %v", rms)
}
}
buf, err := ToJSON(rms)
if err != nil {
t.Errorf("Error in ToJSON: %v", err)
}
spaces := regexp.MustCompile(`\s+`)
nospaces := string(spaces.ReplaceAll(buf, nil))
if nospaces != strings.Replace(js, " ", "", -1) {
t.Errorf("ToJSON failure: %s", nospaces)
}
}
func TestCall(t *testing.T) {
fn := func(a string, b int, c []string) string {
return fmt.Sprintf("%s %d: %v", a, b, c)
}
args := []json.RawMessage{
json.RawMessage(`"There are"`),
json.RawMessage(`3`),
json.RawMessage(`["a","b","c"]`),
}
result, err := Call(fn, args)
if err != nil {
t.Errorf("Call returned error %v", err)
}
str, ok := result.(string)
if !ok {
t.Errorf("Call result was a %T", str)
}
if str != `There are 3: [a b c]` {
t.Errorf("Call result was %T %v", result, result)
}
fn2 := func(a interface {
Foo()
},) {
a.Foo()
}
args2 := []json.RawMessage{
json.RawMessage(`"An argument"`),
}
result, err = Call(fn2, args2)
if err == nil {
t.Errorf("Bad call returned no error")
}
args2 = []json.RawMessage{
json.RawMessage(`null`),
}
result, err = Call(fn2, args2)
if err == nil {
t.Errorf("Bad call returned no error")
}
}