Repository: MarvinJWendt/testza
Pull request #200 (2023-01-17 23:08:34)

feat: proposal FuzzUtilLimit
#154 


Patch:
diff --git a/fuzz-utils.go b/fuzz-utils.go
index 824368f..c5321ff 100644
--- a/fuzz-utils.go
+++ b/fuzz-utils.go
@@ -2,6 +2,7 @@ package testza
 
 import (
 	"fmt"
+	"math/rand"
 	"testing"
 
 	"github.com/MarvinJWendt/testza/internal"
@@ -21,9 +22,9 @@ func FuzzUtilMergeSets[setType any](sets ...[]setType) (merged []setType) {
 	return merged
 }
 
-// FuzzUtilRunTests runs a test for every value in a testset.
+// FuzzUtilRunTests runs a test for every value in a test set.
 // You can use the value as input parameter for your functions, to sanity test against many different cases.
-// This ensures that your functions have a correct error handling and enables you to test against hunderts of cases easily.
+// This ensures that your functions have a correct error handling and enables you to test against hundreds of cases easily.
 //
 // Example:
 //
@@ -104,3 +105,22 @@ func FuzzUtilDistinctSet[setType comparable](testSet []setType) []setType {
 
 	return result
 }
+
+// FuzzUtilLimit returns a random piece of input set.
+//
+// Example:
+//
+//	limited := testza.FuzzUtilLimit(testza.FuzzStringFull(), 10)
+func FuzzUtilLimit[setType any](testSet []setType, limit int) []setType {
+	if len(testSet) <= limit {
+		return testSet
+	}
+
+	if limit <= 0 {
+		return []setType{}
+	}
+
+	rand.Shuffle(len(testSet), func(i, j int) { testSet[i], testSet[j] = testSet[j], testSet[i] })
+
+	return testSet[:limit]
+}


Test patch:
diff --git a/assert_test.go b/assert_test.go
index bac4612..f46d69b 100644
--- a/assert_test.go
+++ b/assert_test.go
@@ -14,8 +14,9 @@ import (
 	"testing"
 	"time"
 
-	. "github.com/MarvinJWendt/testza"
 	"github.com/pterm/pterm"
+
+	. "github.com/MarvinJWendt/testza"
 )
 
 type testMock struct {
@@ -681,7 +682,7 @@ func TestAssertCompletesIn_fails(t *testing.T) {
 
 func TestAssertNotCompletesIn(t *testing.T) {
 	AssertNotCompletesIn(t, 10*time.Microsecond, func() {
-		time.Sleep(15 * time.Millisecond)
+		time.Sleep(20 * time.Millisecond)
 	})
 }
 
diff --git a/fuzz_test.go b/fuzz_test.go
index 1483800..9eaff8d 100644
--- a/fuzz_test.go
+++ b/fuzz_test.go
@@ -9,7 +9,7 @@ import (
 	. "github.com/MarvinJWendt/testza"
 )
 
-//region FuzzUtil
+// region FuzzUtil
 
 func TestFuzzUtilMergeSets(t *testing.T) {
 	t.Run("String", func(t *testing.T) {
@@ -70,6 +70,10 @@ func TestFuzzUtilLimitSet(t *testing.T) {
 			AssertLen(t, FuzzUtilLimitSet(FuzzFloat64Full(), i), i)
 		})
 	}
+
+	t.Run(fmt.Sprintf("Max bigger than len (Limit=%d)", 10), func(t *testing.T) {
+		AssertLen(t, FuzzUtilLimitSet([]string{"a", "b", "c"}, 10), 3)
+	})
 }
 
 func TestFuzzUtilDistinctSet(t *testing.T) {
@@ -77,7 +81,27 @@ func TestFuzzUtilDistinctSet(t *testing.T) {
 	AssertEqual(t, FuzzUtilDistinctSet([]int{1, 2, 2, 1, 3}), []int{1, 2, 3})
 }
 
-//endregion
+func TestFuzzUtilLimit(t *testing.T) {
+	for i := 0; i < 10; i++ {
+		t.Run(fmt.Sprintf("String (Limit=%d)", i), func(t *testing.T) {
+			AssertLen(t, FuzzUtilLimit(FuzzStringFull(), i), i)
+		})
+
+		t.Run(fmt.Sprintf("Int (Limit=%d)", i), func(t *testing.T) {
+			AssertLen(t, FuzzUtilLimit(FuzzIntFull(), i), i)
+		})
+
+		t.Run(fmt.Sprintf("Float64 (Limit=%d)", i), func(t *testing.T) {
+			AssertLen(t, FuzzUtilLimit(FuzzFloat64Full(), i), i)
+		})
+	}
+
+	t.Run(fmt.Sprintf("Limit bigger than length (Limit=%d)", 10), func(t *testing.T) {
+		AssertLen(t, FuzzUtilLimit([]string{"a", "b", "c"}, 10), 3)
+	})
+}
+
+// endregion
 
 // region FuzzString
 func TestFuzzStringGenerateRandom(t *testing.T) {
@@ -143,9 +167,9 @@ func TestFuzzStringUsernames(t *testing.T) {
 	AssertGreater(t, len(FuzzStringUsernames()), 0)
 }
 
-//endregion
+// endregion
 
-//region FuzzBool
+// region FuzzBool
 
 func TestFuzzBoolFull(t *testing.T) {
 	AssertEqual(t, []bool{true, false}, FuzzBoolFull())
@@ -158,7 +182,7 @@ func TestFuzzBoolRunTests(t *testing.T) {
 	})
 }
 
-//endregion
+// endregion
 
 // region FuzzInt
 func TestFuzzIntGenerateRandom(t *testing.T) {
@@ -185,7 +209,7 @@ func TestFuzzIntGenerateRandomRange(t *testing.T) {
 	}
 }
 
-//endregion
+// endregion
 
 // region FuzzFloat64
 func TestFuzzFloat64Full(t *testing.T) {
@@ -223,5 +247,3 @@ func TestFuzzFloat64GenerateRandomRange(t *testing.T) {
 		AssertLessOrEqual(t, generated, i*10+10)
 	}
 }
-
-//endregion