Skip to content

Commit

Permalink
difflib: turn some tests into examples, fix documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
pmezard committed Nov 23, 2013
1 parent 4d91f93 commit 6fb7d10
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 45 deletions.
9 changes: 5 additions & 4 deletions difflib/difflib.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,11 @@ func (m *SequenceMatcher) GetMatchingBlocks() []Match {
// The tags are characters, with these meanings:
//
// 'r' (replace): a[i1:i2] should be replaced by b[j1:j2]
// 'd' (delete): a[i1:i2] should be deleted.
// Note that j1==j2 in this case.
// 'i' (insert): b[j1:j2] should be inserted at a[i1:i1].
// Note that i1==i2 in this case.
//
// 'd' (delete): a[i1:i2] should be deleted, j1==j2 in this case.
//
// 'i' (insert): b[j1:j2] should be inserted at a[i1:i1], i1==i2 in this case.
//
// 'e' (equal): a[i1:i2] == b[j1:j2]
func (m *SequenceMatcher) GetOpCodes() []OpCode {
if m.opCodes != nil {
Expand Down
70 changes: 29 additions & 41 deletions difflib/difflib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ group
}
}

func TestUnifiedDiff(t *testing.T) {
func ExampleGetUnifiedDiffString() {
a := `one
two
three
Expand All @@ -120,27 +120,20 @@ four`
ToDate: "2010-04-02 10:20:52",
Context: 3,
}
result, err := GetUnifiedDiffString(diff)
if err != nil {
t.Errorf("unified diff failed: %s", err)
}
expected := `--- Original\t2005-01-26 23:30:50
+++ Current\t2010-04-02 10:20:52
@@ -1,4 +1,4 @@
+zero
one
-two
three
four
`
// TABs are a pain to preserve through editors
expected = strings.Replace(expected, "\\t", "\t", -1)
if expected != result {
t.Errorf("unexpected diff result:\n%s", result)
}
result, _ := GetUnifiedDiffString(diff)
fmt.Printf(strings.Replace(result, "\t", " ", -1))
// Output:
// --- Original 2005-01-26 23:30:50
// +++ Current 2010-04-02 10:20:52
// @@ -1,4 +1,4 @@
// +zero
// one
// -two
// three
// four
}

func TestContextDiff(t *testing.T) {
func ExampleGetContextDiffString() {
a := `one
two
three
Expand All @@ -157,27 +150,22 @@ four`
Context: 3,
Eol: "\n",
}
result, err := GetContextDiffString(diff)
assertEqual(t, err, nil)
expected := `*** Original
--- Current
***************
*** 1,4 ****
one
! two
! three
four
--- 1,4 ----
+ zero
one
! tree
four
`
// TABs are a pain to preserve through editors
expected = strings.Replace(expected, "\\t", "\t", -1)
if expected != result {
t.Errorf("unexpected diff result:\n%s", result)
}
result, _ := GetContextDiffString(diff)
fmt.Printf(strings.Replace(result, "\t", " ", -1))
// Output:
// *** Original
// --- Current
// ***************
// *** 1,4 ****
// one
// ! two
// ! three
// four
// --- 1,4 ----
// + zero
// one
// ! tree
// four
}

func rep(s string, count int) string {
Expand Down

0 comments on commit 6fb7d10

Please sign in to comment.