Skip to content

Commit 4274bff

Browse files
committed
introduce ScalaInterpolator for better analyzer test writing performance
1 parent d02b9c7 commit 4274bff

15 files changed

+279
-303
lines changed

analyzer/src/test/scala/com/avsystem/commons/analyzer/AnalyzerTest.scala

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.avsystem.commons
22
package analyzer
33

4+
import com.avsystem.commons.analyzer.AnalyzerTest.ScalaInterpolator
45
import org.scalactic.source.Position
56
import org.scalatest.Assertions
67

@@ -25,11 +26,6 @@ trait AnalyzerTest { this: Assertions =>
2526
run.compileSources(List(new BatchSourceFile("test.scala", source)))
2627
}
2728

28-
def assertErrors(source: String)(implicit pos: Position): Unit = {
29-
compile(source)
30-
assert(compiler.reporter.hasErrors)
31-
}
32-
3329
def assertErrors(errors: Int, source: String)(implicit pos: Position): Unit = {
3430
compile(source)
3531
assert(compiler.reporter.errorCount == errors)
@@ -39,4 +35,12 @@ trait AnalyzerTest { this: Assertions =>
3935
compile(source)
4036
assert(!compiler.reporter.hasErrors)
4137
}
38+
39+
implicit final def stringContextToScalaInterpolator(sc: StringContext): ScalaInterpolator = new ScalaInterpolator(sc)
40+
}
41+
42+
object AnalyzerTest {
43+
final class ScalaInterpolator(private val sc: StringContext) extends AnyVal {
44+
def scala(args: Any*): String = s"object TopLevel {${sc.s(args *)}}"
45+
}
4246
}

analyzer/src/test/scala/com/avsystem/commons/analyzer/Any2StringAddTest.scala

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,29 @@ package analyzer
33

44
import org.scalatest.funsuite.AnyFunSuite
55

6-
class Any2StringAddTest extends AnyFunSuite with AnalyzerTest {
6+
final class Any2StringAddTest extends AnyFunSuite with AnalyzerTest {
77
test("any2stringadd should be rejected") {
8-
assertErrors(
9-
"""
10-
|object whatever {
11-
| whatever + "fag"
12-
|}
13-
""".stripMargin
14-
)
8+
assertErrors(1,
9+
scala"""
10+
|val any: Any = ???
11+
|any + "fag"
12+
|""".stripMargin)
1513
}
1614

1715
test("toString should not be rejected") {
1816
assertNoErrors(
19-
"""
20-
|object whatever {
21-
| whatever.toString + "fag"
22-
|}
23-
""".stripMargin
17+
scala"""
18+
|val any: Any = ???
19+
|any.toString + "fag"
20+
|""".stripMargin
2421
)
2522
}
2623

2724
test("string interpolation should not be rejected") {
2825
assertNoErrors(
29-
"""
30-
|object whatever {
31-
| s"${whatever}fag"
32-
|}
33-
""".stripMargin
34-
)
26+
scala"""
27+
|val any: Any = ???
28+
|s"$${any}fag"
29+
|""".stripMargin)
3530
}
3631
}

analyzer/src/test/scala/com/avsystem/commons/analyzer/BadSingletonComponentTest.scala

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@ package analyzer
33

44
import org.scalatest.funsuite.AnyFunSuite
55

6-
class BadSingletonComponentTest extends AnyFunSuite with AnalyzerTest {
6+
final class BadSingletonComponentTest extends AnyFunSuite with AnalyzerTest {
77
test("general") {
88
assertErrors(5,
9-
"""
10-
|import com.avsystem.commons.di._
11-
|
12-
|object test extends Components {
13-
| singleton(123)
14-
| val notDef = singleton(123)
15-
| def hasParams(param: Int) = singleton(param)
16-
| def hasTypeParams[T]: Component[T] = singleton(???)
17-
| def outerMethod: Component[Int] = {
18-
| def innerMethod = singleton(123)
19-
| innerMethod
20-
| }
21-
|
22-
| def good: Component[Int] = singleton(123)
23-
| def alsoGood: Component[Int] = { singleton(123) }
24-
| def goodAsWell: Component[Int] = singleton(123).dependsOn(good)
25-
|}
26-
""".stripMargin
9+
scala"""
10+
|import com.avsystem.commons.di._
11+
|
12+
|object test extends Components {
13+
| singleton(123)
14+
| val notDef = singleton(123)
15+
| def hasParams(param: Int) = singleton(param)
16+
| def hasTypeParams[T]: Component[T] = singleton(???)
17+
| def outerMethod: Component[Int] = {
18+
| def innerMethod = singleton(123)
19+
| innerMethod
20+
| }
21+
|
22+
| def good: Component[Int] = singleton(123)
23+
| def alsoGood: Component[Int] = { singleton(123) }
24+
| def goodAsWell: Component[Int] = singleton(123).dependsOn(good)
25+
|}
26+
|""".stripMargin
2727
)
2828
}
2929
}

analyzer/src/test/scala/com/avsystem/commons/analyzer/BasePackageTest.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package analyzer
33

44
import org.scalatest.funsuite.AnyFunSuite
55

6-
class BasePackageTest extends AnyFunSuite with AnalyzerTest {
6+
final class BasePackageTest extends AnyFunSuite with AnalyzerTest {
77
settings.pluginOptions.value ++= List("AVSystemAnalyzer:+basePackage:com.avsystem.commons")
88

99
test("base package only") {
10+
//language=Scala
1011
assertNoErrors(
1112
"""
1213
|package com.avsystem.commons
@@ -17,6 +18,7 @@ class BasePackageTest extends AnyFunSuite with AnalyzerTest {
1718

1819
test("chained base package") {
1920
assertNoErrors(
21+
//language=Scala
2022
"""
2123
|package com.avsystem
2224
|package commons
@@ -27,6 +29,7 @@ class BasePackageTest extends AnyFunSuite with AnalyzerTest {
2729

2830
test("base package with chained subpackage") {
2931
assertNoErrors(
32+
//language=Scala
3033
"""
3134
|package com.avsystem.commons
3235
|package core
@@ -37,6 +40,7 @@ class BasePackageTest extends AnyFunSuite with AnalyzerTest {
3740

3841
test("base package object") {
3942
assertNoErrors(
43+
//language=Scala
4044
"""
4145
|package com.avsystem
4246
|
@@ -46,6 +50,7 @@ class BasePackageTest extends AnyFunSuite with AnalyzerTest {
4650

4751
test("base package object with imports") {
4852
assertNoErrors(
53+
//language=Scala
4954
"""
5055
|package com.avsystem
5156
|
@@ -58,13 +63,15 @@ class BasePackageTest extends AnyFunSuite with AnalyzerTest {
5863

5964
test("no base package") {
6065
assertErrors(1,
66+
//language=Scala
6167
"""
6268
|object bar
6369
|""".stripMargin)
6470
}
6571

6672
test("no base package with imports") {
6773
assertErrors(1,
74+
//language=Scala
6875
"""
6976
|import scala.collection.mutable.Seq
7077
|import scala.collection.mutable.Set
@@ -76,6 +83,7 @@ class BasePackageTest extends AnyFunSuite with AnalyzerTest {
7683

7784
test("wrong base package") {
7885
assertErrors(1,
86+
//language=Scala
7987
"""
8088
|package com.avsystem.kommons
8189
|
@@ -85,6 +93,7 @@ class BasePackageTest extends AnyFunSuite with AnalyzerTest {
8593

8694
test("unchained subpackage") {
8795
assertErrors(1,
96+
//language=Scala
8897
"""
8998
|package com.avsystem.commons.core
9099
|
@@ -94,6 +103,7 @@ class BasePackageTest extends AnyFunSuite with AnalyzerTest {
94103

95104
test("unchained subpackage with imports") {
96105
assertErrors(1,
106+
//language=Scala
97107
"""
98108
|package com.avsystem.commons.core
99109
|

analyzer/src/test/scala/com/avsystem/commons/analyzer/CheckBincompatTest.scala

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,40 @@ package analyzer
33

44
import org.scalatest.funsuite.AnyFunSuite
55

6-
class CheckBincompatTest extends AnyFunSuite with AnalyzerTest {
6+
final class CheckBincompatTest extends AnyFunSuite with AnalyzerTest {
77
test("definitions of @bincompat annotated symbols should not be rejected") {
88
assertNoErrors(
9-
"""
10-
|import com.avsystem.commons.annotation.bincompat
11-
|
12-
|@bincompat class klass
13-
|
14-
|@bincompat object objekt {
15-
| @bincompat def method: Int = 42
16-
|}
17-
""".stripMargin
9+
scala"""
10+
|import com.avsystem.commons.annotation.bincompat
11+
|
12+
|@bincompat class klass
13+
|
14+
|@bincompat object objekt {
15+
| @bincompat def method: Int = 42
16+
|}
17+
|""".stripMargin
1818
)
1919
}
2020

2121
test("usage of @bincompat annotated symbols should be rejected") {
2222
assertErrors(3,
23-
"""
24-
|import com.avsystem.commons.annotation.bincompat
25-
|
26-
|@bincompat class klass
27-
|
28-
|@bincompat object objekt
29-
|
30-
|object outer {
31-
| @bincompat def method: Int = 42
32-
|}
33-
|
34-
|object test {
35-
| println(objekt)
36-
| println(new klass)
37-
| println(outer.method)
38-
|}
39-
""".stripMargin
23+
scala"""
24+
|import com.avsystem.commons.annotation.bincompat
25+
|
26+
|@bincompat class klass
27+
|
28+
|@bincompat object objekt
29+
|
30+
|object outer {
31+
| @bincompat def method: Int = 42
32+
|}
33+
|
34+
|object test {
35+
| println(objekt)
36+
| println(new klass)
37+
| println(outer.method)
38+
|}
39+
|""".stripMargin
4040
)
4141
}
4242
}
Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,60 @@
11
package com.avsystem.commons
22
package analyzer
33

4+
45
import org.scalatest.funsuite.AnyFunSuite
56

6-
class CheckMacroPrivateTest extends AnyFunSuite with AnalyzerTest {
7+
final class CheckMacroPrivateTest extends AnyFunSuite with AnalyzerTest {
78
test("macro private method invoked directly should be rejected") {
8-
assertErrors(
9-
"""
10-
|import com.avsystem.commons.analyzer.TestUtils
11-
|
12-
|object test {
13-
| TestUtils.macroPrivateMethod
14-
|}
15-
""".stripMargin
9+
assertErrors(1,
10+
scala"""
11+
|import com.avsystem.commons.analyzer.TestUtils
12+
|
13+
|object test {
14+
| TestUtils.macroPrivateMethod
15+
|}
16+
|""".stripMargin
1617
)
1718
}
1819

1920
test("macro private extractor used directly should be rejected") {
20-
assertErrors(
21-
"""
22-
|import com.avsystem.commons.analyzer.TestUtils
23-
|
24-
|object test {
25-
| 123 match {
26-
| case TestUtils.Extractor(_) =>
27-
| }
28-
|}
29-
""".stripMargin
21+
assertErrors(1,
22+
scala"""
23+
|import com.avsystem.commons.analyzer.TestUtils
24+
|
25+
|object test {
26+
| 123 match {
27+
| case TestUtils.Extractor(_) =>
28+
| }
29+
|}
30+
|""".stripMargin
3031
)
3132
}
3233

3334
test("macro private method invoked by macro-generated code should not be rejected") {
3435
assertNoErrors(
35-
"""
36-
|import com.avsystem.commons.analyzer.TestUtils
37-
|
38-
|object test {
39-
| TestUtils.invokeMacroPrivateMethod
40-
|}
41-
""".stripMargin
36+
scala"""
37+
|import com.avsystem.commons.analyzer.TestUtils
38+
|
39+
|object test {
40+
| TestUtils.invokeMacroPrivateMethod
41+
|}
42+
|""".stripMargin
4243
)
4344
}
4445

4546
test("definitions of macro private symbols themselves should not be rejected") {
4647
assertNoErrors(
47-
"""
48-
|import com.avsystem.commons.annotation.macroPrivate
49-
|
50-
|object test {
51-
| @macroPrivate def macroPrivateMethod = { println("whatever"); 5 }
52-
| @macroPrivate object macroPrivateObject {
53-
| final val X = 42
54-
| }
55-
|}
56-
""".stripMargin
48+
scala"""
49+
|import com.avsystem.commons.annotation.macroPrivate
50+
|
51+
|object test {
52+
| @macroPrivate def macroPrivateMethod = { println("whatever"); 5 }
53+
| @macroPrivate object macroPrivateObject {
54+
| final val X = 42
55+
| }
56+
|}
57+
|""".stripMargin
5758
)
5859
}
5960
}

0 commit comments

Comments
 (0)