@@ -66,3 +66,167 @@ index 0000000..3be2928`,
66
66
})
67
67
}
68
68
}
69
+
70
+ func TestLineReader_ReadLine (t * testing.T ) {
71
+ tests := []struct {
72
+ name string
73
+ input string
74
+ want []string
75
+ }{
76
+ {
77
+ name : "empty" ,
78
+ input : "" ,
79
+ want : []string {},
80
+ },
81
+ {
82
+ name : "single_line" ,
83
+ input : "@@ -0,0 +1,62 @@" ,
84
+ want : []string {"@@ -0,0 +1,62 @@" },
85
+ },
86
+ {
87
+ name : "single_lf_terminated_line" ,
88
+ input : "@@ -0,0 +1,62 @@\n " ,
89
+ want : []string {"@@ -0,0 +1,62 @@" },
90
+ },
91
+ {
92
+ name : "single_crlf_terminated_line" ,
93
+ input : "@@ -0,0 +1,62 @@\r \n " ,
94
+ want : []string {"@@ -0,0 +1,62 @@" },
95
+ },
96
+ {
97
+ name : "multi_line" ,
98
+ input : `diff --git a/test.go b/test.go
99
+ new file mode 100644
100
+ index 0000000..3be2928` ,
101
+ want : []string {
102
+ "diff --git a/test.go b/test.go" ,
103
+ "new file mode 100644" ,
104
+ "index 0000000..3be2928" ,
105
+ },
106
+ },
107
+ }
108
+ for _ , test := range tests {
109
+ t .Run (test .name , func (t * testing.T ) {
110
+ in := newLineReader (strings .NewReader (test .input ))
111
+ out := []string {}
112
+ for {
113
+ l , err := in .readLine ()
114
+ if err == io .EOF {
115
+ break
116
+ }
117
+ if err != nil {
118
+ t .Fatal (err )
119
+ }
120
+ out = append (out , string (l ))
121
+ }
122
+ if ! reflect .DeepEqual (test .want , out ) {
123
+ t .Errorf ("read lines not equal: want %v, got %v" , test .want , out )
124
+ }
125
+ })
126
+ }
127
+ }
128
+
129
+ func TestLineReader_NextLineStartsWith (t * testing.T ) {
130
+ input := `aaa rest of line
131
+ bbbrest of line
132
+ ccc rest of line`
133
+
134
+ in := newLineReader (strings .NewReader (input ))
135
+
136
+ type assertion struct {
137
+ prefix string
138
+ want bool
139
+ }
140
+
141
+ testsPerReadLine := []struct {
142
+ nextLine []assertion
143
+ nextNextLine []assertion
144
+ wantReadLineErr error
145
+ }{
146
+ {
147
+ nextLine : []assertion {
148
+ {prefix : "a" , want : true },
149
+ {prefix : "aa" , want : true },
150
+ {prefix : "aaa" , want : true },
151
+ {prefix : "bbb" , want : false },
152
+ {prefix : "ccc" , want : false },
153
+ },
154
+ nextNextLine : []assertion {
155
+ {prefix : "aaa" , want : false },
156
+ {prefix : "bbb" , want : true },
157
+ {prefix : "ccc" , want : false },
158
+ },
159
+ },
160
+ {
161
+ nextLine : []assertion {
162
+ {prefix : "aaa" , want : false },
163
+ {prefix : "bbb" , want : true },
164
+ {prefix : "ccc" , want : false },
165
+ },
166
+ nextNextLine : []assertion {
167
+ {prefix : "aaa" , want : false },
168
+ {prefix : "bbb" , want : false },
169
+ {prefix : "ccc" , want : true },
170
+ },
171
+ },
172
+ {
173
+ nextLine : []assertion {
174
+ {prefix : "aaa" , want : false },
175
+ {prefix : "bbb" , want : false },
176
+ {prefix : "ccc" , want : true },
177
+ {prefix : "ddd" , want : false },
178
+ },
179
+ nextNextLine : []assertion {
180
+ {prefix : "aaa" , want : false },
181
+ {prefix : "bbb" , want : false },
182
+ {prefix : "ccc" , want : false },
183
+ {prefix : "ddd" , want : false },
184
+ },
185
+ },
186
+ {
187
+ nextLine : []assertion {
188
+ {prefix : "aaa" , want : false },
189
+ {prefix : "bbb" , want : false },
190
+ {prefix : "ccc" , want : false },
191
+ {prefix : "ddd" , want : false },
192
+ },
193
+ nextNextLine : []assertion {
194
+ {prefix : "aaa" , want : false },
195
+ {prefix : "bbb" , want : false },
196
+ {prefix : "ccc" , want : false },
197
+ {prefix : "ddd" , want : false },
198
+ },
199
+ wantReadLineErr : io .EOF ,
200
+ },
201
+ }
202
+
203
+ for _ , tc := range testsPerReadLine {
204
+ for _ , assert := range tc .nextLine {
205
+ got , err := in .nextLineStartsWith (assert .prefix )
206
+ if err != nil {
207
+ t .Fatalf ("nextLineStartsWith returned unexpected error: %s" , err )
208
+ }
209
+
210
+ if got != assert .want {
211
+ t .Fatalf ("unexpected result for prefix %q. got=%t, want=%t" , assert .prefix , got , assert .want )
212
+ }
213
+ }
214
+
215
+ for _ , assert := range tc .nextNextLine {
216
+ got , err := in .nextNextLineStartsWith (assert .prefix )
217
+ if err != nil {
218
+ t .Fatalf ("nextLineStartsWith returned unexpected error: %s" , err )
219
+ }
220
+
221
+ if got != assert .want {
222
+ t .Fatalf ("unexpected result for prefix %q. got=%t, want=%t" , assert .prefix , got , assert .want )
223
+ }
224
+ }
225
+
226
+ _ , err := in .readLine ()
227
+ if err != tc .wantReadLineErr {
228
+ t .Fatalf ("readLine returned unexpected error. got=%s, want=%s" , err , tc .wantReadLineErr )
229
+ }
230
+
231
+ }
232
+ }
0 commit comments