Main Content

사용자 지정 제약 조건 만들기

이 예제에서는 주어진 값이 예상 값과 크기가 동일한지를 확인하는 사용자 지정 제약 조건을 생성하는 방법을 보여줍니다.

현재 폴더의 파일에서, matlab.unittest.constraints.Constraint 클래스에서 파생된 IsSameSizeAs 클래스를 생성합니다. 클래스 생성자는 크기가 실제 값의 크기와 비교되는 예상 값을 받습니다. 예상 값은 ValueWithExpectedSize 속성에 저장됩니다. Constraint 구현은 변경 불가(Immutable)로 두는 것이 권장되므로 속성 SetAccess 특성을 immutable로 설정합니다.

classdef IsSameSizeAs < matlab.unittest.constraints.Constraint
    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.Constraint 클래스에서 파생되는 클래스는 satisfiedBy 메서드를 구현해야 합니다. 이 메서드는 논리적 비교를 포함하고 논리값을 반환해야 합니다. methods 블록 내에서 헬퍼 메서드를 불러와 satisfiedBy를 구현하십시오. 실제 크기와 예상 크기가 동일한 경우 이 메서드가 true를 반환합니다.

    methods
        function tf = satisfiedBy(constraint,actual)
            tf = constraint.sizeMatchesExpected(actual);
        end
    end

matlab.unittest.constraints.Constraint 클래스에서 파생되는 클래스는 getDiagnosticFor 메서드도 구현해야 합니다. 이 메서드는 실제 값을 제약 조건에 비교하여 평가하고 Diagnostic 객체를 제공해야 합니다. 이 예제에서 getDiagnosticForStringDiagnostic 객체를 반환합니다.

    methods
        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

IsSameSizeAs 클래스 정의

다음은 IsSameSizeAs 클래스에 대한 전체 코드입니다.

classdef IsSameSizeAs < matlab.unittest.constraints.Constraint
    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=private)
        function tf = sizeMatchesExpected(constraint,actual)
            tf = isequal(size(actual), ...
                size(constraint.ValueWithExpectedSize));
        end
    end
end

예상 크기인지 테스트하기

명령 프롬프트에서 대화형 방식 테스트를 위한 테스트 케이스를 생성합니다.

import matlab.unittest.TestCase
testCase = TestCase.forInteractiveUse;

통과 케이스를 테스트합니다.

testCase.verifyThat(zeros(5),IsSameSizeAs(repmat(1,5)))
Verification passed.

실패 케이스를 테스트합니다.

testCase.verifyThat(zeros(5),IsSameSizeAs(ones(1,5)))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsSameSizeAs failed.
    Actual Size: [5  5]
    ExpectedSize: [1  5]

참고 항목

클래스

관련 항목