Skip to content

Commit

Permalink
#18 Add test for buildCondition {}
Browse files Browse the repository at this point in the history
  • Loading branch information
mvysny committed Jan 24, 2024
1 parent fb44b08 commit ad0dada
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
12 changes: 3 additions & 9 deletions src/main/kotlin/com/github/vokorm/ConditionBuilder.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -9,8 +8,8 @@ import com.gitlab.mvysny.jdbiorm.condition.Expression
/**
* Creates a [Condition] programmatically: `buildCondition { Person::age lt 25 }`
*/
public inline fun <reified T : Any> buildCondition(block: FilterBuilder<T>.() -> Filter<T>): Filter<T> =
block(FilterBuilder(T::class.java))
public inline fun <reified T : Any> buildCondition(block: ConditionBuilder<T>.() -> Condition): Condition =
block(ConditionBuilder(T::class.java))

/**
* Running block with this class as its receiver will allow you to write expressions like this:
Expand All @@ -21,7 +20,7 @@ public inline fun <reified T : Any> buildCondition(block: FilterBuilder<T>.() ->
*/
public class ConditionBuilder<T : Any>(public val clazz: Class<T>) {
/**
* 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 <R : Serializable?> KProperty1<T, R>.eq(value: R): Condition = toProperty(clazz).eq(value)

Expand Down Expand Up @@ -100,8 +99,3 @@ public class ConditionBuilder<T : Any>(public val clazz: Class<T>) {

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()
27 changes: 27 additions & 0 deletions src/test/kotlin/com/github/vokorm/ConditionBuilderTest.kt
Original file line number Diff line number Diff line change
@@ -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()
}
}
})

0 comments on commit ad0dada

Please sign in to comment.