|
| 1 | +package checks |
| 2 | + |
| 3 | +import reflect.ClassTag |
| 4 | + |
| 5 | + |
| 6 | +trait Checks: |
| 7 | + val expectedTopLevelChecksCount: Int |
| 8 | + val expectedMemberChecksCount: Int |
| 9 | + val expectedLocalChecksCount: Int |
| 10 | + |
| 11 | + var topLevelChecksCount = 0 |
| 12 | + var memberChecksCount = 0 |
| 13 | + var localChecksCount = 0 |
| 14 | + |
| 15 | + def verifyChecksCounts() = |
| 16 | + assert(topLevelChecksCount == expectedTopLevelChecksCount, |
| 17 | + s"top level checks: expected $expectedTopLevelChecksCount but was $topLevelChecksCount") |
| 18 | + assert(memberChecksCount == expectedMemberChecksCount, |
| 19 | + s"member checks: expected $expectedMemberChecksCount but was $memberChecksCount") |
| 20 | + assert(localChecksCount == expectedLocalChecksCount, |
| 21 | + s"local checks: expected $expectedLocalChecksCount but was $localChecksCount") |
| 22 | + |
| 23 | + // The methods below rely on the naming convention described in TestCases.scala |
| 24 | + |
| 25 | + /** Check JVM class properties of a top level class */ |
| 26 | + def checkTopLevel(self: AnyRef) = |
| 27 | + val cls = self.getClass |
| 28 | + assert(cls.getEnclosingClass == null, s"Top level class $cls should have no enclosing class") |
| 29 | + assert(cls.getDeclaringClass == null, s"Top level class $cls should have no declaring class") |
| 30 | + assert(cls.getEnclosingMethod == null, s"Top level class $cls should have no enclosing method") |
| 31 | + topLevelChecksCount += 1 |
| 32 | + |
| 33 | + /** Check JVM class properties of a member class (defined directly inside another class) */ |
| 34 | + def checkMember(self: AnyRef, outer: AnyRef) = |
| 35 | + val cls = self.getClass |
| 36 | + def className = cls.simpleName |
| 37 | + def enclosingClassName = cls.getEnclosingClass.simpleName |
| 38 | + def declaringClassName = cls.getDeclaringClass.simpleName |
| 39 | + // Classes defined directly in top level objects should be moved to their companion/mirror classes |
| 40 | + val expectedEnclosingClassName = outer.getClass.simpleName match |
| 41 | + case "B$" => "B" |
| 42 | + case "C$" => "C" |
| 43 | + case name => name |
| 44 | + assert(cls.getEnclosingClass != null, |
| 45 | + s"Member class $className should have an enclosing class") |
| 46 | + assert(enclosingClassName == expectedEnclosingClassName, |
| 47 | + s"The enclosing class of class $className should be $expectedEnclosingClassName but was $enclosingClassName") |
| 48 | + assert(cls.getDeclaringClass == cls.getEnclosingClass, |
| 49 | + s"The declaring class of class $className should be the same as its enclosing class but was $declaringClassName") |
| 50 | + assert(cls.getEnclosingMethod == null, |
| 51 | + s"Member class $className should have no enclosing method") |
| 52 | + memberChecksCount += 1 |
| 53 | + |
| 54 | + /** Check JVM class properties of a local class (defined directly inside a method) */ |
| 55 | + def checkLocal(self: AnyRef, outer: AnyRef) = |
| 56 | + val cls = self.getClass |
| 57 | + def className = cls.simpleName |
| 58 | + def enclosingClassName = cls.getEnclosingClass.simpleName |
| 59 | + def method = cls.getEnclosingMethod |
| 60 | + val expectedEnclosingClassName = outer.getClass.simpleName |
| 61 | + // extracting method name basing on the described naming convention |
| 62 | + // $1 gets added during lambdaLift in case of a method defined inside another method |
| 63 | + val expectedEnclosingMethodName = |
| 64 | + val prefix = className.init |
| 65 | + val suffix = if prefix.filter(_.isLetter).endsWith("DD") then "$1" else "" |
| 66 | + prefix ++ suffix |
| 67 | + assert(cls.getEnclosingClass != null, |
| 68 | + s"Local class $className should have an enclosing class") |
| 69 | + assert(enclosingClassName == expectedEnclosingClassName, |
| 70 | + s"The enclosing class of class $className should be $expectedEnclosingClassName but was $enclosingClassName") |
| 71 | + assert(cls.getDeclaringClass == null, |
| 72 | + s"Local class $className should have no declaring class") |
| 73 | + assert(method != null, |
| 74 | + s"Local class $className should have an enclosing method") |
| 75 | + assert(method.getName == expectedEnclosingMethodName, |
| 76 | + s"The enclosing method of class $className should be $expectedEnclosingMethodName but was ${method.getName}") |
| 77 | + localChecksCount += 1 |
| 78 | + |
| 79 | + extension (cls: Class[?]) |
| 80 | + // java 8 implementation of cls.getSimpleName() is buggy - https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8057919 |
| 81 | + def simpleName = cls.getName |
| 82 | + .stripSuffix("$1").stripSuffix("$2") |
| 83 | + .split("\\.").last |
| 84 | + .split("\\$").last |
0 commit comments