사용자 지정 부울 제약 조건 만들기
이 예제에서는 주어진 값이 예상 값과 크기가 동일한지를 확인하는 사용자 지정 부울 제약 조건을 생성하는 방법을 보여줍니다.
현재 폴더의 파일에서, matlab.unittest.constraints.BooleanConstraint
클래스에서 파생된 IsSameSizeAs
클래스를 생성합니다. 클래스 생성자는 크기가 실제 값의 크기와 비교되는 예상 값을 받습니다. 예상 값은 ValueWithExpectedSize
속성에 저장됩니다. BooleanConstraint
구현은 변경 불가(immutable)로 두는 것이 권장되므로 속성 SetAccess
특성을 immutable
로 설정합니다.
classdef IsSameSizeAs < matlab.unittest.constraints.BooleanConstraint properties (SetAccess=immutable) ValueWithExpectedSize end methods function constraint = IsSameSizeAs(value) constraint.ValueWithExpectedSize = value; end end end
private
액세스 권한이 있는 methods
블록에서 실제 값과 예상 값이 크기가 동일한지 확인하는 헬퍼 메서드 sizeMatchesExpected
를 정의합니다. 이 메서드는 다른 제약 조건 메서드에 의해 호출됩니다.
methods (Access=private) function tf = sizeMatchesExpected(constraint,actual) tf = isequal(size(actual), ... size(constraint.ValueWithExpectedSize)); end end
matlab.unittest.constraints.BooleanConstraint
클래스는 matlab.unittest.constraints.Constraint
클래스의 서브클래스입니다. 따라서 BooleanConstraint
클래스에서 파생된 클래스는 Constraint
클래스의 메서드를 구현해야 합니다. methods
블록 내에서 satisfiedBy
메서드와 getDiagnosticFor
메서드를 구현합니다. satisfiedBy
구현은 논리적 비교를 포함하고 논리값을 반환해야 합니다. getDiagnosticFor
구현은 실제 값을 제약 조건에 비교하여 평가하고 Diagnostic
객체를 제공해야 합니다. 이 예제에서 getDiagnosticFor
는 StringDiagnostic
객체를 반환합니다.
methods function tf = satisfiedBy(constraint,actual) tf = constraint.sizeMatchesExpected(actual); end function diagnostic = getDiagnosticFor(constraint,actual) import matlab.automation.diagnostics.StringDiagnostic if constraint.sizeMatchesExpected(actual) diagnostic = StringDiagnostic("IsSameSizeAs passed."); else diagnostic = StringDiagnostic( ... "IsSameSizeAs failed." + newline + "Actual Size: [" ... + int2str(size(actual)) + "]" + newline ... + "Expected Size: [" ... + int2str(size(constraint.ValueWithExpectedSize)) ... + "]"); end end end
BooleanConstraint
로부터 파생되는 클래스는 getNegativeDiagnosticFor
메서드를 구현해야 합니다. 이 메서드는 제약 조건이 논리 부정되는 경우 Diagnostic
객체를 제공해야 합니다. protected
액세스 권한이 있는 methods
블록에서 getNegativeDiagnosticFor
를 구현하십시오.
methods (Access=protected) function diagnostic = getNegativeDiagnosticFor(constraint,actual) import matlab.automation.diagnostics.StringDiagnostic if constraint.sizeMatchesExpected(actual) diagnostic = StringDiagnostic( ... "Negated IsSameSizeAs failed." + newline + ... "Actual and expected sizes were the same ([" ... + int2str(size(actual)) + ... "]) but should not have been."); else diagnostic = StringDiagnostic( ... "Negated IsSameSizeAs passed."); end end end
필요한 메서드를 구현하는 대신에 해당 제약 조건이 다른 BooleanConstraint
객체와 조합되거나 논리 부정될 수 있도록 적절한 and
, or
및 not
오버로드를 상속합니다.
IsSameSizeAs
클래스 정의
다음은 IsSameSizeAs
클래스에 대한 전체 코드입니다.
classdef IsSameSizeAs < matlab.unittest.constraints.BooleanConstraint properties (SetAccess=immutable) ValueWithExpectedSize end methods function constraint = IsSameSizeAs(value) constraint.ValueWithExpectedSize = value; end function tf = satisfiedBy(constraint,actual) tf = constraint.sizeMatchesExpected(actual); end function diagnostic = getDiagnosticFor(constraint,actual) import matlab.automation.diagnostics.StringDiagnostic if constraint.sizeMatchesExpected(actual) diagnostic = StringDiagnostic("IsSameSizeAs passed."); else diagnostic = StringDiagnostic( ... "IsSameSizeAs failed." + newline + "Actual Size: [" ... + int2str(size(actual)) + "]" + newline ... + "Expected Size: [" ... + int2str(size(constraint.ValueWithExpectedSize)) ... + "]"); end end end methods (Access=protected) function diagnostic = getNegativeDiagnosticFor(constraint,actual) import matlab.automation.diagnostics.StringDiagnostic if constraint.sizeMatchesExpected(actual) diagnostic = StringDiagnostic( ... "Negated IsSameSizeAs failed." + newline + ... "Actual and expected sizes were the same ([" ... + int2str(size(actual)) + ... "]) but should not have been."); else diagnostic = StringDiagnostic( ... "Negated IsSameSizeAs passed."); end end end methods (Access=private) function tf = sizeMatchesExpected(constraint,actual) tf = isequal(size(actual), ... size(constraint.ValueWithExpectedSize)); end end end
예상 크기인지 테스트하기
명령 프롬프트에서 대화형 방식 테스트를 위한 테스트 케이스를 생성합니다.
import matlab.unittest.TestCase import matlab.unittest.constraints.HasLength testCase = TestCase.forInteractiveUse;
통과 케이스를 테스트합니다. or
조건 중 하나인 HasLength(5)
가 true이므로 이 테스트 결과는 통과입니다.
testCase.verifyThat(zeros(5),HasLength(5) | ~IsSameSizeAs(repmat(1,5)))
Verification passed.
실패 케이스를 테스트합니다. 이 테스트는 and
조건 중 하나인 ~IsSameSizeAs(repmat(1,5))
가 false이므로 실패합니다.
testCase.verifyThat(zeros(5),HasLength(5) & ~IsSameSizeAs(repmat(1,5)))
Verification failed. --------------------- Framework Diagnostic: --------------------- AndConstraint failed. --> + [First Condition]: | HasLength passed. | | Actual Value: | 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 ISameSizeAs failed. | Actual and expected sizes were the same ([5 5]) but should not have been. -+---------------------
참고 항목
클래스
matlab.unittest.constraints.BooleanConstraint
|matlab.unittest.constraints.Constraint
|matlab.automation.diagnostics.StringDiagnostic
|matlab.automation.diagnostics.Diagnostic