This repository has been archived by the owner on Aug 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 140
/
unicode_test.go
98 lines (72 loc) · 2 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
package python3
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestUnicodeNew(t *testing.T) {
Py_Initialize()
s := PyUnicode_New(20, 'z')
assert.NotNil(t, s)
defer s.DecRef()
}
func TestUnicodeFromString(t *testing.T) {
Py_Initialize()
u := PyUnicode_FromString("aaa")
assert.True(t, PyUnicode_Check(u))
assert.True(t, PyUnicode_CheckExact(u))
defer u.DecRef()
assert.Equal(t, 3, PyUnicode_GetLength(u))
}
func TestUnicodeFromEncodedObject(t *testing.T) {
Py_Initialize()
b := PyBytes_FromString("bbb")
assert.NotNil(t, b)
defer b.DecRef()
ub := PyUnicode_FromEncodedObject(b, "utf-8", "strict")
assert.NotNil(t, ub)
defer ub.DecRef()
}
func TestUnicodeChar(t *testing.T) {
Py_Initialize()
u := PyUnicode_FromString("aaa")
assert.True(t, PyUnicode_Check(u))
assert.True(t, PyUnicode_CheckExact(u))
defer u.DecRef()
assert.Equal(t, 0, PyUnicode_WriteChar(u, 1, 'd'))
assert.Equal(t, 'd', PyUnicode_ReadChar(u, 1))
}
func TestUnicodeFill(t *testing.T) {
Py_Initialize()
u := PyUnicode_FromString("aaa")
assert.True(t, PyUnicode_Check(u))
assert.True(t, PyUnicode_CheckExact(u))
defer u.DecRef()
assert.Equal(t, 3, PyUnicode_Fill(u, 0, 3, 'c'))
assert.Equal(t, "ccc", PyUnicode_AsUTF8(u))
}
func TestUnicodeCopyCharacters(t *testing.T) {
Py_Initialize()
u := PyUnicode_FromString("aaa")
assert.True(t, PyUnicode_Check(u))
assert.True(t, PyUnicode_CheckExact(u))
defer u.DecRef()
b := PyBytes_FromString("bbb")
assert.NotNil(t, b)
defer b.DecRef()
ub := PyUnicode_FromEncodedObject(b, "utf-8", "strict")
assert.NotNil(t, ub)
defer ub.DecRef()
assert.Equal(t, 3, PyUnicode_CopyCharacters(ub, u, 0, 0, 3))
assert.Equal(t, "aaa", PyUnicode_AsUTF8(ub))
}
func TestUnicodeSubstring(t *testing.T) {
Py_Initialize()
u := PyUnicode_FromString("aaa")
assert.True(t, PyUnicode_Check(u))
assert.True(t, PyUnicode_CheckExact(u))
defer u.DecRef()
sub := PyUnicode_Substring(u, 0, 2)
assert.NotNil(t, sub)
sub.DecRef()
assert.Equal(t, "aa", PyUnicode_AsUTF8(sub))
}