이 번역 페이지는 최신 내용을 담고 있지 않습니다. 최신 내용을 영문으로 보려면 여기를 클릭하십시오.
사용자 지정 부울 제약 조건 만들기
이 예제에서는 주어진 값이 예상 값과 크기가 동일한지를 확인하는 사용자 지정 부울 제약 조건을 생성하는 방법을 보여줍니다.
현재 폴더의 파일에서, matlab.unittest.constraints.BooleanConstraint
클래스에서 파생된 HasSameSizeAs
클래스를 생성하십시오. 클래스 생성자는 크기가 실제 값의 크기와 비교되는 예상 값을 받습니다. 예상 값은 ValueWithExpectedSize
속성에 저장됩니다. BooleanConstraint
구현은 변경 불가(immutable)로 두는 것이 권장되므로 속성 SetAccess
특성을 immutable
로 설정합니다.
classdef HasSameSizeAs < matlab.unittest.constraints.BooleanConstraint properties(SetAccess = immutable) ValueWithExpectedSize end methods function constraint = HasSameSizeAs(value) constraint.ValueWithExpectedSize = value; end end end
private
액세스 권한이 있는 methods
블록에서 실제 값과 예상 값이 크기가 동일한지 확인하는 헬퍼 메서드 sizeMatchesExpected
를 정의하십시오. 이 메서드는 다른 제약 조건 메서드에 의해 호출됩니다.
methods(Access = private) function bool = sizeMatchesExpected(constraint,actual) bool = 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 bool = satisfiedBy(constraint,actual) bool = constraint.sizeMatchesExpected(actual); end function diag = getDiagnosticFor(constraint,actual) import matlab.unittest.diagnostics.StringDiagnostic if constraint.sizeMatchesExpected(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
BooleanConstraint
로부터 파생되는 클래스는 getNegativeDiagnosticFor
메서드를 구현해야 합니다. 이 메서드는 제약 조건이 부정(Negated)되는 경우 Diagnostic
객체를 제공해야 합니다.
protected
액세스 권한이 있는 methods
블록에서 getNegativeDiagnosticFor
를 재정의하십시오.
methods(Access = protected) function diag = getNegativeDiagnosticFor(constraint,actual) import matlab.unittest.diagnostics.StringDiagnostic if constraint.sizeMatchesExpected(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
필요한 메서드를 구현하는 대신에 해당 제약 조건은 다른 BooleanConstraint
객체와 조합하거나 부정할 수 있도록 적절한 and
, or
및 not
오버로드를 상속합니다.
HasSameSizeAs 클래스 정의
다음은 HasSameSizeAs
클래스에 대한 전체 코드입니다.
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 = constraint.sizeMatchesExpected(actual); end function diag = getDiagnosticFor(constraint,actual) import matlab.unittest.diagnostics.StringDiagnostic if constraint.sizeMatchesExpected(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.sizeMatchesExpected(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 methods(Access = private) function bool = sizeMatchesExpected(constraint,actual) bool = 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) | ~HasSameSizeAs(repmat(1,5)))
Verification passed.
실패 케이스를 테스트합니다. 이 테스트는 and
조건 중 하나인 ~HasSameSizeAs(repmat(1,5))
가 false이므로 실패합니다.
testCase.verifyThat(zeros(5),HasLength(5) & ~HasSameSizeAs(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 HasSameSizeAs failed. | Size [5 5] of Actual Value and Expected Value were the same but should not have been. -+---------------------
참고 항목
matlab.unittest.constraints.BooleanConstraint
| matlab.unittest.constraints.Constraint
| satisfiedBy
| getDiagnosticFor
| getNegativeDiagnosticFor
| matlab.unittest.diagnostics.StringDiagnostic
| matlab.unittest.diagnostics.Diagnostic