Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 1 addition & 21 deletions gitdiff/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"io/ioutil"
"path/filepath"
"strings"
"testing"
)

Expand Down Expand Up @@ -221,7 +220,7 @@ func (at applyTest) run(t *testing.T, apply func(io.Writer, *Applier, *File) err
var dst bytes.Buffer
err = apply(&dst, applier, files[0])
if at.Err != nil {
at.assertError(t, err)
assertError(t, at.Err, err, "applying fragment")
return
}
if err != nil {
Expand All @@ -238,25 +237,6 @@ func (at applyTest) run(t *testing.T, apply func(io.Writer, *Applier, *File) err
}
}

func (at applyTest) assertError(t *testing.T, err error) {
if err == nil {
t.Fatalf("expected error applying fragment, but got nil")
}

switch terr := at.Err.(type) {
case string:
if !strings.Contains(err.Error(), terr) {
t.Fatalf("incorrect apply error: %q does not contain %q", err.Error(), terr)
}
case error:
if !errors.Is(err, terr) {
t.Fatalf("incorrect apply error: expected: %T (%v), actual: %T (%v)", terr, terr, err, err)
}
default:
t.Fatalf("unsupported expected error type: %T", terr)
}
}

type applyFiles struct {
Src string
Patch string
Expand Down
30 changes: 30 additions & 0 deletions gitdiff/assert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package gitdiff

import (
"errors"
"strings"
"testing"
)

func assertError(t *testing.T, expected interface{}, actual error, action string) {
if actual == nil {
t.Fatalf("expected error %s, but got nil", action)
}

switch exp := expected.(type) {
case bool:
if !exp {
t.Fatalf("unexpected error %s: %v", action, actual)
}
case string:
if !strings.Contains(actual.Error(), exp) {
t.Fatalf("incorrect error %s: %q does not contain %q", action, actual.Error(), exp)
}
case error:
if !errors.Is(actual, exp) {
t.Fatalf("incorrect error %s: expected %T (%v), actual: %T (%v)", action, exp, exp, actual, actual)
}
default:
t.Fatalf("unsupported expected error type: %T", exp)
}
}
Loading