Completed
Push — main ( 523495...f20dc9 )
by Yume
25s queued 13s
created

json.compareJSON   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
dl 0
loc 4
rs 10
c 0
b 0
f 0
nop 2
1
package json
2
3
import (
4
	"encoding/json"
5
	"testing"
6
)
7
8
// MockJSONHelper is a mock implementation of the Helper interface for testing purposes.
9
type MockJSONHelper struct {
10
	MarshalFunc   func(v interface{}) ([]byte, error)
11
	UnmarshalFunc func(data []byte, v interface{}) error
12
}
13
14
func (m *MockJSONHelper) Marshal(v interface{}) ([]byte, error) {
15
	if m.MarshalFunc != nil {
16
		return m.MarshalFunc(v)
17
	}
18
	return nil, nil
19
}
20
21
func (m *MockJSONHelper) Unmarshal(data []byte, v interface{}) error {
22
	if m.UnmarshalFunc != nil {
23
		return m.UnmarshalFunc(data, v)
24
	}
25
	return nil
26
}
27
28
func TestJSON_Marshal(t *testing.T) {
29
	// Create a JSON instance with a mock JSON helper
30
	mockHelper := &MockJSONHelper{
31
		MarshalFunc: func(v interface{}) ([]byte, error) {
32
			return []byte(`{"mocked":true}`), nil
33
		},
34
	}
35
	jsonHelper := NewJSON(mockHelper)
36
37
	// Test JSON.Marshal method
38
	result, err := jsonHelper.Marshal(map[string]interface{}{"key": "value"})
39
	if err != nil {
40
		t.Errorf("JSON.Marshal should not return an error, but got: %v", err)
41
	}
42
	expectedJSON := `{"mocked":true}`
43
	if string(result) != expectedJSON {
44
		t.Errorf("Expected JSON: %s, but got: %s", expectedJSON, string(result))
45
	}
46
}
47
48
func TestJSON_Unmarshal(t *testing.T) {
49
	// Create a JSON instance with a mock JSON helper
50
	mockHelper := &MockJSONHelper{
51
		UnmarshalFunc: func(data []byte, v interface{}) error {
52
			return json.Unmarshal([]byte(`{"mocked":true}`), v)
53
		},
54
	}
55
	jsonHelper := NewJSON(mockHelper)
56
57
	// Test JSON.Unmarshal method
58
	var output map[string]interface{}
59
	err := jsonHelper.Unmarshal([]byte(`{"key": "value"}`), &output)
60
	if err != nil {
61
		t.Errorf("JSON.Unmarshal should not return an error, but got: %v", err)
62
	}
63
	expectedOutput := map[string]interface{}{"mocked": true}
64
	if !compareJSON(output, expectedOutput) {
65
		t.Errorf("Expected output: %v, but got: %v", expectedOutput, output)
66
	}
67
}
68
69
func TestNativeJSON_Marshal(t *testing.T) {
70
	nativeJSONHelper := &NativeJSON{}
71
72
	// Test NativeJSON.Marshal method
73
	result, err := nativeJSONHelper.Marshal(map[string]interface{}{"key": "value"})
74
	if err != nil {
75
		t.Errorf("NativeJSON.Marshal should not return an error, but got: %v", err)
76
	}
77
	expectedJSON := `{"key":"value"}`
78
	if string(result) != expectedJSON {
79
		t.Errorf("Expected JSON: %s, but got: %s", expectedJSON, string(result))
80
	}
81
}
82
83
func TestNativeJSON_Unmarshal(t *testing.T) {
84
	nativeJSONHelper := &NativeJSON{}
85
86
	// Test NativeJSON.Unmarshal method
87
	var output map[string]interface{}
88
	err := nativeJSONHelper.Unmarshal([]byte(`{"key": "value"}`), &output)
89
	if err != nil {
90
		t.Errorf("NativeJSON.Unmarshal should not return an error, but got: %v", err)
91
	}
92
	expectedOutput := map[string]interface{}{"key": "value"}
93
	if !compareJSON(output, expectedOutput) {
94
		t.Errorf("Expected output: %v, but got: %v", expectedOutput, output)
95
	}
96
}
97
98
// compareJSON compares two JSON objects for equality.
99
func compareJSON(a, b map[string]interface{}) bool {
100
	aJSON, _ := json.Marshal(a)
101
	bJSON, _ := json.Marshal(b)
102
	return string(aJSON) == string(bJSON)
103
}
104