I want to be able to write jasmine-like tests in Matlab. So something like
expect(myfibonacci(0)).toBe(0);
expect(myfibonacci(5)).toBe(15);
expect(myfibonacci(10)).toBe(55);
There are two strategies I tried to implement this:
(1) the first strategy uses structs
expect = @(actual_value) struct('toBe', @(expected_value) assert(actual_value == expected_value));
(The real implementation will not just call assert)
However this does not work:
expect(1).toBe(1); % this triggers a syntax error
??? Improper index matrix reference.
% this will work:
x = expect(1);
x.toBe(1);
(2) The second strategy I tried is using a class:
classdef expect
properties (Hidden)
actual_value
end
methods
function obj = expect(actual_value)
obj.actual_value = actual_value;
end
function obj = toBe(obj, expected_value)
assert(obj.actual_value == expected_value);
end
end
end
At first glance this looks fine: You can run in the console
expect(1).toBe(1);
However, running this not in the console but in a script gives
??? Static method or constructor invocations cannot be indexed.
Do not follow the call to the static method or constructor with
any additional indexing or dot references.
Error in ==> test at 1
expect(1).toBe(1);
Is here any way to make this idea work in matlab at all?