-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathConvertToEnumSuite.scala
162 lines (147 loc) · 4.46 KB
/
ConvertToEnumSuite.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package dotty.tools.pc.tests.edit
import java.net.URI
import java.util.Optional
import scala.util.Failure
import scala.util.Success
import scala.util.Try
import scala.meta.internal.jdk.CollectionConverters._
import scala.meta.internal.metals.CompilerOffsetParams
import scala.meta.pc.CodeActionId
import org.eclipse.lsp4j.TextEdit
import dotty.tools.pc.base.BaseCodeActionSuite
import main.dotty.tools.pc.PcConvertToEnum
import dotty.tools.pc.utils.TextEdits
import org.junit.Test
class ConvertToEnumSuite extends BaseCodeActionSuite:
@Test def basic =
checkEdit(
"""|sealed trait <<C>>ow
|object Cow:
| class HolsteinFriesian extends Cow
| class Highland extends Cow
| class BrownSwiss extends Cow
|""".stripMargin,
"""|enum Cow:
| case HolsteinFriesian, Highland, BrownSwiss
|""".stripMargin
)
@Test def `basic-with-params` =
checkEdit(
"""|sealed class <<C>>ow[T](val i: Int, j: Int)
|object Cow:
| class HolsteinFriesian extends Cow[1](1, 1)
| class Highland extends Cow[2](2, 2)
| class BrownSwiss extends Cow[3](3, 3)
|""".stripMargin,
"""|enum Cow[T](val i: Int, j: Int):
| case HolsteinFriesian extends Cow[1](1, 1)
| case Highland extends Cow[2](2, 2)
| case BrownSwiss extends Cow[3](3, 3)
|""".stripMargin
)
@Test def `class-with-body` =
checkEdit(
"""|trait Spotted
|
|sealed trait <<C>>ow:
| def moo = "Mooo!"
|
|object Cow:
| def of(name: String) = HolsteinFriesian(name)
| case class HolsteinFriesian(name: String) extends Cow, Spotted
| class Highland extends Cow
| class BrownSwiss extends Cow
|""".stripMargin,
"""|trait Spotted
|
|enum Cow:
| def moo = "Mooo!"
| case Highland, BrownSwiss
| case HolsteinFriesian(name: String) extends Cow, Spotted
|
|object Cow:
| def of(name: String) = HolsteinFriesian(name)
|""".stripMargin
)
@Test def `with-indentation` =
checkEdit(
"""|object O {
| sealed class <<C>>ow {
| def moo = "Mooo!"
| def mooooo = "Mooooooo!"
| }
| object Cow {
| case class HolsteinFriesian(name: String) extends Cow
| class Highland extends Cow
| class BrownSwiss extends Cow
| }
|}
|""".stripMargin,
"""|object O {
| enum Cow {
| def moo = "Mooo!"
| def mooooo = "Mooooooo!"
| case Highland, BrownSwiss
| case HolsteinFriesian(name: String) extends Cow
| }
|}
|""".stripMargin
)
@Test def `case-objects` =
checkEdit(
"""|sealed trait <<C>>ow
|case object HolsteinFriesian extends Cow
|case object Highland extends Cow
|case object BrownSwiss extends Cow
|""".stripMargin,
"""|enum Cow:
| case HolsteinFriesian, Highland, BrownSwiss
|export Cow.*
|""".stripMargin
)
@Test def `no-companion-object` =
checkEdit(
"""|sealed trait <<C>>ow
|class HolsteinFriesian extends Cow
|class Highland extends Cow
|class BrownSwiss extends Cow
|""".stripMargin,
"""|enum Cow:
| case HolsteinFriesian, Highland, BrownSwiss
|export Cow.*
|""".stripMargin
)
def checkError(
original: String,
expectedError: String
): Unit =
Try(getConversionToEnum(original)) match
case Failure(exception: Throwable) =>
assertNoDiff(
exception.getCause().getMessage().replaceAll("\\[.*\\]", ""),
expectedError
)
case Success(_) =>
fail("Expected an error but got a result")
def checkEdit(
original: String,
expected: String,
): Unit =
val edits = getConversionToEnum(original)
val (code, _, _) = params(original)
val obtained = TextEdits.applyEdits(code, edits)
assertNoDiff(obtained, expected)
def getConversionToEnum(
original: String,
filename: String = "file:/A.scala"
): List[TextEdit] = {
val (code, _, offset) = params(original)
val result = presentationCompiler
.codeAction[Boolean](
CompilerOffsetParams(URI.create(filename), code, offset, cancelToken),
PcConvertToEnum.codeActionId,
Optional.empty()
)
.get()
result.asScala.toList
}