-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathtrapwriter_test.go
More file actions
68 lines (57 loc) · 2.03 KB
/
trapwriter_test.go
File metadata and controls
68 lines (57 loc) · 2.03 KB
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
package trap
import (
"strings"
"testing"
)
const (
asciiChar = "*"
bmpChar = "\u2028"
nonBmpChar = "\U000101d0"
)
func TestCapStringLength(t *testing.T) {
// test simple cases only involving ASCII characters
short := strings.Repeat(asciiChar, max_strlen-1)
if capStringLength(short) != short {
t.Errorf("Strings shorter than maximum length should not be truncated")
}
short = strings.Repeat(asciiChar, max_strlen)
if capStringLength(short) != short {
t.Errorf("Strings no longer than maximum length should not be truncated")
}
long := strings.Repeat(asciiChar, max_strlen+1)
if capStringLength(long) != long[0:max_strlen] {
t.Errorf("Strings longer than maximum length should be truncated")
}
// test chopping off non-ASCII characters
prefix := strings.Repeat(asciiChar, max_strlen)
long = prefix + bmpChar
if capStringLength(long) != prefix {
t.Errorf("BMP character after max_strlen should be correctly chopped off")
}
prefix = strings.Repeat(asciiChar, max_strlen)
long = prefix + nonBmpChar
if capStringLength(long) != prefix {
t.Errorf("Non-BMP character after max_strlen should be correctly chopped off")
}
prefix = strings.Repeat(asciiChar, max_strlen-(len(bmpChar)-1))
long = prefix + bmpChar
if capStringLength(long) != prefix {
t.Errorf("BMP character straddling max_strlen should be correctly chopped off")
}
prefix = strings.Repeat(asciiChar, max_strlen-(len(nonBmpChar)-1))
long = prefix + nonBmpChar
if capStringLength(long) != prefix {
t.Errorf("Non-BMP character straddling max_strlen should be correctly chopped off")
}
// test preserving non-ASCII characters that just about fit
prefix = strings.Repeat(asciiChar, max_strlen-len(bmpChar))
short = prefix + bmpChar
if capStringLength(short) != short {
t.Errorf("BMP character before max_strlen should be correctly preserved")
}
prefix = strings.Repeat(asciiChar, max_strlen-len(nonBmpChar))
short = prefix + nonBmpChar
if capStringLength(short) != short {
t.Errorf("Non-BMP character before max_strlen should be correctly preserved")
}
}