forked from mitch000001/pystring
/
pystring_test.go
183 lines (166 loc) · 5.05 KB
/
pystring_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
177
178
179
180
181
182
183
package pystring
import (
"fmt"
"os/exec"
"strings"
"testing"
)
func TestCapitalize(t *testing.T) {
const in, out = "hello", "Hello"
result := New(in).Capitalize().String()
if out != result {
t.Errorf("Capitalized version of %v did not become %v, but %v!\n", in, out, result)
}
}
func TestStrip(t *testing.T) {
const in, out = " \n\n\nhello\t\t\n\n\n", "hello"
result := New(in).Strip().String()
if out != result {
t.Errorf("Stripped version of %v did not become %v, but %v!\n", in, out, result)
}
}
func TestCount(t *testing.T) {
const in, out = "aabbaaccaaddaaffaa", 5
result := New(in).Count("aa")
if out != result {
t.Errorf("Count of aa in %v, was not %v but %v!\n", in, out, result)
}
}
func TestIndex(t *testing.T) {
const in, out = "aabb", 2
result, err := New(in).Index("bb")
if (err != nil) || (out != result) {
t.Errorf("Index of bb in %v, was not %v but %v!\n", in, out, result)
}
}
func TestEndsWith(t *testing.T) {
const in1, in2 = "aabbcc", "abcdef"
if New(in1).EndsWith("cc") == false {
t.Errorf("EndsWith: %v does end with cc!\n", in1)
}
if New(in2).EndsWith("dd") == true {
t.Errorf("EndsWith: %v does not end with dd!\n", in2)
}
}
func TestStartsWith(t *testing.T) {
const in1, in2 = "aabbcc", "abcdef"
if New(in1).StartsWith("cc") != false {
t.Errorf("StartsWith: %v does not start with cc!\n", in1)
}
if New(in2).StartsWith("ab") != true {
t.Errorf("StartsWith: %v does start with ab!\n", in2)
}
}
func TestFind(t *testing.T) {
const in = "aabbcc"
if New(in).Find("cc") != 4 {
t.Errorf("Find: %v has cc in position 4!\n", in)
}
if New(in).Find("cd") != -1 {
t.Errorf("Find: %v does not have cd!\n", in)
}
}
func TestIsDigit(t *testing.T) {
const in1, in2 = "223098473", "x1z"
if New(in1).IsDigit() != true {
t.Errorf("IsDigit: %v should be true!\n", in1)
}
if New(in2).IsDigit() != false {
t.Errorf("IsDigit: %v should be false!\n", in2)
}
if New("").IsDigit() != false {
t.Errorf("IsDigit: empty string should be false!\n")
}
}
func TestAdd(t *testing.T) {
a := New("abc")
if a.Add("cde").String() != "abccde" {
t.Errorf("abc + cde should be abccde\n")
}
}
func TestSubstract(t *testing.T) {
a := New("abcdef")
if a.Subtract("def") != "abc" {
t.Errorf("abcdef - def should be abc\n")
}
a = New("ost kake ost kake ost")
if a.Subtract("kake") != "ost kake ost ost" {
t.Errorf("ost kake ost kake ost - kake should be ost kake ost ost\n")
}
}
func TestSplit(t *testing.T) {
a := New("a:b:c")
if a.Split(":")[1] != "b" {
t.Errorf("a:b:c split on : should be a b c\n")
}
}
func TestMultiply(t *testing.T) {
a := New("a")
if a.Multiply(5) != "aaaaa" {
t.Errorf("a*5 should be aaaaa\n")
}
b := New("ost")
if b.Multiply(2) != "ostost" {
t.Errorf("ost*2 should be ostost\n")
}
}
func TestMultiplyUnicode(t *testing.T) {
a := New("┐")
if a.Multiply(5) != "┐┐┐┐┐" {
t.Errorf("a*5 should be ┐┐┐┐┐\n")
}
}
/* Checks if the constants are equal to the ones in Python by running the python interpreter.
Also exercises the New(), Join() and Encode() functions.
*/
func TestConstants(t *testing.T) {
var shouldBeBytes []byte
var cmd *exec.Cmd
constants := []string{"ascii_letters", "ascii_lowercase", "ascii_uppercase", "digits", "hexdigits", "octdigits", "punctuation", "printable", "whitespace"}
shouldbe := []string{ASCII_letters, ASCII_lowercase, ASCII_uppercase, Digits, HexDigits, OctDigits, Punctuation, Printable, Whitespace}
for i, constant := range constants {
// Make sure to use python3. Tested on Fedora and Arch Linux.
cmd = exec.Command("python3", "-c", "import string; print(string."+constant+")")
output, err := cmd.Output()
if err != nil {
/* One of the commands failed, assume Python is not available */
// This can be used if one doesn't want to ignore the lack of python:
//t.Errorf("execution failed: %s %s\n", New(" ").Join(cmd.Args), output)
return
}
shouldBeBytes = New(shouldbe[i]).Encode()
for i2, b := range shouldBeBytes {
if (i2 >= len(output)) || (b != output[i2]) {
if len(output) == 0 {
output = New("no output").Encode()
}
t.Errorf("constant %s failed, got: %s\n", constants[i], output)
return
}
}
}
}
func TestReplace(t *testing.T) {
a := New("hello")
a.Replace("l", "p")
if a.String() != "heppo" {
t.Errorf("hello with l replaced should be heppo\n")
}
}
func TestSplitLines(t *testing.T) {
s := "First line, with LF\nSecond line, with CR\rThird line, with CRLF\r\n" +
"Two blank lines with LFs\n\n\nTwo blank lines with CRs\r\r\rTwo blank" +
"lines with CRLFs\r\n\r\n\r\nThree blank lines with a jumble of things:" +
"\r\n\r\r\n\nEnd without a newline."
s2 := "First line, with LF\nSecond line, with CR\nThird line, with CRLF\n" +
"Two blank lines with LFs\nTwo blank lines with CRs\nTwo blank" +
"lines with CRLFs\nThree blank lines with a jumble of things:" +
"\nEnd without a newline."
a := New(s)
lines := a.SplitLines()
joined := strings.Join(lines, "\n")
fmt.Println(joined)
if joined != s2 {
t.Errorf("SplitLines does not work correctly, is %s, but should be %s.\n", joined, s2)
}
}