Main Content

matlab.unittest.constraints.IsFolder Class

Namespace: matlab.unittest.constraints
Superclasses: matlab.unittest.constraints.BooleanConstraint

Test if value is folder

Description

The matlab.unittest.constraints.IsFolder class provides a constraint to test if a value represents a folder.

Creation

Description

c = matlab.unittest.constraints.IsFolder creates a constraint to test if a value represents a folder. The constraint is satisfied by a string scalar or character vector that specifies the path to an existing folder. The value can be a relative path, but the relative path must be in the current folder. Otherwise, the value must be a full path.

example

Examples

collapse all

Test if a folder exists using the IsFolder constraint.

First, import the classes used in this example.

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsFolder

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Create the subfolder myFolder in your current folder if it does not exist. Then add a file to myFolder.

if ~isfolder("myFolder")
    mkdir myFolder
end
filename = "myFolder" + filesep + "myFile.dat";
writematrix(magic(20),filename)

Verify that myFolder exists in your current folder.

testCase.verifyThat("myFolder",IsFolder)
Verification passed.

Test if a file in myFolder satisfies the IsFolder constraint. The test fails because the constraint is satisfied only by a value that represents a folder.

testCase.verifyThat(filename,IsFolder)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsFolder failed.
    --> Value does not point to an existing folder.
    --> Current folder during evaluation:
            'C:\work'
    
    Actual Value:
        "myFolder\myFile.dat"

Delete the folder, and test if the folder no longer exists. The test passes.

rmdir myFolder s
testCase.verifyThat("myFolder",~IsFolder)
Verification passed.

Version History

Introduced in R2018a