Repository: MarvinJWendt/testza
Pull request #180 (2022-10-03 17:21:27)

Added `AssertInRange` and `AssertNotInRange`


Patch:
diff --git a/README.md b/README.md
index c4a6fc6..4eaa76d 100644
--- a/README.md
+++ b/README.md
@@ -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">
@@ -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)
@@ -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)
@@ -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
@@ -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
diff --git a/assert.go b/assert.go
index b1bd3ea..5485b05 100644
--- a/assert.go
+++ b/assert.go
@@ -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...)
+	}
+}
diff --git a/constraints.go b/constraints.go
new file mode 100644
index 0000000..3809221
--- /dev/null
+++ b/constraints.go
@@ -0,0 +1,5 @@
+package testza
+
+type number interface {
+	int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64
+}


Test patch:
diff --git a/assert_test.go b/assert_test.go
index c19786a..bac4612 100644
--- a/assert_test.go
+++ b/assert_test.go
@@ -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)
+	})
+}