Skip to content

Commit

Permalink
Merge pull request #180 from MarvinJWendt/151-add-assertinrange
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinJWendt committed Oct 3, 2022
2 parents 72f1f34 + b2c2258 commit 9204ba6
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 1 deletion.
34 changes: 33 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-2985-magenta?style=flat-square" alt="Unit test count"><!-- unittestcount:end -->
<!-- unittestcount:start --><img src="https://img.shields.io/badge/Unit_Tests-2999-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,6 +168,7 @@ testza.FuzzStringRunTests(t, emailAddresses, func(t *testing.T, index int, str s
- [AssertGreater](https://github.com/MarvinJWendt/testza#AssertGreater)
- [AssertGreaterOrEqual](https://github.com/MarvinJWendt/testza#AssertGreaterOrEqual)
- [AssertImplements](https://github.com/MarvinJWendt/testza#AssertImplements)
- [AssertInRange](https://github.com/MarvinJWendt/testza#AssertInRange)
- [AssertIncreasing](https://github.com/MarvinJWendt/testza#AssertIncreasing)
- [AssertKindOf](https://github.com/MarvinJWendt/testza#AssertKindOf)
- [AssertLen](https://github.com/MarvinJWendt/testza#AssertLen)
Expand All @@ -184,6 +185,7 @@ testza.FuzzStringRunTests(t, emailAddresses, func(t *testing.T, index int, str s
- [AssertNotEqualValues](https://github.com/MarvinJWendt/testza#AssertNotEqualValues)
- [AssertNotErrorIs](https://github.com/MarvinJWendt/testza#AssertNotErrorIs)
- [AssertNotImplements](https://github.com/MarvinJWendt/testza#AssertNotImplements)
- [AssertNotInRange](https://github.com/MarvinJWendt/testza#AssertNotInRange)
- [AssertNotKindOf](https://github.com/MarvinJWendt/testza#AssertNotKindOf)
- [AssertNotNil](https://github.com/MarvinJWendt/testza#AssertNotNil)
- [AssertNotNumeric](https://github.com/MarvinJWendt/testza#AssertNotNumeric)
Expand Down Expand Up @@ -583,6 +585,21 @@ Example:
testza.AssertImplements(t, (*YourInterface)(nil), new(YourObject))
testza.AssertImplements(t, (*fmt.Stringer)(nil), new(types.Const)) => pass
#### AssertInRange
```go
func AssertInRange[T number](t testRunner, value T, min T, max T, msg ...any)
```
AssertInRange asserts that the value is in the range.
When using a custom message, the same formatting as with fmt.Sprintf() is
used.
Example:
testza.AssertInRange(t, 5, 1, 10)
#### AssertIncreasing
```go
Expand Down Expand Up @@ -869,6 +886,21 @@ Example:
testza.AssertNotImplements(t, (*YourInterface)(nil), new(YourObject))
testza.AssertNotImplements(t, (*fmt.Stringer)(nil), new(types.Const)) => fail, because types.Const does implement fmt.Stringer.
#### AssertNotInRange
```go
func AssertNotInRange[T number](t testRunner, value T, min T, max T, msg ...any)
```
AssertNotInRange asserts that the value is not in the range.
When using a custom message, the same formatting as with fmt.Sprintf() is
used.
Example:
testza.AssertNotInRange(t, 5, 1, 10)
#### AssertNotKindOf
```go
Expand Down
42 changes: 42 additions & 0 deletions assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -1088,3 +1088,45 @@ func AssertNotUnique[elementType comparable](t testRunner, list []elementType, m
internal.Fail(t, "The list !!is unique!!, but should not.", internal.NewObjectsSingleNamed("List", list), msg...)
}
}

// AssertInRange asserts that the value is in the range.
//
// When using a custom message, the same formatting as with fmt.Sprintf() is used.
//
// Example:
//
// testza.AssertInRange(t, 5, 1, 10)
func AssertInRange[T number](t testRunner, value T, min T, max T, msg ...any) {
if test, ok := t.(helper); ok {
test.Helper()
}

if min >= max {
internal.Fail(t, "The minimum value is greater than or equal to the maximum value.", internal.Objects{internal.NewObjectsSingleNamed("Min", min)[0], internal.NewObjectsSingleNamed("Max", max)[0]}, msg...)
}

if value < min || value > max {
internal.Fail(t, "The value is !!not in range!!, but should be.", internal.Objects{internal.NewObjectsSingleNamed("Value", value)[0], internal.NewObjectsSingleNamed("Min", min)[0], internal.NewObjectsSingleNamed("Max", max)[0]}, msg...)
}
}

// AssertNotInRange asserts that the value is not in the range.
//
// When using a custom message, the same formatting as with fmt.Sprintf() is used.
//
// Example:
//
// testza.AssertNotInRange(t, 5, 1, 10)
func AssertNotInRange[T number](t testRunner, value T, min T, max T, msg ...any) {
if test, ok := t.(helper); ok {
test.Helper()
}

if min >= max {
internal.Fail(t, "The minimum value is greater than or equal to the maximum value.", internal.Objects{internal.NewObjectsSingleNamed("Min", min)[0], internal.NewObjectsSingleNamed("Max", max)[0]}, msg...)
}

if value >= min && value <= max {
internal.Fail(t, "The value is in range, but should not be.", internal.Objects{internal.NewObjectsSingleNamed("Value", value)[0], internal.NewObjectsSingleNamed("Min", min)[0], internal.NewObjectsSingleNamed("Max", max)[0]}, msg...)
}
}
30 changes: 30 additions & 0 deletions assert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1570,3 +1570,33 @@ func TestAssertNotUnique_fails(t *testing.T) {
AssertNotUnique(t, []float64{1.1, 1.2, 1.3, 1.4, 1.5})
})
}

func TestAssertInRange(t *testing.T) {
AssertInRange(t, 2, 1, 3)
AssertInRange(t, 1, 1, 3)
}

func TestAssertInRange_fails(t *testing.T) {
AssertTestFails(t, func(t TestingPackageWithFailFunctions) {
AssertNotInRange(t, 1, 1, 3)
})

AssertTestFails(t, func(t TestingPackageWithFailFunctions) {
AssertNotInRange(t, 2, 1, 3)
})
}

func TestAssertNotInRange(t *testing.T) {
AssertNotInRange(t, 4, 1, 3)
AssertNotInRange(t, 0, 1, 3)
}

func TestAssertNotInRange_fails(t *testing.T) {
AssertTestFails(t, func(t TestingPackageWithFailFunctions) {
AssertInRange(t, 4, 1, 3)
})

AssertTestFails(t, func(t TestingPackageWithFailFunctions) {
AssertInRange(t, 0, 1, 3)
})
}
5 changes: 5 additions & 0 deletions constraints.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package testza

type number interface {
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64
}

0 comments on commit 9204ba6

Please sign in to comment.