forked from go-python/cpy3
-
Notifications
You must be signed in to change notification settings - Fork 3
/
dict_test.go
93 lines (70 loc) · 2.06 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
/*
Unless explicitly stated otherwise all files in this repository are licensed
under the MIT License.
This product includes software developed at Datadog (https://www.datadoghq.com/).
Copyright 2018 Datadog, Inc.
*/
package python3
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestDict(t *testing.T) {
Py_Initialize()
dict := PyDict_New()
assert.True(t, PyDict_Check(dict))
assert.True(t, PyDict_CheckExact(dict))
defer dict.DecRef()
proxy := PyDictProxy_New(dict)
assert.NotNil(t, proxy)
proxy.DecRef()
key1 := "key1"
value1 := PyUnicode_FromString("value1")
assert.NotNil(t, value1)
defer value1.DecRef()
key2 := PyUnicode_FromString("key2")
assert.NotNil(t, key2)
defer key2.DecRef()
value2 := PyUnicode_FromString("value2")
assert.NotNil(t, value2)
defer value2.DecRef()
key3 := PyUnicode_FromString("key3")
assert.NotNil(t, key3)
defer key3.DecRef()
value3 := PyUnicode_FromString("value3")
assert.NotNil(t, value3)
defer value3.DecRef()
err := PyDict_SetItem(dict, key2, value2)
assert.Zero(t, err)
err = PyDict_SetItemString(dict, key1, value1)
assert.Zero(t, err)
assert.Equal(t, value2, PyDict_GetItem(dict, key2))
assert.Equal(t, value2, PyDict_SetDefault(dict, key2, Py_None))
assert.Equal(t, value1, PyDict_GetItemString(dict, key1))
assert.Nil(t, PyDict_GetItemWithError(dict, key3))
b := PyDict_Contains(dict, key2) != 0
assert.True(t, b)
assert.Equal(t, 2, PyDict_Size(dict))
keys := PyDict_Keys(dict)
assert.True(t, PyList_Check(keys))
keys.DecRef()
values := PyDict_Values(dict)
assert.True(t, PyList_Check(values))
values.DecRef()
items := PyDict_Items(dict)
assert.True(t, PyList_Check(items))
items.DecRef()
err = PyDict_SetItem(dict, key3, value3)
assert.Zero(t, err)
newDict := PyDict_Copy(dict)
assert.Equal(t, 3, PyDict_Size(newDict))
defer newDict.DecRef()
err = PyDict_DelItem(dict, key2)
assert.Zero(t, err)
err = PyDict_DelItemString(dict, key1)
assert.Zero(t, err)
assert.Equal(t, 1, PyDict_Size(dict))
PyDict_Clear(dict)
assert.Equal(t, 0, PyDict_Size(dict))
dict.DecRef()
}