-
Notifications
You must be signed in to change notification settings - Fork 1
/
dict_test.go
176 lines (152 loc) · 3.63 KB
/
dict_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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package gp
import (
"testing"
)
func TestDictFromPairs(t *testing.T) {
setupTest(t)
// Add panic test case
func() {
defer func() {
if r := recover(); r == nil {
t.Errorf("DictFromPairs() with odd number of arguments should panic")
} else if r != "DictFromPairs requires an even number of arguments" {
t.Errorf("Expected panic message 'DictFromPairs requires an even number of arguments', got '%v'", r)
}
}()
DictFromPairs("key1", "value1", "key2") // Should panic
}()
tests := []struct {
name string
pairs []any
wantKeys []any
wantVals []any
}{
{
name: "string keys and values",
pairs: []any{"key1", "value1", "key2", "value2"},
wantKeys: []any{"key1", "key2"},
wantVals: []any{"value1", "value2"},
},
{
name: "mixed types",
pairs: []any{"key1", 42, "key2", 3.14},
wantKeys: []any{"key1", "key2"},
wantVals: []any{42, 3.14},
},
}
for _, tt := range tests {
dict := DictFromPairs(tt.pairs...)
// Verify each key-value pair
for i := 0; i < len(tt.wantKeys); i++ {
key := From(tt.wantKeys[i])
val := dict.Get(key)
if !ObjectsAreEqual(val, From(tt.wantVals[i])) {
t.Errorf("DictFromPairs() got value %v for key %v, want %v",
val, tt.wantKeys[i], tt.wantVals[i])
}
}
}
}
func TestMakeDict(t *testing.T) {
setupTest(t)
tests := []struct {
name string
m map[any]any
}{
{
name: "string map",
m: map[any]any{
"key1": "value1",
"key2": "value2",
},
},
{
name: "mixed types map",
m: map[any]any{
"int": 42,
"float": 3.14,
"string": "hello",
},
},
}
for _, tt := range tests {
dict := MakeDict(tt.m)
// Verify each key-value pair
for k, v := range tt.m {
key := From(k)
got := dict.Get(key)
if !ObjectsAreEqual(got, From(v)) {
t.Errorf("MakeDict() got value %v for key %v, want %v", got, k, v)
}
}
}
}
func TestDictSetGet(t *testing.T) {
setupTest(t)
dict := DictFromPairs()
// Test Set and Get
key := From("test_key")
value := From("test_value")
dict.Set(key, value)
got := dict.Get(key)
if !ObjectsAreEqual(got, value) {
t.Errorf("Dict.Get() got %v, want %v", got, value)
}
}
func TestDictSetGetString(t *testing.T) {
setupTest(t)
dict := DictFromPairs()
// Test SetString and GetString
value := From("test_value")
dict.SetString("test_key", value)
got := dict.GetString("test_key")
if !ObjectsAreEqual(got, value) {
t.Errorf("Dict.GetString() got %v, want %v", got, value)
}
}
func TestDictDel(t *testing.T) {
setupTest(t)
dict := DictFromPairs("test_key", "test_value")
key := From("test_key")
// Verify key exists
got := dict.Get(key)
if !ObjectsAreEqual(got, From("test_value")) {
t.Errorf("Before deletion, got %v, want %v", got, "test_value")
}
// Delete the key
dict.Del(key)
// After deletion, the key should not exist
if dict.HasKey(key) {
t.Errorf("After deletion, key %v should not exist", key)
}
}
func TestDictForEach(t *testing.T) {
setupTest(t)
dict := DictFromPairs(
"key1", "value1",
"key2", "value2",
"key3", "value3",
)
count := 0
expectedPairs := map[string]string{
"key1": "value1",
"key2": "value2",
"key3": "value3",
}
dict.Items()(func(key, value Object) bool {
count++
k := key.String()
v := value.String()
if expectedVal, ok := expectedPairs[k]; !ok || expectedVal != v {
t.Errorf("ForEach() unexpected pair: %v: %v", k, v)
}
return true
})
if count != len(expectedPairs) {
t.Errorf("ForEach() visited %d pairs, want %d", count, len(expectedPairs))
}
}
// Helper function to compare Python objects
func ObjectsAreEqual(obj1, obj2 Object) bool {
return obj1.String() == obj2.String()
}