Skip to content

Commit

Permalink
Add NotSameNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
bayashi committed Sep 23, 2023
1 parent 11885c1 commit 96e5e1f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
1 change: 1 addition & 0 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
reason_WrongType = "Different type"
reason_SameType = "Unexpectedly same type"
reason_NotSame = "Not same value"
reason_Same = "Unexpectedly same value"
reason_WrongPointerAddress = "Wrong pointer address"
reason_SamePointerAddress = "Unexpectedly same pointer address"
reason_GotIsFunc = "`Got` value is type of function"
Expand Down
39 changes: 39 additions & 0 deletions not_same.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,42 @@ func (a *TestingA) NotSamePointer(t *testing.T, testNames ...string) *TestingA {

return a
}

// NotSameNumber method verifies that two objects are not same number.
func (a *TestingA) NotSameNumber(t *testing.T, testNames ...string) *TestingA {
invalidCallForSame(a)
a.name = a.naming(testNames...)
a.t = t
a.t.Helper()

got := a.got.RawValue()
expect := a.expect.RawValue()

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))
}

if !objectsAreConvertible(expect, got) {
return a.fail(reportForSame(a).Reason(reason_NotConvertibleTypes))
}

if convert2float64(expect) == convert2float64(got) {
return a.fail(reportForSameWithDiff(a).Reason(reason_Same))
}

return a
}
18 changes: 18 additions & 0 deletions not_same_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,21 @@ func TestNotSamePointer(t *testing.T) {
// actually.Got(ptr).Expect("").NotSamePointer(t)
// actually.Got(ptr).Expect(&i).NotSamePointer(t)
}

func TestNotSameNumber(t *testing.T) {
actually.Got(1).Expect(2).NotSameNumber(t, "these should be different number")
actually.Got(int8(1)).Expect(int32(2)).NotSameNumber(t)
actually.Got(float32(1.1)).Expect(int64(1)).NotSameNumber(t)
actually.Got(1).Expect(float64(1.000000000000001)).NotSameNumber(t)

// fail
// actually.Got(1).Expect(1).NotSameNumber(t)
// actually.Got("1").Expect(1).NotSameNumber(t)
// actually.Got(1).Expect("1").NotSameNumber(t)
// actually.Got(nil).Expect(nil).NotSameNumber(t)
// actually.Got(nil).Expect(0).NotSameNumber(t)
// actually.Got(0).Expect(nil).NotSameNumber(t)
// actually.Got([]byte("0")).Expect([]byte("0")).NotSameNumber(t)
// actually.Got("0").Expect("0").NotSameNumber(t)
// actually.Got(1).Expect(float64(1.0000000000000001)).NotSameNumber(t)
}
19 changes: 18 additions & 1 deletion same_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func isValidValue(v any) bool {

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

// Just confirming only types are convertible or not
Expand Down Expand Up @@ -126,3 +126,20 @@ func invalidCallForSame(a *TestingA) {
panic("You called kind of Same() method, but you forgot to call Got().")
}
}

func convert2float64(a any) float64 {
var f float64

b := reflect.TypeOf(a).Kind()

// https://pkg.go.dev/reflect#Kind
if b >= 2 && b <= 6 {
f = float64(reflect.ValueOf(a).Int())
} else if b >= 7 && b <= 11 {
f = float64(reflect.ValueOf(a).Uint())
} else if b == 13 || b == 14 {
f = reflect.ValueOf(a).Float()
}

return f
}

0 comments on commit 96e5e1f

Please sign in to comment.