File size: 2,462 Bytes
ca7217f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package maps

import (
	"encoding/json"
	"slices"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestOrderedMap(t *testing.T) {
	t.Run("JSON Marshal", func(t *testing.T) {
		m := NewOrderedMap[string, any]()
		b, _ := json.Marshal(m)
		assert.JSONEq(t, `{}`, string(b))

		m.Set("a", 1)
		m.Set("c", "2")
		m.Set("b", 3.0)
		m.Set("b", 1.5)
		assert.Equal(t, []any{1, "2", 1.5}, slices.Collect(m.Values()))

		b, _ = json.Marshal(m)
		assert.JSONEq(t, `{
			"a":1,
			"c":"2",
			"b":1.5
		}`, string(b))
	})

	t.Run("fixed type map unmarshal", func(t *testing.T) {
		jsonData := `{
			"a":1,
			"c":2,
			"b":0
		}`
		m := NewOrderedMap[string, int]()
		err := m.UnmarshalJSON([]byte(jsonData))
		if assert.NoError(t, err) {
			b, _ := json.Marshal(m)
			assert.JSONEq(t, `{"a":1,"c":2,"b":0}`, string(b))
		}
	})

	t.Run("any type map unmarshal", func(t *testing.T) {
		jsonData := `{
			"a":1,
			"c":"2",
			"b":1.5,
			"?":{"x":"y","j":"k","3":2}
		}`
		m := NewOrderedMap[string, any]()
		err := m.UnmarshalJSON([]byte(jsonData))
		if assert.NoError(t, err) {
			b, _ := json.Marshal(m)
			assert.JSONEq(t, `{
				"a":1,"c":"2","b":1.5,
				"?":{"3":2,"j":"k","x":"y"}
			}`, string(b))
		}
	})

	t.Run("Sorted sub map unmarshal", func(t *testing.T) {
		jsonData := `{
			"w":{"n":3,"m":5},
			"b":{"f":1,"j":0}
		}`
		m := NewOrderedMap[string, map[string]int]()
		err := m.UnmarshalJSON([]byte(jsonData))
		if assert.NoError(t, err) {
			b, _ := json.Marshal(m)
			assert.JSONEq(t, `{
				"w":{"m":5,"n":3},
				"b":{"f":1,"j":0}
			}`, string(b))
		}
	})

	t.Run("Ordered sub map unmarshal", func(t *testing.T) {
		jsonData := `{
			"w":{"n":3,"m":5},
			"b":{"f":1,"j":0}
		}`
		m := NewOrderedMap[string, *OrderedMap[string, int]]()
		err := m.UnmarshalJSON([]byte(jsonData))
		if assert.NoError(t, err) {
			b, _ := json.Marshal(m)
			assert.JSONEq(t, `{
				"w":{"n":3,"m":5},
				"b":{"f":1,"j":0}
			}`, string(b))
		}
	})

	t.Run("A lot of ordered sub maps unmarshal", func(t *testing.T) {
		jsonData := `{
			"w":{"n":{"g":3,"5":5},"m":{"v":3,"2":5}},
			"b":{"f":{"h":3,"3":5},"j":{"x":3,"c":5}}
		}`
		m := NewOrderedMap[string, *OrderedMap[string, *OrderedMap[string, any]]]()
		err := m.UnmarshalJSON([]byte(jsonData))
		if assert.NoError(t, err) {
			b, _ := json.Marshal(m)
			assert.JSONEq(t, `{
				"w":{"n":{"g":3,"5":5},
				"m":{"v":3,"2":5}},
				"b":{"f":{"h":3,"3":5},
				"j":{"x":3,"c":5}}
			}`, string(b))
		}
	})
}