@@ -4,6 +4,9 @@ import deriving.Mirror
4
4
enum Color :
5
5
case Red , Green , Blue
6
6
7
+ enum Suits extends java.lang.Enum [Suits ]:
8
+ case Clubs , Spades , Diamonds , Hearts
9
+
7
10
enum Tag [T ]:
8
11
case Int extends Tag [Int ]
9
12
case OfClass [T ]()(using val tag : reflect.ClassTag [T ]) extends Tag [T ] // mix order of class and value
@@ -29,7 +32,7 @@ enum ClassOnly: // this should still generate the `ordinal` and `fromOrdinal` co
29
32
case BranchProd (i : Int )
30
33
31
34
@ main def Test : Unit =
32
- import Color ._ , Tag ._ , Expr ._ , ListLike ._ , TypeCtorsK ._ , MixedParams ._ , ClassOnly ._
35
+ import Color ._ , Suits . _ , Tag ._ , Expr ._ , ListLike ._ , TypeCtorsK ._ , MixedParams ._ , ClassOnly ._
33
36
34
37
type FromOrdinal [T ] = {
35
38
def fromOrdinal (ordinal : Int ): T
@@ -47,15 +50,19 @@ enum ClassOnly: // this should still generate the `ordinal` and `fromOrdinal` co
47
50
s " $c does not `eq` companion.fromOrdinal( ${c.ordinal}), got ${companion.fromOrdinal(c.ordinal)}" )
48
51
49
52
def notFromOrdinal [T <: AnyRef & reflect.Enum ](companion : FromOrdinal [T ], compare : T ): Unit =
53
+ cantFind(companion, compare.ordinal)
54
+
55
+ def cantFind [T ](companion : FromOrdinal [T ], ordinal : Int ): Unit =
50
56
try
51
- companion.fromOrdinal(compare. ordinal)
52
- assertFail(s " $companion.fromOrdinal( ${compare. ordinal}) did not fail " )
57
+ companion.fromOrdinal(ordinal)
58
+ assertFail(s " $companion.fromOrdinal( ${ordinal}) did not fail " )
53
59
catch
54
60
case e : java.lang.reflect.InvocationTargetException => // TODO: maybe reflect.Selectable should catch this?
55
61
assert(e.getCause.isInstanceOf [java.util.NoSuchElementException ]
56
- && e.getCause.getMessage == compare. ordinal.toString)
62
+ && e.getCause.getMessage == ordinal.toString)
57
63
58
64
fetchFromOrdinal(companion = Color , compare = Red , Green , Blue )
65
+ fetchFromOrdinal(companion = Suits , compare = Clubs , Spades , Diamonds , Hearts )
59
66
fetchFromOrdinal(companion = Tag , compare = Int , String )
60
67
fetchFromOrdinal(companion = Expr , compare = EmptyTree , AnyTree )
61
68
fetchFromOrdinal(companion = ListLike , compare = EmptyListLike )
@@ -66,6 +73,11 @@ enum ClassOnly: // this should still generate the `ordinal` and `fromOrdinal` co
66
73
notFromOrdinal(companion = TypeCtorsK , compare = Const [String ]())
67
74
notFromOrdinal(companion = ClassOnly , compare = BranchProd (1 )) // ClassOnly has the `fromOrdinal` method
68
75
76
+ cantFind(companion = Color , ordinal = 500 ) // test default case for enumeration
77
+ cantFind(companion = Suits , ordinal = 500 ) // test default case for Java style enumeration
78
+ cantFind(companion = Tag , ordinal = 500 ) // test default case for mixed adt with non-simple values
79
+ cantFind(companion = ClassOnly , ordinal = 500 ) // should always throw
80
+
69
81
assert(summon[Mirror .SumOf [ClassOnly ]].ordinal(BranchProd (1 )) == 0 )
70
82
71
83
val colors : Array [Color ] = Color .values
0 commit comments