Skip to content

Commit 6fb7d10

Browse files
committed
difflib: turn some tests into examples, fix documentation
1 parent 4d91f93 commit 6fb7d10

File tree

2 files changed

+34
-45
lines changed

2 files changed

+34
-45
lines changed

difflib/difflib.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,10 +363,11 @@ func (m *SequenceMatcher) GetMatchingBlocks() []Match {
363363
// The tags are characters, with these meanings:
364364
//
365365
// 'r' (replace): a[i1:i2] should be replaced by b[j1:j2]
366-
// 'd' (delete): a[i1:i2] should be deleted.
367-
// Note that j1==j2 in this case.
368-
// 'i' (insert): b[j1:j2] should be inserted at a[i1:i1].
369-
// Note that i1==i2 in this case.
366+
//
367+
// 'd' (delete): a[i1:i2] should be deleted, j1==j2 in this case.
368+
//
369+
// 'i' (insert): b[j1:j2] should be inserted at a[i1:i1], i1==i2 in this case.
370+
//
370371
// 'e' (equal): a[i1:i2] == b[j1:j2]
371372
func (m *SequenceMatcher) GetOpCodes() []OpCode {
372373
if m.opCodes != nil {

difflib/difflib_test.go

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ group
102102
}
103103
}
104104

105-
func TestUnifiedDiff(t *testing.T) {
105+
func ExampleGetUnifiedDiffString() {
106106
a := `one
107107
two
108108
three
@@ -120,27 +120,20 @@ four`
120120
ToDate: "2010-04-02 10:20:52",
121121
Context: 3,
122122
}
123-
result, err := GetUnifiedDiffString(diff)
124-
if err != nil {
125-
t.Errorf("unified diff failed: %s", err)
126-
}
127-
expected := `--- Original\t2005-01-26 23:30:50
128-
+++ Current\t2010-04-02 10:20:52
129-
@@ -1,4 +1,4 @@
130-
+zero
131-
one
132-
-two
133-
three
134-
four
135-
`
136-
// TABs are a pain to preserve through editors
137-
expected = strings.Replace(expected, "\\t", "\t", -1)
138-
if expected != result {
139-
t.Errorf("unexpected diff result:\n%s", result)
140-
}
123+
result, _ := GetUnifiedDiffString(diff)
124+
fmt.Printf(strings.Replace(result, "\t", " ", -1))
125+
// Output:
126+
// --- Original 2005-01-26 23:30:50
127+
// +++ Current 2010-04-02 10:20:52
128+
// @@ -1,4 +1,4 @@
129+
// +zero
130+
// one
131+
// -two
132+
// three
133+
// four
141134
}
142135

143-
func TestContextDiff(t *testing.T) {
136+
func ExampleGetContextDiffString() {
144137
a := `one
145138
two
146139
three
@@ -157,27 +150,22 @@ four`
157150
Context: 3,
158151
Eol: "\n",
159152
}
160-
result, err := GetContextDiffString(diff)
161-
assertEqual(t, err, nil)
162-
expected := `*** Original
163-
--- Current
164-
***************
165-
*** 1,4 ****
166-
one
167-
! two
168-
! three
169-
four
170-
--- 1,4 ----
171-
+ zero
172-
one
173-
! tree
174-
four
175-
`
176-
// TABs are a pain to preserve through editors
177-
expected = strings.Replace(expected, "\\t", "\t", -1)
178-
if expected != result {
179-
t.Errorf("unexpected diff result:\n%s", result)
180-
}
153+
result, _ := GetContextDiffString(diff)
154+
fmt.Printf(strings.Replace(result, "\t", " ", -1))
155+
// Output:
156+
// *** Original
157+
// --- Current
158+
// ***************
159+
// *** 1,4 ****
160+
// one
161+
// ! two
162+
// ! three
163+
// four
164+
// --- 1,4 ----
165+
// + zero
166+
// one
167+
// ! tree
168+
// four
181169
}
182170

183171
func rep(s string, count int) string {

0 commit comments

Comments
 (0)