-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunicode_test.go
105 lines (73 loc) · 2.17 KB
/
unicode_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
package cpy_test
import (
"testing"
"github.com/stretchr/testify/assert"
"go.nhat.io/cpy/v3"
)
func TestUnicodeNew(t *testing.T) {
cpy.Py_Initialize()
s := cpy.PyUnicode_New(20, 'z')
defer s.DecRef()
assert.NotNil(t, s)
}
func TestUnicodeFromString(t *testing.T) {
cpy.Py_Initialize()
u := cpy.PyUnicode_FromString("aaa")
defer u.DecRef()
assert.True(t, cpy.PyUnicode_Check(u))
assert.True(t, cpy.PyUnicode_CheckExact(u))
assert.Equal(t, 3, cpy.PyUnicode_GetLength(u))
}
func TestUnicodeFromEncodedObject(t *testing.T) {
cpy.Py_Initialize()
b := cpy.PyBytes_FromString("bbb")
defer b.DecRef()
assert.NotNil(t, b)
ub := cpy.PyUnicode_FromEncodedObject(b, "utf-8", "strict")
defer ub.DecRef()
assert.NotNil(t, ub)
}
func TestUnicodeChar(t *testing.T) {
cpy.Py_Initialize()
u := cpy.PyUnicode_FromString("aaa")
defer u.DecRef()
assert.True(t, cpy.PyUnicode_Check(u))
assert.True(t, cpy.PyUnicode_CheckExact(u))
assert.Equal(t, 0, cpy.PyUnicode_WriteChar(u, 1, 'd'))
assert.Equal(t, 'd', cpy.PyUnicode_ReadChar(u, 1))
}
func TestUnicodeFill(t *testing.T) {
cpy.Py_Initialize()
u := cpy.PyUnicode_FromString("aaa")
defer u.DecRef()
assert.True(t, cpy.PyUnicode_Check(u))
assert.True(t, cpy.PyUnicode_CheckExact(u))
assert.Equal(t, 3, cpy.PyUnicode_Fill(u, 0, 3, 'c'))
assert.Equal(t, "ccc", cpy.PyUnicode_AsUTF8(u))
}
func TestUnicodeCopyCharacters(t *testing.T) {
cpy.Py_Initialize()
u := cpy.PyUnicode_FromString("aaa")
defer u.DecRef()
assert.True(t, cpy.PyUnicode_Check(u))
assert.True(t, cpy.PyUnicode_CheckExact(u))
b := cpy.PyBytes_FromString("bbb")
defer b.DecRef()
assert.NotNil(t, b)
ub := cpy.PyUnicode_FromEncodedObject(b, "utf-8", "strict")
defer ub.DecRef()
assert.NotNil(t, ub)
assert.Equal(t, 3, cpy.PyUnicode_CopyCharacters(ub, u, 0, 0, 3))
assert.Equal(t, "aaa", cpy.PyUnicode_AsUTF8(ub))
}
func TestUnicodeSubstring(t *testing.T) {
cpy.Py_Initialize()
u := cpy.PyUnicode_FromString("aaa")
defer u.DecRef()
assert.True(t, cpy.PyUnicode_Check(u))
assert.True(t, cpy.PyUnicode_CheckExact(u))
sub := cpy.PyUnicode_Substring(u, 0, 2)
defer sub.DecRef()
assert.NotNil(t, sub)
assert.Equal(t, "aa", cpy.PyUnicode_AsUTF8(sub))
}