Skip to content

Commit

Permalink
Ensures function name matches filename for matlab custom functions an…
Browse files Browse the repository at this point in the history
…d hides function name in display table (#235)
  • Loading branch information
StephenNneji authored Jun 13, 2024
1 parent f0cc59c commit 02d87fb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
25 changes: 19 additions & 6 deletions API/projectClass/customFileClass.m
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions tests/testCustomFileClass.m
Original file line number Diff line number Diff line change
Expand Up @@ -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],...
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 02d87fb

Please sign in to comment.