-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathother_test.go
More file actions
74 lines (64 loc) · 1.7 KB
/
Copy pathother_test.go
File metadata and controls
74 lines (64 loc) · 1.7 KB
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
package jrm1
import (
"encoding/json"
"testing"
"github.com/vault-thirteen/auxie/tester"
)
func Test_ParseParameters(t *testing.T) {
aTest := tester.New(t)
var re *RpcError
var paramsRaw json.RawMessage
initErrorMessages()
// Test #1. Null parameters.
{
re = ParseParameters(nil, nil)
aTest.MustBeDifferent(re, (*RpcError)(nil))
aTest.MustBeEqual(re.Code, RpcErrorCode(-16))
}
// Test #2. Bad JSON.
{
paramsRaw = json.RawMessage([]byte(`x`))
p := struct{}{}
re = ParseParameters(¶msRaw, &p)
aTest.MustBeDifferent(re, (*RpcError)(nil))
aTest.MustBeEqual(re.Code, RpcErrorCode(-16))
}
// Test #3. OK.
{
type P3 struct {
N byte `json:"n"`
}
paramsRaw = json.RawMessage([]byte(`{"n":123}`))
p := P3{}
re = ParseParameters(¶msRaw, &p)
aTest.MustBeEqual(re, (*RpcError)(nil))
aTest.MustBeEqual(p.N, byte(123))
}
// Test #4. Missing parameter.
// This test is not possible in Go language while its JSON parser does not
// check for missing fields, it just leaves them with their initial values,
// i.e. pointers will be null if no field was found. Unfortunately, Go
// language does not support strict parsing of JSON format.
//{
// type P4 struct {
// Name string `json:"name"`
// Surname string `json:"surname"`
// }
//
// paramsRaw = json.RawMessage([]byte(`{"name":"John"}`))
// p := P4{}
// re = ParseParameters(¶msRaw, &p)
// aTest.MustBeDifferent(re, (*RpcError)(nil))
//}
// Test #5. Unknown parameter.
{
type P5 struct {
Name string `json:"name"`
Surname string `json:"surname"`
}
paramsRaw = json.RawMessage([]byte(`{"age":125}`))
p := P5{}
re = ParseParameters(¶msRaw, &p)
aTest.MustBeDifferent(re, (*RpcError)(nil))
}
}