This example shows how to create a custom Boolean constraint that determines if a given value has the same size as an expected value.
在当前文件夹中的文件中,创建一个名称的类HasSameSizeAs
从matlab.unittest.constraints.booleanconstraint
班级。类构造函数接受一个预期值,其大小与实际值的大小相比。预期值存储在ValueWithExpectedSize
财产。推荐的做法是BooleanConstraint
implementations immutable, so set the propertySetAccess
attribute toimmutable
.
classdefHasSameSizeAs < matlab.unittest.constraints.BooleanConstraint特性(SetAccess =不可变)ValueWithExpectedSizeendmethodsfunction约束= hassamesizeas(value)约束。endendend
In amethods
block withprivate
访问,定义辅助方法SizeMatchEsexpected
that determines if the actual and expected values have the same size. This method is invoked by other constraint methods.
methods(Access = private)functionbool = sizeMatchesExpected(constraint,actual) bool = isequal(size(actual),size(constraint.ValueWithExpectedSize));endend
这matlab.unittest.constraints.booleanconstraint
class is a subclass of thematlab.unittest.constraints.Constraint
班级。这refore, classes that derive from theBooleanConstraint
class must override the methods of theConstraint
班级。Within amethods
块,覆盖satisfiedBy
和getDiagnosticFor
方法。这satisfiedBy
implementation must contain the comparison logic and return a logical value. ThegetDiagnosticFor
implementation must evaluate the actual value against the constraint and provide aDiagnostic
目的。In this example,getDiagnosticFor
返回一个StringDiagnostic
目的。
methodsfunctionbool = satisfiedBy(constraint,actual) bool = constraint.sizeMatchesExpected(actual);endfunctiondiag = getDiagnosticFor(constraint,actual) importmatlab.unittest.diagnostics.stringdiagnosticifconstraint.sizeMatchesExpected(actual) diag = StringDiagnostic('HasSameSizeAs passed.');elsediag = StringDiagnostic(sprintf(...'HasSameSizeAs failed.\nActual Size: [%s]\nExpectedSize: [%s]',...int2str(size(actual)),...int2str(size(constraint.ValueWithExpectedSize))));endendend
Classes that derive fromBooleanConstraint
必须实施getNegativeDiagnosticFor
method. This method must provide aDiagnostic
object when the constraint is negated.
OverridegetNegativeDiagnosticFor
in amethods
block withprotected
access.
methods(访问=受保护)functiondiag = getNegativeDiagnosticFor(constraint,actual) importmatlab.unittest.diagnostics.stringdiagnosticifconstraint.sizeMatchesExpected(actual) diag = StringDiagnostic(sprintf(...['否定的hassamesizeas失败了。\ nsize [%s]...“实际价值和期望值相同”...'but should not have been.'],int2str(size(actual))));elsediag = StringDiagnostic('Negated HasSameSizeAs passed.');endendend
In exchange for implementing the required methods, the constraint inherits the appropriate和
,或者
, andnot
overloads, so it can be combined with otherBooleanConstraint
objects or negated.
这是完整的代码HasSameSizeAs
班级。
classdefHasSameSizeAs < matlab.unittest.constraints.BooleanConstraint特性(SetAccess =不可变)ValueWithExpectedSizeendmethodsfunction约束= hassamesizeas(value)约束。endfunctionbool = satisfiedBy(constraint,actual) bool = constraint.sizeMatchesExpected(actual);endfunctiondiag = getDiagnosticFor(constraint,actual) importmatlab.unittest.diagnostics.stringdiagnosticifconstraint.sizeMatchesExpected(actual) diag = StringDiagnostic('HasSameSizeAs passed.');elsediag = StringDiagnostic(sprintf(...'HasSameSizeAs failed.\nActual Size: [%s]\nExpectedSize: [%s]',...int2str(size(actual)),...int2str(size(constraint.ValueWithExpectedSize))));endendendmethods(访问=受保护)functiondiag = getNegativeDiagnosticFor(constraint,actual) importmatlab.unittest.diagnostics.stringdiagnosticifconstraint.sizeMatchesExpected(actual) diag = StringDiagnostic(sprintf(...['Negated HasSameSizeAs failed.\nSize [%s] of '...“实际价值和期望值相同”...'but should not have been.'],int2str(size(actual))));elsediag = StringDiagnostic('Negated HasSameSizeAs passed.');endendendmethods(Access = private)functionbool = sizeMatchesExpected(constraint,actual) bool = isequal(size(actual),size(constraint.ValueWithExpectedSize));endendend
At the command prompt, create a test case for interactive testing.
importmatlab.unittest.TestCaseimportmatlab.unittest.constraints.HasLengthtestCase = TestCase.forInteractiveUse;
Test a passing case. The test passes because one of the或者
conditions,哈长(5)
, is true.
testCase.verifyThat(zeros(5),HasLength(5) | ~HasSameSizeAs(repmat(1,5)))
验证通过了。
Test a failing case. The test fails because one of the和
conditions,~HasSameSizeAs(repmat(1,5))
, is false.
testCase.verifyThat(zeros(5),HasLength(5) & ~HasSameSizeAs(repmat(1,5)))
验证失败。--------------------- Framework Diagnostic: --------------------- AndConstraint failed.- > + [第一个条件]:|HASLENGTH通过。||实际值:|0 0 0 0 0 |0 0 0 0 0 |0 0 0 0 0 | 0 0 0 0 0 | 0 0 0 0 0 | Expected Length: | 5 --> AND + [Second Condition]: | Negated HasSameSizeAs failed. | Size [5 5] of Actual Value and Expected Value were the same but should not have been. -+---------------------
getDiagnosticFor
|getNegativeDiagnosticFor
|matlab.unittest.constraints.booleanconstraint
|matlab.unittest.constraints.Constraint
|matlab.unittest.diagnostics.Diagnostic
|matlab.unittest.diagnostics.stringdiagnostic
|satisfiedBy