-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support 'mod' arithmetic operator
- Loading branch information
Showing
9 changed files
with
254 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/expression/ModDslTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.linecorp.kotlinjdsl.dsl.jpql.expression | ||
|
||
import com.linecorp.kotlinjdsl.dsl.jpql.entity.book.Book | ||
import com.linecorp.kotlinjdsl.dsl.jpql.queryPart | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expression | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expressions | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.path.Paths | ||
import org.assertj.core.api.WithAssertions | ||
import org.junit.jupiter.api.Test | ||
import java.math.BigDecimal | ||
|
||
class ModDslTest : WithAssertions { | ||
private val bigDecimal1 = BigDecimal.valueOf(100) | ||
|
||
private val bigDecimalExpression1 = Expressions.value(bigDecimal1) | ||
|
||
@Test | ||
fun `mod() with a property and a bigDecimal`() { | ||
// when | ||
val expression1 = queryPart { | ||
path(Book::price).mod(bigDecimal1) | ||
} | ||
|
||
val expression2 = queryPart { | ||
mod(path(Book::price), bigDecimal1) | ||
} | ||
|
||
val actual1: Expression<BigDecimal> = expression1 // for type check | ||
val actual2: Expression<BigDecimal> = expression2 // for type check | ||
|
||
// then | ||
val expected1 = Expressions.mod( | ||
value1 = Paths.path(Book::price), | ||
value2 = Expressions.value(bigDecimal1), | ||
) | ||
|
||
val expected2 = Expressions.mod( | ||
value1 = Expressions.parentheses(Paths.path(Book::price)), | ||
value2 = Expressions.parentheses(Expressions.value(bigDecimal1)), | ||
) | ||
|
||
assertThat(actual1).isEqualTo(expected1) | ||
assertThat(actual2).isEqualTo(expected2) | ||
} | ||
|
||
@Test | ||
fun `mod() with a property and a bigDecimal expression`() { | ||
// when | ||
val expression1 = queryPart { | ||
path(Book::price).mod(bigDecimalExpression1) | ||
} | ||
|
||
val expression2 = queryPart { | ||
mod(path(Book::price), bigDecimalExpression1) | ||
} | ||
|
||
val actual1: Expression<BigDecimal> = expression1 // for type check | ||
val actual2: Expression<BigDecimal> = expression2 // for type check | ||
|
||
// then | ||
val expected1 = Expressions.mod( | ||
value1 = Paths.path(Book::price), | ||
value2 = bigDecimalExpression1, | ||
) | ||
|
||
val expected2 = Expressions.mod( | ||
value1 = Expressions.parentheses(Paths.path(Book::price)), | ||
value2 = Expressions.parentheses(bigDecimalExpression1), | ||
) | ||
|
||
assertThat(actual1).isEqualTo(expected1) | ||
assertThat(actual2).isEqualTo(expected2) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...pql/src/main/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/expression/impl/JpqlModulo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl | ||
|
||
import com.linecorp.kotlinjdsl.Internal | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expression | ||
|
||
/** | ||
* Expression that mods [value1] by [value2]. | ||
*/ | ||
@Internal | ||
data class JpqlModulo<T : Number> internal constructor( | ||
val value1: Expression<*>, | ||
val value2: Expression<*>, | ||
) : Expression<T> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...c/main/kotlin/com/linecorp/kotlinjdsl/render/jpql/serializer/impl/JpqlModuloSerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.linecorp.kotlinjdsl.render.jpql.serializer.impl | ||
|
||
import com.linecorp.kotlinjdsl.Internal | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlModulo | ||
import com.linecorp.kotlinjdsl.render.RenderContext | ||
import com.linecorp.kotlinjdsl.render.jpql.serializer.JpqlRenderSerializer | ||
import com.linecorp.kotlinjdsl.render.jpql.serializer.JpqlSerializer | ||
import com.linecorp.kotlinjdsl.render.jpql.writer.JpqlWriter | ||
import kotlin.reflect.KClass | ||
|
||
@Internal | ||
class JpqlModuloSerializer : JpqlSerializer<JpqlModulo<*>> { | ||
override fun handledType(): KClass<JpqlModulo<*>> { | ||
return JpqlModulo::class | ||
} | ||
|
||
override fun serialize(part: JpqlModulo<*>, writer: JpqlWriter, context: RenderContext) { | ||
val delegate = context.getValue(JpqlRenderSerializer) | ||
|
||
delegate.serialize(part.value1, writer, context) | ||
|
||
writer.write(" ") | ||
writer.write("%") | ||
writer.write(" ") | ||
|
||
delegate.serialize(part.value2, writer, context) | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...st/kotlin/com/linecorp/kotlinjdsl/render/jpql/serializer/impl/JpqlModuloSerializerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.linecorp.kotlinjdsl.render.jpql.serializer.impl | ||
|
||
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expressions | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlModulo | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.path.Paths | ||
import com.linecorp.kotlinjdsl.render.TestRenderContext | ||
import com.linecorp.kotlinjdsl.render.jpql.entity.book.Book | ||
import com.linecorp.kotlinjdsl.render.jpql.serializer.JpqlRenderSerializer | ||
import com.linecorp.kotlinjdsl.render.jpql.serializer.JpqlSerializerTest | ||
import com.linecorp.kotlinjdsl.render.jpql.writer.JpqlWriter | ||
import io.mockk.impl.annotations.MockK | ||
import io.mockk.verifySequence | ||
import org.assertj.core.api.WithAssertions | ||
import org.junit.jupiter.api.Test | ||
|
||
@JpqlSerializerTest | ||
class JpqlModuloSerializerTest : WithAssertions { | ||
private val sut = JpqlModuloSerializer() | ||
|
||
@MockK | ||
private lateinit var writer: JpqlWriter | ||
|
||
@MockK | ||
private lateinit var serializer: JpqlRenderSerializer | ||
|
||
private val expression1 = Paths.path(Book::price) | ||
private val expression2 = Paths.path(Book::salePrice) | ||
|
||
@Test | ||
fun handledType() { | ||
// when | ||
val actual = sut.handledType() | ||
|
||
// then | ||
assertThat(actual).isEqualTo(JpqlModulo::class) | ||
} | ||
|
||
@Test | ||
fun serialize() { | ||
// given | ||
val part = Expressions.mod( | ||
expression1, | ||
expression2, | ||
) | ||
val context = TestRenderContext(serializer) | ||
|
||
// when | ||
sut.serialize(part as JpqlModulo<*>, writer, context) | ||
|
||
// then | ||
verifySequence { | ||
serializer.serialize(expression1, writer, context) | ||
writer.write(" ") | ||
writer.write("%") | ||
writer.write(" ") | ||
serializer.serialize(expression2, writer, context) | ||
} | ||
} | ||
} |