Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not #77

Merged
merged 3 commits into from
Sep 23, 2023
Merged

Not #77

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ const (

// fail reason
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"
reason_ExpectIsFunc = "`Expect` value is type of function"
reason_GotIsNotPointer = "`Got` is NOT type of Pointer"
Expand All @@ -28,9 +31,10 @@ const (
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(int or float) for SameNumber() 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(int or float) for SameNumber() method"
notice_NotSamePointer_ShouldPointer = "It should be a Pointer for NotSamePointer() method"

template_Dump = "Type: %Y, Dump: %#v"
template_DumpStringType = "Dump: %#v"
Expand Down
83 changes: 83 additions & 0 deletions not_same.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package actually

import (
"testing"
)

// NotSamePointer method verifies that two objets are not same pointer.
func (a *TestingA) NotSamePointer(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 !isPointerType(got) {
return a.fail(reportForSame(a).Reason(reason_GotIsNotPointer).Notice(notice_NotSamePointer_ShouldPointer))
}
if !isPointerType(expect) {
return a.fail(reportForSame(a).Reason(reason_ExpectIsNotPointer).Notice(notice_NotSamePointer_ShouldPointer))
}

if got == expect {
return a.fail(reportForNotSameType(a).Reason(reason_SamePointerAddress))
}

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
}

// NotSameType method verifies that each pair of values are NOT same type.
// Not care about actual value of these. Just verify the type.
func (a *TestingA) NotSameType(t *testing.T, testNames ...string) *TestingA {
invalidCallForSame(a)
a.name = a.naming(testNames...)
a.t = t
a.t.Helper()

if !objectsAreSameType(a.expect.RawValue(), a.got.RawValue()) {
return a
}

return a.fail(reportForNotSameType(a))
}
54 changes: 54 additions & 0 deletions not_same_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package actually_test

import (
"testing"

"github.com/bayashi/actually"
)

func TestNotSamePointer(t *testing.T) {
i := 7
j := 7
ptr := &i
ptr2 := &j
actually.Got(ptr).Expect(ptr2).NotSamePointer(t)
actually.Got(ptr).Expect(&j).NotSamePointer(t)
actually.Got(ptr2).Expect(&i).NotSamePointer(t)

// test name
actually.Got(&i).Expect(&j).NotSamePointer(t, "Not Same Pointer")

// fail
// actually.Got("").Expect(ptr).NotSamePointer(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)
}

func TestNotSameType(t *testing.T) {
actually.Got(nil).Expect(0).NotSameType(t)
actually.Got("1").Expect(1).NotSameType(t)
actually.Got(t).Expect(&testing.B{}).NotSameType(t)

// fail
// actually.Got(nil).Expect(nil).NotSameType(t)
// actually.Got(true).Expect(false).NotSameType(t) // both are same boolean
// actually.Got(&testing.T{}).Expect(t).NotSameType(t)
}
26 changes: 25 additions & 1 deletion same_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ func reportForSameType(a *TestingA) *report.Report {
Notice("SameType() just verifies the type. It doesn't care about the actual value")
}

func reportForNotSameType(a *TestingA) *report.Report {
return report.New().
Expectf("Type: %#v", a.expect).
Gotf("Type: %#v", a.got).
Reason(reason_SameType)
}

func isFuncType(v any) bool {
return v != nil && reflect.TypeOf(v).Kind() == reflect.Func
}
Expand All @@ -73,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 @@ -119,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
}