diff --git a/API/projectClass/customFileClass.m b/API/projectClass/customFileClass.m index 4e15f818e..bd7c8c0ff 100644 --- a/API/projectClass/customFileClass.m +++ b/API/projectClass/customFileClass.m @@ -121,11 +121,9 @@ function delete(obj) throw(exceptions.invalidType('Fifth argument (Function name) must be text.')); end - if isempty(newFunc) - [~, newFunc, ~] = fileparts(newFile); - end % Check language is valid, then add the new entry newLang = validateOption(newLang, 'supportedLanguages', obj.invalidLanguageMessage).value; + newFunc = obj.validateFunctionName(newFile, newFunc, newLang); obj.addRow(newName, newFile, newFunc, newLang, obj.validatePath(newPath)); end @@ -171,9 +169,9 @@ function delete(obj) obj.setCustomName(row, results.name); obj.varTable{row, 2} = {results.filename}; - obj.varTable{row, 3} = {results.functionName}; obj.varTable{row, 4} = {validateOption(results.language, 'supportedLanguages', obj.invalidLanguageMessage).value}; obj.varTable{row, 5} = {obj.validatePath(results.path)}; + obj.varTable{row, 3} = {obj.validateFunctionName(results.filename, results.functionName, results.language)}; end function displayTable(obj) @@ -206,13 +204,14 @@ function displayTable(obj) end thisFunctionName = thisRow{1,3}{:}; - if isempty(thisFunctionName) + thisFileLanguage = thisRow{1,4}{:}; + if isempty(thisFunctionName) || strcmp(thisFileLanguage, supportedLanguages.Matlab.value) functionNameString = '-'; else functionNameString = thisFunctionName; end - thisFileLanguage = thisRow{1,4}{:}; + if isempty(thisFileLanguage) fileLanguageString = '-'; else @@ -320,6 +319,20 @@ function displayTable(obj) throw(exceptions.invalidPath(sprintf('The given path (%s) is not a valid directory', path))); end end + + function newFunctionName = validateFunctionName(filename, functionName, language) + % Validate a func + [~, newFunctionName, ~] = fileparts(filename); + if ~isempty(functionName) + if strcmp(language, supportedLanguages.Matlab.value) + if ~strcmp(functionName, newFunctionName) + warning('For Matlab custom functions, the function Name must be the same as filename without .m extension.'); + end + else + newFunctionName = functionName; + end + end + end end end diff --git a/tests/testCustomFileClass.m b/tests/testCustomFileClass.m index 6cccb844a..189575002 100644 --- a/tests/testCustomFileClass.m +++ b/tests/testCustomFileClass.m @@ -22,12 +22,12 @@ } testRow = {1, 1, 2, 2} inputData = {{'DPPC Model', 'name', 'New Model', 'path', '../../'},... - {1, 'filename', 'model.m'},... + {1, 'filename', 'model.m', 'functionName', 'model'},... {'DSPC Model', 'language', upper(supportedLanguages.Python.value)},... {2, 'language', supportedLanguages.Python, 'filename', 'model.m', 'functionName', 'modelFunc', 'name', 'New Model', 'path', pwd},... } expectedRow = {["New Model", "DPPCCustomXY.m", "DPPCCustomXY", string(supportedLanguages.Matlab.value), "../../"],... - ["DPPC Model", "model.m", "DPPCCustomXY", string(supportedLanguages.Matlab.value), "tests/nonPolarisedTFReflectivityCalculation/"],... + ["DPPC Model", "model.m", "model", string(supportedLanguages.Matlab.value), "tests/nonPolarisedTFReflectivityCalculation/"],... ["DSPC Model", "customBilayer.m", "customBilayer", string(supportedLanguages.Python.value), "tests/nonPolarisedTFReflectivityCalculation/"],... ["New Model", "model.m", "modelFunc", string(supportedLanguages.Python.value), pwd],... } @@ -178,6 +178,9 @@ function testSetCustomFileInvalidInput(testCase) % Duplicate custom object names testCase.verifyError(@() testCase.exampleClass.setCustomFile(2, 'Name', 'DPPC Model'), exceptions.duplicateName.errorID) + % Bad function name + testCase.verifyWarning(@() testCase.exampleClass.setCustomFile(2, 'FunctionName', 'newModel'), ''); + testCase.verifyEqual(testCase.exampleClass.varTable{2, 3}, "customBilayer", 'setCustomFile does not work correctly'); end function testSetCustomFileInvalidNumberOfParams(testCase) @@ -186,7 +189,7 @@ function testSetCustomFileInvalidNumberOfParams(testCase) % should raise an error testCase.verifyError(@() testCase.exampleClass.setCustomFile(1), exceptions.invalidNumberOfInputs.errorID); testCase.verifyError(@() testCase.exampleClass.setCustomFile(1, 1), exceptions.invalidNumberOfInputs.errorID); - testCase.verifyError(@() testCase.exampleClass.setCustomFile(2, 'Name', 'New Model', 'Language'), exceptions.invalidNumberOfInputs.errorID); + testCase.verifyError(@() testCase.exampleClass.setCustomFile(2, 'Name', 'New Model', 'Language'), exceptions.invalidNumberOfInputs.errorID); end function testDisplayTable(testCase) @@ -221,7 +224,7 @@ function testDisplayTable(testCase) % space using regular expressions, and remove '"' % characters outRow = strip(replace(regexprep(displayedTable(i+2), '\s+', ' '), '"', '')); - + testCase.exampleClass.varTable{i, 3} = {'-'}; % Get data from this row and join into a single string rowString = strip(strjoin(testCase.exampleClass.varTable{i,:})); testCase.verifyEqual(outRow, rowString, 'Row does not contain the correct data');