Main Content

matlab.unittest.constraints.Constraint 클래스

네임스페이스: matlab.unittest.constraints

제약 조건에 대한 기본 인터페이스

설명

matlab.unittest.constraints.Constraint 클래스는 가설 검정(Qualification)에서 비교 논리를 코드로 작성하고 진단 정보를 생성하는 데 사용할 수 있는 인터페이스를 제공합니다. 사용자가 제공한 제약 조건이든 프레임워크 제약 조건이든 관계없이, 모든 제약 조건은 Constraint 클래스에서 파생됩니다.

사용자 지정 제약 조건 클래스를 만들려면 matlab.unittest.constraints.Constraint에서 클래스를 파생하고 다음과 같은 그 추상 메서드를 구현하십시오.

  • 비교 논리를 코드로 작성한 satisfiedBy 메서드를 구현합니다.

  • 테스트 프레임워크가 실제 값을 제약 조건과 비교하여 평가할 때 진단 정보를 생성하도록 getDiagnosticFor 메서드를 구현합니다.

그런 다음, Constraint 서브클래스를 matlab.unittest.qualifications 네임스페이스의 assertThat, assumeThat, fatalAssertThat, verifyThat 가설 검정 메서드와 함께 사용할 수 있습니다.

and(&), or(|) 및 not(~) 연산자를 사용하여 조합하고 부정(Negate)할 수 있는 제약 조건을 만들려면 대신 matlab.unittest.constraints.BooleanConstraint에서 클래스를 파생하십시오.

클래스 특성

Abstract
true
HandleCompatible
true

클래스 특성에 대한 자세한 내용은 클래스 특성 항목을 참조하십시오.

메서드

모두 확장

예제

모두 축소

값이 예상 값과 동일한 크기인지를 확인하는 제약 조건을 만듭니다.

현재 폴더에 있는 파일에서 matlab.unittest.constraints.Constraint에서 파생된 IsSameSizeAs라는 클래스를 만들고 satisfiedBygetDiagnosticFor 메서드를 구현합니다.

classdef IsSameSizeAs < matlab.unittest.constraints.Constraint
    properties (SetAccess=immutable)
        ValueWithExpectedSize
    end

    methods
        % Class constructor
        function constraint = IsSameSizeAs(value)
            constraint.ValueWithExpectedSize = value;
        end

        % Determine if the actual value satisfies the constraint 
        function tf = satisfiedBy(constraint,actual)
            tf = constraint.sizeMatchesExpected(actual);
        end

        % Produce a diagnostic for the constraint
        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=private)
        % Determine if the actual and expected values are the same size
        function tf = sizeMatchesExpected(constraint,actual)
            tf = isequal(size(actual), ...
                size(constraint.ValueWithExpectedSize));
        end
    end
end

대화형 방식 테스트를 위한 테스트 케이스를 생성합니다.

testCase = matlab.unittest.TestCase.forInteractiveUse;

IsSameSizeAs 제약 조건을 사용하여 0으로 구성된 5×5 행렬이 1로 구성된 5×5 행렬과 크기가 같은지 확인합니다.

testCase.verifyThat(zeros(5),IsSameSizeAs(ones(5)))
Verification passed.

버전 내역

R2013a에 개발됨