module Tests {
class SimpleTest {
function add(a, b) {
return a + b;
}
(:test)
function test(logger) {
logger.debug("Createing callback...");
var m = method(:add);
logger.debug("Created callback.");
logger.debug("Calling callback...");
var res = m.invoke(1, 2);
logger.debug("Callback result: " + res);
return true;
}
}
}
The output for this test is:
Executing test Tests.SimpleTest.test...
DEBUG (14:47): Createing callback...
Failed invoking <symbol>
Invalid Value
ERROR
I've tried the following code as well (replaced "method()" call with "new Method()"):
module Tests {
class SimpleTest {
function add(a, b) {
return a + b;
}
(:test)
function test(logger) {
logger.debug("Createing callback...");
var m = new Toybox.Lang.Method(self, :add);
logger.debug("Created callback.");
logger.debug("Calling callback...");
var res = m.invoke(1, 2);
logger.debug("Callback result: " + res);
return true;
}
}
}
This produces a slightly different output:
Executing test Tests.SimpleTest.test...
DEBUG (14:54): Createing callback...
DEBUG (14:54): Created callback.
DEBUG (14:54): Calling callback...
Failed invoking <symbol>
Symbol Not Found Error
ERROR
However, extracting the "add()" into a separate module works:
module Ops {
function add(a, b) {
return a + b;
}
}
module Tests {
class SimpleTest {
(:test)
function test(logger) {
logger.debug("Createing callback...");
var m = new Toybox.Lang.Method(Ops, :add);
logger.debug("Created callback.");
logger.debug("Calling callback...");
var res = m.invoke(1, 2);
logger.debug("Callback result: " + res);
return true;
}
}
}
It produces the following output:
Executing test Tests.SimpleTest.test...
DEBUG (15:05): Createing callback...
DEBUG (15:05): Created callback.
DEBUG (15:05): Calling callback...
DEBUG (15:05): Callback result: 3
PASS
A couple of notes:
- I didn't try callbacks on real devices.
- I did try this code only in unit tests.
- I would not like to remove this logic from unit tests, because I'm covering some utility functions, which I need to get tested.