diff --git a/README.md b/README.md index 0e354838..cbd5577b 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ -Unit test count +Unit test count @@ -168,11 +168,13 @@ testza.FuzzStringRunTests(t, emailAddresses, func(t *testing.T, index int, str s - [AssertFalse](https://github.com/MarvinJWendt/testza#AssertFalse) - [AssertFileExists](https://github.com/MarvinJWendt/testza#AssertFileExists) - [AssertGreater](https://github.com/MarvinJWendt/testza#AssertGreater) + - [AssertGreaterOrEqual](https://github.com/MarvinJWendt/testza#AssertGreaterOrEqual) - [AssertImplements](https://github.com/MarvinJWendt/testza#AssertImplements) - [AssertIncreasing](https://github.com/MarvinJWendt/testza#AssertIncreasing) - [AssertKindOf](https://github.com/MarvinJWendt/testza#AssertKindOf) - [AssertLen](https://github.com/MarvinJWendt/testza#AssertLen) - [AssertLess](https://github.com/MarvinJWendt/testza#AssertLess) + - [AssertLessOrEqual](https://github.com/MarvinJWendt/testza#AssertLessOrEqual) - [AssertNil](https://github.com/MarvinJWendt/testza#AssertNil) - [AssertNoDirExists](https://github.com/MarvinJWendt/testza#AssertNoDirExists) - [AssertNoError](https://github.com/MarvinJWendt/testza#AssertNoError) @@ -538,6 +540,25 @@ Example: testza.AssertGreater(t, 5, 1) testza.AssertGreater(t, 10, -10) +#### AssertGreaterOrEqual + +```go +func AssertGreaterOrEqual(t testRunner, object1, object2 interface{}, msg ...interface{}) +``` + +AssertGreaterOrEqual asserts that the first object is greater than or equal +to the second. + +When using a custom message, the same formatting as with fmt.Sprintf() is +used. + +Example: + + testza.AssertGreaterOrEqual(t, 5, 1) + testza.AssertGreaterOrEqual(t, 10, -10) + +testza.AssertGreaterOrEqual(t, 10, 10) + #### AssertImplements ```go @@ -627,6 +648,24 @@ Example: testza.AssertLess(t, 1, 5) testza.AssertLess(t, -10, 10) +#### AssertLessOrEqual + +```go +func AssertLessOrEqual(t testRunner, object1, object2 interface{}, msg ...interface{}) +``` + +AssertLessOrEqual asserts that the first object is less than or equal to the +second. + +When using a custom message, the same formatting as with fmt.Sprintf() is +used. + +Example: + + testza.AssertLessOrEqual(t, 1, 5) + testza.AssertLessOrEqual(t, -10, 10) + testza.AssertLessOrEqual(t, 1, 1) + #### AssertNil ```go diff --git a/assert.go b/assert.go index fb7db225..df608ec7 100644 --- a/assert.go +++ b/assert.go @@ -565,6 +565,31 @@ func AssertGreater(t testRunner, object1, object2 interface{}, msg ...interface{ } } +// AssertGreaterOrEqual asserts that the first object is greater than or equal to the second. +// +// When using a custom message, the same formatting as with fmt.Sprintf() is used. +// +// Example: +// testza.AssertGreaterOrEqual(t, 5, 1) +// testza.AssertGreaterOrEqual(t, 10, -10) +// testza.AssertGreaterOrEqual(t, 10, 10) +func AssertGreaterOrEqual(t testRunner, object1, object2 interface{}, msg ...interface{}) { + if test, ok := t.(helper); ok { + test.Helper() + } + + v1, err := strconv.ParseFloat(fmt.Sprint(object1), 64) + v2, err2 := strconv.ParseFloat(fmt.Sprint(object2), 64) + + if err != nil || err2 != nil { + internal.Fail(t, "An error occurred while parsing the objects as numbers.", internal.NewObjectsUnknown(object1, object2), msg...) + } + + if !(v1 >= v2) { + internal.Fail(t, "An object that !!should be greater!! than the second object is not.", internal.Objects{{Name: "Object 1", Data: object1}, {Name: "Should be greater than object 2", Data: object2}}, msg...) + } +} + // AssertLess asserts that the first object is less than the second. // // When using a custom message, the same formatting as with fmt.Sprintf() is used. @@ -592,6 +617,34 @@ func AssertLess(t testRunner, object1, object2 interface{}, msg ...interface{}) } } +// AssertLessOrEqual asserts that the first object is less than or equal to the second. +// +// When using a custom message, the same formatting as with fmt.Sprintf() is used. +// +// Example: +// testza.AssertLessOrEqual(t, 1, 5) +// testza.AssertLessOrEqual(t, -10, 10) +// testza.AssertLessOrEqual(t, 1, 1) +func AssertLessOrEqual(t testRunner, object1, object2 interface{}, msg ...interface{}) { + if test, ok := t.(helper); ok { + test.Helper() + } + + v1, err := strconv.ParseFloat(fmt.Sprint(object1), 64) + v2, err2 := strconv.ParseFloat(fmt.Sprint(object2), 64) + + if err != nil || err2 != nil { + internal.Fail(t, "An error occurred while parsing the objects as numbers.", internal.NewObjectsUnknown(object1, object2), msg...) + } + + if !(v1 <= v2) { + internal.Fail(t, "An object that !!should be less or equal!! than the second object is not.", internal.Objects{ + internal.NewObjectsSingleNamed("Should be less or equal to", object1)[0], + internal.NewObjectsSingleNamed("Actual", object2)[0], + }, msg...) + } +} + // AssertTestFails asserts that a unit test fails. // A unit test fails if one of the following methods is called in the test function: Error, Errorf, Fail, FailNow, Fatal, Fatalf // diff --git a/assert_test.go b/assert_test.go index 363a8bd6..2efe3a14 100644 --- a/assert_test.go +++ b/assert_test.go @@ -15,6 +15,7 @@ import ( "time" "github.com/pterm/pterm" + "github.com/stretchr/testify/assert" . "github.com/MarvinJWendt/testza" ) @@ -717,6 +718,26 @@ func TestAssertGreater_fails(t *testing.T) { AssertTestFails(t, func(t TestingPackageWithFailFunctions) { AssertGreater(t, 4, 5) }) + + AssertTestFails(t, func(t TestingPackageWithFailFunctions) { + AssertGreater(t, 5, 5) + }) +} + +func TestAssertGreaterOrEqual(t *testing.T) { + assert.GreaterOrEqual(t, 2, 1) + assert.GreaterOrEqual(t, 5, 4) + assert.GreaterOrEqual(t, 5, 5) +} + +func TestAssertGreaterOrEqual_fails(t *testing.T) { + AssertTestFails(t, func(t TestingPackageWithFailFunctions) { + assert.GreaterOrEqual(t, 1, 2) + }) + + AssertTestFails(t, func(t TestingPackageWithFailFunctions) { + assert.GreaterOrEqual(t, 4, 5) + }) } func TestAssertLess(t *testing.T) { @@ -732,6 +753,26 @@ func TestAssertLess_fails(t *testing.T) { AssertTestFails(t, func(t TestingPackageWithFailFunctions) { AssertLess(t, 5, 4) }) + + AssertTestFails(t, func(t TestingPackageWithFailFunctions) { + AssertLess(t, 5, 5) + }) +} + +func TestAssertLessOrEqual(t *testing.T) { + AssertLessOrEqual(t, 1, 2) + AssertLessOrEqual(t, 4, 5) + AssertLessOrEqual(t, 5, 5) +} + +func TestAssertLessOrEqual_fails(t *testing.T) { + AssertTestFails(t, func(t TestingPackageWithFailFunctions) { + AssertLessOrEqual(t, 2, 1) + }) + + AssertTestFails(t, func(t TestingPackageWithFailFunctions) { + AssertLessOrEqual(t, 5, 4) + }) } func TestAssertTestFails(t *testing.T) { diff --git a/go.mod b/go.mod index b021d125..a5a0580f 100644 --- a/go.mod +++ b/go.mod @@ -7,4 +7,5 @@ require ( github.com/klauspost/cpuid/v2 v2.0.12 github.com/pterm/pterm v0.12.40 github.com/sergi/go-diff v1.2.0 + github.com/stretchr/testify v1.7.0 )