Add error tests for TextFragment#ApplyStrict

These cover the errors that can happen with a single fragment and no
additional manipulation by the caller of ApplyStrict.
This commit is contained in:
Billy Keyes
2020-01-12 22:03:21 -08:00
parent 6a055aa600
commit 1b341e0693
7 changed files with 132 additions and 26 deletions
+64 -26
View File
@@ -4,44 +4,79 @@ import (
"bytes"
"io/ioutil"
"path/filepath"
"strings"
"testing"
)
func TestTextFragmentApplyStrict(t *testing.T) {
tests := map[string]struct {
File string
Err bool
File string
SrcFile string
PatchFile string
DstFile string
Err string
}{
"createFile": {File: "new"},
"deleteFile": {File: "delete_all"},
"createFile": {File: "text_fragment_new"},
"deleteFile": {File: "text_fragment_delete_all"},
"addStart": {File: "add_start"},
"addMiddle": {File: "add_middle"},
"addEnd": {File: "add_end"},
"addEndNoEOL": {File: "add_end_noeol"},
"addStart": {File: "text_fragment_add_start"},
"addMiddle": {File: "text_fragment_add_middle"},
"addEnd": {File: "text_fragment_add_end"},
"addEndNoEOL": {File: "text_fragment_add_end_noeol"},
"changeStart": {File: "change_start"},
"changeMiddle": {File: "change_middle"},
"changeEnd": {File: "change_end"},
"changeExact": {File: "change_exact"},
"changeSingleNoEOL": {File: "change_single_noeol"},
"changeStart": {File: "text_fragment_change_start"},
"changeMiddle": {File: "text_fragment_change_middle"},
"changeEnd": {File: "text_fragment_change_end"},
"changeExact": {File: "text_fragment_change_exact"},
"changeSingleNoEOL": {File: "text_fragment_change_single_noeol"},
"errorShortSrcBefore": {
SrcFile: "text_fragment_error",
PatchFile: "text_fragment_error_short_src_before",
Err: "unexpected EOF",
},
"errorShortSrc": {
SrcFile: "text_fragment_error",
PatchFile: "text_fragment_error_short_src",
Err: "unexpected EOF",
},
"errorContextConflict": {
SrcFile: "text_fragment_error",
PatchFile: "text_fragment_error_context_conflict",
Err: "conflict",
},
"errorDeleteConflict": {
SrcFile: "text_fragment_error",
PatchFile: "text_fragment_error_delete_conflict",
Err: "conflict",
},
"errorNewFile": {
SrcFile: "text_fragment_error",
PatchFile: "text_fragment_error_new_file",
Err: "conflict",
},
}
loadFile := func(name, defaultName, ext string) []byte {
if name == "" {
name = defaultName
}
d, err := ioutil.ReadFile(filepath.Join("testdata", "apply", name+"."+ext))
if err != nil {
t.Fatalf("failed to read %s file: %v", ext, err)
}
return d
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
base := filepath.Join("testdata", "apply", "text_fragment_"+test.File)
src := loadFile(test.SrcFile, test.File, "src")
patch := loadFile(test.PatchFile, test.File, "patch")
src, err := ioutil.ReadFile(base + ".src")
if err != nil {
t.Fatalf("failed to read source file: %v", err)
}
patch, err := ioutil.ReadFile(base + ".patch")
if err != nil {
t.Fatalf("failed to read patch file: %v", err)
}
result, err := ioutil.ReadFile(base + ".dst")
if err != nil {
t.Fatalf("failed to read result file: %v", err)
var result []byte
if test.Err == "" {
result = loadFile(test.DstFile, test.File, "dst")
}
files, _, err := Parse(bytes.NewReader(patch))
@@ -53,10 +88,13 @@ func TestTextFragmentApplyStrict(t *testing.T) {
var dst bytes.Buffer
err = frag.ApplyStrict(&dst, NewLineReader(bytes.NewReader(src), 0))
if test.Err {
if test.Err != "" {
if err == nil {
t.Fatalf("expected error applying fragment, but got nil")
}
if !strings.Contains(err.Error(), test.Err) {
t.Fatalf("incorrect apply error: expected %q, actual %q", test.Err, err.Error())
}
return
}
if err != nil {
+13
View File
@@ -0,0 +1,13 @@
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10
line 11
line 12
line 13
@@ -0,0 +1,12 @@
diff --git a/gitdiff/testdata/apply/text_fragment_error.src b/gitdiff/testdata/apply/text_fragment_error.src
--- a/gitdiff/testdata/apply/text_fragment_error.src
+++ b/gitdiff/testdata/apply/text_fragment_error.src
@@ -4,7 +4,7 @@ line 3
line 4
line 5
line conflict
-line 7
+new line a
line 8
line 9
line 10
@@ -0,0 +1,12 @@
diff --git a/gitdiff/testdata/apply/text_fragment_error.src b/gitdiff/testdata/apply/text_fragment_error.src
--- a/gitdiff/testdata/apply/text_fragment_error.src
+++ b/gitdiff/testdata/apply/text_fragment_error.src
@@ -4,7 +4,7 @@ line 3
line 4
line 5
line 6
-line conflict
+new line a
line 8
line 9
line 10
@@ -0,0 +1,7 @@
diff --git a/gitdiff/testdata/apply/text_fragment_error.src b/gitdiff/testdata/apply/text_fragment_error.src
--- a/gitdiff/testdata/apply/text_fragment_error.src
+++ b/gitdiff/testdata/apply/text_fragment_error.src
@@ -0,0 +1,3 @@
+line 1
+line 2
+line 3
@@ -0,0 +1,12 @@
diff --git a/gitdiff/testdata/apply/text_fragment_error.src b/gitdiff/testdata/apply/text_fragment_error.src
--- a/gitdiff/testdata/apply/text_fragment_error.src
+++ b/gitdiff/testdata/apply/text_fragment_error.src
@@ -9,7 +9,7 @@ line 8
line 9
line 10
line 11
-line 12
+new line a
line 13
line 14
line 15
@@ -0,0 +1,12 @@
diff --git a/gitdiff/testdata/apply/text_fragment_error.src b/gitdiff/testdata/apply/text_fragment_error.src
--- a/gitdiff/testdata/apply/text_fragment_error.src
+++ b/gitdiff/testdata/apply/text_fragment_error.src
@@ -15,7 +15,7 @@ line 14
line 15
line 16
line 17
-line 18
+new line a
line 19
line 20
line 21