-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcontent_test.go
67 lines (52 loc) · 1.36 KB
/
content_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
package extedit
import (
"bufio"
"fmt"
"io/ioutil"
"strings"
"testing"
)
func TestCreateContent(t *testing.T) {
strRd := strings.NewReader("Line1\nLine2")
c, err := contentFromReader(strRd, bufio.ScanLines)
ok(t, err)
equals(t, c.Length(), 2)
equals(t, c.c[0], "Line1")
equals(t, c.c[1], "Line2")
}
func TestCreateContentFromString(t *testing.T) {
c, err := contentFromString("Line1\nLine2", bufio.ScanLines)
ok(t, err)
equals(t, c.Length(), 2)
equals(t, c.c[0], "Line1")
equals(t, c.c[1], "Line2")
}
func TestCreateContentFromFile(t *testing.T) {
f, err := ioutil.TempFile("", "")
ok(t, err)
_, err = f.Write([]byte("Line1\nLine2"))
ok(t, err)
c, err := contentFromFile(f.Name(), bufio.ScanLines)
equals(t, c.Length(), 2)
equals(t, c.c[0], "Line1")
equals(t, c.c[1], "Line2")
}
func TestSplitByWords(t *testing.T) {
c, err := contentFromString("Word1 Word2", bufio.ScanWords)
ok(t, err)
equals(t, c.Length(), 2)
equals(t, c.c[0], "Word1")
equals(t, c.c[1], "Word2")
}
func TestUseContentAsString(t *testing.T) {
c, err := contentFromString("Line1\nLine2", bufio.ScanLines)
ok(t, err)
equals(t, fmt.Sprintf("x%sx", c), "xLine1\nLine2x")
}
func TestUseContentAsReader(t *testing.T) {
c, err := contentFromString("Line1\nLine2", bufio.ScanLines)
ok(t, err)
byteCnt, err := ioutil.ReadAll(c)
ok(t, err)
equals(t, string(byteCnt), "Line1\nLine2")
}