-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from m-mizutani/fix/remove-testify
Rewrite tests without testify
- Loading branch information
Showing
6 changed files
with
172 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.