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

Added AssertLessOrEqual and AssertGreaterOrEqual #153

Merged
merged 7 commits into from
Apr 10, 2022
Merged
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
41 changes: 40 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</a>

<a href="https://codecov.io/gh/MarvinJWendt/testza">
<!-- unittestcount:start --><img src="https://img.shields.io/badge/Unit_Tests-3213-magenta?style=flat-square" alt="Unit test count"><!-- unittestcount:end -->
<!-- unittestcount:start --><img src="https://img.shields.io/badge/Unit_Tests-3215-magenta?style=flat-square" alt="Unit test count"><!-- unittestcount:end -->
</a>

<a href="https://pkg.go.dev/github.com/MarvinJWendt/testza" target="_blank">
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
53 changes: 53 additions & 0 deletions assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
//
Expand Down
41 changes: 41 additions & 0 deletions assert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"time"

"github.com/pterm/pterm"
"github.com/stretchr/testify/assert"

. "github.com/MarvinJWendt/testza"
)
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)