Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.avsystem.commons
package analyzer

import com.avsystem.commons.analyzer.AnalyzerTest.ScalaInterpolator
import org.scalactic.source.Position
import org.scalatest.Assertions

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

def assertErrors(source: String)(implicit pos: Position): Unit = {
compile(source)
assert(compiler.reporter.hasErrors)
}

def assertErrors(errors: Int, source: String)(implicit pos: Position): Unit = {
compile(source)
assert(compiler.reporter.errorCount == errors)
Expand All @@ -39,4 +35,12 @@ trait AnalyzerTest { this: Assertions =>
compile(source)
assert(!compiler.reporter.hasErrors)
}

implicit final def stringContextToScalaInterpolator(sc: StringContext): ScalaInterpolator = new ScalaInterpolator(sc)
}

object AnalyzerTest {
final class ScalaInterpolator(private val sc: StringContext) extends AnyVal {
def scala(args: Any*): String = s"object TopLevel {${sc.s(args *)}}"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,29 @@ package analyzer

import org.scalatest.funsuite.AnyFunSuite

class Any2StringAddTest extends AnyFunSuite with AnalyzerTest {
final class Any2StringAddTest extends AnyFunSuite with AnalyzerTest {
test("any2stringadd should be rejected") {
assertErrors(
"""
|object whatever {
| whatever + "fag"
|}
""".stripMargin
)
assertErrors(1,
scala"""
|val any: Any = ???
|any + "fag"
|""".stripMargin)
}

test("toString should not be rejected") {
assertNoErrors(
"""
|object whatever {
| whatever.toString + "fag"
|}
""".stripMargin
scala"""
|val any: Any = ???
|any.toString + "fag"
|""".stripMargin
)
}

test("string interpolation should not be rejected") {
assertNoErrors(
"""
|object whatever {
| s"${whatever}fag"
|}
""".stripMargin
)
scala"""
|val any: Any = ???
|s"$${any}fag"
|""".stripMargin)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@ package analyzer

import org.scalatest.funsuite.AnyFunSuite

class BadSingletonComponentTest extends AnyFunSuite with AnalyzerTest {
final class BadSingletonComponentTest extends AnyFunSuite with AnalyzerTest {
test("general") {
assertErrors(5,
"""
|import com.avsystem.commons.di._
|
|object test extends Components {
| singleton(123)
| val notDef = singleton(123)
| def hasParams(param: Int) = singleton(param)
| def hasTypeParams[T]: Component[T] = singleton(???)
| def outerMethod: Component[Int] = {
| def innerMethod = singleton(123)
| innerMethod
| }
|
| def good: Component[Int] = singleton(123)
| def alsoGood: Component[Int] = { singleton(123) }
| def goodAsWell: Component[Int] = singleton(123).dependsOn(good)
|}
""".stripMargin
scala"""
|import com.avsystem.commons.di._
|
|object test extends Components {
| singleton(123)
| val notDef = singleton(123)
| def hasParams(param: Int) = singleton(param)
| def hasTypeParams[T]: Component[T] = singleton(???)
| def outerMethod: Component[Int] = {
| def innerMethod = singleton(123)
| innerMethod
| }
|
| def good: Component[Int] = singleton(123)
| def alsoGood: Component[Int] = { singleton(123) }
| def goodAsWell: Component[Int] = singleton(123).dependsOn(good)
|}
|""".stripMargin
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package analyzer

import org.scalatest.funsuite.AnyFunSuite

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

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

test("chained base package") {
assertNoErrors(
//language=Scala
"""
|package com.avsystem
|package commons
Expand All @@ -27,6 +29,7 @@ class BasePackageTest extends AnyFunSuite with AnalyzerTest {

test("base package with chained subpackage") {
assertNoErrors(
//language=Scala
"""
|package com.avsystem.commons
|package core
Expand All @@ -37,6 +40,7 @@ class BasePackageTest extends AnyFunSuite with AnalyzerTest {

test("base package object") {
assertNoErrors(
//language=Scala
"""
|package com.avsystem
|
Expand All @@ -46,6 +50,7 @@ class BasePackageTest extends AnyFunSuite with AnalyzerTest {

test("base package object with imports") {
assertNoErrors(
//language=Scala
"""
|package com.avsystem
|
Expand All @@ -58,13 +63,15 @@ class BasePackageTest extends AnyFunSuite with AnalyzerTest {

test("no base package") {
assertErrors(1,
//language=Scala
"""
|object bar
|""".stripMargin)
}

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

test("wrong base package") {
assertErrors(1,
//language=Scala
"""
|package com.avsystem.kommons
|
Expand All @@ -85,6 +93,7 @@ class BasePackageTest extends AnyFunSuite with AnalyzerTest {

test("unchained subpackage") {
assertErrors(1,
//language=Scala
"""
|package com.avsystem.commons.core
|
Expand All @@ -94,6 +103,7 @@ class BasePackageTest extends AnyFunSuite with AnalyzerTest {

test("unchained subpackage with imports") {
assertErrors(1,
//language=Scala
"""
|package com.avsystem.commons.core
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,40 @@ package analyzer

import org.scalatest.funsuite.AnyFunSuite

class CheckBincompatTest extends AnyFunSuite with AnalyzerTest {
final class CheckBincompatTest extends AnyFunSuite with AnalyzerTest {
test("definitions of @bincompat annotated symbols should not be rejected") {
assertNoErrors(
"""
|import com.avsystem.commons.annotation.bincompat
|
|@bincompat class klass
|
|@bincompat object objekt {
| @bincompat def method: Int = 42
|}
""".stripMargin
scala"""
|import com.avsystem.commons.annotation.bincompat
|
|@bincompat class klass
|
|@bincompat object objekt {
| @bincompat def method: Int = 42
|}
|""".stripMargin
)
}

test("usage of @bincompat annotated symbols should be rejected") {
assertErrors(3,
"""
|import com.avsystem.commons.annotation.bincompat
|
|@bincompat class klass
|
|@bincompat object objekt
|
|object outer {
| @bincompat def method: Int = 42
|}
|
|object test {
| println(objekt)
| println(new klass)
| println(outer.method)
|}
""".stripMargin
scala"""
|import com.avsystem.commons.annotation.bincompat
|
|@bincompat class klass
|
|@bincompat object objekt
|
|object outer {
| @bincompat def method: Int = 42
|}
|
|object test {
| println(objekt)
| println(new klass)
| println(outer.method)
|}
|""".stripMargin
)
}
}
Original file line number Diff line number Diff line change
@@ -1,59 +1,60 @@
package com.avsystem.commons
package analyzer


import org.scalatest.funsuite.AnyFunSuite

class CheckMacroPrivateTest extends AnyFunSuite with AnalyzerTest {
final class CheckMacroPrivateTest extends AnyFunSuite with AnalyzerTest {
test("macro private method invoked directly should be rejected") {
assertErrors(
"""
|import com.avsystem.commons.analyzer.TestUtils
|
|object test {
| TestUtils.macroPrivateMethod
|}
""".stripMargin
assertErrors(1,
scala"""
|import com.avsystem.commons.analyzer.TestUtils
|
|object test {
| TestUtils.macroPrivateMethod
|}
|""".stripMargin
)
}

test("macro private extractor used directly should be rejected") {
assertErrors(
"""
|import com.avsystem.commons.analyzer.TestUtils
|
|object test {
| 123 match {
| case TestUtils.Extractor(_) =>
| }
|}
""".stripMargin
assertErrors(1,
scala"""
|import com.avsystem.commons.analyzer.TestUtils
|
|object test {
| 123 match {
| case TestUtils.Extractor(_) =>
| }
|}
|""".stripMargin
)
}

test("macro private method invoked by macro-generated code should not be rejected") {
assertNoErrors(
"""
|import com.avsystem.commons.analyzer.TestUtils
|
|object test {
| TestUtils.invokeMacroPrivateMethod
|}
""".stripMargin
scala"""
|import com.avsystem.commons.analyzer.TestUtils
|
|object test {
| TestUtils.invokeMacroPrivateMethod
|}
|""".stripMargin
)
}

test("definitions of macro private symbols themselves should not be rejected") {
assertNoErrors(
"""
|import com.avsystem.commons.annotation.macroPrivate
|
|object test {
| @macroPrivate def macroPrivateMethod = { println("whatever"); 5 }
| @macroPrivate object macroPrivateObject {
| final val X = 42
| }
|}
""".stripMargin
scala"""
|import com.avsystem.commons.annotation.macroPrivate
|
|object test {
| @macroPrivate def macroPrivateMethod = { println("whatever"); 5 }
| @macroPrivate object macroPrivateObject {
| final val X = 42
| }
|}
|""".stripMargin
)
}
}
Loading