-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathstring_test.go
146 lines (141 loc) · 2.29 KB
/
string_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
// Copyright 2022 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package py
import "testing"
func TestStringFind(t *testing.T) {
for _, tc := range []struct {
str string
sub string
beg int
end int
idx int
}{
{
str: "hello world",
sub: "world",
idx: 6,
},
{
str: "hello world",
sub: "o",
idx: 4,
},
{
str: "hello world",
sub: "o",
beg: 5,
idx: 7,
},
{
str: "hello world",
sub: "bye",
idx: -1,
},
{
str: "Hello, 世界",
sub: "界",
idx: 8,
},
{
str: "01234 6789",
sub: " ",
beg: 6,
idx: -1,
},
{
str: "0123456789",
sub: "6",
beg: 1,
end: 6,
idx: -1,
},
{
str: "0123456789",
sub: "6",
beg: 1,
end: 7,
idx: 6,
},
{
str: "0123456789",
sub: "6",
beg: 1,
end: -1,
idx: 6,
},
{
str: "0123456789",
sub: "6",
beg: 100,
end: -1,
idx: -1,
},
{
str: "0123456789",
sub: "6",
beg: 2,
end: 1,
idx: -1,
},
} {
t.Run(tc.str+":"+tc.sub, func(t *testing.T) {
beg := tc.beg
end := tc.end
if end == 0 {
end = len(tc.str)
}
idx, err := String(tc.str).find(Tuple{String(tc.sub), Int(beg), Int(end)})
if err != nil {
t.Fatalf("invalid: %+v", err)
}
if got, want := int(idx.(Int)), tc.idx; got != want {
t.Fatalf("got=%d, want=%d", got, want)
}
})
}
}
func TestStringUpper(t *testing.T) {
tests := []struct {
name string
s String
want Object
}{{
name: "abc",
s: String("abc"),
want: String("ABC")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.s.Upper()
if err != nil {
t.Fatalf("Upper() error = %v", err)
}
if got.(String) != tt.want.(String) {
t.Fatalf("Upper() got = %v, want %v", got, tt.want)
}
})
}
}
func TestStringLower(t *testing.T) {
tests := []struct {
name string
s String
want Object
}{{
name: "ABC",
s: String("ABC"),
want: String("abc")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.s.Lower()
if err != nil {
t.Fatalf("Lower() error = %v", err)
}
if got.(String) != tt.want.(String) {
t.Fatalf("Lower() got = %v, want %v", got, tt.want)
}
})
}
}