mirror of
https://github.com/duke-git/lancet.git
synced 2025-10-08 08:50:14 +08:00
Add functional predicate (#171)
Enable the execution of assertion functions in a functional manner.
This commit is contained in:
75
function/predicate_test.go
Normal file
75
function/predicate_test.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package function
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/duke-git/lancet/v2/internal"
|
||||
)
|
||||
|
||||
func TestPredicatesNegatePure(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert := internal.NewAssert(t, "TestPredicatesNegatePure")
|
||||
|
||||
// Define some simple predicates for demonstration
|
||||
isUpperCase := func(s string) bool {
|
||||
return strings.ToUpper(s) == s
|
||||
}
|
||||
isLowerCase := func(s string) bool {
|
||||
return strings.ToLower(s) == s
|
||||
}
|
||||
isMixedCase := Negate(Or(isUpperCase, isLowerCase))
|
||||
|
||||
assert.ShouldBeFalse(isMixedCase("ABC"))
|
||||
assert.ShouldBeTrue(isMixedCase("AbC"))
|
||||
}
|
||||
|
||||
func TestPredicatesOrPure(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert := internal.NewAssert(t, "TestPredicatesOrPure")
|
||||
|
||||
containsDigitOrSpecialChar := Or(
|
||||
func(s string) bool { return strings.ContainsAny(s, "0123456789") },
|
||||
func(s string) bool { return strings.ContainsAny(s, "!@#$%") },
|
||||
)
|
||||
|
||||
assert.ShouldBeTrue(containsDigitOrSpecialChar("hello!"))
|
||||
assert.ShouldBeFalse(containsDigitOrSpecialChar("hello"))
|
||||
}
|
||||
|
||||
func TestPredicatesAndPure(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert := internal.NewAssert(t, "TestPredicatesAndPure")
|
||||
|
||||
isNumericAndLength5 := And(
|
||||
func(s string) bool { return strings.ContainsAny(s, "0123456789") },
|
||||
func(s string) bool { return len(s) == 5 },
|
||||
)
|
||||
|
||||
assert.ShouldBeTrue(isNumericAndLength5("12345"))
|
||||
assert.ShouldBeFalse(isNumericAndLength5("1234"))
|
||||
assert.ShouldBeFalse(isNumericAndLength5("abcde"))
|
||||
}
|
||||
|
||||
func TestPredicatesMix(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert := internal.NewAssert(t, "TestPredicatesMix")
|
||||
|
||||
a := Or(
|
||||
func(s string) bool { return strings.ContainsAny(s, "0123456789") },
|
||||
func(s string) bool { return strings.ContainsAny(s, "!") },
|
||||
)
|
||||
|
||||
b := And(
|
||||
func(s string) bool { return strings.ContainsAny(s, "hello") },
|
||||
func(s string) bool { return strings.ContainsAny(s, "!") },
|
||||
)
|
||||
|
||||
c := Negate(And(a, b))
|
||||
|
||||
assert.ShouldBeFalse(c("hello!"))
|
||||
}
|
Reference in New Issue
Block a user