@@ -148,6 +148,46 @@ four`
148148 }
149149}
150150
151+ func TestContextDiff (t * testing.T ) {
152+ a := `one
153+ two
154+ three
155+ four`
156+ b := `zero
157+ one
158+ tree
159+ four`
160+ diff := ContextDiff {
161+ A : splitLines (a ),
162+ B : splitLines (b ),
163+ FromFile : "Original" ,
164+ ToFile : "Current" ,
165+ Context : 3 ,
166+ Eol : "\n " ,
167+ }
168+ result , err := GetContextDiffString (diff )
169+ assertEqual (t , err , nil )
170+ expected := `*** Original
171+ --- Current
172+ ***************
173+ *** 1,4 ****
174+ one
175+ ! two
176+ ! three
177+ four
178+ --- 1,4 ----
179+ + zero
180+ one
181+ ! tree
182+ four
183+ `
184+ // TABs are a pain to preserve through editors
185+ expected = strings .Replace (expected , "\\ t" , "\t " , - 1 )
186+ if expected != result {
187+ t .Errorf ("unexpected diff result:\n %s" , result )
188+ }
189+ }
190+
151191func rep (s string , count int ) string {
152192 return strings .Repeat (s , count )
153193}
@@ -232,3 +272,68 @@ func TestOutputFormatRangeFormatUnified(t *testing.T) {
232272 assertEqual (t , fm (3 , 6 ), "4,3" )
233273 assertEqual (t , fm (0 , 0 ), "0,0" )
234274}
275+
276+ func TestOutputFormatRangeFormatContext (t * testing.T ) {
277+ // Per the diff spec at http://www.unix.org/single_unix_specification/
278+ //
279+ // The range of lines in file1 shall be written in the following format
280+ // if the range contains two or more lines:
281+ // "*** %d,%d ****\n", <beginning line number>, <ending line number>
282+ // and the following format otherwise:
283+ // "*** %d ****\n", <ending line number>
284+ // The ending line number of an empty range shall be the number of the preceding line,
285+ // or 0 if the range is at the start of the file.
286+ //
287+ // Next, the range of lines in file2 shall be written in the following format
288+ // if the range contains two or more lines:
289+ // "--- %d,%d ----\n", <beginning line number>, <ending line number>
290+ // and the following format otherwise:
291+ // "--- %d ----\n", <ending line number>
292+ fm := formatRangeContext
293+ assertEqual (t , fm (3 , 3 ), "3" )
294+ assertEqual (t , fm (3 , 4 ), "4" )
295+ assertEqual (t , fm (3 , 5 ), "4,5" )
296+ assertEqual (t , fm (3 , 6 ), "4,6" )
297+ assertEqual (t , fm (0 , 0 ), "0" )
298+ }
299+
300+ func TestOutputFormatTabDelimiter (t * testing.T ) {
301+ diff := UnifiedDiff {
302+ A : splitChars ("one" ),
303+ B : splitChars ("two" ),
304+ FromFile : "Original" ,
305+ FromDate : "2005-01-26 23:30:50" ,
306+ ToFile : "Current" ,
307+ ToDate : "2010-04-12 10:20:52" ,
308+ Eol : "\n " ,
309+ }
310+ ud , err := GetUnifiedDiffString (diff )
311+ assertEqual (t , err , nil )
312+ assertEqual (t , splitLines (ud )[:2 ], []string {
313+ "--- Original\t 2005-01-26 23:30:50\n " ,
314+ "+++ Current\t 2010-04-12 10:20:52\n " ,
315+ })
316+ cd , err := GetContextDiffString (ContextDiff (diff ))
317+ assertEqual (t , err , nil )
318+ assertEqual (t , splitLines (cd )[:2 ], []string {
319+ "*** Original\t 2005-01-26 23:30:50\n " ,
320+ "--- Current\t 2010-04-12 10:20:52\n " ,
321+ })
322+ }
323+
324+ func TestOutputFormatNoTrailingTabOnEmptyFiledate (t * testing.T ) {
325+ diff := UnifiedDiff {
326+ A : splitChars ("one" ),
327+ B : splitChars ("two" ),
328+ FromFile : "Original" ,
329+ ToFile : "Current" ,
330+ Eol : "\n " ,
331+ }
332+ ud , err := GetUnifiedDiffString (diff )
333+ assertEqual (t , err , nil )
334+ assertEqual (t , splitLines (ud )[:2 ], []string {"--- Original\n " , "+++ Current\n " })
335+
336+ cd , err := GetContextDiffString (ContextDiff (diff ))
337+ assertEqual (t , err , nil )
338+ assertEqual (t , splitLines (cd )[:2 ], []string {"*** Original\n " , "--- Current\n " })
339+ }
0 commit comments