this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add positive tests for TextFragment#ApplyStrict

These cover all of the cases (I think) where application should succeed.

+234 -8
+14 -8
gitdiff/apply.go
··· 122 122 return applyError(err, lineNum(n)) 123 123 } 124 124 125 + used := int64(0) 125 126 for i, line := range f.Lines { 126 - fromSrc, err := applyTextLine(dst, nextLine, line) 127 - if err != nil { 127 + if err := applyTextLine(dst, nextLine, line); err != nil { 128 128 return applyError(err, lineNum(n), fragLineNum(i)) 129 129 } 130 - 131 - if fromSrc && i < len(f.Lines)-1 { 130 + if fromSrc(line) { 131 + used++ 132 + } 133 + // advance reader if the next fragment line appears in src and we're behind 134 + if i < len(f.Lines)-1 && fromSrc(f.Lines[i+1]) && int64(n)-limit < used { 132 135 nextLine, n, err = src.ReadLine() 133 136 if err != nil { 134 137 if err == io.EOF { 135 138 err = io.ErrUnexpectedEOF 136 139 } 137 - return applyError(err, lineNum(n), fragLineNum(i+1)) 140 + return applyError(err, lineNum(n), fragLineNum(i+1)) // report for _next_ line in fragment 138 141 } 139 142 } 140 143 } ··· 142 145 return nil 143 146 } 144 147 145 - func applyTextLine(dst io.Writer, src string, line Line) (fromSrc bool, err error) { 148 + func applyTextLine(dst io.Writer, src string, line Line) (err error) { 146 149 switch line.Op { 147 150 case OpContext, OpDelete: 148 - fromSrc = true 149 151 if src != line.Line { 150 - return fromSrc, conflictError("fragment line does not match src line") 152 + return conflictError("fragment line does not match src line") 151 153 } 152 154 } 153 155 switch line.Op { ··· 155 157 _, err = io.WriteString(dst, line.Line) 156 158 } 157 159 return 160 + } 161 + 162 + func fromSrc(line Line) bool { 163 + return line.Op != OpAdd 158 164 } 159 165 160 166 // copyLines copies from src to dst until the line at limit, exclusive. Returns
+66
gitdiff/apply_test.go
··· 1 + package gitdiff 2 + 3 + import ( 4 + "bytes" 5 + "io/ioutil" 6 + "path/filepath" 7 + "testing" 8 + ) 9 + 10 + func TestTextFragmentApplyStrict(t *testing.T) { 11 + tests := map[string]struct { 12 + File string 13 + Err bool 14 + }{ 15 + "createFile": {File: "new"}, 16 + "deleteFile": {File: "delete_all"}, 17 + "addStart": {File: "add_start"}, 18 + "addMiddle": {File: "add_middle"}, 19 + "addEnd": {File: "add_end"}, 20 + "changeStart": {File: "change_start"}, 21 + "changeMiddle": {File: "change_middle"}, 22 + "changeEnd": {File: "change_end"}, 23 + } 24 + 25 + for name, test := range tests { 26 + t.Run(name, func(t *testing.T) { 27 + base := filepath.Join("testdata", "apply", "text_fragment_"+test.File) 28 + 29 + src, err := ioutil.ReadFile(base + ".src") 30 + if err != nil { 31 + t.Fatalf("failed to read source file: %v", err) 32 + } 33 + patch, err := ioutil.ReadFile(base + ".patch") 34 + if err != nil { 35 + t.Fatalf("failed to read patch file: %v", err) 36 + } 37 + result, err := ioutil.ReadFile(base + ".dst") 38 + if err != nil { 39 + t.Fatalf("failed to read result file: %v", err) 40 + } 41 + 42 + files, _, err := Parse(bytes.NewReader(patch)) 43 + if err != nil { 44 + t.Fatalf("failed to parse patch file: %v", err) 45 + } 46 + 47 + frag := files[0].TextFragments[0] 48 + 49 + var dst bytes.Buffer 50 + err = frag.ApplyStrict(&dst, NewLineReader(bytes.NewReader(src), 0)) 51 + if test.Err { 52 + if err == nil { 53 + t.Fatalf("expected error applying fragment, but got nil") 54 + } 55 + return 56 + } 57 + if err != nil { 58 + t.Fatalf("unexpected error applying fragment: %v", err) 59 + } 60 + 61 + if !bytes.Equal(result, dst.Bytes()) { 62 + t.Errorf("incorrect result after apply\nexpected:\n%s\nactual:\n%s", result, dst.Bytes()) 63 + } 64 + }) 65 + } 66 + }
+5
gitdiff/testdata/apply/text_fragment_add_end.dst
··· 1 + line 1 2 + line 2 3 + line 3 4 + new line a 5 + new line b
+9
gitdiff/testdata/apply/text_fragment_add_end.patch
··· 1 + diff --git a/gitdiff/testdata/apply/fragment_add_end.src b/gitdiff/testdata/apply/fragment_add_end.src 2 + --- a/gitdiff/testdata/apply/fragment_add_end.src 3 + +++ b/gitdiff/testdata/apply/fragment_add_end.src 4 + @@ -1,3 +1,5 @@ 5 + line 1 6 + line 2 7 + line 3 8 + +new line a 9 + +new line b
+3
gitdiff/testdata/apply/text_fragment_add_end.src
··· 1 + line 1 2 + line 2 3 + line 3
+5
gitdiff/testdata/apply/text_fragment_add_middle.dst
··· 1 + line 1 2 + line 2 3 + new line a 4 + new line b 5 + line 3
+9
gitdiff/testdata/apply/text_fragment_add_middle.patch
··· 1 + diff --git a/gitdiff/testdata/apply/fragment_add_middle.src b/gitdiff/testdata/apply/fragment_add_middle.src 2 + --- a/gitdiff/testdata/apply/fragment_add_middle.src 3 + +++ b/gitdiff/testdata/apply/fragment_add_middle.src 4 + @@ -1,3 +1,5 @@ 5 + line 1 6 + line 2 7 + +new line a 8 + +new line b 9 + line 3
+3
gitdiff/testdata/apply/text_fragment_add_middle.src
··· 1 + line 1 2 + line 2 3 + line 3
+4
gitdiff/testdata/apply/text_fragment_add_start.dst
··· 1 + new line a 2 + line 1 3 + line 2 4 + line 3
+8
gitdiff/testdata/apply/text_fragment_add_start.patch
··· 1 + diff --git a/gitdiff/testdata/apply/fragment_add_start.src b/gitdiff/testdata/apply/fragment_add_start.src 2 + --- a/gitdiff/testdata/apply/fragment_add_start.src 3 + +++ b/gitdiff/testdata/apply/fragment_add_start.src 4 + @@ -1,3 +1,4 @@ 5 + +new line a 6 + line 1 7 + line 2 8 + line 3
+3
gitdiff/testdata/apply/text_fragment_add_start.src
··· 1 + line 1 2 + line 2 3 + line 3
+10
gitdiff/testdata/apply/text_fragment_change_end.dst
··· 1 + line 1 2 + line 2 3 + line 3 4 + line 4 5 + line 5 6 + line 6 7 + line 7 8 + line 8 9 + line 9 10 + new line a
+9
gitdiff/testdata/apply/text_fragment_change_end.patch
··· 1 + diff --git a/gitdiff/testdata/apply/text_fragment_change_end.src b/gitdiff/testdata/apply/text_fragment_change_end.src 2 + --- a/gitdiff/testdata/apply/text_fragment_change_end.src 3 + +++ b/gitdiff/testdata/apply/text_fragment_change_end.src 4 + @@ -7,4 +7,4 @@ line 6 5 + line 7 6 + line 8 7 + line 9 8 + -line 10 9 + +new line a
+10
gitdiff/testdata/apply/text_fragment_change_end.src
··· 1 + line 1 2 + line 2 3 + line 3 4 + line 4 5 + line 5 6 + line 6 7 + line 7 8 + line 8 9 + line 9 10 + line 10
+9
gitdiff/testdata/apply/text_fragment_change_middle.dst
··· 1 + line 1 2 + line 2 3 + line 3 4 + line 4 5 + line 5 6 + new line a 7 + line 7 8 + line 8 9 + line 9
+12
gitdiff/testdata/apply/text_fragment_change_middle.patch
··· 1 + diff --git a/gitdiff/testdata/apply/text_fragment_change_middle.src b/gitdiff/testdata/apply/text_fragment_change_middle.src 2 + --- a/gitdiff/testdata/apply/text_fragment_change_middle.src 3 + +++ b/gitdiff/testdata/apply/text_fragment_change_middle.src 4 + @@ -3,7 +3,7 @@ line 2 5 + line 3 6 + line 4 7 + line 5 8 + -line 6 9 + +new line a 10 + line 7 11 + line 8 12 + line 9
+10
gitdiff/testdata/apply/text_fragment_change_middle.src
··· 1 + line 1 2 + line 2 3 + line 3 4 + line 4 5 + line 5 6 + line 6 7 + line 7 8 + line 8 9 + line 9 10 + line 10
+4
gitdiff/testdata/apply/text_fragment_change_start.dst
··· 1 + new line a 2 + line 2 3 + line 3 4 + line 4
+9
gitdiff/testdata/apply/text_fragment_change_start.patch
··· 1 + diff --git a/gitdiff/testdata/apply/text_fragment_change_start.src b/gitdiff/testdata/apply/text_fragment_change_start.src 2 + --- a/gitdiff/testdata/apply/text_fragment_change_start.src 3 + +++ b/gitdiff/testdata/apply/text_fragment_change_start.src 4 + @@ -1,4 +1,4 @@ 5 + -line 1 6 + +new line a 7 + line 2 8 + line 3 9 + line 4
+10
gitdiff/testdata/apply/text_fragment_change_start.src
··· 1 + line 1 2 + line 2 3 + line 3 4 + line 4 5 + line 5 6 + line 6 7 + line 7 8 + line 8 9 + line 9 10 + line 10
gitdiff/testdata/apply/text_fragment_delete_all.dst

This is a binary file and will not be displayed.

+8
gitdiff/testdata/apply/text_fragment_delete_all.patch
··· 1 + diff --git a/gitdiff/testdata/apply/fragment_delete_all.src b/gitdiff/testdata/apply/fragment_delete_all.src 2 + --- a/gitdiff/testdata/apply/fragment_delete_all.src 3 + +++ b/gitdiff/testdata/apply/fragment_delete_all.src 4 + @@ -1,4 +0,0 @@ 5 + -line a 6 + -line b 7 + -line c 8 + -line d
+4
gitdiff/testdata/apply/text_fragment_delete_all.src
··· 1 + line a 2 + line b 3 + line c 4 + line d
+3
gitdiff/testdata/apply/text_fragment_new.dst
··· 1 + line 1 2 + line 2 3 + line 3
+7
gitdiff/testdata/apply/text_fragment_new.patch
··· 1 + diff --git a/gitdiff/testdata/apply/fragment_new.src b/gitdiff/testdata/apply/fragment_new.src 2 + --- a/gitdiff/testdata/apply/fragment_new.src 3 + +++ b/gitdiff/testdata/apply/fragment_new.src 4 + @@ -0,0 +1,3 @@ 5 + +line 1 6 + +line 2 7 + +line 3
gitdiff/testdata/apply/text_fragment_new.src

This is a binary file and will not be displayed.