diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 860446f4..b8a8cf19 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,7 +14,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v2 with: - go-version: 1.16 + go-version: 1.18 - name: Run CI run: | go get diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index b5e56739..eb9c548a 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -13,7 +13,7 @@ jobs: strategy: matrix: os: [ ubuntu-latest, windows-latest, macos-latest ] - go: [1.15, 1.18] + go: [1.18] steps: - name: Set up Go ${{ matrix.go }} uses: actions/setup-go@v2 diff --git a/.github/workflows/golangci.yml b/.github/workflows/golangci.yml.bak similarity index 61% rename from .github/workflows/golangci.yml rename to .github/workflows/golangci.yml.bak index 558b850b..fdd2ae81 100644 --- a/.github/workflows/golangci.yml +++ b/.github/workflows/golangci.yml.bak @@ -10,10 +10,9 @@ jobs: - name: Set up Go uses: actions/setup-go@v2 with: - go-version: 1.16 + go-version: 1.18 - name: golangci-lint uses: golangci/golangci-lint-action@v3 with: - # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. - version: v1.39 + version: latest diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index d882f4c2..e8d160dc 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -7,7 +7,7 @@ This guide will explain how you can contribute to testza. Every function in testza follows a specific name scheme. As you can see in the [documentations Table of Contents](https://github.com/MarvinJWendt/testza#documentation), testza has many different modules. The first word of a new function, is always the module name (eg. `Assert`, `Capture`, `Snapshot`, etc.). -If the function is inside a nested sub-module, the module names will be chained as in `FuzzInputBool`. +If the function is inside a nested sub-module, the module names will be chained as in `FuzzBool`. The last word of the function name is the actual function name itself. ### Example @@ -18,7 +18,7 @@ The last word of the function name is the actual function name itself. | | ┌ Fuzz integer inputs | | | ┌ Full set of integer input fuzzing | | | | -FuzzInputIntFull() +FuzzIntFull() ``` ## File Naming diff --git a/README.md b/README.md index cbd5577b..4f867c51 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ -Unit test count +Unit test count @@ -220,6 +220,21 @@ testza.FuzzStringRunTests(t, emailAddresses, func(t *testing.T, index int, str s + + +Fuzz Utils + + +
+Click to expand + + - [FuzzUtilLimitSet](https://github.com/MarvinJWendt/testza#FuzzUtilLimitSet) + - [FuzzUtilModifySet](https://github.com/MarvinJWendt/testza#FuzzUtilModifySet) + - [FuzzUtilRunTests](https://github.com/MarvinJWendt/testza#FuzzUtilRunTests) + + +
+ Fuzz Booleans @@ -228,9 +243,7 @@ testza.FuzzStringRunTests(t, emailAddresses, func(t *testing.T, index int, str s
Click to expand - - [FuzzInputBoolFull](https://github.com/MarvinJWendt/testza#FuzzInputBoolFull) - - [FuzzInputBoolModify](https://github.com/MarvinJWendt/testza#FuzzInputBoolModify) - - [FuzzInputBoolRunTests](https://github.com/MarvinJWendt/testza#FuzzInputBoolRunTests) + - [FuzzBoolFull](https://github.com/MarvinJWendt/testza#FuzzBoolFull)
@@ -243,17 +256,14 @@ testza.FuzzStringRunTests(t, emailAddresses, func(t *testing.T, index int, str s
Click to expand - - [FuzzInputStringEmailAddresses](https://github.com/MarvinJWendt/testza#FuzzInputStringEmailAddresses) - - [FuzzInputStringEmpty](https://github.com/MarvinJWendt/testza#FuzzInputStringEmpty) - - [FuzzInputStringFull](https://github.com/MarvinJWendt/testza#FuzzInputStringFull) - - [FuzzInputStringGenerateRandom](https://github.com/MarvinJWendt/testza#FuzzInputStringGenerateRandom) - - [FuzzInputStringHtmlTags](https://github.com/MarvinJWendt/testza#FuzzInputStringHtmlTags) - - [FuzzInputStringLimit](https://github.com/MarvinJWendt/testza#FuzzInputStringLimit) - - [FuzzInputStringLong](https://github.com/MarvinJWendt/testza#FuzzInputStringLong) - - [FuzzInputStringModify](https://github.com/MarvinJWendt/testza#FuzzInputStringModify) - - [FuzzInputStringNumeric](https://github.com/MarvinJWendt/testza#FuzzInputStringNumeric) - - [FuzzInputStringRunTests](https://github.com/MarvinJWendt/testza#FuzzInputStringRunTests) - - [FuzzInputStringUsernames](https://github.com/MarvinJWendt/testza#FuzzInputStringUsernames) + - [FuzzStringEmailAddresses](https://github.com/MarvinJWendt/testza#FuzzStringEmailAddresses) + - [FuzzStringEmpty](https://github.com/MarvinJWendt/testza#FuzzStringEmpty) + - [FuzzStringFull](https://github.com/MarvinJWendt/testza#FuzzStringFull) + - [FuzzStringGenerateRandom](https://github.com/MarvinJWendt/testza#FuzzStringGenerateRandom) + - [FuzzStringHtmlTags](https://github.com/MarvinJWendt/testza#FuzzStringHtmlTags) + - [FuzzStringLong](https://github.com/MarvinJWendt/testza#FuzzStringLong) + - [FuzzStringNumeric](https://github.com/MarvinJWendt/testza#FuzzStringNumeric) + - [FuzzStringUsernames](https://github.com/MarvinJWendt/testza#FuzzStringUsernames)
@@ -266,12 +276,10 @@ testza.FuzzStringRunTests(t, emailAddresses, func(t *testing.T, index int, str s
Click to expand - - [FuzzInputFloat64Full](https://github.com/MarvinJWendt/testza#FuzzInputFloat64Full) - - [FuzzInputFloat64GenerateRandomNegative](https://github.com/MarvinJWendt/testza#FuzzInputFloat64GenerateRandomNegative) - - [FuzzInputFloat64GenerateRandomPositive](https://github.com/MarvinJWendt/testza#FuzzInputFloat64GenerateRandomPositive) - - [FuzzInputFloat64GenerateRandomRange](https://github.com/MarvinJWendt/testza#FuzzInputFloat64GenerateRandomRange) - - [FuzzInputFloat64Modify](https://github.com/MarvinJWendt/testza#FuzzInputFloat64Modify) - - [FuzzInputFloat64RunTests](https://github.com/MarvinJWendt/testza#FuzzInputFloat64RunTests) + - [FuzzFloat64Full](https://github.com/MarvinJWendt/testza#FuzzFloat64Full) + - [FuzzFloat64GenerateRandomNegative](https://github.com/MarvinJWendt/testza#FuzzFloat64GenerateRandomNegative) + - [FuzzFloat64GenerateRandomPositive](https://github.com/MarvinJWendt/testza#FuzzFloat64GenerateRandomPositive) + - [FuzzFloat64GenerateRandomRange](https://github.com/MarvinJWendt/testza#FuzzFloat64GenerateRandomRange)
@@ -284,12 +292,10 @@ testza.FuzzStringRunTests(t, emailAddresses, func(t *testing.T, index int, str s
Click to expand - - [FuzzInputIntFull](https://github.com/MarvinJWendt/testza#FuzzInputIntFull) - - [FuzzInputIntGenerateRandomNegative](https://github.com/MarvinJWendt/testza#FuzzInputIntGenerateRandomNegative) - - [FuzzInputIntGenerateRandomPositive](https://github.com/MarvinJWendt/testza#FuzzInputIntGenerateRandomPositive) - - [FuzzInputIntGenerateRandomRange](https://github.com/MarvinJWendt/testza#FuzzInputIntGenerateRandomRange) - - [FuzzInputIntModify](https://github.com/MarvinJWendt/testza#FuzzInputIntModify) - - [FuzzInputIntRunTests](https://github.com/MarvinJWendt/testza#FuzzInputIntRunTests) + - [FuzzIntFull](https://github.com/MarvinJWendt/testza#FuzzIntFull) + - [FuzzIntGenerateRandomNegative](https://github.com/MarvinJWendt/testza#FuzzIntGenerateRandomNegative) + - [FuzzIntGenerateRandomPositive](https://github.com/MarvinJWendt/testza#FuzzIntGenerateRandomPositive) + - [FuzzIntGenerateRandomRange](https://github.com/MarvinJWendt/testza#FuzzIntGenerateRandomRange)
@@ -317,7 +323,7 @@ testza.FuzzStringRunTests(t, emailAddresses, func(t *testing.T, index int, str s #### AssertCompletesIn ```go -func AssertCompletesIn(t testRunner, duration time.Duration, f func(), msg ...interface{}) +func AssertCompletesIn(t testRunner, duration time.Duration, f func(), msg ...any) ``` AssertCompletesIn asserts that a function completes in a given time. Use @@ -338,7 +344,7 @@ Example: #### AssertContains ```go -func AssertContains(t testRunner, object, element interface{}, msg ...interface{}) +func AssertContains(t testRunner, object, element any, msg ...any) ``` AssertContains asserts that a string/list/array/slice/map contains the @@ -356,7 +362,7 @@ Example: #### AssertDecreasing ```go -func AssertDecreasing(t testRunner, object interface{}, msg ...interface{}) +func AssertDecreasing(t testRunner, object any, msg ...any) ``` AssertDecreasing asserts that the values in a slice are decreasing. the test @@ -376,7 +382,7 @@ Example: #### AssertDirEmpty ```go -func AssertDirEmpty(t testRunner, dir string, msg ...interface{}) +func AssertDirEmpty(t testRunner, dir string, msg ...any) ``` AssertDirEmpty asserts that a directory is empty. The test will pass when @@ -392,7 +398,7 @@ Example: #### AssertDirExists ```go -func AssertDirExists(t testRunner, dir string, msg ...interface{}) +func AssertDirExists(t testRunner, dir string, msg ...any) ``` AssertDirExists asserts that a directory exists. The test will pass when the @@ -409,7 +415,7 @@ Example: #### AssertDirNotEmpty ```go -func AssertDirNotEmpty(t testRunner, dir string, msg ...interface{}) +func AssertDirNotEmpty(t testRunner, dir string, msg ...any) ``` AssertDirNotEmpty asserts that a directory is not empty The test will pass @@ -426,7 +432,7 @@ Example: #### AssertEqual ```go -func AssertEqual(t testRunner, expected interface{}, actual interface{}, msg ...interface{}) +func AssertEqual(t testRunner, expected any, actual any, msg ...any) ``` AssertEqual asserts that two objects are equal. @@ -442,7 +448,7 @@ Example: #### AssertEqualValues ```go -func AssertEqualValues(t testRunner, expected interface{}, actual interface{}, msg ...interface{}) +func AssertEqualValues(t testRunner, expected any, actual any, msg ...any) ``` AssertEqualValues asserts that two objects have equal values. The order of @@ -476,7 +482,7 @@ Comparing struct values: #### AssertErrorIs ```go -func AssertErrorIs(t testRunner, err, target error, msg ...interface{}) +func AssertErrorIs(t testRunner, err, target error, msg ...any) ``` AssertErrorIs asserts that target is inside the error chain of err. @@ -493,7 +499,7 @@ Example: #### AssertFalse ```go -func AssertFalse(t testRunner, value interface{}, msg ...interface{}) +func AssertFalse(t testRunner, value any, msg ...any) ``` AssertFalse asserts that an expression or object resolves to false. @@ -511,7 +517,7 @@ Example: #### AssertFileExists ```go -func AssertFileExists(t testRunner, file string, msg ...interface{}) +func AssertFileExists(t testRunner, file string, msg ...any) ``` AssertFileExists asserts that a file exists. @@ -527,7 +533,7 @@ Example: #### AssertGreater ```go -func AssertGreater(t testRunner, object1, object2 interface{}, msg ...interface{}) +func AssertGreater(t testRunner, object1, object2 any, msg ...any) ``` AssertGreater asserts that the first object is greater than the second. @@ -562,7 +568,7 @@ testza.AssertGreaterOrEqual(t, 10, 10) #### AssertImplements ```go -func AssertImplements(t testRunner, interfaceObject, object interface{}, msg ...interface{}) +func AssertImplements(t testRunner, interfaceObject, object any, msg ...any) ``` AssertImplements asserts that an objects implements an interface. @@ -578,7 +584,7 @@ Example: #### AssertIncreasing ```go -func AssertIncreasing(t testRunner, object interface{}, msg ...interface{}) +func AssertIncreasing(t testRunner, object any, msg ...any) ``` AssertIncreasing asserts that the values in a slice are increasing. the test @@ -598,7 +604,7 @@ Example: #### AssertKindOf ```go -func AssertKindOf(t testRunner, expectedKind reflect.Kind, object interface{}, msg ...interface{}) +func AssertKindOf(t testRunner, expectedKind reflect.Kind, object any, msg ...any) ``` AssertKindOf asserts that the object is a type of kind exptectedKind. @@ -617,7 +623,7 @@ Example: #### AssertLen ```go -func AssertLen(t testRunner, object interface{}, length int, msg ...interface{}) +func AssertLen(t testRunner, object any, length int, msg ...any) ``` AssertLen asserts that the length of an object is equal to the given length. @@ -635,7 +641,7 @@ Example: #### AssertLess ```go -func AssertLess(t testRunner, object1, object2 interface{}, msg ...interface{}) +func AssertLess(t testRunner, object1, object2 any, msg ...any) ``` AssertLess asserts that the first object is less than the second. @@ -669,7 +675,7 @@ Example: #### AssertNil ```go -func AssertNil(t testRunner, object interface{}, msg ...interface{}) +func AssertNil(t testRunner, object any, msg ...any) ``` AssertNil asserts that an object is nil. @@ -684,7 +690,7 @@ Example: #### AssertNoDirExists ```go -func AssertNoDirExists(t testRunner, dir string, msg ...interface{}) +func AssertNoDirExists(t testRunner, dir string, msg ...any) ``` AssertNoDirExists asserts that a directory does not exists. The test will @@ -701,7 +707,7 @@ Example: #### AssertNoError ```go -func AssertNoError(t testRunner, err error, msg ...interface{}) +func AssertNoError(t testRunner, err error, msg ...any) ``` AssertNoError asserts that an error is nil. @@ -717,7 +723,7 @@ Example: #### AssertNoFileExists ```go -func AssertNoFileExists(t testRunner, file string, msg ...interface{}) +func AssertNoFileExists(t testRunner, file string, msg ...any) ``` @@ -725,7 +731,7 @@ func AssertNoFileExists(t testRunner, file string, msg ...interface{}) #### AssertNoSubset ```go -func AssertNoSubset(t testRunner, list interface{}, subset interface{}, msg ...interface{}) +func AssertNoSubset(t testRunner, list any, subset any, msg ...any) ``` AssertNoSubset asserts that the second parameter is not a subset of the @@ -742,7 +748,7 @@ Example: #### AssertNotCompletesIn ```go -func AssertNotCompletesIn(t testRunner, duration time.Duration, f func(), msg ...interface{}) +func AssertNotCompletesIn(t testRunner, duration time.Duration, f func(), msg ...any) ``` AssertNotCompletesIn asserts that a function does not complete in a given @@ -766,7 +772,7 @@ Example: #### AssertNotContains ```go -func AssertNotContains(t testRunner, object, element interface{}, msg ...interface{}) +func AssertNotContains(t testRunner, object, element any, msg ...any) ``` AssertNotContains asserts that a string/list/array/slice/map does not @@ -783,7 +789,7 @@ Example: #### AssertNotEqual ```go -func AssertNotEqual(t testRunner, expected interface{}, actual interface{}, msg ...interface{}) +func AssertNotEqual(t testRunner, expected any, actual any, msg ...any) ``` AssertNotEqual asserts that two objects are not equal. @@ -799,7 +805,7 @@ Example: #### AssertNotEqualValues ```go -func AssertNotEqualValues(t testRunner, expected interface{}, actual interface{}, msg ...interface{}) +func AssertNotEqualValues(t testRunner, expected any, actual any, msg ...any) ``` AssertNotEqualValues asserts that two objects do not have equal values. @@ -830,7 +836,7 @@ Comparing struct values: #### AssertNotErrorIs ```go -func AssertNotErrorIs(t testRunner, err, target error, msg ...interface{}) +func AssertNotErrorIs(t testRunner, err, target error, msg ...any) ``` AssertNotErrorIs @@ -848,7 +854,7 @@ Example: #### AssertNotImplements ```go -func AssertNotImplements(t testRunner, interfaceObject, object interface{}, msg ...interface{}) +func AssertNotImplements(t testRunner, interfaceObject, object any, msg ...any) ``` AssertNotImplements asserts that an object does not implement an interface. @@ -864,7 +870,7 @@ Example: #### AssertNotKindOf ```go -func AssertNotKindOf(t testRunner, kind reflect.Kind, object interface{}, msg ...interface{}) +func AssertNotKindOf(t testRunner, kind reflect.Kind, object any, msg ...any) ``` AssertNotKindOf asserts that the object is not a type of kind `kind`. @@ -883,7 +889,7 @@ Example: #### AssertNotNil ```go -func AssertNotNil(t testRunner, object interface{}, msg ...interface{}) +func AssertNotNil(t testRunner, object any, msg ...any) ``` AssertNotNil asserts that an object is not nil. @@ -900,7 +906,7 @@ Example: #### AssertNotNumeric ```go -func AssertNotNumeric(t testRunner, object interface{}, msg ...interface{}) +func AssertNotNumeric(t testRunner, object any, msg ...any) ``` AssertNotNumeric checks if the object is not a numeric type. Numeric types @@ -918,7 +924,7 @@ Example: #### AssertNotPanics ```go -func AssertNotPanics(t testRunner, f func(), msg ...interface{}) +func AssertNotPanics(t testRunner, f func(), msg ...any) ``` AssertNotPanics asserts that a function does not panic. @@ -935,7 +941,7 @@ Example: #### AssertNotRegexp ```go -func AssertNotRegexp(t testRunner, regex interface{}, txt interface{}, msg ...interface{}) +func AssertNotRegexp(t testRunner, regex any, txt any, msg ...any) ``` AssertNotRegexp asserts that a string does not match a given regexp. @@ -950,7 +956,7 @@ Example: #### AssertNotSameElements ```go -func AssertNotSameElements(t testRunner, expected interface{}, actual interface{}, msg ...interface{}) +func AssertNotSameElements(t testRunner, expected any, actual any, msg ...any) ``` AssertNotSameElements asserts that two slices contains same elements @@ -972,7 +978,7 @@ Example: #### AssertNotZero ```go -func AssertNotZero(t testRunner, value interface{}, msg ...interface{}) +func AssertNotZero(t testRunner, value any, msg ...any) ``` AssertNotZero asserts that the value is not the zero value for it's type. @@ -989,7 +995,7 @@ Example: #### AssertNumeric ```go -func AssertNumeric(t testRunner, object interface{}, msg ...interface{}) +func AssertNumeric(t testRunner, object any, msg ...any) ``` AssertNumeric asserts that the object is a numeric type. Numeric types are: @@ -1008,7 +1014,7 @@ Example: #### AssertPanics ```go -func AssertPanics(t testRunner, f func(), msg ...interface{}) +func AssertPanics(t testRunner, f func(), msg ...any) ``` AssertPanics asserts that a function panics. @@ -1026,7 +1032,7 @@ Example: #### AssertRegexp ```go -func AssertRegexp(t testRunner, regex interface{}, txt interface{}, msg ...interface{}) +func AssertRegexp(t testRunner, regex any, txt any, msg ...any) ``` AssertRegexp asserts that a string matches a given regexp. @@ -1041,7 +1047,7 @@ Example: #### AssertSameElements ```go -func AssertSameElements(t testRunner, expected interface{}, actual interface{}, msg ...interface{}) +func AssertSameElements(t testRunner, expected any, actual any, msg ...any) ``` AssertSameElements asserts that two slices contains same elements (including @@ -1064,7 +1070,7 @@ Example: #### AssertSubset ```go -func AssertSubset(t testRunner, list interface{}, subset interface{}, msg ...interface{}) +func AssertSubset(t testRunner, list any, subset any, msg ...any) ``` AssertSubset asserts that the second parameter is a subset of the list. The @@ -1081,7 +1087,7 @@ Example: #### AssertTestFails ```go -func AssertTestFails(t testRunner, test func(t TestingPackageWithFailFunctions), msg ...interface{}) +func AssertTestFails(t testRunner, test func(t TestingPackageWithFailFunctions), msg ...any) ``` AssertTestFails asserts that a unit test fails. A unit test fails if one of @@ -1105,7 +1111,7 @@ Example: #### AssertTrue ```go -func AssertTrue(t testRunner, value interface{}, msg ...interface{}) +func AssertTrue(t testRunner, value any, msg ...any) ``` AssertTrue asserts that an expression or object resolves to true. @@ -1123,7 +1129,7 @@ Example: #### AssertZero ```go -func AssertZero(t testRunner, value interface{}, msg ...interface{}) +func AssertZero(t testRunner, value any, msg ...any) ``` AssertZero asserts that the value is the zero value for it's type. @@ -1204,243 +1210,141 @@ Example: ### Fuzz Booleans -#### FuzzInputBoolFull +#### FuzzBoolFull ```go -func FuzzInputBoolFull() []bool +func FuzzBoolFull() []bool ``` -FuzzInputBoolFull returns true and false in a boolean slice. - -#### FuzzInputBoolModify - -```go -func FuzzInputBoolModify(inputSlice []bool, modifier func(index int, value bool) bool) (floats []bool) -``` - -FuzzInputBoolModify returns a modified version of a test set. - -Example: - - testset := testza.FuzzInputBoolModify(testza.FuzzInputBoolFull(), func(index int, value bool) bool { - return !value - }) - -#### FuzzInputBoolRunTests - -```go -func FuzzInputBoolRunTests(t testRunner, testSet []bool, testFunc func(t *testing.T, index int, f bool)) -``` - -FuzzInputBoolRunTests runs a test for every value in a testset. 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. - -Example: - - testza.FuzzInputBoolRunTests(t, testza.FuzzInputBoolFull(), func(t *testing.T, index int, b bool) { - // Test logic - // err := YourFunction(b) - // testza.AssertNoError(t, err) - // ... - }) +FuzzBoolFull returns true and false in a boolean slice. ### Fuzz Float64s -#### FuzzInputFloat64Full +#### FuzzFloat64Full ```go -func FuzzInputFloat64Full() (floats []float64) +func FuzzFloat64Full() (floats []float64) ``` -FuzzInputFloat64Full returns a combination of every float64 testset and some +FuzzFloat64Full returns a combination of every float64 testset and some random float64s (positive and negative). -#### FuzzInputFloat64GenerateRandomNegative +#### FuzzFloat64GenerateRandomNegative ```go -func FuzzInputFloat64GenerateRandomNegative(count int, min float64) (floats []float64) +func FuzzFloat64GenerateRandomNegative(count int, min float64) (floats []float64) ``` -FuzzInputFloat64GenerateRandomNegative generates random negative integers -with a minimum of min. If the minimum is positive, it will be converted to a +FuzzFloat64GenerateRandomNegative generates random negative integers with a +minimum of min. If the minimum is positive, it will be converted to a negative number. If it is set to 0, there is no limit. -#### FuzzInputFloat64GenerateRandomPositive +#### FuzzFloat64GenerateRandomPositive ```go -func FuzzInputFloat64GenerateRandomPositive(count int, max float64) (floats []float64) +func FuzzFloat64GenerateRandomPositive(count int, max float64) (floats []float64) ``` -FuzzInputFloat64GenerateRandomPositive generates random positive integers -with a maximum of max. If the maximum is 0, or below, the maximum will be -set to math.MaxInt64. - -#### FuzzInputFloat64GenerateRandomRange - -```go -func FuzzInputFloat64GenerateRandomRange(count int, min, max float64) (floats []float64) -``` - -FuzzInputFloat64GenerateRandomRange generates random positive integers with -a maximum of max. If the maximum is 0, or below, the maximum will be set to +FuzzFloat64GenerateRandomPositive generates random positive integers with a +maximum of max. If the maximum is 0, or below, the maximum will be set to math.MaxInt64. -#### FuzzInputFloat64Modify +#### FuzzFloat64GenerateRandomRange ```go -func FuzzInputFloat64Modify(inputSlice []float64, modifier func(index int, value float64) float64) (floats []float64) +func FuzzFloat64GenerateRandomRange(count int, min, max float64) (floats []float64) ``` -FuzzInputFloat64Modify returns a modified version of a test set. - -Example: - - testset := testza.FuzzInputFloat64Modify(testza.FuzzInputFloat64Full(), func(index int, value float64) float64 { - return value * 2 - }) - -#### FuzzInputFloat64RunTests - -```go -func FuzzInputFloat64RunTests(t testRunner, testSet []float64, testFunc func(t *testing.T, index int, f float64)) -``` - -FuzzInputFloat64RunTests runs a test for every value in a testset. 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. - -Example: - - testza.FuzzInputFloat64RunTests(t, testza.FuzzInputFloat64Full(), func(t *testing.T, index int, f float64) { - // Test logic - // err := YourFunction(f) - // testza.AssertNoError(t, err) - // ... - }) +FuzzFloat64GenerateRandomRange generates random positive integers with a +maximum of max. If the maximum is 0, or below, the maximum will be set to +math.MaxInt64. ### Fuzz Integers -#### FuzzInputIntFull +#### FuzzIntFull ```go -func FuzzInputIntFull() (ints []int) +func FuzzIntFull() (ints []int) ``` -FuzzInputIntFull returns a combination of every integer testset and some -random integers (positive and negative). +FuzzIntFull returns a combination of every integer testset and some random +integers (positive and negative). -#### FuzzInputIntGenerateRandomNegative +#### FuzzIntGenerateRandomNegative ```go -func FuzzInputIntGenerateRandomNegative(count, min int) (ints []int) +func FuzzIntGenerateRandomNegative(count, min int) (ints []int) ``` -FuzzInputIntGenerateRandomNegative generates random negative integers with a +FuzzIntGenerateRandomNegative generates random negative integers with a minimum of min. If the minimum is 0, or above, the maximum will be set to math.MinInt64. -#### FuzzInputIntGenerateRandomPositive +#### FuzzIntGenerateRandomPositive ```go -func FuzzInputIntGenerateRandomPositive(count, max int) (ints []int) +func FuzzIntGenerateRandomPositive(count, max int) (ints []int) ``` -FuzzInputIntGenerateRandomPositive generates random positive integers with a +FuzzIntGenerateRandomPositive generates random positive integers with a maximum of max. If the maximum is 0, or below, the maximum will be set to math.MaxInt64. -#### FuzzInputIntGenerateRandomRange - -```go -func FuzzInputIntGenerateRandomRange(count, min, max int) (ints []int) -``` - -FuzzInputIntGenerateRandomRange generates random integers with a range of -min to max. - -#### FuzzInputIntModify +#### FuzzIntGenerateRandomRange ```go -func FuzzInputIntModify(inputSlice []int, modifier func(index int, value int) int) (ints []int) +func FuzzIntGenerateRandomRange(count, min, max int) (ints []int) ``` -FuzzInputIntModify returns a modified version of a test set. - -Example: - - testset := testza.FuzzInputIntModify(testza.FuzzInputIntFull(), func(index int, value int) int { - return value * 2 - }) - -#### FuzzInputIntRunTests - -```go -func FuzzInputIntRunTests(t testRunner, testSet []int, testFunc func(t *testing.T, index int, i int)) -``` - -FuzzInputIntRunTests runs a test for every value in a testset. 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. - -Example: - - testza.FuzzInputIntRunTests(t, testza.FuzzInputIntFull(), func(t *testing.T, index int, i int) { - // Test logic - // err := YourFunction(i) - // testza.AssertNoError(t, err) - // ... - }) +FuzzIntGenerateRandomRange generates random integers with a range of min to +max. ### Fuzz Strings -#### FuzzInputStringEmailAddresses +#### FuzzStringEmailAddresses ```go -func FuzzInputStringEmailAddresses() []string +func FuzzStringEmailAddresses() []string ``` -FuzzInputStringEmailAddresses returns a test set with valid email addresses. -The addresses may look like they are invalid, but they are all conform to -RFC 2822 and could be used. You can use this test set to test your email +FuzzStringEmailAddresses returns a test set with valid email addresses. The +addresses may look like they are invalid, but they are all conform to RFC +2822 and could be used. You can use this test set to test your email validation process. -#### FuzzInputStringEmpty +#### FuzzStringEmpty ```go -func FuzzInputStringEmpty() []string +func FuzzStringEmpty() []string ``` -FuzzInputStringEmpty returns a test set with a single empty string. +FuzzStringEmpty returns a test set with a single empty string. -#### FuzzInputStringFull +#### FuzzStringFull ```go -func FuzzInputStringFull() (ret []string) +func FuzzStringFull() (ret []string) ``` -FuzzInputStringFull contains all string test sets plus ten generated random +FuzzStringFull contains all string test sets plus ten generated random strings. This test set is huge and should only be used if you want to make sure that no string, at all, can crash a process. -#### FuzzInputStringGenerateRandom +#### FuzzStringGenerateRandom ```go -func FuzzInputStringGenerateRandom(count, length int) (result []string) +func FuzzStringGenerateRandom(count, length int) (result []string) ``` -FuzzInputStringGenerateRandom returns random strings in a test set. +FuzzStringGenerateRandom returns random strings in a test set. -#### FuzzInputStringHtmlTags +#### FuzzStringHtmlTags ```go -func FuzzInputStringHtmlTags() []string +func FuzzStringHtmlTags() []string ``` -FuzzInputStringHtmlTags returns a test set with different html tags. +FuzzStringHtmlTags returns a test set with different html tags. Example: @@ -1448,77 +1352,83 @@ Example: - - link -#### FuzzInputStringLimit +#### FuzzStringLong ```go -func FuzzInputStringLimit(testSet []string, max int) []string +func FuzzStringLong() (testSet []string) ``` -FuzzInputStringLimit limits a test set in size. +FuzzStringLong returns a test set with long random strings. Returns: [0]: +Random string (length: 25) [1]: Random string (length: 50) [2]: Random +string (length: 100) [3]: Random string (length: 1,000) [4]: Random string +(length: 100,000) -#### FuzzInputStringLong +#### FuzzStringNumeric ```go -func FuzzInputStringLong() (testSet []string) +func FuzzStringNumeric() []string ``` -FuzzInputStringLong returns a test set with long random strings. Returns: -[0]: Random string (length: 25) [1]: Random string (length: 50) [2]: Random -string (length: 100) [3]: Random string (length: 1,000) [4]: Random string -(length: 100,000) +FuzzStringNumeric returns a test set with strings that are numeric. The +highest number in here is "9223372036854775807", which is equal to the +maxmim int64. + +#### FuzzStringUsernames + +```go +func FuzzStringUsernames() []string +``` + +FuzzStringUsernames returns a test set with usernames. -#### FuzzInputStringModify +### Fuzz Utils + +#### FuzzUtilLimitSet ```go -func FuzzInputStringModify(inputSlice []string, modifier func(index int, value string) string) (ret []string) +func FuzzUtilLimitSet[setType any](testSet []setType, max int) []setType ``` -FuzzInputStringModify returns a modified version of a test set. +FuzzUtilLimitSet limits a test set in size. Example: - testset := testza.FuzzInputStringModify(testza.FuzzInputStringFull(), func(index int, value string) string { - return value + " some suffix" - }) + limitedSet := testza.FuzzUtilLimitSet(testza.FuzzStringFull(), 10) -#### FuzzInputStringNumeric +#### FuzzUtilModifySet ```go -func FuzzInputStringNumeric() []string +func FuzzUtilModifySet[setType any](inputSet []setType, modifier func(index int, value setType) setType) (floats []setType) ``` -FuzzInputStringNumeric returns a test set with strings that are numeric. The -highest number in here is "9223372036854775807", which is equal to the -maxmim int64. +FuzzUtilModifySet returns a modified version of a test set. + +Example: + + modifiedSet := testza.FuzzUtilModifySet(testza.FuzzIntFull(), func(i int, value int) int { + return i * 2 // double every value in the test set + }) -#### FuzzInputStringRunTests +#### FuzzUtilRunTests ```go -func FuzzInputStringRunTests(t testRunner, testSet []string, testFunc func(t *testing.T, index int, str string)) +func FuzzUtilRunTests[setType any](t testRunner, testSet []setType, testFunc func(t *testing.T, index int, f setType)) ``` -FuzzInputStringRunTests runs a test for every value in a testset. 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 +FuzzUtilRunTests runs a test for every value in a testset. 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. Example: - testza.FuzzInputStringRunTests(t, testza.FuzzInputStringFull(), func(t *testing.T, index int, str string) { + testza.FuzzUtilRunTests(t, testza.FuzzStringEmailAddresses(), func(t *testing.T, index int, emailAddress string) { // Test logic - // err := YourFunction(str) + // err := YourFunction(emailAddress) // testza.AssertNoError(t, err) // ... }) -#### FuzzInputStringUsernames - -```go -func FuzzInputStringUsernames() []string -``` - -FuzzInputStringUsernames returns a test set with usernames. - ### Settings #### SetColorsEnabled @@ -1626,7 +1536,7 @@ Example: #### SnapshotCreate ```go -func SnapshotCreate(name string, snapshotObject interface{}) error +func SnapshotCreate(name string, snapshotObject any) error ``` SnapshotCreate creates a snapshot of an object, which can be validated in @@ -1644,7 +1554,7 @@ Example: #### SnapshotCreateOrValidate ```go -func SnapshotCreateOrValidate(t testRunner, name string, object interface{}, msg ...interface{}) error +func SnapshotCreateOrValidate(t testRunner, name string, object any, msg ...any) error ``` SnapshotCreateOrValidate creates a snapshot of an object which can be used @@ -1666,7 +1576,7 @@ Example: #### SnapshotValidate ```go -func SnapshotValidate(t testRunner, name string, actual interface{}, msg ...interface{}) error +func SnapshotValidate(t testRunner, name string, actual any, msg ...any) error ``` SnapshotValidate validates an already exisiting snapshot of an object. You diff --git a/assert.go b/assert.go index df608ec7..ef231a31 100644 --- a/assert.go +++ b/assert.go @@ -18,17 +18,17 @@ type testMock struct { ErrorMessage string } -func (m *testMock) fail(msg ...interface{}) { +func (m *testMock) fail(msg ...any) { m.ErrorCalled = true m.ErrorMessage = fmt.Sprint(msg...) } -func (m *testMock) Error(args ...interface{}) { +func (m *testMock) Error(args ...any) { m.fail(args...) } // Errorf is a mock of testing.T. -func (m *testMock) Errorf(format string, args ...interface{}) { +func (m *testMock) Errorf(format string, args ...any) { m.fail(fmt.Sprintf(format, args...)) } @@ -43,12 +43,12 @@ func (m *testMock) FailNow() { } // Fatal is a mock of testing.T. -func (m *testMock) Fatal(args ...interface{}) { +func (m *testMock) Fatal(args ...any) { m.fail(args...) } // Fatalf is a mock of testing.T. -func (m *testMock) Fatalf(format string, args ...interface{}) { +func (m *testMock) Fatalf(format string, args ...any) { m.fail(fmt.Sprintf(format, args...)) } @@ -62,7 +62,7 @@ func (m *testMock) Fatalf(format string, args ...interface{}) { // testza.AssertKindOf(t, reflect.Int, 1337) // testza.AssertKindOf(t, reflect.Bool, true) // testza.AssertKindOf(t, reflect.Map, map[string]bool{}) -func AssertKindOf(t testRunner, expectedKind reflect.Kind, object interface{}, msg ...interface{}) { +func AssertKindOf(t testRunner, expectedKind reflect.Kind, object any, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -86,7 +86,7 @@ func AssertKindOf(t testRunner, expectedKind reflect.Kind, object interface{}, m // testza.AssertNotKindOf(t, reflect.Int, 13.37) // testza.AssertNotKindOf(t, reflect.Bool, map[string]bool{}) // testza.AssertNotKindOf(t, reflect.Map, false) -func AssertNotKindOf(t testRunner, kind reflect.Kind, object interface{}, msg ...interface{}) { +func AssertNotKindOf(t testRunner, kind reflect.Kind, object any, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -113,7 +113,7 @@ func AssertNotKindOf(t testRunner, kind reflect.Kind, object interface{}, msg .. // testza.AssertNumeric(t, 123) // testza.AssertNumeric(t, 1.23) // testza.AssertNumeric(t, uint(123)) -func AssertNumeric(t testRunner, object interface{}, msg ...interface{}) { +func AssertNumeric(t testRunner, object any, msg ...any) { if !internal.IsNumber(object) { internal.Fail(t, "An object that !!should be a number!! is not of a numeric type.", internal.NewObjectsSingleUnknown(object), msg...) } @@ -128,7 +128,7 @@ func AssertNumeric(t testRunner, object interface{}, msg ...interface{}) { // Example: // testza.AssertNotNumeric(t, true) // testza.AssertNotNumeric(t, "123") -func AssertNotNumeric(t testRunner, object interface{}, msg ...interface{}) { +func AssertNotNumeric(t testRunner, object any, msg ...any) { if internal.IsNumber(object) { internal.Fail(t, "An object that !!should not be a number!! is of a numeric type.", internal.NewObjectsSingleUnknown(object), msg...) } @@ -142,7 +142,7 @@ func AssertNotNumeric(t testRunner, object interface{}, msg ...interface{}) { // testza.AssertZero(t, 0) // testza.AssertZero(t, false) // testza.AssertZero(t, "") -func AssertZero(t testRunner, value interface{}, msg ...interface{}) { +func AssertZero(t testRunner, value any, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -160,7 +160,7 @@ func AssertZero(t testRunner, value interface{}, msg ...interface{}) { // testza.AssertNotZero(t, 1337) // testza.AssertNotZero(t, true) // testza.AssertNotZero(t, "Hello, World") -func AssertNotZero(t testRunner, value interface{}, msg ...interface{}) { +func AssertNotZero(t testRunner, value any, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -177,7 +177,7 @@ func AssertNotZero(t testRunner, value interface{}, msg ...interface{}) { // Example: // testza.AssertEqual(t, "Hello, World!", "Hello, World!") // testza.AssertEqual(t, true, true) -func AssertEqual(t testRunner, expected interface{}, actual interface{}, msg ...interface{}) { +func AssertEqual(t testRunner, expected any, actual any, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -194,7 +194,7 @@ func AssertEqual(t testRunner, expected interface{}, actual interface{}, msg ... // Example: // testza.AssertNotEqual(t, true, false) // testza.AssertNotEqual(t, "Hello", "World") -func AssertNotEqual(t testRunner, expected interface{}, actual interface{}, msg ...interface{}) { +func AssertNotEqual(t testRunner, expected any, actual any, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -235,7 +235,7 @@ func AssertNotEqual(t testRunner, expected interface{}, actual interface{}, msg // } // // testza.AssertEqualValues(t, person1, person2) -func AssertEqualValues(t testRunner, expected interface{}, actual interface{}, msg ...interface{}) { +func AssertEqualValues(t testRunner, expected any, actual any, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -266,7 +266,7 @@ func AssertEqualValues(t testRunner, expected interface{}, actual interface{}, m // } // // testza.AssertNotEqualValues(t, person1, person2) -func AssertNotEqualValues(t testRunner, expected interface{}, actual interface{}, msg ...interface{}) { +func AssertNotEqualValues(t testRunner, expected any, actual any, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -285,7 +285,7 @@ func AssertNotEqualValues(t testRunner, expected interface{}, actual interface{} // testza.AssertTrue(t, 1 == 1) // testza.AssertTrue(t, 2 != 3) // testza.AssertTrue(t, 1 > 0 && 4 < 5) -func AssertTrue(t testRunner, value interface{}, msg ...interface{}) { +func AssertTrue(t testRunner, value any, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -304,7 +304,7 @@ func AssertTrue(t testRunner, value interface{}, msg ...interface{}) { // testza.AssertFalse(t, 1 == 2) // testza.AssertFalse(t, 2 != 2) // testza.AssertFalse(t, 1 > 5 && 4 < 0) -func AssertFalse(t testRunner, value interface{}, msg ...interface{}) { +func AssertFalse(t testRunner, value any, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -321,7 +321,7 @@ func AssertFalse(t testRunner, value interface{}, msg ...interface{}) { // Example: // testza.AssertImplements(t, (*YourInterface)(nil), new(YourObject)) // testza.AssertImplements(t, (*fmt.Stringer)(nil), new(types.Const)) => pass -func AssertImplements(t testRunner, interfaceObject, object interface{}, msg ...interface{}) { +func AssertImplements(t testRunner, interfaceObject, object any, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -338,7 +338,7 @@ func AssertImplements(t testRunner, interfaceObject, object interface{}, msg ... // 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. -func AssertNotImplements(t testRunner, interfaceObject, object interface{}, msg ...interface{}) { +func AssertNotImplements(t testRunner, interfaceObject, object any, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -356,13 +356,16 @@ func AssertNotImplements(t testRunner, interfaceObject, object interface{}, msg // testza.AssertContains(t, []int{1,2,3}, 2) // testza.AssertContains(t, []string{"Hello", "World"}, "World") // testza.AssertContains(t, "Hello, World!", "World") -func AssertContains(t testRunner, object, element interface{}, msg ...interface{}) { +func AssertContains(t testRunner, object, element any, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } if !internal.DoesContain(object, element) { - internal.Fail(t, "An object !!does not contain!! the object it should contain.", internal.Objects{{Name: "object", Data: object}, {Name: "element that is missing in object", Data: element}}, msg...) + internal.Fail(t, "An object !!does not contain!! the object it should contain.", internal.Objects{ + internal.NewObjectsSingleNamed("Missing Object", element)[0], + internal.NewObjectsSingleNamed("Full Object", object)[0], + }, msg...) } } @@ -373,7 +376,7 @@ func AssertContains(t testRunner, object, element interface{}, msg ...interface{ // Example: // testza.AssertNotContains(t, []string{"Hello", "World"}, "Spaceship") // testza.AssertNotContains(t, "Hello, World!", "Spaceship") -func AssertNotContains(t testRunner, object, element interface{}, msg ...interface{}) { +func AssertNotContains(t testRunner, object, element any, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -395,7 +398,7 @@ func AssertNotContains(t testRunner, object, element interface{}, msg ...interfa // // ... // panic("some panic") // }) // => PASS -func AssertPanics(t testRunner, f func(), msg ...interface{}) { +func AssertPanics(t testRunner, f func(), msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -417,7 +420,7 @@ func AssertPanics(t testRunner, f func(), msg ...interface{}) { // testza.AssertNotPanics(t, func() { // // some code that does not call a panic... // }) // => PASS -func AssertNotPanics(t testRunner, f func(), msg ...interface{}) { +func AssertNotPanics(t testRunner, f func(), msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -437,7 +440,7 @@ func AssertNotPanics(t testRunner, f func(), msg ...interface{}) { // // Example: // testza.AssertNil(t, nil) -func AssertNil(t testRunner, object interface{}, msg ...interface{}) { +func AssertNil(t testRunner, object any, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -455,7 +458,7 @@ func AssertNil(t testRunner, object interface{}, msg ...interface{}) { // testza.AssertNotNil(t, true) // testza.AssertNotNil(t, "Hello, World!") // testza.AssertNotNil(t, 0) -func AssertNotNil(t testRunner, object interface{}, msg ...interface{}) { +func AssertNotNil(t testRunner, object any, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -477,7 +480,7 @@ func AssertNotNil(t testRunner, object interface{}, msg ...interface{}) { // testza.AssertCompletesIn(t, 2 * time.Second, func() { // // some code that should take less than 2 seconds... // }) // => PASS -func AssertCompletesIn(t testRunner, duration time.Duration, f func(), msg ...interface{}) { +func AssertCompletesIn(t testRunner, duration time.Duration, f func(), msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -501,7 +504,7 @@ func AssertCompletesIn(t testRunner, duration time.Duration, f func(), msg ...in // // some code that should take more than 2 seconds... // time.Sleep(3 * time.Second) // }) // => PASS -func AssertNotCompletesIn(t testRunner, duration time.Duration, f func(), msg ...interface{}) { +func AssertNotCompletesIn(t testRunner, duration time.Duration, f func(), msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -518,7 +521,7 @@ func AssertNotCompletesIn(t testRunner, duration time.Duration, f func(), msg .. // Example: // err := nil // testza.AssertNoError(t, err) -func AssertNoError(t testRunner, err error, msg ...interface{}) { +func AssertNoError(t testRunner, err error, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -548,7 +551,7 @@ func AssertNoError(t testRunner, err error, msg ...interface{}) { // Example: // testza.AssertGreater(t, 5, 1) // testza.AssertGreater(t, 10, -10) -func AssertGreater(t testRunner, object1, object2 interface{}, msg ...interface{}) { +func AssertGreater(t testRunner, object1, object2 any, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -597,7 +600,7 @@ func AssertGreaterOrEqual(t testRunner, object1, object2 interface{}, msg ...int // Example: // testza.AssertLess(t, 1, 5) // testza.AssertLess(t, -10, 10) -func AssertLess(t testRunner, object1, object2 interface{}, msg ...interface{}) { +func AssertLess(t testRunner, object1, object2 any, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -659,7 +662,7 @@ func AssertLessOrEqual(t testRunner, object1, object2 interface{}, msg ...interf // // ... // t.Fail() // Or any other failing method. // }) // => Pass -func AssertTestFails(t testRunner, test func(t TestingPackageWithFailFunctions), msg ...interface{}) { +func AssertTestFails(t testRunner, test func(t TestingPackageWithFailFunctions), msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -680,7 +683,7 @@ func AssertTestFails(t testRunner, test func(t TestingPackageWithFailFunctions), // var testErr = errors.New("hello world") // var testErrWrapped = fmt.Errorf("test err: %w", testErr) // testza.AssertErrorIs(t, testErrWrapped ,testErr) -func AssertErrorIs(t testRunner, err, target error, msg ...interface{}) { +func AssertErrorIs(t testRunner, err, target error, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -699,7 +702,7 @@ func AssertErrorIs(t testRunner, err, target error, msg ...interface{}) { // var test2Err = errors.New("hello world 2") // var testErrWrapped = fmt.Errorf("test err: %w", testErr) // testza.AssertNotErrorIs(t, testErrWrapped, test2Err) -func AssertNotErrorIs(t testRunner, err, target error, msg ...interface{}) { +func AssertNotErrorIs(t testRunner, err, target error, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -718,7 +721,7 @@ func AssertNotErrorIs(t testRunner, err, target error, msg ...interface{}) { // testza.AssertLen(t, "Assert", 6) // testza.AssertLen(t, []int{1, 2, 1337, 25}, 4) // testza.AssertLen(t, map[string]int{"asd": 1, "test": 1337}, 2) -func AssertLen(t testRunner, object interface{}, length int, msg ...interface{}) { +func AssertLen(t testRunner, object any, length int, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -761,7 +764,7 @@ func AssertLen(t testRunner, object interface{}, length int, msg ...interface{}) // Example: // testza.AssertIncreasing(t, []int{1, 2, 137, 1000}) // testza.AssertIncreasing(t, []float32{-10.3, 0.1, 7, 13.5}) -func AssertIncreasing(t testRunner, object interface{}, msg ...interface{}) { +func AssertIncreasing(t testRunner, object any, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -779,7 +782,7 @@ func AssertIncreasing(t testRunner, object interface{}, msg ...interface{}) { // Example: // testza.AssertDecreasing(t, []int{1000, 137, 2, 1}) // testza.AssertDecreasing(t, []float32{13.5, 7, 0.1, -10.3}) -func AssertDecreasing(t testRunner, object interface{}, msg ...interface{}) { +func AssertDecreasing(t testRunner, object any, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -793,7 +796,7 @@ func AssertDecreasing(t testRunner, object interface{}, msg ...interface{}) { // // Example: // testza.AssertRegexp(t, "^a.*c$", "abc") -func AssertRegexp(t testRunner, regex interface{}, txt interface{}, msg ...interface{}) { +func AssertRegexp(t testRunner, regex any, txt any, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -807,7 +810,7 @@ func AssertRegexp(t testRunner, regex interface{}, txt interface{}, msg ...inter // // Example: // testza.AssertNotRegexp(t, "ab.*", "Hello, World!") -func AssertNotRegexp(t testRunner, regex interface{}, txt interface{}, msg ...interface{}) { +func AssertNotRegexp(t testRunner, regex any, txt any, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -822,7 +825,7 @@ func AssertNotRegexp(t testRunner, regex interface{}, txt interface{}, msg ...in // Example: // testza.AssertFileExists(t, "./test.txt") // testza.AssertFileExists(t, "./config.yaml", "the config file is missing") -func AssertFileExists(t testRunner, file string, msg ...interface{}) { +func AssertFileExists(t testRunner, file string, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -833,7 +836,7 @@ func AssertFileExists(t testRunner, file string, msg ...interface{}) { } } -func AssertNoFileExists(t testRunner, file string, msg ...interface{}) { +func AssertNoFileExists(t testRunner, file string, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -852,7 +855,7 @@ func AssertNoFileExists(t testRunner, file string, msg ...interface{}) { // // Example: // testza.AssertDirExists(t, "FolderName") -func AssertDirExists(t testRunner, dir string, msg ...interface{}) { +func AssertDirExists(t testRunner, dir string, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -872,7 +875,7 @@ func AssertDirExists(t testRunner, dir string, msg ...interface{}) { // // Example: // testza.AssertNoDirExists(t, "FolderName") -func AssertNoDirExists(t testRunner, dir string, msg ...interface{}) { +func AssertNoDirExists(t testRunner, dir string, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -893,7 +896,7 @@ func AssertNoDirExists(t testRunner, dir string, msg ...interface{}) { // // Example: // testza.AssertDirEmpty(t, "FolderName") -func AssertDirEmpty(t testRunner, dir string, msg ...interface{}) { +func AssertDirEmpty(t testRunner, dir string, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -910,7 +913,7 @@ func AssertDirEmpty(t testRunner, dir string, msg ...interface{}) { // // Example: // testza.AssertDirNotEmpty(t, "FolderName") -func AssertDirNotEmpty(t testRunner, dir string, msg ...interface{}) { +func AssertDirNotEmpty(t testRunner, dir string, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -934,7 +937,7 @@ func AssertDirNotEmpty(t testRunner, dir string, msg ...interface{}) { // a string // } // testza.AssertSameElements(t, []*A{{a: "A"}, {a: "B"}, {a: "C"}}, []*A{{a: "A"}, {a: "B"}, {a: "C"}}) -func AssertSameElements(t testRunner, expected interface{}, actual interface{}, msg ...interface{}) { +func AssertSameElements(t testRunner, expected any, actual any, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -957,7 +960,7 @@ func AssertSameElements(t testRunner, expected interface{}, actual interface{}, // a string // } // testza.AssertNotSameElements(t, []*A{{a: "A"}, {a: "B"}, {a: "C"}}, []*A{{a: "A"}, {a: "B"}, {a: "C"}, {a: "D"}}) -func AssertNotSameElements(t testRunner, expected interface{}, actual interface{}, msg ...interface{}) { +func AssertNotSameElements(t testRunner, expected any, actual any, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -975,7 +978,7 @@ func AssertNotSameElements(t testRunner, expected interface{}, actual interface{ // Example: // testza.AssertSubset(t, []int{1, 2, 3}, []int{1, 2}) // testza.AssertSubset(t, []string{"Hello", "World", "Test"}, []string{"Test", "World"}) -func AssertSubset(t testRunner, list interface{}, subset interface{}, msg ...interface{}) { +func AssertSubset(t testRunner, list any, subset any, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } @@ -993,7 +996,7 @@ func AssertSubset(t testRunner, list interface{}, subset interface{}, msg ...int // Example: // testza.AssertNoSubset(t, []int{1, 2, 3}, []int{1, 7}) // testza.AssertNoSubset(t, []string{"Hello", "World", "Test"}, []string{"Test", "John"}) -func AssertNoSubset(t testRunner, list interface{}, subset interface{}, msg ...interface{}) { +func AssertNoSubset(t testRunner, list any, subset any, msg ...any) { if test, ok := t.(helper); ok { test.Helper() } diff --git a/assert_test.go b/assert_test.go index 2efe3a14..a187ff5e 100644 --- a/assert_test.go +++ b/assert_test.go @@ -14,10 +14,8 @@ import ( "testing" "time" - "github.com/pterm/pterm" - "github.com/stretchr/testify/assert" - . "github.com/MarvinJWendt/testza" + "github.com/pterm/pterm" ) type testMock struct { @@ -25,17 +23,17 @@ type testMock struct { ErrorMessage string } -func (m *testMock) fail(msg ...interface{}) { +func (m *testMock) fail(msg ...any) { m.ErrorCalled = true m.ErrorMessage = fmt.Sprint(msg...) } -func (m *testMock) Error(args ...interface{}) { +func (m *testMock) Error(args ...any) { m.fail(args...) } // Errorf is a mock of testing.T. -func (m *testMock) Errorf(format string, args ...interface{}) { +func (m *testMock) Errorf(format string, args ...any) { m.fail(fmt.Sprintf(format, args...)) } @@ -50,12 +48,12 @@ func (m *testMock) FailNow() { } // Fatal is a mock of testing.T. -func (m *testMock) Fatal(args ...interface{}) { +func (m *testMock) Fatal(args ...any) { m.fail(args...) } // Fatalf is a mock of testing.T. -func (m *testMock) Fatalf(format string, args ...interface{}) { +func (m *testMock) Fatalf(format string, args ...any) { m.fail(fmt.Sprintf(format, args...)) } @@ -71,7 +69,7 @@ type assertionTestStructNested struct { } func randomString() string { - return FuzzInputStringGenerateRandom(1, rand.Intn(10))[0] + return FuzzStringGenerateRandom(1, rand.Intn(10))[0] } func generateStruct() (ret assertionTestStruct) { @@ -83,7 +81,7 @@ func generateStruct() (ret assertionTestStruct) { return } -func testEqual(t *testing.T, expected, actual interface{}) { +func testEqual(t *testing.T, expected, actual any) { t.Helper() t.Run("Equal", func(t *testing.T) { @@ -102,7 +100,7 @@ func testEqual(t *testing.T, expected, actual interface{}) { func TestAssertKindOf(t *testing.T) { tests := []struct { kind reflect.Kind - value interface{} + value any }{ {kind: reflect.String, value: "Hello, World!"}, {kind: reflect.Int, value: 1337}, @@ -128,7 +126,7 @@ func TestAssertKindOf(t *testing.T) { func TestAssertKindOf_fails(t *testing.T) { tests := []struct { kind reflect.Kind - value interface{} + value any }{ {kind: reflect.Int, value: "Hello, World!"}, {kind: reflect.Bool, value: 1337}, @@ -156,7 +154,7 @@ func TestAssertKindOf_fails(t *testing.T) { func TestAssertNotKindOf(t *testing.T) { tests := []struct { kind reflect.Kind - value interface{} + value any }{ {kind: reflect.Int, value: "Hello, World!"}, {kind: reflect.Bool, value: 1337}, @@ -182,7 +180,7 @@ func TestAssertNotKindOf(t *testing.T) { func TestAssertNotKindOf_fails(t *testing.T) { tests := []struct { kind reflect.Kind - value interface{} + value any }{ {kind: reflect.String, value: "Hello, World!"}, {kind: reflect.Int, value: 1337}, @@ -208,7 +206,7 @@ func TestAssertNotKindOf_fails(t *testing.T) { } func TestAssertNumeric(t *testing.T) { - var numbers []interface{} + var numbers []any for i := 0; i < 10; i++ { numbers = append(numbers, @@ -239,7 +237,7 @@ func TestAssertNumeric(t *testing.T) { } func TestAssertNumeric_fails(t *testing.T) { - noNumbers := []interface{}{"Hello, World!", true, false} + noNumbers := []any{"Hello, World!", true, false} for _, number := range noNumbers { t.Run(pterm.Sprintf("Type=%s;Value=%#v", reflect.TypeOf(number).Kind().String(), number), func(t *testing.T) { AssertTestFails(t, func(t TestingPackageWithFailFunctions) { @@ -250,7 +248,7 @@ func TestAssertNumeric_fails(t *testing.T) { } func TestAssertNotNumeric_fails(t *testing.T) { - var numbers []interface{} + var numbers []any for i := 0; i < 10; i++ { numbers = append(numbers, @@ -283,7 +281,7 @@ func TestAssertNotNumeric_fails(t *testing.T) { } func TestAssertZero(t *testing.T) { - var zeroValues []interface{} + var zeroValues []any zeroValues = append(zeroValues, 0, "", false, nil) for i, value := range zeroValues { @@ -294,10 +292,10 @@ func TestAssertZero(t *testing.T) { } func TestAssertZero_fails(t *testing.T) { - var zeroValues []interface{} - zeroValues = append(zeroValues, true, "asd", 123, 1.5, 'a') + var nonZeroValues []any + nonZeroValues = append(nonZeroValues, true, "asd", 123, 1.5, 'a') - for i, value := range zeroValues { + for i, value := range nonZeroValues { t.Run(fmt.Sprint(i), func(t *testing.T) { AssertTestFails(t, func(t TestingPackageWithFailFunctions) { AssertZero(t, value) @@ -307,10 +305,10 @@ func TestAssertZero_fails(t *testing.T) { } func TestAssertNotZero(t *testing.T) { - var zeroValues []interface{} - zeroValues = append(zeroValues, true, "asd", 123, 1.5, 'a') + var nonZeroValues []any + nonZeroValues = append(nonZeroValues, true, "asd", 123, 1.5, 'a') - for i, value := range zeroValues { + for i, value := range nonZeroValues { t.Run(fmt.Sprint(i), func(t *testing.T) { AssertNotZero(t, value) }) @@ -318,7 +316,7 @@ func TestAssertNotZero(t *testing.T) { } func TestAssertNotZero_fails(t *testing.T) { - var zeroValues []interface{} + var zeroValues []any zeroValues = append(zeroValues, 0, "", false, nil) for i, value := range zeroValues { @@ -332,7 +330,7 @@ func TestAssertNotZero_fails(t *testing.T) { func TestAssertEqual(t *testing.T) { t.Run("String", func(t *testing.T) { - FuzzInputStringRunTests(t, FuzzInputStringFull(), func(t *testing.T, index int, str string) { + FuzzUtilRunTests(t, FuzzStringFull(), func(t *testing.T, index int, str string) { t.Helper() testEqual(t, str, str) @@ -365,7 +363,7 @@ func TestAssertEqual(t *testing.T) { func TestAssertEqual_fails(t *testing.T) { t.Run("String", func(t *testing.T) { - FuzzInputStringRunTests(t, FuzzInputStringFull(), func(t *testing.T, index int, str string) { + FuzzUtilRunTests(t, FuzzStringFull(), func(t *testing.T, index int, str string) { AssertTestFails(t, func(t TestingPackageWithFailFunctions) { AssertEqual(t, str, str+" addon") }) @@ -394,7 +392,7 @@ func TestAssertEqual_fails(t *testing.T) { func TestAssertNotEqual(t *testing.T) { t.Run("String", func(t *testing.T) { - FuzzInputStringRunTests(t, FuzzInputStringFull(), func(t *testing.T, index int, str string) { + FuzzUtilRunTests(t, FuzzStringFull(), func(t *testing.T, index int, str string) { AssertNotEqual(t, str, str+" addon") }) }) @@ -417,7 +415,7 @@ func TestAssertNotEqual(t *testing.T) { func TestAssertNotEqual_fails(t *testing.T) { t.Run("String", func(t *testing.T) { - FuzzInputStringRunTests(t, FuzzInputStringFull(), func(t *testing.T, index int, str string) { + FuzzUtilRunTests(t, FuzzStringFull(), func(t *testing.T, index int, str string) { t.Helper() AssertTestFails(t, func(t TestingPackageWithFailFunctions) { @@ -460,7 +458,7 @@ func TestAssertNotEqual_fails(t *testing.T) { func TestAssertEqualValues(t *testing.T) { t.Run("String", func(t *testing.T) { - FuzzInputStringRunTests(t, FuzzInputStringFull(), func(t *testing.T, index int, str string) { + FuzzUtilRunTests(t, FuzzStringFull(), func(t *testing.T, index int, str string) { AssertEqualValues(t, str, str) }) }) @@ -468,7 +466,7 @@ func TestAssertEqualValues(t *testing.T) { func TestAssertEqualValues_fails(t *testing.T) { t.Run("String", func(t *testing.T) { - FuzzInputStringRunTests(t, FuzzInputStringFull(), func(t *testing.T, index int, str string) { + FuzzUtilRunTests(t, FuzzStringFull(), func(t *testing.T, index int, str string) { AssertTestFails(t, func(t TestingPackageWithFailFunctions) { AssertEqualValues(t, str, str+" addon") }) @@ -478,7 +476,7 @@ func TestAssertEqualValues_fails(t *testing.T) { func TestAssertNotEqualValues(t *testing.T) { t.Run("String", func(t *testing.T) { - FuzzInputStringRunTests(t, FuzzInputStringFull(), func(t *testing.T, index int, str string) { + FuzzUtilRunTests(t, FuzzStringFull(), func(t *testing.T, index int, str string) { AssertNotEqualValues(t, str, str+" addon") }) }) @@ -486,7 +484,7 @@ func TestAssertNotEqualValues(t *testing.T) { func TestAssertNotEqualValues_fails(t *testing.T) { t.Run("String", func(t *testing.T) { - FuzzInputStringRunTests(t, FuzzInputStringFull(), func(t *testing.T, index int, str string) { + FuzzUtilRunTests(t, FuzzStringFull(), func(t *testing.T, index int, str string) { AssertTestFails(t, func(t TestingPackageWithFailFunctions) { AssertNotEqualValues(t, str, str) }) @@ -630,7 +628,7 @@ func TestAssertNil_pointer(t *testing.T) { func TestAssertNil_interface(t *testing.T) { var i *int = nil - var a interface{} = i + var a any = i AssertNil(t, a) } @@ -640,7 +638,7 @@ func TestAssertNil_pointer_struct(t *testing.T) { } func TestAssertNil_fails(t *testing.T) { - objectsNotNil := []interface{}{"asd", 0, false, true, 'c', "", []int{1, 2, 3}} + objectsNotNil := []any{"asd", 0, false, true, 'c', "", []int{1, 2, 3}} for _, v := range objectsNotNil { t.Run(pterm.Sprintf("Value=%#v", v), func(t *testing.T) { @@ -652,7 +650,7 @@ func TestAssertNil_fails(t *testing.T) { } func TestAssertNotNil(t *testing.T) { - objectsNotNil := []interface{}{"asd", 0, false, true, 'c', "", []int{1, 2, 3}} + objectsNotNil := []any{"asd", 0, false, true, 'c', "", []int{1, 2, 3}} for _, v := range objectsNotNil { t.Run(pterm.Sprintf("Value=%#v", v), func(t *testing.T) { @@ -725,18 +723,18 @@ func TestAssertGreater_fails(t *testing.T) { } func TestAssertGreaterOrEqual(t *testing.T) { - assert.GreaterOrEqual(t, 2, 1) - assert.GreaterOrEqual(t, 5, 4) - assert.GreaterOrEqual(t, 5, 5) + AssertGreaterOrEqual(t, 2, 1) + AssertGreaterOrEqual(t, 5, 4) + AssertGreaterOrEqual(t, 5, 5) } func TestAssertGreaterOrEqual_fails(t *testing.T) { AssertTestFails(t, func(t TestingPackageWithFailFunctions) { - assert.GreaterOrEqual(t, 1, 2) + AssertGreaterOrEqual(t, 1, 2) }) AssertTestFails(t, func(t TestingPackageWithFailFunctions) { - assert.GreaterOrEqual(t, 4, 5) + AssertGreaterOrEqual(t, 4, 5) }) } @@ -859,7 +857,7 @@ func TestAssertNotErrorIs_fails(t *testing.T) { func TestAssertLen(t *testing.T) { tests := []struct { - object interface{} + object any expLen int }{ {object: "", expLen: 0}, @@ -878,7 +876,7 @@ func TestAssertLen(t *testing.T) { func TestAssertLen_fails(t *testing.T) { tests := []struct { - object interface{} + object any expLen int }{ {object: "", expLen: 1}, @@ -890,7 +888,7 @@ func TestAssertLen_fails(t *testing.T) { {object: 1, expLen: 4}, {object: 0.1, expLen: 4}, {object: generateStruct(), expLen: 1}, - {object: interface{}(nil), expLen: 1}, + {object: any(nil), expLen: 1}, {object: true, expLen: 0}, {object: uint(137), expLen: 0}, } @@ -904,7 +902,7 @@ func TestAssertLen_fails(t *testing.T) { } func TestAssertLen_full(t *testing.T) { - FuzzInputStringRunTests(t, FuzzInputStringFull(), func(t *testing.T, index int, str string) { + FuzzUtilRunTests(t, FuzzStringFull(), func(t *testing.T, index int, str string) { AssertLen(t, str, len(str)) }) } @@ -912,7 +910,7 @@ func TestAssertLen_full(t *testing.T) { func TestAssertIsIncreasing(t *testing.T) { tests := []struct { name string - object interface{} + object any }{ {name: "int", object: []int{1, 2, 3, 4, 5, 6}}, {name: "int2", object: []int{1, 7, 10, 11, 134, 700432}}, @@ -939,7 +937,7 @@ func TestAssertIsIncreasing(t *testing.T) { func TestAssertIsIncreasing_fail(t *testing.T) { tests := []struct { name string - object interface{} + object any }{ {name: "int_empty", object: []int{}}, {name: "int_one", object: []int{4}}, @@ -973,7 +971,7 @@ func TestAssertIsIncreasing_fail(t *testing.T) { func TestAssertIsDecreasing(t *testing.T) { tests := []struct { name string - object interface{} + object any }{ {name: "int", object: []int{6, 5, 4, 3, 2, 1}}, {name: "int2", object: []int{700432, 134, 11, 10, 7, 1}}, @@ -1000,7 +998,7 @@ func TestAssertIsDecreasing(t *testing.T) { func TestAssertIsDecreasing_fail(t *testing.T) { tests := []struct { name string - object interface{} + object any }{ {name: "int_empty", object: []int{}}, {name: "int_one", object: []int{4}}, @@ -1033,7 +1031,7 @@ func TestAssertIsDecreasing_fail(t *testing.T) { func TestAssertSameElements(t *testing.T) { t.Run("String", func(t *testing.T) { - FuzzInputStringRunTests(t, FuzzInputStringFull(), func(t *testing.T, index int, str string) { + FuzzUtilRunTests(t, FuzzStringFull(), func(t *testing.T, index int, str string) { t.Helper() testEqual(t, str, str) @@ -1083,7 +1081,7 @@ func TestAssertSameElements(t *testing.T) { func TestAssertSameElementsFails(t *testing.T) { t.Run("Not an array", func(t *testing.T) { - FuzzInputStringRunTests(t, FuzzInputStringFull(), func(t *testing.T, index int, str string) { + FuzzUtilRunTests(t, FuzzStringFull(), func(t *testing.T, index int, str string) { AssertTestFails(t, func(t TestingPackageWithFailFunctions) { AssertSameElements(t, str, str) }) diff --git a/capture.go b/capture.go index 5b85a87f..6864196c 100644 --- a/capture.go +++ b/capture.go @@ -3,7 +3,6 @@ package testza import ( "fmt" "io" - "io/ioutil" "os" ) @@ -35,7 +34,7 @@ func CaptureStdout(capture func(w io.Writer) error) (string, error) { if err != nil { return "", fmt.Errorf("could not capture stdout: %w", err) } - out, err := ioutil.ReadAll(r) + out, err := io.ReadAll(r) if err != nil { return "", fmt.Errorf("could not capture stdout: %w", err) } @@ -77,7 +76,7 @@ func CaptureStderr(capture func(w io.Writer) error) (string, error) { if err != nil { return "", fmt.Errorf("could not capture stderr: %w", err) } - out, err := ioutil.ReadAll(r) + out, err := io.ReadAll(r) if err != nil { return "", fmt.Errorf("could not capture stderr: %w", err) } @@ -135,12 +134,12 @@ func CaptureStdoutAndStderr(capture func(stdoutWriter, stderrWriter io.Writer) e return "", "", fmt.Errorf("could not capture stdout or stderr: %w", err) } - stdoutOut, err := ioutil.ReadAll(stdoutR) + stdoutOut, err := io.ReadAll(stdoutR) if err != nil { return "", "", fmt.Errorf("could not capture stdout or stderr: %w", err) } - stderrOut, err := ioutil.ReadAll(stderrR) + stderrOut, err := io.ReadAll(stderrR) if err != nil { return "", "", fmt.Errorf("could not capture stdout or stderr: %w", err) } diff --git a/ci/main.go b/ci/main.go index a085e3b5..f77112db 100644 --- a/ci/main.go +++ b/ci/main.go @@ -19,10 +19,11 @@ var Categories = []Category{ {Name: "Settings", Prefix: "Set"}, {Name: "Assert", Prefix: "Assert"}, {Name: "Capture", Prefix: "Capture"}, - {Name: "Fuzz Booleans", Prefix: "FuzzInputBool"}, - {Name: "Fuzz Strings", Prefix: "FuzzInputString"}, - {Name: "Fuzz Float64s", Prefix: "FuzzInputFloat64"}, - {Name: "Fuzz Integers", Prefix: "FuzzInputInt"}, + {Name: "Fuzz Utils", Prefix: "FuzzUtil"}, + {Name: "Fuzz Booleans", Prefix: "FuzzBool"}, + {Name: "Fuzz Strings", Prefix: "FuzzString"}, + {Name: "Fuzz Float64s", Prefix: "FuzzFloat64"}, + {Name: "Fuzz Integers", Prefix: "FuzzInt"}, {Name: "Snapshot", Prefix: "Snapshot"}, } diff --git a/configuration_test.go b/configuration_test.go index c10c5934..76162dc3 100644 --- a/configuration_test.go +++ b/configuration_test.go @@ -35,7 +35,7 @@ func TestSetLineNumbersEnabled(t *testing.T) { func TestSetRandomSeed(t *testing.T) { SetRandomSeed(1337) AssertEqual(t, int64(1337), randomSeed) - AssertEqual(t, "4U390O49B9", FuzzInputStringGenerateRandom(1, 10)[0]) + AssertEqual(t, "4U390O49B9", FuzzStringGenerateRandom(1, 10)[0]) un := time.Now().UnixNano() SetRandomSeed(un) AssertEqual(t, un, randomSeed) diff --git a/fuzz-bools.go b/fuzz-bools.go index 45eb5b19..2a15d5b5 100644 --- a/fuzz-bools.go +++ b/fuzz-bools.go @@ -1,58 +1,6 @@ package testza -import ( - "fmt" - "testing" - - "github.com/MarvinJWendt/testza/internal" -) - -// FuzzInputBoolFull returns true and false in a boolean slice. -func FuzzInputBoolFull() []bool { +// FuzzBoolFull returns true and false in a boolean slice. +func FuzzBoolFull() []bool { return []bool{true, false} } - -// FuzzInputBoolRunTests runs a test for every value in a testset. -// 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. -// -// Example: -// testza.FuzzInputBoolRunTests(t, testza.FuzzInputBoolFull(), func(t *testing.T, index int, b bool) { -// // Test logic -// // err := YourFunction(b) -// // testza.AssertNoError(t, err) -// // ... -// }) -func FuzzInputBoolRunTests(t testRunner, testSet []bool, testFunc func(t *testing.T, index int, f bool)) { - if test, ok := t.(helper); ok { - test.Helper() - } - - test := internal.GetTest(t) - if test == nil { - t.Error(internal.ErrCanNotRunIfNotBuiltinTesting) - return - } - - for i, v := range testSet { - test.Run(fmt.Sprint(v), func(t *testing.T) { - t.Helper() - - testFunc(t, i, v) - }) - } -} - -// FuzzInputBoolModify returns a modified version of a test set. -// -// Example: -// testset := testza.FuzzInputBoolModify(testza.FuzzInputBoolFull(), func(index int, value bool) bool { -// return !value -// }) -func FuzzInputBoolModify(inputSlice []bool, modifier func(index int, value bool) bool) (floats []bool) { - for i, input := range inputSlice { - floats = append(floats, modifier(i, input)) - } - - return -} diff --git a/fuzz-floats64.go b/fuzz-floats64.go index 583c37e6..ce2bcb4a 100644 --- a/fuzz-floats64.go +++ b/fuzz-floats64.go @@ -1,28 +1,24 @@ package testza import ( - "fmt" "math" "math/rand" - "testing" - - "github.com/MarvinJWendt/testza/internal" ) -// FuzzInputFloat64Full returns a combination of every float64 testset and some random float64s (positive and negative). -func FuzzInputFloat64Full() (floats []float64) { +// FuzzFloat64Full returns a combination of every float64 testset and some random float64s (positive and negative). +func FuzzFloat64Full() (floats []float64) { for i := 0; i < 50; i++ { floats = append(floats, - FuzzInputFloat64GenerateRandomPositive(1, float64(i*1000))[0], - FuzzInputFloat64GenerateRandomNegative(1, float64(i*1000*-1))[0], + FuzzFloat64GenerateRandomPositive(1, float64(i*1000))[0], + FuzzFloat64GenerateRandomNegative(1, float64(i*1000*-1))[0], ) } return } -// FuzzInputFloat64GenerateRandomRange generates random positive integers with a maximum of max. +// FuzzFloat64GenerateRandomRange generates random positive integers with a maximum of max. // If the maximum is 0, or below, the maximum will be set to math.MaxInt64. -func FuzzInputFloat64GenerateRandomRange(count int, min, max float64) (floats []float64) { +func FuzzFloat64GenerateRandomRange(count int, min, max float64) (floats []float64) { for i := 0; i < count; i++ { floats = append(floats, min+rand.Float64()*(max-min)) } @@ -30,74 +26,29 @@ func FuzzInputFloat64GenerateRandomRange(count int, min, max float64) (floats [] return } -// FuzzInputFloat64GenerateRandomPositive generates random positive integers with a maximum of max. +// FuzzFloat64GenerateRandomPositive generates random positive integers with a maximum of max. // If the maximum is 0, or below, the maximum will be set to math.MaxInt64. -func FuzzInputFloat64GenerateRandomPositive(count int, max float64) (floats []float64) { +func FuzzFloat64GenerateRandomPositive(count int, max float64) (floats []float64) { if max <= 0 { max = math.MaxFloat64 } - floats = append(floats, FuzzInputFloat64GenerateRandomRange(count, 0, max)...) + floats = append(floats, FuzzFloat64GenerateRandomRange(count, 0, max)...) return } -// FuzzInputFloat64GenerateRandomNegative generates random negative integers with a minimum of min. +// FuzzFloat64GenerateRandomNegative generates random negative integers with a minimum of min. // If the minimum is positive, it will be converted to a negative number. // If it is set to 0, there is no limit. -func FuzzInputFloat64GenerateRandomNegative(count int, min float64) (floats []float64) { +func FuzzFloat64GenerateRandomNegative(count int, min float64) (floats []float64) { if min > 0 { min *= -1 } else if min == 0 { min = math.MaxFloat64 * -1 } - floats = append(floats, FuzzInputFloat64GenerateRandomRange(count, min, 0)...) - - return -} - -// FuzzInputFloat64RunTests runs a test for every value in a testset. -// 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. -// -// Example: -// testza.FuzzInputFloat64RunTests(t, testza.FuzzInputFloat64Full(), func(t *testing.T, index int, f float64) { -// // Test logic -// // err := YourFunction(f) -// // testza.AssertNoError(t, err) -// // ... -// }) -func FuzzInputFloat64RunTests(t testRunner, testSet []float64, testFunc func(t *testing.T, index int, f float64)) { - if test, ok := t.(helper); ok { - test.Helper() - } - - test := internal.GetTest(t) - if test == nil { - t.Error(internal.ErrCanNotRunIfNotBuiltinTesting) - return - } - - for i, v := range testSet { - test.Run(fmt.Sprint(v), func(t *testing.T) { - t.Helper() - - testFunc(t, i, v) - }) - } -} - -// FuzzInputFloat64Modify returns a modified version of a test set. -// -// Example: -// testset := testza.FuzzInputFloat64Modify(testza.FuzzInputFloat64Full(), func(index int, value float64) float64 { -// return value * 2 -// }) -func FuzzInputFloat64Modify(inputSlice []float64, modifier func(index int, value float64) float64) (floats []float64) { - for i, input := range inputSlice { - floats = append(floats, modifier(i, input)) - } + floats = append(floats, FuzzFloat64GenerateRandomRange(count, min, 0)...) return } diff --git a/fuzz-ints.go b/fuzz-ints.go index a00c1865..f84f3b25 100644 --- a/fuzz-ints.go +++ b/fuzz-ints.go @@ -1,27 +1,23 @@ package testza import ( - "fmt" "math" "math/rand" - "testing" - - "github.com/MarvinJWendt/testza/internal" ) -// FuzzInputIntFull returns a combination of every integer testset and some random integers (positive and negative). -func FuzzInputIntFull() (ints []int) { +// FuzzIntFull returns a combination of every integer testset and some random integers (positive and negative). +func FuzzIntFull() (ints []int) { for i := 0; i < 50; i++ { ints = append(ints, - FuzzInputIntGenerateRandomPositive(1, i*1000)[0], - FuzzInputIntGenerateRandomNegative(1, i*1000*-1)[0], + FuzzIntGenerateRandomPositive(1, i*1000)[0], + FuzzIntGenerateRandomNegative(1, i*1000*-1)[0], ) } return } -// FuzzInputIntGenerateRandomRange generates random integers with a range of min to max. -func FuzzInputIntGenerateRandomRange(count, min, max int) (ints []int) { +// FuzzIntGenerateRandomRange generates random integers with a range of min to max. +func FuzzIntGenerateRandomRange(count, min, max int) (ints []int) { for i := 0; i < count; i++ { ints = append(ints, rand.Intn(max-min)+min) } @@ -29,28 +25,28 @@ func FuzzInputIntGenerateRandomRange(count, min, max int) (ints []int) { return } -// FuzzInputIntGenerateRandomPositive generates random positive integers with a maximum of max. +// FuzzIntGenerateRandomPositive generates random positive integers with a maximum of max. // If the maximum is 0, or below, the maximum will be set to math.MaxInt64. -func FuzzInputIntGenerateRandomPositive(count, max int) (ints []int) { +func FuzzIntGenerateRandomPositive(count, max int) (ints []int) { if max <= 0 { max = math.MaxInt64 } - ints = append(ints, FuzzInputIntGenerateRandomRange(count, 1, max)...) + ints = append(ints, FuzzIntGenerateRandomRange(count, 1, max)...) return } -// FuzzInputIntGenerateRandomNegative generates random negative integers with a minimum of min. +// FuzzIntGenerateRandomNegative generates random negative integers with a minimum of min. // If the minimum is 0, or above, the maximum will be set to math.MinInt64. -func FuzzInputIntGenerateRandomNegative(count, min int) (ints []int) { +func FuzzIntGenerateRandomNegative(count, min int) (ints []int) { if min >= 0 { min = math.MinInt64 } min = int(math.Abs(float64(min))) - randomPositives := FuzzInputIntGenerateRandomPositive(count, min) + randomPositives := FuzzIntGenerateRandomPositive(count, min) for _, p := range randomPositives { ints = append(ints, p*-1) @@ -58,48 +54,3 @@ func FuzzInputIntGenerateRandomNegative(count, min int) (ints []int) { return } - -// FuzzInputIntRunTests runs a test for every value in a testset. -// 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. -// -// Example: -// testza.FuzzInputIntRunTests(t, testza.FuzzInputIntFull(), func(t *testing.T, index int, i int) { -// // Test logic -// // err := YourFunction(i) -// // testza.AssertNoError(t, err) -// // ... -// }) -func FuzzInputIntRunTests(t testRunner, testSet []int, testFunc func(t *testing.T, index int, i int)) { - if test, ok := t.(helper); ok { - test.Helper() - } - - test := internal.GetTest(t) - if test == nil { - t.Error(internal.ErrCanNotRunIfNotBuiltinTesting) - return - } - - for i, v := range testSet { - test.Run(fmt.Sprint(v), func(t *testing.T) { - t.Helper() - - testFunc(t, i, v) - }) - } -} - -// FuzzInputIntModify returns a modified version of a test set. -// -// Example: -// testset := testza.FuzzInputIntModify(testza.FuzzInputIntFull(), func(index int, value int) int { -// return value * 2 -// }) -func FuzzInputIntModify(inputSlice []int, modifier func(index int, value int) int) (ints []int) { - for i, input := range inputSlice { - ints = append(ints, modifier(i, input)) - } - - return -} diff --git a/fuzz-strings.go b/fuzz-strings.go index 9c51e1fe..d261ea64 100644 --- a/fuzz-strings.go +++ b/fuzz-strings.go @@ -2,50 +2,47 @@ package testza import ( "math/rand" - "testing" - - "github.com/MarvinJWendt/testza/internal" ) -// FuzzInputStringEmpty returns a test set with a single empty string. -func FuzzInputStringEmpty() []string { +// FuzzStringEmpty returns a test set with a single empty string. +func FuzzStringEmpty() []string { return []string{""} } -// FuzzInputStringLong returns a test set with long random strings. +// FuzzStringLong returns a test set with long random strings. // Returns: // [0]: Random string (length: 25) // [1]: Random string (length: 50) // [2]: Random string (length: 100) // [3]: Random string (length: 1,000) // [4]: Random string (length: 100,000) -func FuzzInputStringLong() (testSet []string) { - testSet = append(testSet, FuzzInputStringGenerateRandom(1, 25)...) - testSet = append(testSet, FuzzInputStringGenerateRandom(1, 50)...) - testSet = append(testSet, FuzzInputStringGenerateRandom(1, 100)...) - testSet = append(testSet, FuzzInputStringGenerateRandom(1, 1_000)...) - testSet = append(testSet, FuzzInputStringGenerateRandom(1, 100_000)...) +func FuzzStringLong() (testSet []string) { + testSet = append(testSet, FuzzStringGenerateRandom(1, 25)...) + testSet = append(testSet, FuzzStringGenerateRandom(1, 50)...) + testSet = append(testSet, FuzzStringGenerateRandom(1, 100)...) + testSet = append(testSet, FuzzStringGenerateRandom(1, 1_000)...) + testSet = append(testSet, FuzzStringGenerateRandom(1, 100_000)...) return } -// FuzzInputStringNumeric returns a test set with strings that are numeric. +// FuzzStringNumeric returns a test set with strings that are numeric. // The highest number in here is "9223372036854775807", which is equal to the maxmim int64. -func FuzzInputStringNumeric() []string { +func FuzzStringNumeric() []string { positiveNumbers := []string{"0", "1", "2", "3", "100", "1.1", "1337", "13.37", "0.000000000001", "9223372036854775807"} - negativeNumbers := FuzzInputStringModify(positiveNumbers, func(index int, value string) string { return "-" + value }) + negativeNumbers := FuzzUtilModifySet(positiveNumbers, func(index int, value string) string { return "-" + value }) return append(positiveNumbers, negativeNumbers...) } -// FuzzInputStringUsernames returns a test set with usernames. -func FuzzInputStringUsernames() []string { +// FuzzStringUsernames returns a test set with usernames. +func FuzzStringUsernames() []string { return []string{"MarvinJWendt", "Zipper1337", "n00b", "l33t", "j0rgan", "test", "test123", "TEST", "test_", "TEST_"} } -// FuzzInputStringEmailAddresses returns a test set with valid email addresses. +// FuzzStringEmailAddresses returns a test set with valid email addresses. // The addresses may look like they are invalid, but they are all conform to RFC 2822 and could be used. // You can use this test set to test your email validation process. -func FuzzInputStringEmailAddresses() []string { +func FuzzStringEmailAddresses() []string { return []string{ "hello@world.com", "hello+world@example.com", @@ -88,13 +85,13 @@ func FuzzInputStringEmailAddresses() []string { } } -// FuzzInputStringHtmlTags returns a test set with different html tags. +// FuzzStringHtmlTags returns a test set with different html tags. // // Example: // - // - link -func FuzzInputStringHtmlTags() []string { +func FuzzStringHtmlTags() []string { return []string{ "", "