summaryrefslogtreecommitdiff
path: root/diff_test.go
diff options
context:
space:
mode:
authorJesse Ezell <[email protected]>2014-03-21 17:20:48 -0700
committerJesse Ezell <[email protected]>2014-03-21 17:20:48 -0700
commitf85c38ce22391ef8a932673dabf82219527ab433 (patch)
tree7e23c73e1c533124f6758d39e7622cd21882fd7d /diff_test.go
parentaea899e877cd9567fad5a8be7cc2cfb6d8fbb00f (diff)
Allow diff.ForEach to enumerate files, hunks, and lines with single call. Support use of closures for enumeration.
Diffstat (limited to 'diff_test.go')
-rw-r--r--diff_test.go41
1 files changed, 34 insertions, 7 deletions
diff --git a/diff_test.go b/diff_test.go
index 6ddd433..f3a1ea6 100644
--- a/diff_test.go
+++ b/diff_test.go
@@ -28,11 +28,18 @@ func TestDiffTreeToTree(t *testing.T) {
}
files := make([]string, 0)
-
- err = diff.ForEachFile(func(file *DiffDelta) error {
+ hunks := make([]*DiffHunk, 0)
+ lines := make([]*DiffLine, 0)
+ err = diff.ForEach(func(file *DiffDelta, progress float64) (DiffForEachHunkCallback, error) {
files = append(files, file.OldFile.Path)
- return nil
- })
+ return func(hunk *DiffHunk) (DiffForEachLineCallback, error) {
+ hunks = append(hunks, hunk)
+ return func(line *DiffLine) error {
+ lines = append(lines, line)
+ return nil
+ }, nil
+ }, nil
+ }, true, true)
checkFatal(t, err)
@@ -44,11 +51,31 @@ func TestDiffTreeToTree(t *testing.T) {
t.Fatal("File in diff was expected to be README")
}
+ if len(hunks) != 1 {
+ t.Fatal("Incorrect number of hunks in diff")
+ }
+
+ if hunks[0].OldStart != 1 || hunks[0].NewStart != 1 {
+ t.Fatal("Incorrect hunk")
+ }
+
+ if len(lines) != 2 {
+ t.Fatal("Incorrect number of lines in diff")
+ }
+
+ if lines[0].Content != "foo\n" {
+ t.Fatal("Incorrect lines in diff")
+ }
+
+ if lines[1].Content != "file changed\n" {
+ t.Fatal("Incorrect lines in diff")
+ }
+
errTest := errors.New("test error")
- err = diff.ForEachFile(func(file *DiffDelta) error {
- return errTest
- })
+ err = diff.ForEach(func(file *DiffDelta, progress float64) (DiffForEachHunkCallback, error) {
+ return nil, errTest
+ }, false, false)
if err != errTest {
t.Fatal("Expected custom error to be returned")