Skip to content

Commit

Permalink
feat: add stringer into rule coder (#51)
Browse files Browse the repository at this point in the history
* feat: add stringer into rule coder

* fix: change error check to use errors.Is
  • Loading branch information
josestg authored Jul 29, 2023
1 parent 2b2ccd8 commit 1bccdaa
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
5 changes: 5 additions & 0 deletions codes.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package goval

import "fmt"

type ruleCode int

func (r ruleCode) Equal(other RuleCoder) bool {
v, ok := other.(ruleCode)
return ok && r == v
}

func (r ruleCode) String() string { return fmt.Sprintf("%d", r) }

type RuleCoder interface {
Equal(other RuleCoder) bool
fmt.Stringer
}

const (
Expand Down
4 changes: 4 additions & 0 deletions codes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ 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 {
v, ok := other.(customTypeButSameRootBase)
return ok && v == c
}

func (c customTypeButSameRootBase) String() string { return ruleCode(c).String() }

func TestRuleCode_Equal(t *testing.T) {
tests := []struct {
desc string
Expand Down
5 changes: 4 additions & 1 deletion errtrans/errtrans_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package errtrans

import (
"context"
"github.com/pkg-id/goval"
"strings"
"testing"

"github.com/pkg-id/goval"
)

type ruleCode bool
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion goval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
Expand Down

0 comments on commit 1bccdaa

Please sign in to comment.