Skip to main content

module lang::flybytes::tests::ClassTests

rascal-0.40.17
flybytes-0.2.8

Usage

import lang::flybytes::tests::ClassTests;

Source code

http://github.com/usethesource/flybytes/blob/main/src/lang/flybytes/tests/ClassTests.rsc

Dependencies

import lang::flybytes::Compiler;
import lang::flybytes::Mirror;
import lang::flybytes::api::JavaLang;
import lang::flybytes::api::System;

function testClass

Class testClass()

function compiledTestClass

Mirror compiledTestClass()

function extendClass

Class extendClass()

function main

void main()

Tests

test newInstanceGetUnitializedInteger

test bool newInstanceGetUnitializedInteger() {
c = compiledTestClass();
i = c.newInstance(constructorDesc([]),[]);
return i.getField("field").toValue(#int) == 0;
}

test newInstanceCallGetter

test bool newInstanceCallGetter() {
c = compiledTestClass();
i = c.newInstance(constructorDesc([]),[]);
return i.invoke(methodDesc(integer(), "getField", []), []).toValue(#int) == 0;
}

test newInstanceCallSetter

test bool newInstanceCallSetter() {
c = compiledTestClass();
i = c.newInstance(constructorDesc([]),[]);

int tester = 32;

// call method with the side-effect
i.invoke(methodDesc(\void(), "setField", [integer()]), [integer(tester)]);

// test if it worked by retrieving the field
return i.getField("field").toValue(#int) == tester;
}

test staticMethod

test bool staticMethod() {
c = compiledTestClass();
int tester = 666;
c.invokeStatic(methodDesc(\void(), "putStatic", [integer()]), [integer(tester)]);
r = c.getStatic("staticField").toValue(#int);
c.invokeStatic(methodDesc(\void(), "putStatic", [integer()]), [integer(42)]); // set it back
return r == tester;
}

test staticFieldInitializer

test bool staticFieldInitializer() 
= compiledTestClass().getStatic("staticField").toValue(#int) == 42;

test extendTest

test bool extendTest() {
// load the classes together
cs = loadClasses([extendClass(), testClass()], prefix=just(|project://flybytes/generated/|));

// get a mirror instance of the subclass
c = cs["ExtendedClass"];
i = c.newInstance(constructorDesc([]),[]);

// call super method with a side-effect
return i.invoke(methodDesc(\void(), "testMethod", []), []).toValue(#bool);
}