@@ -9,51 +9,175 @@ class ScalafixTests extends ScalaCliSuite {
9
9
10
10
val emptyInputs : TestInputs = TestInputs (os.rel / " .placeholder" -> " " )
11
11
12
- val simpleInputsOriginalContent : String =
12
+ val simpleInputsScala3OriginalContent : String =
13
13
""" package foo
14
14
|
15
- |final object Hello {
16
- | def main(args: Array[String]): Unit = {
15
+ |final object Hello:
16
+ | def main(args: Array[String]): Unit =
17
17
| println("Hello")
18
- | }
19
- |}
20
18
|""" .stripMargin
21
- val simpleInputs : TestInputs = TestInputs (
19
+ val simpleInputs3 : TestInputs = TestInputs (
22
20
os.rel / confFileName ->
23
21
s """ |rules = [
24
22
| RedundantSyntax
25
23
|]
26
24
| """ .stripMargin,
27
- os.rel / " Hello.scala" -> simpleInputsOriginalContent
25
+ os.rel / " Hello.scala" -> simpleInputsScala3OriginalContent
28
26
)
29
- val expectedSimpleInputsRewrittenContent : String = noCrLf {
30
- """ package foo
31
- |
32
- |object Hello {
33
- | def main(args: Array[String]): Unit = {
34
- | println("Hello")
35
- | }
36
- |}
37
- |""" .stripMargin
38
- }
39
27
40
28
private def noCrLf (input : String ): String =
41
29
input.replaceAll(" \r\n " , " \n " )
42
30
43
- test(" simple" ) {
44
- simpleInputs.fromRoot { root =>
45
- os.proc(TestUtil .cli, " scalafix" , " ." ).call(cwd = root)
31
+ test(" simple for scala2 code" ) {
32
+ val simpleInputsScala2OriginalContent : String =
33
+ """ package foo
34
+ |
35
+ |final object Hello {
36
+ | def main(args: Array[String]): Unit = {
37
+ | println("Hello")
38
+ | }
39
+ |}
40
+ |""" .stripMargin
41
+ val simpleInputs2 : TestInputs = TestInputs (
42
+ os.rel / confFileName ->
43
+ s """ |rules = [
44
+ | RedundantSyntax
45
+ |]
46
+ | """ .stripMargin,
47
+ os.rel / " Hello.scala" -> simpleInputsScala2OriginalContent
48
+ )
49
+ val expectedContent : String = noCrLf {
50
+ """ package foo
51
+ |
52
+ |object Hello {
53
+ | def main(args: Array[String]): Unit = {
54
+ | println("Hello")
55
+ | }
56
+ |}
57
+ |""" .stripMargin
58
+ }
59
+
60
+ simpleInputs2.fromRoot { root =>
61
+ os.proc(TestUtil .cli, " scalafix" , " ." , " -S" , " 2" , " --power" ).call(cwd = root)
62
+ val updatedContent = noCrLf(os.read(root / " Hello.scala" ))
63
+ expect(updatedContent == expectedContent)
64
+ }
65
+ }
66
+
67
+ test(" simple for scala3 code" ) {
68
+ val expectedContent : String = noCrLf {
69
+ """ package foo
70
+ |
71
+ |object Hello:
72
+ | def main(args: Array[String]): Unit =
73
+ | println("Hello")
74
+ |""" .stripMargin
75
+ }
76
+
77
+ simpleInputs3.fromRoot { root =>
78
+ os.proc(TestUtil .cli, " scalafix" , " ." , " -S" , " 3" , " --power" ).call(cwd = root)
46
79
val updatedContent = noCrLf(os.read(root / " Hello.scala" ))
47
- expect(updatedContent == expectedSimpleInputsRewrittenContent )
80
+ expect(updatedContent == expectedContent )
48
81
}
49
82
}
50
83
51
84
test(" with --check" ) {
52
- simpleInputs .fromRoot { root =>
53
- val res = os.proc(TestUtil .cli, " scalafix" , " --check" , " ." ).call(cwd = root, check = false )
85
+ simpleInputs3 .fromRoot { root =>
86
+ val res = os.proc(TestUtil .cli, " scalafix" , " --power " , " -- check" , " ." ).call(cwd = root, check = false )
54
87
expect(res.exitCode == 1 )
55
88
val updatedContent = noCrLf(os.read(root / " Hello.scala" ))
56
- expect(updatedContent == noCrLf(simpleInputsOriginalContent))
89
+ expect(updatedContent == noCrLf(simpleInputsScala3OriginalContent))
90
+ }
91
+ }
92
+
93
+ test(" semantic rule" ) {
94
+ val unusedValueInputsContent : String =
95
+ """ //> using options -Wunused:all
96
+ |package foo
97
+ |
98
+ |object Hello:
99
+ | def main(args: Array[String]): Unit =
100
+ | val name = "John"
101
+ | println("Hello")
102
+ |""" .stripMargin
103
+ val semanticRuleInputs : TestInputs = TestInputs (
104
+ os.rel / confFileName ->
105
+ s """ |rules = [
106
+ | RemoveUnused
107
+ |]
108
+ | """ .stripMargin,
109
+ os.rel / " Hello.scala" -> unusedValueInputsContent
110
+ )
111
+ val expectedContent : String = noCrLf {
112
+ """ //> using options -Wunused:all
113
+ |package foo
114
+ |
115
+ |object Hello:
116
+ | def main(args: Array[String]): Unit =
117
+ |
118
+ | println("Hello")
119
+ |""" .stripMargin
120
+ }
121
+
122
+ semanticRuleInputs.fromRoot { root =>
123
+ os.proc(TestUtil .cli, " scalafix" , " --power" , " ." ).call(cwd = root)
124
+ val updatedContent = noCrLf(os.read(root / " Hello.scala" ))
125
+ expect(updatedContent == expectedContent)
126
+ }
127
+ }
128
+ test(" external rule" ) {
129
+ val unnamedParamsInputsContent : String =
130
+ """ //> using options -P:semanticdb:synthetics:on
131
+ |//> using compileOnly.dep "com.github.jatcwang::scalafix-named-params:0.2.4"
132
+ |
133
+ |package foo
134
+ |
135
+ |object Hello {
136
+ | def greetMany(name: String, times: Int) =
137
+ | for {
138
+ | i <- 0 to times
139
+ | _ = println(s"Hello $name")
140
+ | } yield ()
141
+ |
142
+ | def main(args: Array[String]): Unit =
143
+ | greetMany("John", 42)
144
+ |}
145
+ |""" .stripMargin
146
+ val externalRuleInputs : TestInputs = TestInputs (
147
+ os.rel / confFileName ->
148
+ s """ |rules = [
149
+ | UseNamedParameters
150
+ |]
151
+ |
152
+ |UseNamedParameters.minParams = 2
153
+ | """ .stripMargin,
154
+ os.rel / " Hello.scala" -> unnamedParamsInputsContent
155
+ )
156
+ val expectedContent : String = noCrLf {
157
+ """ //> using options -P:semanticdb:synthetics:on
158
+ |//> using compileOnly.dep "com.github.jatcwang::scalafix-named-params:0.2.4"
159
+ |
160
+ |package foo
161
+ |
162
+ |object Hello {
163
+ | def greetMany(name: String, times: Int) =
164
+ | for {
165
+ | i <- 0 to times
166
+ | _ = println(s"Hello $name")
167
+ | } yield ()
168
+ |
169
+ | def main(args: Array[String]): Unit =
170
+ | greetMany(name = "John", times = 42)
171
+ |}
172
+ |""" .stripMargin
173
+ }
174
+
175
+ externalRuleInputs.fromRoot { root =>
176
+ os.proc(TestUtil .cli, " scalafix" , " --power" , " ." , " -S" , " 2" ).call(cwd = root)
177
+ val updatedContent = noCrLf(os.read(root / " Hello.scala" ))
178
+ println(updatedContent)
179
+ println(expectedContent)
180
+ expect(updatedContent == expectedContent)
57
181
}
58
182
}
59
183
}
0 commit comments