(t *testing.T, apply func(io.Writer, io.ReaderAt, *File) error)
| 169 | } |
| 170 | |
| 171 | func (at applyTest) run(t *testing.T, apply func(io.Writer, io.ReaderAt, *File) error) { |
| 172 | src, patch, out := at.Files.Load(t) |
| 173 | |
| 174 | files, _, err := Parse(bytes.NewReader(patch)) |
| 175 | if err != nil { |
| 176 | t.Fatalf("failed to parse patch file: %v", err) |
| 177 | } |
| 178 | if len(files) != 1 { |
| 179 | t.Fatalf("patch should contain exactly one file, but it has %d", len(files)) |
| 180 | } |
| 181 | |
| 182 | var dst bytes.Buffer |
| 183 | err = apply(&dst, bytes.NewReader(src), files[0]) |
| 184 | if at.Err != nil { |
| 185 | assertError(t, at.Err, err, "applying fragment") |
| 186 | return |
| 187 | } |
| 188 | if err != nil { |
| 189 | var aerr *ApplyError |
| 190 | if errors.As(err, &aerr) { |
| 191 | t.Fatalf("unexpected error applying: at %d: fragment %d at %d: %v", aerr.Line, aerr.Fragment, aerr.FragmentLine, err) |
| 192 | } else { |
| 193 | t.Fatalf("unexpected error applying: %v", err) |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | if !bytes.Equal(out, dst.Bytes()) { |
| 198 | t.Errorf("incorrect result after apply\nexpected:\n%q\nactual:\n%q", out, dst.Bytes()) |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | type applyFiles struct { |
| 203 | Src string |
no test coverage detected