Skip to content

Commit f0395c8

Browse files
committed
add tests for enums compiled with Scala.js
1 parent 240f618 commit f0395c8

File tree

3 files changed

+165
-0
lines changed

3 files changed

+165
-0
lines changed

tests/neg-scalajs/js-enums.check

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
-- Error: tests/neg-scalajs/js-enums.scala:4:5 -------------------------------------------------------------------------
2+
4 |enum MyEnum extends js.Object: // error
3+
|^
4+
|MyEnum extends scala.reflect.Enum which does not extend js.Any.
5+
5 | case Foo
6+
-- Error: tests/neg-scalajs/js-enums.scala:9:5 -------------------------------------------------------------------------
7+
7 |@js.native
8+
8 |@JSGlobal
9+
9 |enum MyEnumNative extends js.Object: // error
10+
|^
11+
|MyEnumNative extends scala.reflect.Enum which does not extend js.Any.
12+
10 | case Bar
13+
-- Error: tests/neg-scalajs/js-enums.scala:12:5 ------------------------------------------------------------------------
14+
12 |enum MyEnumAny extends js.Any: // error
15+
|^
16+
|Non-native JS classes and objects cannot directly extend AnyRef. They must extend a JS class (native or not).
17+
13 | case Foo
18+
-- Error: tests/neg-scalajs/js-enums.scala:17:5 ------------------------------------------------------------------------
19+
15 |@js.native
20+
16 |@JSGlobal
21+
17 |enum MyEnumNativeAny extends js.Any: // error
22+
|^
23+
|MyEnumNativeAny extends scala.reflect.Enum which does not extend js.Any.
24+
18 | case Bar

tests/neg-scalajs/js-enums.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import scala.scalajs.js
2+
import scala.scalajs.js.annotation._
3+
4+
enum MyEnum extends js.Object: // error
5+
case Foo
6+
7+
@js.native
8+
@JSGlobal
9+
enum MyEnumNative extends js.Object: // error
10+
case Bar
11+
12+
enum MyEnumAny extends js.Any: // error
13+
case Foo
14+
15+
@js.native
16+
@JSGlobal
17+
enum MyEnumNativeAny extends js.Any: // error
18+
case Bar
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package org.scalajs.testsuite.compiler
2+
3+
import org.junit.Assert._
4+
import org.junit.Test
5+
6+
object EnumTestScala3:
7+
8+
enum Color1 derives Eql:
9+
case Red, Green, Blue
10+
11+
enum Color2 extends java.lang.Enum[Color2] derives Eql:
12+
case Red, Green, Blue
13+
14+
def color1(): Unit =
15+
import EnumTestScala3.{Color1 => Color}
16+
assert(Color.Red.ordinal == 0)
17+
assert(Color.Green.ordinal == 1)
18+
assert(Color.Blue.ordinal == 2)
19+
assert(Color.Red.enumLabel == "Red")
20+
assert(Color.Green.enumLabel == "Green")
21+
assert(Color.Blue.enumLabel == "Blue")
22+
assert(Color.valueOf("Red") == Color.Red)
23+
assert(Color.valueOf("Green") == Color.Green)
24+
assert(Color.valueOf("Blue") == Color.Blue)
25+
assert(Color.valueOf("Blue") != Color.Red)
26+
assert(Color.valueOf("Blue") != Color.Green)
27+
assert(Color.values(0) == Color.Red)
28+
assert(Color.values(1) == Color.Green)
29+
assert(Color.values(2) == Color.Blue)
30+
end color1
31+
32+
def color2(): Unit = // copied from `color1`
33+
import EnumTestScala3.{Color2 => Color}
34+
assert(Color.Red.ordinal == 0)
35+
assert(Color.Green.ordinal == 1)
36+
assert(Color.Blue.ordinal == 2)
37+
assert(Color.Red.enumLabel == "Red")
38+
assert(Color.Green.enumLabel == "Green")
39+
assert(Color.Blue.enumLabel == "Blue")
40+
assert(Color.valueOf("Red") == Color.Red)
41+
assert(Color.valueOf("Green") == Color.Green)
42+
assert(Color.valueOf("Blue") == Color.Blue)
43+
assert(Color.valueOf("Blue") != Color.Red)
44+
assert(Color.valueOf("Blue") != Color.Green)
45+
assert(Color.values(0) == Color.Red)
46+
assert(Color.values(1) == Color.Green)
47+
assert(Color.values(2) == Color.Blue)
48+
end color2
49+
50+
// test "non-simple" cases with anonymous subclasses
51+
enum Currency1(val dollarValue: Double) derives Eql:
52+
case Dollar extends Currency1(1.0)
53+
case SwissFanc extends Currency1(1.09)
54+
case Euro extends Currency1(1.18)
55+
56+
enum Currency2(val dollarValue: Double) extends java.lang.Enum[Currency2] derives Eql:
57+
case Dollar extends Currency2(1.0)
58+
case SwissFanc extends Currency2(1.09)
59+
case Euro extends Currency2(1.18)
60+
61+
def currency1(): Unit =
62+
import EnumTestScala3.{Currency1 => Currency}
63+
assert(Currency.Dollar.ordinal == 0)
64+
assert(Currency.SwissFanc.ordinal == 1)
65+
assert(Currency.Euro.ordinal == 2)
66+
assert(Currency.Dollar.enumLabel == "Dollar")
67+
assert(Currency.SwissFanc.enumLabel == "SwissFanc")
68+
assert(Currency.Euro.enumLabel == "Euro")
69+
assert(Currency.valueOf("Dollar") == Currency.Dollar)
70+
assert(Currency.valueOf("SwissFanc") == Currency.SwissFanc)
71+
assert(Currency.valueOf("Euro") == Currency.Euro)
72+
assert(Currency.valueOf("Euro") != Currency.Dollar)
73+
assert(Currency.valueOf("Euro") != Currency.SwissFanc)
74+
assert(Currency.values(0) == Currency.Dollar)
75+
assert(Currency.values(1) == Currency.SwissFanc)
76+
assert(Currency.values(2) == Currency.Euro)
77+
assert(Currency.Dollar.dollarValue == 1.00)
78+
assert(Currency.SwissFanc.dollarValue == 1.09)
79+
assert(Currency.Euro.dollarValue == 1.18)
80+
end currency1
81+
82+
def currency2(): Unit = // copied from `currency1`
83+
import EnumTestScala3.{Currency2 => Currency}
84+
assert(Currency.Dollar.ordinal == 0)
85+
assert(Currency.SwissFanc.ordinal == 1)
86+
assert(Currency.Euro.ordinal == 2)
87+
assert(Currency.Dollar.enumLabel == "Dollar")
88+
assert(Currency.SwissFanc.enumLabel == "SwissFanc")
89+
assert(Currency.Euro.enumLabel == "Euro")
90+
assert(Currency.valueOf("Dollar") == Currency.Dollar)
91+
assert(Currency.valueOf("SwissFanc") == Currency.SwissFanc)
92+
assert(Currency.valueOf("Euro") == Currency.Euro)
93+
assert(Currency.valueOf("Euro") != Currency.Dollar)
94+
assert(Currency.valueOf("Euro") != Currency.SwissFanc)
95+
assert(Currency.values(0) == Currency.Dollar)
96+
assert(Currency.values(1) == Currency.SwissFanc)
97+
assert(Currency.values(2) == Currency.Euro)
98+
assert(Currency.Dollar.dollarValue == 1.00)
99+
assert(Currency.SwissFanc.dollarValue == 1.09)
100+
assert(Currency.Euro.dollarValue == 1.18)
101+
end currency2
102+
103+
enum Opt[+T]:
104+
case Sm[+T1](value: T1) extends Opt[T1]
105+
case Nn extends Opt[Nothing]
106+
107+
def opt(): Unit =
108+
assert(Opt.Sm(1).ordinal == 0)
109+
assert(Opt.Nn.ordinal == 1)
110+
assert(Opt.Sm(1).enumLabel == "Sm")
111+
assert(Opt.Nn.enumLabel == "Nn")
112+
assert(Opt.valueOf("Nn") == Opt.Nn)
113+
assert(Opt.values(0) == Opt.Nn)
114+
assert(Opt.Sm("hello").value == "hello")
115+
end opt
116+
117+
class EnumTestScala3:
118+
import EnumTestScala3._
119+
@Test def testColor1(): Unit = color1()
120+
@Test def testColor2(): Unit = color2()
121+
@Test def testCurrency1(): Unit = currency1()
122+
@Test def testCurrency2(): Unit = currency2()
123+
@Test def testOpt(): Unit = opt()

0 commit comments

Comments
 (0)