forked from travissimon/ghaml
/
view_writer_test.go
102 lines (84 loc) · 2.36 KB
/
view_writer_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
package main
import (
"log"
// "os"
"bytes"
"fmt"
"github.com/travissimon/formatting"
"strings"
"testing"
)
func Test_ViewWriterHookup(t *testing.T) {
log.Printf("Hookup Succeeded - testing view writer")
}
func Test_SmokeTest(t *testing.T) {
parser := NewParser("Writer smoke test", ".cl1.cl2#id1 Some text\n\t%p Child paragraph")
parser.Parse()
//vw := newViewWriter(os.Stdout, parser.root, "Test")
//vw.WriteView()
}
func Test_GetCharStr15(t *testing.T) {
str := genString(15, 3)
if str != "12 45 78 01 34 " {
t.Error("String not as expected: " + str)
}
str = genString(20, 10)
if str != "123456789 123456789 " {
t.Error("String not as expected: " + str)
}
}
func Test_GetWhitespaceIndicies(t *testing.T) {
str := genString(15, 3)
vw := NewViewWriter(nil, nil, nil, "")
spaces := vw.getWhitespaceIndicies(str)
if len(spaces) != 5 {
t.Error("Number of spaces not as expected: %d", spaces)
}
chkSpace(0, 2, spaces, t)
chkSpace(1, 5, spaces, t)
chkSpace(2, 8, spaces, t)
chkSpace(3, 11, spaces, t)
chkSpace(4, 14, spaces, t)
}
func chkSpace(sliceIndex int, expectedValue int, spaces []int, t *testing.T) {
if spaces[sliceIndex] != expectedValue {
t.Error(fmt.Sprintf("Unexpected value at index %d: %d", sliceIndex, spaces[sliceIndex]))
}
}
func Test_FormattingShortText(t *testing.T) {
txt := genString(9, 10)
expected := `123456789
`
testFormatStr(txt, expected, t)
}
func Test_FormattingOneWrap(t *testing.T) {
txt := genString(130, 10)
expected :=
`123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
123456789 123456789
`
testFormatStr(txt, expected, t)
}
func genString(count int, moduloToSpace int) string {
buffer := bytes.NewBuffer(make([]byte, 0))
for i := 1; i <= count; i++ {
if i%moduloToSpace == 0 {
fmt.Fprintf(buffer, " ")
} else {
fmt.Fprintf(buffer, "%d", i%10)
}
}
return buffer.String()
}
func testFormatStr(s string, expected string, t *testing.T) {
vw := NewViewWriter(nil, nil, nil, "")
buffer := bytes.NewBuffer(make([]byte, 0))
ind := formatting.NewIndentingWriter(buffer)
vw.writeLongText(s, ind)
result := buffer.String()
if result != expected {
t.Error("result not as expected")
t.Log(fmt.Sprintf("Expected: [%s]", strings.Replace(expected, " ", "+", -1)))
t.Log(fmt.Sprintf("Output: [%s]", strings.Replace(result, " ", "+", -1)))
}
}