-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder_test.go
60 lines (50 loc) · 1.45 KB
/
decoder_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
package godantic
import (
"encoding/json"
"testing"
)
func TestBindJSON(t *testing.T) {
// Test case 1: Valid JSON data and object
jsonData := []byte(`{"name": "John", "age": 30}`)
obj := &struct {
Name string `json:"name"`
Age int `json:"age"`
}{}
v := &Validate{}
err := v.BindJSON(jsonData, obj)
if err != nil {
t.Errorf("Test case 1 failed. Unexpected error: %v", err)
}
// Verify the object has been populated correctly
expectedObj := &struct {
Name string `json:"name"`
Age int `json:"age"`
}{
Name: "John",
Age: 30,
}
if !jsonEqual(obj, expectedObj) {
t.Errorf("Test case 1 failed. Object mismatch. Expected: %+v, Got: %+v", expectedObj, obj)
}
// Test case 2: Invalid JSON data
jsonData = []byte(`{"name": "John", "age": }`) // Invalid JSON, missing value for "age"
err = v.BindJSON(jsonData, obj)
if err == nil {
t.Errorf("Test case 2 failed. Expected an error, but got nil")
} else if _, ok := err.(*Error); !ok {
t.Errorf("Test case 2 failed. Expected an *Error, but got %T", err)
}
// Test case 3: Empty JSON data
jsonData = []byte(`{}`)
err = v.BindJSON(jsonData, obj)
if err == nil {
t.Errorf("Test case 3 failed. Expected an error, but got nil")
} else if _, ok := err.(*Error); !ok {
t.Errorf("Test case 3 failed. Expected an *Error, but got %T", err)
}
}
func jsonEqual(a, b interface{}) bool {
jsonA, _ := json.Marshal(a)
jsonB, _ := json.Marshal(b)
return string(jsonA) == string(jsonB)
}