-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubstring_test.go
53 lines (40 loc) · 1.11 KB
/
substring_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
package str
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func ExampleSubstring() {
fmt.Println(Substring("This is a sentence", 5, 2))
// Output: is
}
func TestSubstring(t *testing.T) {
testString := "This is a test string that you should use for tests"
// forward in bounds
s1 := Substring(testString, 0, 4)
assert.Equal(t, "This", s1)
// backward in bounds
s2 := Substring(testString, -5, 4)
assert.Equal(t, "test", s2)
// forward in bounds
s3 := Substring(testString, 10, 11)
assert.Equal(t, "test string", s3)
// backward in bounds
s4 := Substring(testString, -13, 3)
assert.Equal(t, "use", s4)
// forward with too large length
s5 := Substring(testString, 38, 100)
assert.Equal(t, "use for tests", s5)
// backward with too large length
s6 := Substring(testString, -13, 100)
assert.Equal(t, "use for tests", s6)
// forward with invalid start
s7 := Substring(testString, 60, 1)
assert.Equal(t, "", s7)
// backward with invalid start
s8 := Substring(testString, -60, 1)
assert.Equal(t, "", s8)
// zero length
s9 := Substring(testString, 0, 0)
assert.Equal(t, "", s9)
}