This repository has been archived by the owner on Nov 29, 2018. It is now read-only.
/
bytes_test.go
89 lines (63 loc) · 2.03 KB
/
bytes_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
// Copyright 2016 Mhd Sulhan <ms@kilabit.info>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tekstus_test
import (
"github.com/shuLhan/tekstus"
"testing"
)
func testBytesFind(t *testing.T, line, token []byte, startat int, exp []int) {
got := []int{}
tokenlen := len(token)
for {
foundat := tekstus.BytesFind(line, token, startat)
if foundat < 0 {
break
}
got = append(got, foundat)
startat = foundat + tokenlen
}
assert(t, exp, got, true)
}
func TestBytesFind(t *testing.T) {
line := []byte(dataLines[0])
token := []byte("//")
exp := []int{0}
testBytesFind(t, line, token, 0, exp)
token = []byte(".")
exp = []int{42, 48, 69}
testBytesFind(t, line, token, 0, exp)
token = []byte("d.")
exp = []int{68}
testBytesFind(t, line, token, 0, exp)
}
func testEncasulateToken(t *testing.T, token, line, leftcap, rightcap,
exp []byte) {
newline, changed := tekstus.BytesEncapsulate(token, line, leftcap, rightcap)
assert(t, string(exp), string(newline), changed)
}
func TestBytesEncapsulate(t *testing.T) {
line := []byte(dataLines[1])
token := []byte("/")
leftcap := []byte("\\")
rightcap := []byte{}
exp := []byte("\\/\\/ Copyright 2016-2018 Shulhan \"<ms@kilabit.info>\". All rights reserved.")
testEncasulateToken(t, token, line, leftcap, rightcap, exp)
token = []byte("<")
leftcap = []byte("<")
rightcap = []byte(" ")
exp = []byte("// Copyright 2016-2018 Shulhan \"<< ms@kilabit.info>\". All rights reserved.")
testEncasulateToken(t, token, line, leftcap, rightcap, exp)
token = []byte("\"")
leftcap = []byte("\\")
rightcap = []byte(" ")
exp = []byte("// Copyright 2016-2018 Shulhan \\\" <ms@kilabit.info>\\\" . All rights reserved.")
testEncasulateToken(t, token, line, leftcap, rightcap, exp)
}
func TestBytesRemoveUntil(t *testing.T) {
for _, td := range dataCut {
got, _ := tekstus.BytesRemoveUntil([]byte(td.text),
[]byte(td.leftcap), []byte(td.rightcap))
assert(t, string(td.exp), string(got), true)
}
}