File tree Expand file tree Collapse file tree 3 files changed +75
-1
lines changed
test/scala/exercises/shapeless Expand file tree Collapse file tree 3 files changed +75
-1
lines changed Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ object ShapelessLib extends exercise.Library {
24
24
AutoTypeClassExercises ,
25
25
LazyExercises ,
26
26
SizedExercises ,
27
- TypeSafeCastExercises
27
+ TypeSafeCastExercises ,
28
+ TypeCheckingExercises
28
29
)
29
30
}
Original file line number Diff line number Diff line change
1
+ package shapelessex
2
+
3
+ import org .scalatest ._
4
+ import shapeless ._
5
+
6
+ import scala .util .Try
7
+
8
+ /** == Testing for non-compilation ==
9
+ *
10
+ * Libraries like shapeless which make extensive use of type-level computation and implicit resolution often need to
11
+ * provide guarantees that certain expressions *don't* typecheck. Testing these guarantees is supported in shapeless via
12
+ * the `illTyped` macro,
13
+ * {{{
14
+ * import shapeless.test.illTyped
15
+ *
16
+ * scala> illTyped { """1+1 : Boolean""" }
17
+ *
18
+ * scala> illTyped { """1+1 : Int""" }
19
+ * <console>:19: error: Type-checking succeeded unexpectedly.
20
+ * Expected some error.
21
+ * illTyped { """1+1 : Int""" }
22
+ * }}}
23
+ *
24
+ * @param name type_checking
25
+ */
26
+ object TypeCheckingExercises extends FlatSpec with Matchers with exercise.Section {
27
+
28
+ /** The testing library ScalaTest also has a way of checking that a snippet of code
29
+ * does not compile: pass it in to `assertTypeError`. What will happen if we combine
30
+ * `illTyped` and `assertTypeError`? (Hint: it's like applying a double negative.)
31
+ */
32
+ def typeCheckingTest (res0 : Boolean , res1 : Boolean ) = {
33
+
34
+ import shapeless .test .illTyped
35
+
36
+ val matchedTypes = Try {
37
+ assertTypeError(" illTyped { \" val a: Int = 1\" }" )
38
+ true
39
+ } getOrElse {
40
+ false
41
+ }
42
+ matchedTypes should be(res0)
43
+
44
+ val mismatchedTypes = Try {
45
+ assertTypeError(" illTyped { \" val a: String = 1\" }" )
46
+ true
47
+ } getOrElse {
48
+ false
49
+ }
50
+ mismatchedTypes should be(res1)
51
+ }
52
+
53
+ }
Original file line number Diff line number Diff line change
1
+ package exercises
2
+
3
+ import shapelessex ._
4
+ import shapeless .HNil
5
+
6
+ import org .scalatest .Spec
7
+ import org .scalatest .prop .Checkers
8
+
9
+ import org .scalacheck .Shapeless ._
10
+
11
+ class TypeCheckingExercisesSpec extends Spec with Checkers {
12
+ def `testing for non-compilation` = {
13
+ check(
14
+ Test .testSuccess(
15
+ TypeCheckingExercises .typeCheckingTest _,
16
+ true :: false :: HNil
17
+ )
18
+ )
19
+ }
20
+ }
You can’t perform that action at this time.
0 commit comments