From f6010bdcbc0bd3024ac27fedeb17606a82421135 Mon Sep 17 00:00:00 2001 From: Masayoshi Mizutani Date: Sat, 6 Apr 2024 09:57:28 +0900 Subject: [PATCH] Rewrite tests without testify --- .github/workflows/test.yml | 5 +- errors_test.go | 169 +++++++++++++++++++++++++++++++++++++ go.work | 7 -- test/errors_test.go | 115 ------------------------- test/go.mod | 16 ---- test/go.sum | 28 ------ 6 files changed, 172 insertions(+), 168 deletions(-) create mode 100644 errors_test.go delete mode 100644 go.work delete mode 100644 test/errors_test.go delete mode 100644 test/go.mod delete mode 100644 test/go.sum diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1040fab..8a37e73 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,5 +14,6 @@ jobs: - uses: actions/setup-go@v4 with: go-version-file: "go.mod" - - run: go test --tags github ./... - - run: go vet --tags github ./... + - run: go test . + - run: go vet . + diff --git a/errors_test.go b/errors_test.go new file mode 100644 index 0000000..3da7ee3 --- /dev/null +++ b/errors_test.go @@ -0,0 +1,169 @@ +package goerr_test + +import ( + "bytes" + "errors" + "fmt" + "log/slog" + "regexp" + "strings" + "testing" + + "github.com/m-mizutani/goerr" +) + +func oops() *goerr.Error { + return goerr.New("omg") +} + +func normalError() error { + return fmt.Errorf("red") +} + +func wrapError() *goerr.Error { + err := normalError() + return goerr.Wrap(err, "orange") +} + +func TestNew(t *testing.T) { + err := oops() + v := fmt.Sprintf("%+v", err) + if !strings.Contains(v, "goerr_test.oops") { + t.Error("Stack trace 'goerr_test.oops' is not found") + } + if !strings.Contains(err.Error(), "omg") { + t.Error("Error message is not correct") + } +} + +func TestWrapError(t *testing.T) { + err := wrapError() + st := fmt.Sprintf("%+v", err) + if !strings.Contains(st, "github.com/m-mizutani/goerr_test.wrapError") { + t.Error("Stack trace 'wrapError' is not found") + } + if !strings.Contains(st, "github.com/m-mizutani/goerr_test.TestWrapError") { + t.Error("Stack trace 'TestWrapError' is not found") + } + if strings.Contains(st, "github.com/m-mizutani/goerr_test.normalError") { + t.Error("Stack trace 'normalError' is found") + } + if !strings.Contains(err.Error(), "orange: red") { + t.Error("Error message is not correct") + } +} + +func TestStackTrace(t *testing.T) { + err := oops() + st := err.Stacks() + if len(st) != 4 { + t.Errorf("Expected stack length of 4, got %d", len(st)) + } + if st[0].Func != "github.com/m-mizutani/goerr_test.oops" { + t.Error("Stack trace 'github.com/m-mizutani/goerr_test.oops' is not found") + } + if !regexp.MustCompile(`/goerr/errors_test\.go$`).MatchString(st[0].File) { + t.Error("Stack trace file is not correct") + } + if st[0].Line != 16 { + t.Errorf("Expected line number 13, got %d", st[0].Line) + } +} + +func TestMultiWrap(t *testing.T) { + err1 := oops() + err2 := goerr.Wrap(err1) + if err1 == err2 { + t.Error("Expected err1 and err2 to be different") + } + + err3 := goerr.Wrap(err1, "some message") + if err1 == err3 { + t.Error("Expected err1 and err3 to be different") + } +} + +func TestErrorCode(t *testing.T) { + rootErr := goerr.New("something bad") + baseErr1 := goerr.New("oops").ID("code1") + baseErr2 := goerr.New("oops").ID("code2") + + newErr := baseErr1.Wrap(rootErr).With("v", 1) + + if !errors.Is(newErr, baseErr1) { + t.Error("Expected newErr to be based on baseErr1") + } + if newErr == baseErr1 { + t.Error("Expected newErr and baseErr1 to be different") + } + if newErr.Values()["v"] == nil { + t.Error("Expected newErr to have a non-nil value for 'v'") + } + if baseErr1.Values()["v"] != nil { + t.Error("Expected baseErr1 to have a nil value for 'v'") + } + if errors.Is(newErr, baseErr2) { + t.Error("Expected newErr to not be based on baseErr2") + } +} + +func TestPrintable(t *testing.T) { + cause := errors.New("test") + err := goerr.Wrap(cause, "oops").ID("E001").With("blue", "five") + + p := err.Printable() + if p.Message != "oops" { + t.Errorf("Expected message to be 'oops', got '%s'", p.Message) + } + if p.ID != "E001" { + t.Errorf("Expected ID to be 'E001', got '%s'", p.ID) + } + if p.Cause != cause { + t.Errorf("Expected cause to be '%v', got '%v'", cause, p.Cause) + } + if p.Values["blue"] != "five" { + t.Errorf("Expected value for 'blue' to be 'five', got '%v'", p.Values["blue"]) + } +} + +func TestUnwrap(t *testing.T) { + err1 := goerr.New("omg").With("color", "five") + err2 := fmt.Errorf("oops: %w", err1) + + err := goerr.Unwrap(err2) + if err == nil { + t.Error("Expected unwrapped error to be non-nil") + } + values := err.Values() + if values["color"] != "five" { + t.Errorf("Expected value for 'color' to be 'five', got '%v'", values["color"]) + } +} + +func TestFormat(t *testing.T) { + err := goerr.New("test: %s", "blue") + if err.Error() != "test: blue" { + t.Errorf("Expected error message to be 'test: blue', got '%s'", err.Error()) + } +} + +func TestErrorString(t *testing.T) { + err := goerr.Wrap(goerr.Wrap(goerr.New("blue"), "orange"), "red") + if err.Error() != "red: orange: blue" { + t.Errorf("Expected error message to be 'red: orange: blue', got '%s'", err.Error()) + } +} + +func TestLoggingNestedError(t *testing.T) { + err1 := goerr.New("e1").With("color", "orange") + err2 := goerr.Wrap(err1, "e2").With("number", "five") + out := &bytes.Buffer{} + logger := slog.New(slog.NewJSONHandler(out, nil)) + logger.Error("fail", slog.Any("error", err2)) + if !strings.Contains(out.String(), `"number":"five"`) { + t.Errorf("Expected log output to contain '\"number\":\"five\"', got '%s'", out.String()) + } + if !strings.Contains(out.String(), `"color":"orange"`) { + t.Errorf("Expected log output to contain '\"color\":\"orange\"', got '%s'", out.String()) + } +} diff --git a/go.work b/go.work deleted file mode 100644 index dc94c8f..0000000 --- a/go.work +++ /dev/null @@ -1,7 +0,0 @@ -go 1.21 - -use ( - . - ./test - ./examples/stacktrace -) diff --git a/test/errors_test.go b/test/errors_test.go deleted file mode 100644 index 79cb985..0000000 --- a/test/errors_test.go +++ /dev/null @@ -1,115 +0,0 @@ -package main_test - -import ( - "bytes" - "fmt" - "log/slog" - "testing" - - "github.com/m-mizutani/goerr" - "github.com/pkg/errors" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -func oops() *goerr.Error { - return goerr.New("omg") -} - -func normalError() error { - return fmt.Errorf("red") -} - -func wrapError() *goerr.Error { - err := normalError() - return goerr.Wrap(err, "orange") -} - -func TestNew(t *testing.T) { - err := oops() - assert.Contains(t, fmt.Sprintf("%+v", err), "test_test.oops") - assert.Contains(t, err.Error(), "omg") -} - -func TestWrapError(t *testing.T) { - err := wrapError() - st := fmt.Sprintf("%+v", err) - assert.Contains(t, st, "github.com/m-mizutani/goerr/test_test.wrapError\n") - assert.Contains(t, st, "github.com/m-mizutani/goerr/test_test.TestWrapError\n") - assert.NotContains(t, st, "github.com/m-mizutani/goerr/test_test.normalError\n") - assert.Contains(t, err.Error(), "orange: red") -} - -func TestStackTrace(t *testing.T) { - err := oops() - st := err.Stacks() - require.Equal(t, 4, len(st)) - assert.Equal(t, "github.com/m-mizutani/goerr/test_test.oops", st[0].Func) - assert.Regexp(t, `/goerr/test/errors_test\.go$`, st[0].File) - assert.Equal(t, 16, st[0].Line) -} - -func TestMultileWrap(t *testing.T) { - err1 := oops() - err2 := goerr.Wrap(err1) - assert.NotEqual(t, err1, err2) - - err3 := goerr.Wrap(err1, "some message") - assert.NotEqual(t, err1, err3) -} - -func TestErrorCode(t *testing.T) { - rootErr := goerr.New("something bad") - baseErr1 := goerr.New("oops").ID("code1") - baseErr2 := goerr.New("oops").ID("code2") - - newErr := baseErr1.Wrap(rootErr).With("v", 1) - - assert.True(t, errors.Is(newErr, baseErr1)) - assert.NotEqual(t, newErr, baseErr1) - assert.NotNil(t, newErr.Values()["v"]) - assert.Nil(t, baseErr1.Values()["v"]) - - assert.False(t, errors.Is(newErr, baseErr2)) -} - -func TestPrintable(t *testing.T) { - cause := errors.New("test") - err := goerr.Wrap(cause, "oops").ID("E001").With("blue", "five") - - p := err.Printable() - assert.Equal(t, "oops", p.Message) - assert.Equal(t, "E001", p.ID) - assert.Equal(t, cause, p.Cause) - assert.Equal(t, "five", p.Values["blue"]) -} - -func TestUnwrap(t *testing.T) { - err1 := goerr.New("omg").With("color", "five") - err2 := errors.Wrap(err1, "oops") - - err := goerr.Unwrap(err2) - require.NotNil(t, err) - values := err.Values() - assert.Equal(t, "five", values["color"]) -} - -func TestFormat(t *testing.T) { - err := goerr.New("test: %s", "blue") - assert.Equal(t, "test: blue", err.Error()) -} - -func TestErrorString(t *testing.T) { - err := goerr.Wrap(goerr.Wrap(goerr.New("blue"), "orange"), "red") - assert.Equal(t, "red: orange: blue", err.Error()) -} - -func TestLoggingNestedError(t *testing.T) { - err1 := goerr.New("e1").With("color", "orange") - err2 := goerr.Wrap(err1, "e2").With("number", "five") - out := &bytes.Buffer{} - logger := slog.New(slog.NewJSONHandler(out, nil)) - logger.Error("fail", slog.Any("error", err2)) - assert.Contains(t, out.String(), `"number":"five"`) - assert.Contains(t, out.String(), `"color":"orange"`) -} diff --git a/test/go.mod b/test/go.mod deleted file mode 100644 index 5bd59ce..0000000 --- a/test/go.mod +++ /dev/null @@ -1,16 +0,0 @@ -module github.com/m-mizutani/goerr/test - -go 1.21 - -require ( - github.com/m-mizutani/goerr v0.1.5 - github.com/pkg/errors v0.9.1 - github.com/stretchr/testify v1.7.1 -) - -require ( - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/google/uuid v1.3.0 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect - gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect -) diff --git a/test/go.sum b/test/go.sum deleted file mode 100644 index 33ba6a3..0000000 --- a/test/go.sum +++ /dev/null @@ -1,28 +0,0 @@ -github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= -github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= -github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/m-mizutani/goerr v0.1.5 h1:vCHgJ2eICSHkl30QOHrBXswFFrS+a/HmmD9zsiUFYrQ= -github.com/m-mizutani/goerr v0.1.5/go.mod h1:j3cf1KSIhehhIY38cCdbixepJ2LQBpJdIQ3G8pbVUiY= -github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= -github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= -gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= -gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=