Skip to content

Commit

Permalink
continue
Browse files Browse the repository at this point in the history
  • Loading branch information
deusaquilus committed Nov 4, 2019
1 parent c2c6bb3 commit b2079a3
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ case class BetaReduction(replacements: Replacements)
def unapply(ast: Ast): Boolean =
ast match {
case TriviallyCheckable(Constant(true)) => true
case _ => false
case _ => false
}
}

object TriviallyFalseCondition {
def unapply(ast: Ast): Boolean =
ast match {
case TriviallyCheckable(Constant(true)) => true
case _ => false
case TriviallyCheckable(Constant(false)) => true
case _ => false
}
}

Expand All @@ -28,7 +28,7 @@ case class BetaReduction(replacements: Replacements)
case ast if replacements.contains(ast) =>
BetaReduction(replacements - ast - replacements(ast))(replacements(ast))

case If(TriviallyTrueCondition(), thenClause, _) => apply(thenClause)
case If(TriviallyTrueCondition(), thenClause, _) => apply(thenClause)
case If(TriviallyFalseCondition(), _, elseClause) => apply(elseClause)

case Property(Tuple(values), name) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package io.getquill.norm

import io.getquill.ast.{+!=+, +&&+, +==+, +||+, Ast, Constant, ScalarValueLift}
import io.getquill.ast.{ +!=+, +&&+, +==+, +||+, Ast, Constant }

object TriviallyCheckable {

def unapply(ast: Ast): Option[Ast] = ast match {
case c @ Constant(_) => Some(c)
case ScalarValueLift(_, value, _) => Some(Constant(value))

case TriviallyCheckable(Constant(true)) +&&+ TriviallyCheckable(Constant(true)) => Some(Constant(true))
case TriviallyCheckable(Constant(false)) +&&+ _ => Some(Constant(false))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,19 @@ class ConditionalReductionSpec extends Spec {
"trivial conditionals must" - {
val q = quote {
(value: String) =>
if (value == "foo") qr1.filter(_.i == 1)
else qr1.filter(_.s == "blah")
if (value == "foo") qr1.filter(r => r.i == 1)
else qr1.filter(r => r.s == "blah")
}

"reduce to 'the' clause when true" in
Normalize(q("foo").ast) mustEqual (qr1.filter(_.i == 1))

"reduce to 'else' clause when false" in
Normalize(q("bar").ast) mustEqual (qr1.filter(_.s == "blah"))
"reduce to 'then' clause when true" in {
Normalize(quote(q("foo")).ast) mustEqual (quote(qr1.filter(r => r.i == 1)).ast) //helloo
}

"reduce to 'the' clause when true - lifted" in
Normalize(q(lift("foo")).ast) mustEqual (qr1.filter(_.i == 1))
"reduce to 'else' clause when false" in {
Normalize(quote(q("bar")).ast) mustEqual (quote(qr1.filter(r => r.s == "blah")).ast)
}

"reduce to 'else' clause when false - lifted" in
Normalize(q(lift("bar")).ast) mustEqual (qr1.filter(_.s == "blah"))
// TODO More tests. Compound statement
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.getquill.norm

import io.getquill.Spec
import io.getquill.ast.Implicits._
import io.getquill.ast.{ Ast, Constant, Ident }

class TriviallyCheckableSpec extends Spec {

def ua(ast: Ast) = TriviallyCheckable.unapply(ast)
val other = Ident("something")

val True = Constant(true)
val False = Constant(false)

"const/const" - {
"const == const => true" in { ua(Constant(123) +==+ Constant(123)) mustEqual Some(True) }
"const == const => false" in { ua(Constant(123) +==+ Constant(456)) mustEqual Some(False) }
"const != const => true" in { ua(Constant(123) +!=+ Constant(123)) mustEqual Some(False) }
"const != const => false" in { ua(Constant(123) +!=+ Constant(456)) mustEqual Some(True) }

"const == other => true" in { ua(Constant(123) +==+ other) mustEqual None }
"const != other => true" in { ua(Constant(123) +!=+ other) mustEqual None }
"other == const => true" in { ua(other +==+ Constant(123)) mustEqual None }
"other != const => true" in { ua(other +!=+ Constant(123)) mustEqual None }
()
}

"exp/exp" - {
"true || true => true" in { ua(True +||+ True) mustEqual Some(True) }
"true || false => false" in { ua(True +||+ False) mustEqual Some(True) }
"const || const => false" in { ua(False +||+ True) mustEqual Some(True) }
"true && true => true" in { ua(True +&&+ True) mustEqual Some(True) }
"true && false => false" in { ua(True +&&+ False) mustEqual Some(False) }
"false && true => false" in { ua(False +&&+ True) mustEqual Some(False) }

"false && other => false" in { ua(False +&&+ other) mustEqual Some(False) }
"other && false => false" in { ua(other +&&+ False) mustEqual Some(False) }
"true && const => none" in { ua(True +&&+ other) mustEqual None }
"other && true => none" in { ua(other +&&+ True) mustEqual None }
"other && other => none" in { ua(other +&&+ other) mustEqual None }

"true || other => true" in { ua(True +||+ other) mustEqual Some(True) }
"other || true => true" in { ua(other +||+ True) mustEqual Some(True) }
"false || other => none" in { ua(False +||+ other) mustEqual None }
"other || false => none" in { ua(other +||+ False) mustEqual None }
"other || other => none" in { ua(other +||+ other) mustEqual None }
()
}

// TODO Finish testing
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,18 @@ class SqlStaticReductionSpec extends Spec {
"trivial conditionals must" - {
val q = quote {
(value: String) =>
if (value == "foo") qr1.filter(_.i == 1)
else qr1.filter(_.s == "blah")
if (value == "foo") qr1.filter(r => r.i == 1)
else qr1.filter(r => r.s == "blah")
}

"reduce to 'the' clause when true" in
testContext.run(q("foo")).string mustEqual testContext.run(qr1.filter(_.i == 1)).string

"reduce to 'else' clause when false" in
testContext.run(q("bar")).string mustEqual testContext.run(qr1.filter(_.s == "blah")).string
"reduce to 'the' clause when true" in {
testContext.run(q("foo")).string mustEqual testContext.run(qr1.filter(r => r.i == 1)).string
}

"reduce to 'the' clause when true - lifted" in
testContext.run(q(lift("foo"))).string mustEqual testContext.run(qr1.filter(_.i == 1)).string
"reduce to 'else' clause when false" in {
testContext.run(q("bar")).string mustEqual testContext.run(qr1.filter(r => r.s == "blah")).string
}

"reduce to 'else' clause when false - lifted" in
testContext.run(q(lift("bar"))).string mustEqual testContext.run(qr1.filter(_.s == "blah")).string
// TODO Test some conditionals
}
}

0 comments on commit b2079a3

Please sign in to comment.