Skip to content

TypeCheckingExercises #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/scala/shapeless/ShapelessLib.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ object ShapelessLib extends exercise.Library {
AutoTypeClassExercises,
LazyExercises,
SizedExercises,
TypeSafeCastExercises
TypeSafeCastExercises,
TypeCheckingExercises
)
}
53 changes: 53 additions & 0 deletions src/main/scala/shapeless/TypeCheckingExercises.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package shapelessex

import org.scalatest._
import shapeless._

import scala.util.Try

/** == Testing for non-compilation ==
*
* Libraries like shapeless which make extensive use of type-level computation and implicit resolution often need to
* provide guarantees that certain expressions *don't* typecheck. Testing these guarantees is supported in shapeless via
* the `illTyped` macro,
* {{{
* import shapeless.test.illTyped
*
* scala> illTyped { """1+1 : Boolean""" }
*
* scala> illTyped { """1+1 : Int""" }
* <console>:19: error: Type-checking succeeded unexpectedly.
* Expected some error.
* illTyped { """1+1 : Int""" }
* }}}
*
* @param name type_checking
*/
object TypeCheckingExercises extends FlatSpec with Matchers with exercise.Section {

/** The testing library ScalaTest also has a way of checking that a snippet of code
* does not compile: pass it in to `assertTypeError`. What will happen if we combine
* `illTyped` and `assertTypeError`? (Hint: it's like applying a double negative.)
*/
def typeCheckingTest(res0: Boolean, res1: Boolean) = {

import shapeless.test.illTyped

val matchedTypes = Try {
assertTypeError("illTyped { \"val a: Int = 1\" }")
true
} getOrElse {
false
}
matchedTypes should be(res0)

val mismatchedTypes = Try {
assertTypeError("illTyped { \"val a: String = 1\" }")
true
} getOrElse {
false
}
mismatchedTypes should be(res1)
}

}
20 changes: 20 additions & 0 deletions src/test/scala/exercises/shapeless/TypeCheckingExercisesSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package exercises

import shapelessex._
import shapeless.HNil

import org.scalatest.Spec
import org.scalatest.prop.Checkers

import org.scalacheck.Shapeless._

class TypeCheckingExercisesSpec extends Spec with Checkers {
def `testing for non-compilation` = {
check(
Test.testSuccess(
TypeCheckingExercises.typeCheckingTest _,
true :: false :: HNil
)
)
}
}