Main Content

Create Custom Boolean Constraint

This example shows how to create a custom Boolean constraint that determines if a given value has the same size as an expected value.

在当前文件夹中的文件中,创建一个名称的类HasSameSizeAsmatlab.unittest.constraints.booleanconstraint班级。类构造函数接受一个预期值,其大小与实际值的大小相比。预期值存储在ValueWithExpectedSize财产。推荐的做法是BooleanConstraintimplementations immutable, so set the propertySetAccessattribute toimmutable.

classdefHasSameSizeAs < matlab.unittest.constraints.BooleanConstraint特性(SetAccess =不可变)ValueWithExpectedSizeendmethodsfunction约束= hassamesizeas(value)约束。endendend

In amethodsblock withprivate访问,定义辅助方法SizeMatchEsexpectedthat 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.booleanconstraintclass is a subclass of thematlab.unittest.constraints.Constraint班级。这refore, classes that derive from theBooleanConstraintclass must override the methods of theConstraint班级。Within amethods块,覆盖satisfiedBygetDiagnosticFor方法。这satisfiedByimplementation must contain the comparison logic and return a logical value. ThegetDiagnosticForimplementation 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必须实施getNegativeDiagnosticFormethod. This method must provide aDiagnosticobject when the constraint is negated.

OverridegetNegativeDiagnosticForin amethodsblock withprotectedaccess.

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,或者, andnotoverloads, so it can be combined with otherBooleanConstraintobjects or negated.

HasSameSizeAs Class Definition

这是完整的代码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

Test for Expected Size

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 theconditions,~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. -+---------------------

See Also

||||||

相关话题