Skip to content

Commit

Permalink
Add tests; update coverage exclusions in t/
Browse files Browse the repository at this point in the history
- Additional tests of VMV
- Exclude from coverage lines in t/*.vala that cannot be reached
  on a successful test run
  • Loading branch information
cxw42 committed Mar 5, 2021
1 parent 53028ff commit 7284b5a
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 33 deletions.
2 changes: 1 addition & 1 deletion t/100-vmv-t.vala
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void test_misc() // for coverage
""");
assert_true(ok == SUCCESS);
if(ok != SUCCESS) {
return;
return; // LCOV_EXCL_LINE - unreachable on a successful test
}

assert_true(vm.has_module("main"));
Expand Down
2 changes: 1 addition & 1 deletion t/120-read-var-from-wren-t.vala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void test_read()
""");
assert_true(ok == SUCCESS);
if(ok != SUCCESS) {
return;
return; // LCOV_EXCL_LINE - unreachable on a successful test
}

vm.ensure_slots(1);
Expand Down
8 changes: 4 additions & 4 deletions t/130-call-wren-t.vala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void test_call()
var vm = new Wren.VMV.with_configuration(conf);
assert_nonnull(vm);
if(vm == null) {
return;
return; // LCOV_EXCL_LINE - unreachable on a successful test
}

var ok = vm.interpret("main", """
Expand All @@ -28,23 +28,23 @@ void test_call()
""");
assert_true(ok == SUCCESS);
if(ok != SUCCESS) {
return;
return; // LCOV_EXCL_LINE - unreachable on a successful test
}

vm.ensure_slots(2);

var printhandle = vm.make_call_handle("print(_)");
assert_nonnull(printhandle);
if(printhandle == null) {
return;
return; // LCOV_EXCL_LINE - unreachable on a successful test
}

vm.get_variable("main", "System", 0);
vm.set_slot_string(1, "Hello");
ok = vm.call(printhandle);
assert_true(ok == SUCCESS);
if(ok != SUCCESS) {
return;
return; // LCOV_EXCL_LINE - unreachable on a successful test
}

assert_cmpstr(g_str, EQ, "Hello\n");
Expand Down
128 changes: 101 additions & 27 deletions t/140-roundtrip-data-t.vala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,31 @@ errordomain Err {
}

private Wren.VMV g_vm;
private HandleV g_stash;

// Writes the variable in g_vm's slot 1 to g_vm's main.Answer.
private void stash() throws Err
{
g_vm.get_variable("main", "C", 0);
var ok = g_vm.call(g_stash);
assert_cmpuint(ok, EQ, InterpretResult.SUCCESS);
if(ok != InterpretResult.SUCCESS) {
throw new Err.OOPS("Couldn't set string value"); // LCOV_EXCL_LINE - unreachable on a successful test
}
}

// Throws unless g_vm's main.Answer holds a value of the given type
void confirm(Wren.Type want) throws Err
{
g_vm.get_variable("main", "Answer", 0);
var ty = g_vm.get_slot_type(0);
assert_cmpuint(ty, EQ, want);
if(ty != want) {
// LCOV_EXCL_START
throw new Err.OOPS("Wrong type --- got %s but expected %s",
ty.to_string(), want.to_string());
} // LCOV_EXCL_STOP
}

void test_call()
{
Expand All @@ -23,49 +48,98 @@ void test_call()
}
""");
if(ok != InterpretResult.SUCCESS) {
throw new Err.OOPS("Got %s; expected SUCCESS\n".printf(ok.to_string()));
throw new Err.OOPS("Got %s; expected SUCCESS\n".printf(ok.to_string())); // LCOV_EXCL_LINE - unreachable on a successful test
}

g_vm.ensure_slots(2);

var stashhandle = g_vm.make_call_handle("stash(_)");
if(stashhandle == null) {
throw new Err.OOPS("Couldn't get stash(_) handle");
g_stash = g_vm.make_call_handle("stash(_)");
if(g_stash == null) {
throw new Err.OOPS("Couldn't get stash(_) handle"); // LCOV_EXCL_LINE - unreachable on a successful test
}

g_vm.get_variable("main", "Answer", 0);
var ty = g_vm.get_slot_type(0);
assert_cmpuint(ty, EQ, Wren.Type.NULL);

// Write a string by calling C.stash()
g_vm.get_variable("main", "C", 0);
confirm(NULL); // Before writing anything

// Now, for each type of Wren value, write by calling stash() and then
// read using confirm() and get_slot_*(). Tests are in order of
// Wren.Type value.

// BOOL
g_vm.set_slot_bool(1, true);
stash();
confirm(BOOL);
var boo = g_vm.get_slot_bool(0);
assert_true(boo);

// NUM
g_vm.set_slot_double(1, 42);
stash();
confirm(NUM);
assert_cmpfloat(g_vm.get_slot_double(0), EQ, 42);

g_vm.set_slot_double(1, 3.14159);
stash();
confirm(NUM);
assert_cmpfloat(g_vm.get_slot_double(0), EQ, 3.14159);

// FOREIGN

// LIST
g_vm.set_slot_new_list(1);
stash();
confirm(LIST);
assert_cmpint(g_vm.get_list_count(0), EQ, 0);

// MAP
g_vm.set_slot_new_map(1);
stash();
confirm(MAP);
assert_cmpint(g_vm.get_map_count(0), EQ, 0);

// NULL
g_vm.set_slot_null(1);
stash();
confirm(NULL);

// STRING
g_vm.set_slot_string(1, "Hello");
ok = g_vm.call(stashhandle);
assert_cmpuint(ok, EQ, InterpretResult.SUCCESS);
if(ok != InterpretResult.SUCCESS) {
throw new Err.OOPS("Couldn't set string value");
stash();

confirm(STRING);
var str = g_vm.get_slot_string(0);
assert_cmpstr(str, EQ, "Hello");

// byte arrays - also STRING
uint8 arr[3] = {0x00, 0xc1, 0x40};
g_vm.set_slot_bytes(1, arr);
stash();

confirm(STRING);
var otherarr = g_vm.get_slot_bytes(0);
assert_cmpuint(arr.length, EQ, otherarr.length);
for(int i=0; i<otherarr.length; ++i) {
print("# %d %02x\n", i, otherarr[i]);
}
if(arr.length != otherarr.length)
{
throw new Err.OOPS("Length mismatch"); // LCOV_EXCL_LINE - unreachable on a successful test
}

// Check the string
g_vm.get_variable("main", "Answer", 0);
ty = g_vm.get_slot_type(0);
assert_cmpuint(ty, EQ, Wren.Type.STRING);
if(ty == Wren.Type.STRING) {
var str = g_vm.get_slot_string(0);
assert_cmpstr(str, EQ, "Hello");
for(int i=0; i<arr.length; ++i) {
assert_cmpuint(arr[i], EQ, otherarr[i]);
}

// TODO test other types
// UNKNOWN - any way to test this?

assert_true(true);
} catch(Err e) {
} catch(Err e) { // LCOV_EXCL_START
if(e is Err.BAIL) {
print("Bail out! %s\n", e.message);
} else {
print("# Got error %s\n", e.message);
}
assert_not_reached();
}
} // LCOV_EXCL_STOP
} // test_call

public static int main(string[] args)
Expand All @@ -79,19 +153,19 @@ public static int main(string[] args)
g_vm = new Wren.VMV();
assert_nonnull(g_vm);
if(g_vm == null) {
throw new Err.BAIL("Couldn't create VM");
throw new Err.BAIL("Couldn't create VM"); // LCOV_EXCL_LINE - unreachable on a successful test
}
exitcode = Test.run();

} catch(Err e) {
} catch(Err e) { // LCOV_EXCL_START
if(e is Err.BAIL) {
print("Bail out! %s\n", e.message);
} else {
print("# Got error %s\n", e.message);
}
exitcode = 1;
assert_not_reached();
}
} // LCOV_EXCL_STOP

g_vm = null;

Expand Down

0 comments on commit 7284b5a

Please sign in to comment.