-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfold_test.go
177 lines (165 loc) · 3.61 KB
/
fold_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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package wordwrap
import (
"fmt"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
"image"
"reflect"
"testing"
)
func TestSimpleFolder(t *testing.T) {
type WantedLine struct {
words []string
N int
}
tests := []struct {
name string
folder Folder
wantLines []*WantedLine
wantErr bool
}{
{
name: "just word that fits",
folder: NewSimpleFolder(&FixedWordWidthBoxer{
text: []rune("word that fits"),
}, image.Rect(0, 0, 6, 6), nil),
wantLines: []*WantedLine{
{
words: []string{"word", " ", "that", " ", "fits"},
N: len("word that fits"),
},
},
wantErr: false,
},
{
name: "Empty",
folder: NewSimpleFolder(&FixedWordWidthBoxer{
text: []rune(""),
}, image.Rect(0, 0, 2, 5), nil),
wantLines: []*WantedLine{
{
words: nil,
},
},
wantErr: false,
},
{
name: "word that folder over onto a new line",
folder: NewSimpleFolder(&FixedWordWidthBoxer{
text: []rune("word that folder over onto a new line"),
}, image.Rect(0, 0, 6, 5), nil),
wantLines: []*WantedLine{
{
words: []string{"word", " ", "that", " ", "folder"},
N: len("word that folder "),
},
{
words: []string{"over", " ", "onto", " ", "a"},
N: len("over onto a "),
},
{
words: []string{"new", " ", "line"},
N: len("new line"),
},
},
wantErr: false,
},
{
name: "eod is nil",
folder: NewSimpleFolder(&FixedWordWidthBoxer{
text: []rune("word that"),
}, image.Rect(0, 0, 6, 5), nil),
wantLines: []*WantedLine{
{
words: []string{"word", " ", "that"},
N: len("word that"),
},
nil,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for _, wantWords := range tt.wantLines {
gotL, err := tt.folder.Next(7)
if (err != nil) != tt.wantErr {
t.Errorf("SimpleFolder() error = %v, wantErr %v", err, tt.wantErr)
return
}
var gotWords []string
switch l := gotL.(type) {
case *SimpleLine:
for _, b := range l.boxes {
switch b := b.(type) {
case *SimpleTextBox:
gotWords = append(gotWords, b.Contents)
}
}
}
if wantWords == nil {
if gotL != nil {
t.Errorf("SimpleFolder() gotN = %v, expected no line", gotL)
}
return
}
if !reflect.DeepEqual(gotWords, wantWords.words) {
t.Errorf("SimpleFolder() gotWords = %v, wantWords.words %v", gotWords, wantWords.words)
}
}
})
}
}
type FixedWordWidthBoxer struct {
text []rune
n int
}
func (fwb *FixedWordWidthBoxer) Shift() Box {
panic("implement me")
}
func (fwb *FixedWordWidthBoxer) Unshift(b ...Box) {
panic("implement me")
}
func (fwb *FixedWordWidthBoxer) Pos() int {
panic("implement me")
}
func (fwb *FixedWordWidthBoxer) Push(box ...Box) {
panic("implement me")
}
func (fwb *FixedWordWidthBoxer) HasNext() bool {
panic("implement me")
}
func (fwb *FixedWordWidthBoxer) SetFontDrawer(face *font.Drawer) {
panic("implement me")
}
func (fwb *FixedWordWidthBoxer) FontDrawer() *font.Drawer {
panic("implement me")
}
func (fwb *FixedWordWidthBoxer) Back(i int) {
fwb.n -= i
}
func (fwb *FixedWordWidthBoxer) Next() (Box, int, error) {
n, rs, rmode := SimpleBoxerGrab(fwb.text[fwb.n:])
var b Box
switch rmode {
case RNIL:
return nil, fwb.n, nil
case RSimpleBox, RCRLF:
t := string(rs)
b = &SimpleTextBox{
Contents: t,
Bounds: fixed.R(0, 0, 1, 1),
Advance: fixed.I(1),
}
default:
return nil, fwb.n, fmt.Errorf("unknown rmode %d", rmode)
}
switch rmode {
case RCRLF:
b = &LineBreakBox{
Box: b,
}
}
fwb.n += n
return b, fwb.n, nil
}