Skip to content

Commit

Permalink
Merge pull request #76 from bayashi/fix-SameNumber
Browse files Browse the repository at this point in the history
Fix SameNumber() method.
  • Loading branch information
bayashi authored Sep 18, 2023
2 parents 834a68a + 416fa2c commit 79c4263
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
4 changes: 3 additions & 1 deletion constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ const (
reason_ExpectIsNotPointer = "`Expect` is NOT type of Pointer"
reason_GotIsNilType = "Type of `Got` is a <nil> value"
reason_ExpectIsNilType = "Type of `Expect` is a <nil> value"
reason_GotIsNotNumber = "Type of `Got` is not a number"
reason_ExpectIsNotNumber = "Type of `Expect` is not a number"
reason_ExpectIsNotNil = "Expected other than <nil>, but got <nil>"
reason_ExpectIsNotValidValue = "`Expect` value is NOT a valid value"
reason_NotConvertibleTypes = "The types of `Got` and `Expect` are NOT convertible"

// notice_Method_*
notice_Same_NotAcceptable = "It's not acceptable in Same() method"
notice_SamePointer_ShouldPointer = "It should be a Pointer for SamePointer() method"
notice_SameNumber_ShouldNumber = "It should be a number for SameNumber() method"
notice_SameNumber_ShouldNumber = "It should be a number(int or float) for SameNumber() method"

template_Dump = "Type: %Y, Dump: %#v"
template_DumpStringType = "Dump: %#v"
Expand Down
11 changes: 7 additions & 4 deletions same.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,20 @@ func (a *TestingA) SameNumber(t *testing.T, testNames ...string) *TestingA {
got := a.got.RawValue()
expect := a.expect.RawValue()

if !isFuncType(got) && !isFuncType(expect) && objectsAreSame(expect, got) {
return a // Pass
}

if isTypeNil(got) {
return a.fail(reportForSame(a).Reason(reason_GotIsNilType).Notice(notice_SameNumber_ShouldNumber))
}
if isTypeNil(expect) {
return a.fail(reportForSame(a).Reason(reason_ExpectIsNilType).Notice(notice_SameNumber_ShouldNumber))
}

if !isTypeNumber(got) {
return a.fail(reportForSame(a).Reason(reason_GotIsNotNumber).Notice(notice_SameNumber_ShouldNumber))
}
if !isTypeNumber(expect) {
return a.fail(reportForSame(a).Reason(reason_ExpectIsNotNumber).Notice(notice_SameNumber_ShouldNumber))
}

if !isValidValue(expect) {
return a.fail(reportForSame(a).Reason(reason_ExpectIsNotValidValue))
}
Expand Down
4 changes: 4 additions & 0 deletions same_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ func TestSameNumber(t *testing.T) {
// fail
// actually.Got("1").Expect(1).SameNumber(t)
// actually.Got(1).Expect("1").SameNumber(t)
// actually.Got(nil).Expect(nil).SameNumber(t)
// actually.Got(nil).Expect(0).SameNumber(t)
// actually.Got(0).Expect(nil).SameNumber(t)
// actually.Got([]byte("0")).Expect([]byte("0")).SameNumber(t)
// actually.Got("0").Expect("0").SameNumber(t)
}

func TestChain(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions same_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"reflect"
"strings"

"github.com/bayashi/actually/diff"
"github.com/bayashi/actually/report"
Expand Down Expand Up @@ -70,6 +71,11 @@ func isValidValue(v any) bool {
return reflect.ValueOf(v).IsValid()
}

func isTypeNumber(v any) bool {
typ := reflect.TypeOf(v).Name()
return strings.HasPrefix(typ, "int") || strings.HasPrefix(typ, "float")
}

// Just confirming only types are convertible or not
func objectsAreConvertible(expectv any, gotv any) bool {
return reflect.ValueOf(expectv).Type().ConvertibleTo(reflect.TypeOf(gotv)) &&
Expand Down

0 comments on commit 79c4263

Please sign in to comment.