MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
methods(Access=protected)
function diag = getNegativeDiagnosticFor(constraint, actual)
import matlab.unittest.diagnostics.StringDiagnostic
if constraint.satisfiedBy(actual)
diag = StringDiagnostic(sprintf(...
['Negated HasSameSizeAs failed.\nSize [%s] of ' ...
'Actual Value and Expected Value were the same ' ...
'but should not have been.'], int2str(size(actual))));
else
diag = StringDiagnostic('Negated HasSameSizeAs passed.');
end
end
end

In exchange for implementing the required methods, the constraint inherits the
appropriate and, or, and not overloads so it can be combined with other
BooleanConstraint objects or negated.

HasSameSizeAs Class Definition Summary
classdef HasSameSizeAs < matlab.unittest.constraints.BooleanConstraint
properties(SetAccess=immutable)
ValueWithExpectedSize
end
methods
function constraint = HasSameSizeAs(value)
constraint.ValueWithExpectedSize = value;
end
function bool = satisfiedBy(constraint, actual)
bool = isequal(size(actual), size(constraint.ValueWithExpectedSize));
end
function diag = getDiagnosticFor(constraint, actual)
import matlab.unittest.diagnostics.StringDiagnostic
if constraint.satisfiedBy(actual)
diag = StringDiagnostic('HasSameSizeAs passed.');
else
diag = StringDiagnostic(sprintf(...
'HasSameSizeAs failed.\nActual Size: [%s]\nExpectedSize: [%s]',...
int2str(size(actual)), ...
int2str(size(constraint.ValueWithExpectedSize))));
end
end
end
methods(Access=protected)
function diag = getNegativeDiagnosticFor(constraint, actual)
import matlab.unittest.diagnostics.StringDiagnostic
if constraint.satisfiedBy(actual)
diag = StringDiagnostic(sprintf(...
['Negated HasSameSizeAs failed.\nSize [%s] of ' ...
'Actual Value and Expected Value were the same ' ...
'but should not have been.'], int2str(size(actual))));
else
diag = StringDiagnostic('Negated HasSameSizeAs passed.');
end
end
end
end

33 Unit Testing

Free download pdf