0

I want to write an arch unit test to assert that a class extends AnyVal type.

val rule = classes().should().beAssignableTo(classOf[AnyVal])
val importedClasses = new ClassFileImporter().importPackages("a.b.c")

isAnyVal.check(importedClasses) // Always returns true

The above code doesn't actually catch anything and passes for classes that don't extend AnyVal also.

tusharmath
  • 10,622
  • 12
  • 56
  • 83
  • 3
    Why not enforcing this on type-level and getting rid of the need of testing? Especially since compile-time `AnyVal`s don't map exactly into classes extending `AnyVal` interface? – Mateusz Kubuszok Jun 06 '20 at 09:47
  • I want that devs, while creating a new type, always ensure that it extends AnyVal. – tusharmath Jun 07 '20 at 06:20

1 Answers1

0

classOf[AnyVal] is java.lang.Object, so you are just asking that all classes extend Object, which they do.

From ArchUnit user guide:

It does so by analyzing given Java bytecode, importing all classes into a Java code structure.

I was hoping you'd get Class etc. and could go into Scala reflection from there, even if you wouldn't get the nice DSL, but they use their own API instead.

So to answer Scala-specific questions, it would need to parse @ScalaSignature annotations and that would probably be a very large effort for the developers (not to mention maintenance, or dependence on specific Scala version at least until Scala 3).

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487