diff --git a/src/main/kotlin/com/github/vokorm/ConditionBuilder.kt b/src/main/kotlin/com/github/vokorm/ConditionBuilder.kt index 736666a..2eff780 100644 --- a/src/main/kotlin/com/github/vokorm/ConditionBuilder.kt +++ b/src/main/kotlin/com/github/vokorm/ConditionBuilder.kt @@ -1,6 +1,5 @@ package com.github.vokorm -import com.github.mvysny.vokdataloader.* import com.gitlab.mvysny.jdbiorm.condition.Condition import java.io.Serializable import kotlin.reflect.KProperty1 @@ -9,8 +8,8 @@ import com.gitlab.mvysny.jdbiorm.condition.Expression /** * Creates a [Condition] programmatically: `buildCondition { Person::age lt 25 }` */ -public inline fun buildCondition(block: FilterBuilder.() -> Filter): Filter = - block(FilterBuilder(T::class.java)) +public inline fun buildCondition(block: ConditionBuilder.() -> Condition): Condition = + block(ConditionBuilder(T::class.java)) /** * Running block with this class as its receiver will allow you to write expressions like this: @@ -21,7 +20,7 @@ public inline fun buildCondition(block: FilterBuilder.() -> */ public class ConditionBuilder(public val clazz: Class) { /** - * Creates an condition where this property should be equal to [value]. Calls [Expression.eq]. + * Creates a condition where this property should be equal to [value]. Calls [Expression.eq]. */ public infix fun KProperty1.eq(value: R): Condition = toProperty(clazz).eq(value) @@ -100,8 +99,3 @@ public class ConditionBuilder(public val clazz: Class) { public infix fun Condition.and(other: Condition): Condition = and(other) public infix fun Condition.or(other: Condition): Condition = or(other) - -/** - * Returns a filter that represents the logical negation of this filter. - */ -public operator fun Condition.not(): Condition = not() diff --git a/src/test/kotlin/com/github/vokorm/ConditionBuilderTest.kt b/src/test/kotlin/com/github/vokorm/ConditionBuilderTest.kt new file mode 100644 index 0000000..ed7ca29 --- /dev/null +++ b/src/test/kotlin/com/github/vokorm/ConditionBuilderTest.kt @@ -0,0 +1,27 @@ +package com.github.vokorm + +import com.github.mvysny.dynatest.DynaTest +import kotlin.test.expect + +class ConditionBuilderTest : DynaTest({ + test("smoke API tests") { + buildCondition { Person::name eq "foo" } + buildCondition { !(Person::name eq "foo") } + buildCondition { (Person::name eq "foo") and (Person::id gt 25)} + buildCondition { (Person::name eq "foo") or (Person::id gt 25)} + } + test("produced condition") { + expect("Person.name = foo") { + buildCondition { Person::name eq "foo" } .toString() + } + expect("NOT(Person.name = foo)") { + buildCondition { !(Person::name eq "foo") } .toString() + } + expect("(Person.name = foo) AND (Person.id > 25)") { + buildCondition { (Person::name eq "foo") and (Person::id gt 25) } .toString() + } + expect("(Person.name = foo) OR (Person.id > 25)") { + buildCondition { (Person::name eq "foo") or (Person::id gt 25)} .toString() + } + } +})