From fbd172b9de975bc68c358dbf60e8b6202140927f Mon Sep 17 00:00:00 2001 From: Jose Sitanggang Date: Sat, 29 Jul 2023 17:48:25 +0700 Subject: [PATCH 1/2] feat: add stringer into rule coder --- codes.go | 5 +++++ codes_test.go | 4 ++++ errtrans/errtrans_test.go | 5 ++++- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/codes.go b/codes.go index 1c40232..098e3a5 100644 --- a/codes.go +++ b/codes.go @@ -1,5 +1,7 @@ package goval +import "fmt" + type ruleCode int func (r ruleCode) Equal(other RuleCoder) bool { @@ -7,8 +9,11 @@ func (r ruleCode) Equal(other RuleCoder) bool { return ok && r == v } +func (r ruleCode) String() string { return fmt.Sprintf("%d", r) } + type RuleCoder interface { Equal(other RuleCoder) bool + fmt.Stringer } const ( diff --git a/codes_test.go b/codes_test.go index 8857fd8..7b3990c 100644 --- a/codes_test.go +++ b/codes_test.go @@ -11,6 +11,8 @@ func (c customTypeButSameBase) Equal(other RuleCoder) bool { return ok && v == c } +func (c customTypeButSameBase) String() string { return ruleCode(c).String() } + type customTypeButSameRootBase int func (c customTypeButSameRootBase) Equal(other RuleCoder) bool { @@ -18,6 +20,8 @@ func (c customTypeButSameRootBase) Equal(other RuleCoder) bool { return ok && v == c } +func (c customTypeButSameRootBase) String() string { return ruleCode(c).String() } + func TestRuleCode_Equal(t *testing.T) { tests := []struct { desc string diff --git a/errtrans/errtrans_test.go b/errtrans/errtrans_test.go index afba669..a58cd43 100644 --- a/errtrans/errtrans_test.go +++ b/errtrans/errtrans_test.go @@ -2,9 +2,10 @@ package errtrans import ( "context" - "github.com/pkg-id/goval" "strings" "testing" + + "github.com/pkg-id/goval" ) type ruleCode bool @@ -13,6 +14,8 @@ func (r ruleCode) Equal(_ goval.RuleCoder) bool { return bool(r) } +func (r ruleCode) String() string { return "ruleCode" } + func TestTranslator_Translate(t *testing.T) { ctx := context.Background() bundle, _ := DefaultBundle() From 326e18949c5682e86b93d3a80950f879711db80a Mon Sep 17 00:00:00 2001 From: Jose Sitanggang Date: Sat, 29 Jul 2023 17:53:49 +0700 Subject: [PATCH 2/2] fix: change error check to use errors.Is --- goval_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/goval_test.go b/goval_test.go index c97c6cc..1cc74ed 100644 --- a/goval_test.go +++ b/goval_test.go @@ -139,7 +139,7 @@ func TestExecute(t *testing.T) { goval.Bind[int](8, goval.Number[int]().Required().With(customValidator)), ) - if err != internalError { + if !errors.Is(err, internalError) { t.Fatalf("expect validation error is discarded and internal error is returned; but got %v", err) } })