-
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.
Merge pull request #651 from progress0407/feat/arith-power
Implement power operator
- Loading branch information
Showing
10 changed files
with
244 additions
and
8 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/PowerDslTest.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.employee.Employee | ||
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 | ||
|
||
class PowerDslTest : WithAssertions { | ||
private val intExpression1 = Paths.path(Employee::age) | ||
private val intExpression2 = Expressions.value(2) | ||
|
||
private val int1 = 2 | ||
|
||
@Test | ||
fun `power() with a property`() { | ||
// when | ||
val expression1 = queryPart { | ||
power(Employee::age, int1) | ||
}.toExpression() | ||
|
||
val expression2 = queryPart { | ||
power(Employee::age, intExpression2) | ||
}.toExpression() | ||
|
||
val actual1: Expression<Number> = expression1 // for type check | ||
val actual2: Expression<Number> = expression2 // for type check | ||
|
||
// then | ||
val expected1 = Expressions.power( | ||
base = Paths.path(Employee::age), | ||
exponent = Expressions.value(int1), | ||
) | ||
|
||
val expected2 = Expressions.power( | ||
base = Paths.path(Employee::age), | ||
exponent = intExpression2, | ||
) | ||
|
||
assertThat(actual1).isEqualTo(expected1) | ||
assertThat(actual2).isEqualTo(expected2) | ||
} | ||
|
||
@Test | ||
fun `power() with a expression`() { | ||
// when | ||
val expression1 = queryPart { | ||
power(intExpression1, int1) | ||
}.toExpression() | ||
|
||
val expression2 = queryPart { | ||
power(intExpression1, intExpression2) | ||
}.toExpression() | ||
|
||
val actual1: Expression<Number> = expression1 // for type check | ||
val actual2: Expression<Number> = expression2 // for type check | ||
|
||
// then | ||
val expected1 = Expressions.power( | ||
base = intExpression1, | ||
exponent = Expressions.value(int1), | ||
) | ||
|
||
val expected2 = Expressions.power( | ||
base = intExpression1, | ||
exponent = intExpression2, | ||
) | ||
|
||
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
...jpql/src/main/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/expression/impl/JpqlPower.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 calculates the powering of a numeric [base] to a specified [exponent]. | ||
*/ | ||
@Internal | ||
data class JpqlPower<T : Number> internal constructor( | ||
val base: Expression<T>, | ||
val exponent: Expression<T>, | ||
) : Expression<Double> |
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
31 changes: 31 additions & 0 deletions
31
...rc/main/kotlin/com/linecorp/kotlinjdsl/render/jpql/serializer/impl/JpqlPowerSerializer.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,31 @@ | ||
package com.linecorp.kotlinjdsl.render.jpql.serializer.impl | ||
|
||
import com.linecorp.kotlinjdsl.Internal | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlPower | ||
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 JpqlPowerSerializer : JpqlSerializer<JpqlPower<*>> { | ||
override fun handledType(): KClass<JpqlPower<*>> { | ||
return JpqlPower::class | ||
} | ||
|
||
override fun serialize(part: JpqlPower<*>, writer: JpqlWriter, context: RenderContext) { | ||
val delegate = context.getValue(JpqlRenderSerializer) | ||
|
||
writer.write("POWER") | ||
|
||
writer.writeParentheses { | ||
delegate.serialize(part.base, writer, context) | ||
|
||
writer.write(",") | ||
writer.write(" ") | ||
|
||
delegate.serialize(part.exponent, writer, context) | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...est/kotlin/com/linecorp/kotlinjdsl/render/jpql/serializer/impl/JpqlPowerSerializerTest.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,60 @@ | ||
package com.linecorp.kotlinjdsl.render.jpql.serializer.impl | ||
|
||
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expressions | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlPower | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.path.Paths | ||
import com.linecorp.kotlinjdsl.render.TestRenderContext | ||
import com.linecorp.kotlinjdsl.render.jpql.entity.employee.Employee | ||
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.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
|
||
@JpqlSerializerTest | ||
class JpqlPowerSerializerTest { | ||
private val sut = JpqlPowerSerializer() | ||
|
||
@MockK | ||
private lateinit var writer: JpqlWriter | ||
|
||
@MockK | ||
private lateinit var serializer: JpqlRenderSerializer | ||
|
||
private val expression1 = Paths.path(Employee::age) | ||
private val expression2 = Expressions.value(2) | ||
|
||
@Test | ||
fun handledType() { | ||
// when | ||
val actual = sut.handledType() | ||
|
||
// then | ||
assertThat(actual).isEqualTo(JpqlPower::class) | ||
} | ||
|
||
@Test | ||
fun serialize() { | ||
// given | ||
val part = Expressions.power( | ||
base = expression1, | ||
exponent = expression2, | ||
) | ||
val context = TestRenderContext(serializer) | ||
|
||
// when | ||
sut.serialize(part as JpqlPower<*>, writer, context) | ||
|
||
// then | ||
verifySequence { | ||
writer.write("POWER") | ||
writer.writeParentheses(any()) | ||
serializer.serialize(expression1, writer, context) | ||
writer.write(",") | ||
writer.write(" ") | ||
serializer.serialize(expression2, writer, context) | ||
} | ||
} | ||
} |