Skip to content

Commit

Permalink
Add a reflection based construction tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Gene Gleyzer committed Mar 5, 2024
1 parent 9b72fc3 commit ec398b3
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 10 deletions.
7 changes: 5 additions & 2 deletions tck/src/main/x/tck.x
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ module tck.xtclang.org {
void run() {
// constructors.Basic b = new constructors.Basic();
// b.testUnFreezable();
//

// constructors.Medium m = new constructors.Medium();
// m.testFinalizerChain();
// m.testAssertChain();

// constructors.Reflect r = new constructors.Reflect();
// r.testAssertChain();
}
}
31 changes: 23 additions & 8 deletions tck/src/main/x/tck/constructors/Medium.x
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@
*/
class Medium {

void run() {
testAssert();
testFinalizer();
testFinalizerChain();
}

/**
* At the end of construction the assert() function must be invoked.
* At the end of construction the validator (assert function) must be invoked.
*/
@Test
void testAssert() {
Expand All @@ -28,6 +22,27 @@ class Medium {
}
}

/**
* The validators must be invoked in the natural inheritance order (derived first).
*/
@Test
void testAssertChain() {
static class Base(String s) {
assert() {
this.s = s + "-BA";
}
}
static class Derived(String s)
extends Base(s) {
assert() {
this.s = s + "-DA";
}
}

Base b = new Derived("Test");
assert b.s == "Test-DA-BA";
}

/**
* At the end of construction the finalizer must be invoked.
*/
Expand Down Expand Up @@ -67,7 +82,7 @@ class Medium {
}
}
static class Derived(String s)
extends Base {
extends Base {

construct(String s) {
construct Base(s + "-DC");
Expand Down
73 changes: 73 additions & 0 deletions tck/src/main/x/tck/constructors/Reflect.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Tests for construction via reflection.
*/
class Reflect {

/**
* At the end of reflection-based instantiation all properties must be assigned.
*/
@Test
void testUnassigned() {
static class Test(Int a1, String a2);

try {
Class<public Test, protected Test, private Test, struct Test> clz = Test;
assert (struct Test) structure := clz.allocate();
structure.a1 = 4;
// structure.a2 assignment is missing
Test t = clz.instantiate(structure);
assert;
} catch (IllegalState e) {
assert e.message.indexOf("a2");
}
}

/**
* At the end of reflection-based instantiation the validator (assert function) must be invoked.
*/
@Test
void testAssert() {
static class Test(Int a1, String a2) {
assert() {
assert a2.size == a1;
}
}

try {
Class<Test> clz = Test;
assert Struct structure := clz.allocate();
assert structure.is(struct Test);
structure.a1 = 4;
structure.a2 = "hello";
Test t = clz.instantiate(structure);
assert;
} catch (IllegalState e) {
assert e.message.indexOf("a2.size");
}
}

/**
* The validators must be invoked in the natural inheritance order (derived first).
*/
@Test
void testAssertChain() {
static class Base(String s) {
assert() {
this.s = s + "-BA";
}
}
static class Derived(String s)
extends Base(s) {
assert() {
this.s = s + "-DA";
}
}

Class<Derived> clz = Derived;
assert Struct structure := clz.allocate();
assert structure.is(struct Derived);
structure.s = "Test";
Derived d = clz.instantiate(structure);
assert d.s == "Test-DA-BA";
}
}

0 comments on commit ec398b3

Please sign in to comment.