1
1
package japgolly .univeq
2
2
3
- class BytecodeTest {
4
- def testByte (a : Byte ) = (a !=* a) || (a ==* a)
5
- def testChar (a : Char ) = (a !=* a) || (a ==* a)
6
- def testShort (a : Short ) = (a !=* a) || (a ==* a)
7
- def testInt (a : Int ) = (a !=* a) || (a ==* a)
8
- def testLong (a : Long ) = (a !=* a) || (a ==* a)
9
- def testFloat (a : Float ) = (a !=* a) || (a ==* a)
10
- def testDouble (a : Double ) = (a !=* a) || (a ==* a)
11
- def testBoolean (a : Boolean ) = (a !=* a) || (a ==* a)
12
- }
3
+ import scala .sys .process ._
4
+ import utest .{assert => _ , _ }
13
5
14
- /*
15
- TODO: Add a bytecode test to sbt
6
+ object BytecodeTest extends TestSuite {
16
7
17
- > javap -c univeq/jvm/target/scala-3*"/"test-classes/japgolly/univeq/BytecodeTest.class
8
+ class TestSubject {
9
+ def testByte (a : Byte ) = (a !=* a) || (a ==* a)
10
+ def testChar (a : Char ) = (a !=* a) || (a ==* a)
11
+ def testShort (a : Short ) = (a !=* a) || (a ==* a)
12
+ def testInt (a : Int ) = (a !=* a) || (a ==* a)
13
+ def testLong (a : Long ) = (a !=* a) || (a ==* a)
14
+ def testFloat (a : Float ) = (a !=* a) || (a ==* a)
15
+ def testDouble (a : Double ) = (a !=* a) || (a ==* a)
16
+ def testBoolean (a : Boolean ) = (a !=* a) || (a ==* a)
17
+ }
18
18
19
- Compiled from "BytecodeTest.scala"
20
- public class japgolly.univeq.BytecodeTest {
21
- public japgolly.univeq.BytecodeTest();
22
- Code:
23
- 0: aload_0
24
- 1: invokespecial #9 // Method java/lang/Object."<init>":()V
25
- 4: return
19
+ // Force class evaluation
20
+ private val testSubject = new TestSubject
21
+
22
+ private def classesDir = System .getProperty(" classes.dir" )
23
+
24
+ private lazy val actualDisassembly = {
25
+ val clsFile = s " $classesDir/japgolly/univeq/BytecodeTest $$ TestSubject.class "
26
+ Seq (" javap" , " -c" , clsFile).!!
27
+ }
28
+
29
+ private def methodBytecode (name : String , disassembly : String ) =
30
+ disassembly
31
+ .linesIterator
32
+ .dropWhile(! _.matches(s " ^ public \\ S+ $name(?: \\ (.* \\ ))?; $$ " ))
33
+ .takeWhile(_.trim.nonEmpty)
34
+ .mkString(" \n " )
35
+
36
+ private def testMethodBytecode (method : String ): Unit = {
37
+ val actual = methodBytecode(method, actualDisassembly)
38
+ val expect = methodBytecode(method, expectedDisassembly)
39
+ // assert(actual == expect)
40
+ assert(actual == expect, s " Bytecode mismatch for $method() \n\n $actual\n " )
41
+ }
42
+
43
+ // > javap -c univeq/jvm/target/scala-3*/test-classes/japgolly/univeq/'BytecodeTest$TestSubject.class' >> univeq/jvm/src/test/scala/japgolly/univeq/BytecodeTest.scala
44
+ private def expectedDisassembly = """
26
45
27
46
public boolean testByte(byte);
28
47
Code:
@@ -262,4 +281,16 @@ public class japgolly.univeq.BytecodeTest {
262
281
42: iconst_0
263
282
43: ireturn
264
283
}
265
- */
284
+ """
285
+
286
+ override def tests = Tests {
287
+ " testByte" - testMethodBytecode(" testByte" )
288
+ " testChar" - testMethodBytecode(" testChar" )
289
+ " testShort" - testMethodBytecode(" testShort" )
290
+ " testInt" - testMethodBytecode(" testInt" )
291
+ " testLong" - testMethodBytecode(" testLong" )
292
+ " testFloat" - testMethodBytecode(" testFloat" )
293
+ " testDouble" - testMethodBytecode(" testDouble" )
294
+ " testBoolean" - testMethodBytecode(" testBoolean" )
295
+ }
296
+ }
0 commit comments