I want to be able to write jasmine in matlab. So something like
expect(myfibonacci(0)).toBe(0); expect(myfibonacci(5)).toBe(15); expect(myfibonacci(10)).toBe(55);
There are two strategies that I tried to implement:
(1) The first strategy uses structures
expect = @(actual_value) struct('toBe', @(expected_value) assert(actual_value == expected_value));
(The actual 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 to use 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 it looks great: you can start the console
expect(1).toBe(1);
However, running this is not in the console, but in the 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 there any way to get this idea working in Matlab at all?
source share