From 28d11b840f803fea16559634bfb8593ede2bcd8c Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Thu, 21 Nov 2019 20:42:05 +0100 Subject: [PATCH] test internal/cerr package + add WrapString() to use a raw string --- internal/cerr/cerr.go | 8 ++++++ internal/cerr/cerr_test.go | 57 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 internal/cerr/cerr_test.go diff --git a/internal/cerr/cerr.go b/internal/cerr/cerr.go index 57f4431..19a8e6d 100644 --- a/internal/cerr/cerr.go +++ b/internal/cerr/cerr.go @@ -16,6 +16,14 @@ func (err Error) Wrap(e error) *WrapError { } } +// WrapString returns a new error which wraps a new error created from a string. +func (err Error) WrapString(e string) *WrapError { + return &WrapError{ + base: err, + wrap: Error(e), + } +} + // WrapError is way to wrap errors recursively. type WrapError struct { base error diff --git a/internal/cerr/cerr_test.go b/internal/cerr/cerr_test.go new file mode 100644 index 0000000..476996c --- /dev/null +++ b/internal/cerr/cerr_test.go @@ -0,0 +1,57 @@ +package cerr + +import ( + "errors" + "fmt" + "testing" +) + +func TestConstError(t *testing.T) { + const cerr1 = Error("some-string") + const cerr2 = Error("some-other-string") + const cerr3 = Error("some-string") // same const value as @cerr1 + + if cerr1.Error() == cerr2.Error() { + t.Errorf("cerr1 should not be equal to cerr2 ('%s', '%s')", cerr1.Error(), cerr2.Error()) + } + if cerr2.Error() == cerr3.Error() { + t.Errorf("cerr2 should not be equal to cerr3 ('%s', '%s')", cerr2.Error(), cerr3.Error()) + } + if cerr1.Error() != cerr3.Error() { + t.Errorf("cerr1 should be equal to cerr3 ('%s', '%s')", cerr1.Error(), cerr3.Error()) + } +} + +func TestWrappedConstError(t *testing.T) { + const parent = Error("file error") + + const readErrorConst = Error("cannot read file") + var wrappedReadError = parent.Wrap(readErrorConst) + + expectedWrappedReadError := fmt.Sprintf("%s: %s", parent.Error(), readErrorConst.Error()) + if wrappedReadError.Error() != expectedWrappedReadError { + t.Errorf("expected '%s' (got '%s')", wrappedReadError.Error(), expectedWrappedReadError) + } +} +func TestWrappedStandardError(t *testing.T) { + const parent = Error("file error") + + var writeErrorStandard error = errors.New("cannot write file") + var wrappedWriteError = parent.Wrap(writeErrorStandard) + + expectedWrappedWriteError := fmt.Sprintf("%s: %s", parent.Error(), writeErrorStandard.Error()) + if wrappedWriteError.Error() != expectedWrappedWriteError { + t.Errorf("expected '%s' (got '%s')", wrappedWriteError.Error(), expectedWrappedWriteError) + } +} +func TestWrappedStringError(t *testing.T) { + const parent = Error("file error") + + var closeErrorString string = "cannot close file" + var wrappedCloseError = parent.WrapString(closeErrorString) + + expectedWrappedCloseError := fmt.Sprintf("%s: %s", parent.Error(), closeErrorString) + if wrappedCloseError.Error() != expectedWrappedCloseError { + t.Errorf("expected '%s' (got '%s')", wrappedCloseError.Error(), expectedWrappedCloseError) + } +}