Skip to content

Commit

Permalink
Merge pull request #6 from m-mizutani/fix/remove-testify
Browse files Browse the repository at this point in the history
Rewrite tests without testify
  • Loading branch information
m-mizutani authored Apr 6, 2024
2 parents c8adef3 + f6010bd commit bdb418e
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 168 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 .

169 changes: 169 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
}
7 changes: 0 additions & 7 deletions go.work

This file was deleted.

115 changes: 0 additions & 115 deletions test/errors_test.go

This file was deleted.

16 changes: 0 additions & 16 deletions test/go.mod

This file was deleted.

28 changes: 0 additions & 28 deletions test/go.sum

This file was deleted.

0 comments on commit bdb418e

Please sign in to comment.