forked from rockorager/go-jmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
invocation_test.go
55 lines (48 loc) · 1.1 KB
/
invocation_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
package jmap
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
type test struct {
Hello string
}
func newTest() MethodResponse {
return &test{}
}
func TestInvocationMarshal(t *testing.T) {
RegisterMethod("Test/method", newTest)
assert := assert.New(t)
inv := &Invocation{
Name: "Test/method",
Args: &struct {
Hello string
}{
Hello: "world",
},
CallID: "0",
}
data, err := json.Marshal(inv)
assert.NoError(err)
if err != nil {
t.Logf("expected: no error")
t.Logf("actual: %v", err)
t.FailNow()
}
expected := `["Test/method",{"Hello":"world"},"0"]`
assert.Equal(expected, string(data))
}
func TestInvocationUnmarshal(t *testing.T) {
RegisterMethod("Test/method", newTest)
assert := assert.New(t)
raw := []byte(`["Test/method",{"Hello":"world"},"0"]`)
inv := &Invocation{}
err := json.Unmarshal(raw, inv)
assert.NoError(err)
assert.Equal("Test/method", inv.Name)
assert.Equal("0", inv.CallID)
test, ok := inv.Args.(*test)
t.Logf("TIMBUG %T", inv.Args)
assert.Truef(ok, "invocation arguments are not type test")
assert.Equal("world", test.Hello)
}