diff --git a/tests/+did/+unittest/test_document.m b/tests/+did/+unittest/test_document.m new file mode 100644 index 0000000..53eb320 --- /dev/null +++ b/tests/+did/+unittest/test_document.m @@ -0,0 +1,45 @@ +classdef test_document < matlab.unittest.TestCase + methods (Test) + function test_dependency_management(testCase) + % Create a document of type 'demoC', which has 'depends_on' fields + doc = did.document('demoC'); + + % Verify 'depends_on' field exists + testCase.verifyTrue(isfield(doc.document_properties, 'depends_on'), ... + "The 'depends_on' field should exist for 'demoC' document type."); + + % Test setting a new dependency value + doc = doc.set_dependency_value('item1', 'new_value'); + retrieved_value = doc.dependency_value('item1'); + testCase.verifyEqual(retrieved_value, 'new_value', ... + "Failed to set and retrieve a new dependency value."); + + % Test updating an existing dependency value + doc = doc.set_dependency_value('item1', 'updated_value'); + retrieved_value = doc.dependency_value('item1'); + testCase.verifyEqual(retrieved_value, 'updated_value', ... + "Failed to update an existing dependency value."); + end + + function test_file_management(testCase) + % Create a document of type 'demoFile', which is defined to handle files + doc = did.document('demoFile'); + + % Add a file and verify it was added + doc = doc.add_file('filename1.ext', '/path/to/file1.txt'); + [isIn, ~, fI_index] = doc.is_in_file_list('filename1.ext'); + testCase.verifyTrue((isIn==1)||(isIn==true), "File 'filename1.ext' should be in the file list."); + testCase.verifyNotEmpty(fI_index, "File info index should not be empty after adding a file."); + + % Verify the location of the added file + testCase.verifyEqual(doc.document_properties.files.file_info(fI_index).locations.location, '/path/to/file1.txt', ... + "The location of the added file is incorrect."); + + % Remove the file and verify it was removed + doc = doc.remove_file('filename1.ext'); + [~, ~, fI_index_after_removal] = doc.is_in_file_list('filename1.ext'); + % After removal, searching for the file info should yield an empty index + testCase.verifyEmpty(fI_index_after_removal, "File info should be empty after removing the file."); + end + end +end diff --git a/tests/+did/+unittest/test_fileobj.m b/tests/+did/+unittest/test_fileobj.m new file mode 100644 index 0000000..99305b3 --- /dev/null +++ b/tests/+did/+unittest/test_fileobj.m @@ -0,0 +1,21 @@ +classdef test_fileobj < matlab.unittest.TestCase + methods (Test) + function test_constructor(testCase) + % Test creating a fileobj + theFileObj = did.file.fileobj(); + testCase.verifyEqual(theFileObj.fid, -1); + testCase.verifyEqual(theFileObj.permission, 'r'); + testCase.verifyEqual(theFileObj.machineformat, 'n'); + testCase.verifyEqual(theFileObj.fullpathfilename, ''); + end + + function test_custom_file_handler_error(testCase) + % Test that passing customFileHandler to the constructor throws an error + my_handler = @(x) disp(['File operation: ' x]); + + testCase.verifyError(@() did.file.fileobj('customFileHandler', my_handler), ... + 'MATLAB:TooManyInputs', ... + 'The constructor should throw a standard parser error for an unmatched parameter.'); + end + end +end diff --git a/tests/+did/+unittest/test_query.m b/tests/+did/+unittest/test_query.m new file mode 100644 index 0000000..c5ee84c --- /dev/null +++ b/tests/+did/+unittest/test_query.m @@ -0,0 +1,44 @@ +classdef test_query < matlab.unittest.TestCase + methods (Test) + function test_creation(testCase) + % Test creating a query + q = did.query('base.name', 'exact_string', 'myname'); + testCase.verifyEqual(q.searchstructure.field, 'base.name'); + testCase.verifyEqual(q.searchstructure.operation, 'exact_string'); + testCase.verifyEqual(q.searchstructure.param1, 'myname'); + end + + function test_invalid_operator(testCase) + % Test that an invalid operator throws an error + testCase.verifyError(@() did.query('base.name', 'invalid_op', 'myname'), 'MATLAB:validators:mustBeMember'); + end + + function test_and_query(testCase) + % Test combining queries with AND + q1 = did.query('base.name', 'exact_string', 'myname'); + q2 = did.query('base.age', 'greaterthan', 30); + q_and = q1 & q2; + testCase.verifyEqual(numel(q_and.searchstructure), 2); + testCase.verifyEqual(q_and.searchstructure(1).field, 'base.name'); + testCase.verifyEqual(q_and.searchstructure(1).operation, 'exact_string'); + testCase.verifyEqual(q_and.searchstructure(1).param1, 'myname'); + testCase.verifyEqual(q_and.searchstructure(2).field, 'base.age'); + testCase.verifyEqual(q_and.searchstructure(2).operation, 'greaterthan'); + testCase.verifyEqual(q_and.searchstructure(2).param1, 30); + end + + function test_or_query(testCase) + % Test combining queries with OR + q1 = did.query('base.name', 'exact_string', 'myname'); + q2 = did.query('base.age', 'greaterthan', 30); + q_or = q1 | q2; + testCase.verifyEqual(q_or.searchstructure.operation, 'or'); + testCase.verifyEqual(q_or.searchstructure.param1(1).field, 'base.name'); + testCase.verifyEqual(q_or.searchstructure.param1(1).operation, 'exact_string'); + testCase.verifyEqual(q_or.searchstructure.param1(1).param1, 'myname'); + testCase.verifyEqual(q_or.searchstructure.param2(1).field, 'base.age'); + testCase.verifyEqual(q_or.searchstructure.param2(1).operation, 'greaterthan'); + testCase.verifyEqual(q_or.searchstructure.param2(1).param1, 30); + end + end +end