@@ -4,44 +4,79 @@ import (
44 "bytes"
55 "io/ioutil"
66 "path/filepath"
7+ "strings"
78 "testing"
89)
910
1011func TestTextFragmentApplyStrict (t * testing.T ) {
1112 tests := map [string ]struct {
12- File string
13- Err bool
13+ File string
14+ SrcFile string
15+ PatchFile string
16+ DstFile string
17+
18+ Err string
1419 }{
15- "createFile" : {File : "new" },
16- "deleteFile" : {File : "delete_all" },
20+ "createFile" : {File : "text_fragment_new" },
21+ "deleteFile" : {File : "text_fragment_delete_all" },
22+
23+ "addStart" : {File : "text_fragment_add_start" },
24+ "addMiddle" : {File : "text_fragment_add_middle" },
25+ "addEnd" : {File : "text_fragment_add_end" },
26+ "addEndNoEOL" : {File : "text_fragment_add_end_noeol" },
1727
18- "addStart" : {File : "add_start" },
19- "addMiddle" : {File : "add_middle" },
20- "addEnd" : {File : "add_end" },
21- "addEndNoEOL" : {File : "add_end_noeol" },
28+ "changeStart" : {File : "text_fragment_change_start" },
29+ "changeMiddle" : {File : "text_fragment_change_middle" },
30+ "changeEnd" : {File : "text_fragment_change_end" },
31+ "changeExact" : {File : "text_fragment_change_exact" },
32+ "changeSingleNoEOL" : {File : "text_fragment_change_single_noeol" },
2233
23- "changeStart" : {File : "change_start" },
24- "changeMiddle" : {File : "change_middle" },
25- "changeEnd" : {File : "change_end" },
26- "changeExact" : {File : "change_exact" },
27- "changeSingleNoEOL" : {File : "change_single_noeol" },
34+ "errorShortSrcBefore" : {
35+ SrcFile : "text_fragment_error" ,
36+ PatchFile : "text_fragment_error_short_src_before" ,
37+ Err : "unexpected EOF" ,
38+ },
39+ "errorShortSrc" : {
40+ SrcFile : "text_fragment_error" ,
41+ PatchFile : "text_fragment_error_short_src" ,
42+ Err : "unexpected EOF" ,
43+ },
44+ "errorContextConflict" : {
45+ SrcFile : "text_fragment_error" ,
46+ PatchFile : "text_fragment_error_context_conflict" ,
47+ Err : "conflict" ,
48+ },
49+ "errorDeleteConflict" : {
50+ SrcFile : "text_fragment_error" ,
51+ PatchFile : "text_fragment_error_delete_conflict" ,
52+ Err : "conflict" ,
53+ },
54+ "errorNewFile" : {
55+ SrcFile : "text_fragment_error" ,
56+ PatchFile : "text_fragment_error_new_file" ,
57+ Err : "conflict" ,
58+ },
59+ }
60+
61+ loadFile := func (name , defaultName , ext string ) []byte {
62+ if name == "" {
63+ name = defaultName
64+ }
65+ d , err := ioutil .ReadFile (filepath .Join ("testdata" , "apply" , name + "." + ext ))
66+ if err != nil {
67+ t .Fatalf ("failed to read %s file: %v" , ext , err )
68+ }
69+ return d
2870 }
2971
3072 for name , test := range tests {
3173 t .Run (name , func (t * testing.T ) {
32- base := filepath .Join ("testdata" , "apply" , "text_fragment_" + test .File )
74+ src := loadFile (test .SrcFile , test .File , "src" )
75+ patch := loadFile (test .PatchFile , test .File , "patch" )
3376
34- src , err := ioutil .ReadFile (base + ".src" )
35- if err != nil {
36- t .Fatalf ("failed to read source file: %v" , err )
37- }
38- patch , err := ioutil .ReadFile (base + ".patch" )
39- if err != nil {
40- t .Fatalf ("failed to read patch file: %v" , err )
41- }
42- result , err := ioutil .ReadFile (base + ".dst" )
43- if err != nil {
44- t .Fatalf ("failed to read result file: %v" , err )
77+ var result []byte
78+ if test .Err == "" {
79+ result = loadFile (test .DstFile , test .File , "dst" )
4580 }
4681
4782 files , _ , err := Parse (bytes .NewReader (patch ))
@@ -53,10 +88,13 @@ func TestTextFragmentApplyStrict(t *testing.T) {
5388
5489 var dst bytes.Buffer
5590 err = frag .ApplyStrict (& dst , NewLineReader (bytes .NewReader (src ), 0 ))
56- if test .Err {
91+ if test .Err != "" {
5792 if err == nil {
5893 t .Fatalf ("expected error applying fragment, but got nil" )
5994 }
95+ if ! strings .Contains (err .Error (), test .Err ) {
96+ t .Fatalf ("incorrect apply error: expected %q, actual %q" , test .Err , err .Error ())
97+ }
6098 return
6199 }
62100 if err != nil {
0 commit comments