1
+ import org .scalacheck .Properties
2
+ import org .scalacheck .Prop .forAll
3
+ import org .scalatest .Assertions .{assertCompiles , assertDoesNotCompile }
4
+ import org .scalatest .flatspec .AnyFlatSpec
5
+ import org .scalatest .matchers .should .Matchers
6
+ import workshop .TypeSafePrintf ._
7
+
8
+
9
+ object TypeSafePrintfPropertiesSpec extends Properties (" TypeSafePrintf" ) {
10
+
11
+ property(" supports %s %d format" ) = forAll { (s : String , i : Int ) =>
12
+ printf(" %s is %d" )(s, i)
13
+ true
14
+ }
15
+
16
+ property(" support %d %s format" ) = forAll { (s : String , i : Int ) =>
17
+ printf(" %d is %s" )(i, s)
18
+ true
19
+ }
20
+ }
21
+
22
+ class TypeSafePrintfSpec extends AnyFlatSpec with Matchers {
23
+
24
+ it should " represent %s %s as (String, String)" in {
25
+ assertCompiles(" summon[ArgTypes[\" %s %s\" ] =:= (String, String)]" )
26
+ }
27
+
28
+ it should " represent %s %s %s as (String, String, String)" in {
29
+ assertCompiles(" summon[ArgTypes[\" %s %s %s\" ] =:= (String, String, String)]" )
30
+ }
31
+
32
+ it should " represent %s %d as (String, Int)" in {
33
+ assertCompiles(" summon[ArgTypes[\" %s is %d\" ] =:= (String, Int)]" )
34
+ }
35
+
36
+ it should " represent %d %s as (Int, String)" in {
37
+ assertCompiles(" summon[ArgTypes[\" %d is %s\" ] =:= (Int, String)]" )
38
+ }
39
+
40
+ it should " compile %s %d format with correctly ordered params" in {
41
+ assertCompiles(" printf(\" %d is %s\" )(5, \" a\" )" )
42
+ }
43
+
44
+ it should " not compile %s %d format with wrong ordered params" in {
45
+ assertDoesNotCompile(" printf(\" %s %d\" )(5, \" a\" )" )
46
+ }
47
+
48
+ it should " not compile %d %s format with wrong ordered params" in {
49
+ assertDoesNotCompile(" printf(\" %d %s\" )(\" a\" , 5)" )
50
+ }
51
+
52
+ it should " not compile %d %s format with too many params" in {
53
+ assertDoesNotCompile(" printf(\" %s %d\" )(\" a\" , 5, 6)" )
54
+ }
55
+
56
+ it should " not compile %d %s format with too few params" in {
57
+ assertDoesNotCompile(" printf(\" %s %d\" )(\" a\" )" )
58
+ }
59
+ }
0 commit comments