From 15aa3cae0b732e3eb309284a09a873a7c2d20ea0 Mon Sep 17 00:00:00 2001 From: Stephen Nneji Date: Thu, 3 Apr 2025 16:37:37 +0100 Subject: [PATCH] Improves error messages --- API/projectClass/contrastsClass.m | 25 ++++++++++++++++++------- tests/testContrastsClass.m | 9 +++++++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/API/projectClass/contrastsClass.m b/API/projectClass/contrastsClass.m index cd4da1687..eb2329cb5 100644 --- a/API/projectClass/contrastsClass.m +++ b/API/projectClass/contrastsClass.m @@ -242,14 +242,25 @@ inputArray = cellstr(input); - % Check the input is as expected - modelNames tells us what the - % model type is - if all(size(allowedNames.modelNames) == size(allowedNames.customFileNames)) && all(cellfun(@strcmp, allowedNames.modelNames, allowedNames.customFileNames)) + % Check the input is as expected - layerNames is only present in standard layers + if ~isfield(allowedNames, 'layerNames') + if isempty(allowedNames.customFileNames) + throw(exceptions.invalidValue('At least one custom file must be added to the contrast model in a standard layers project. Did you forget to add the custom file?')); + end + if length(inputArray) > 1 - throw(exceptions.invalidValue('Only one model value is allowed for custom models')); + throw(exceptions.invalidValue('One custom file is required as the contrast model in a custom layers or xy project')); + end + else + if isempty(allowedNames.layerNames) && ~isempty(allowedNames.customFileNames) && any(cellfun(@(x) any(strcmp(x, allowedNames.customFileNames)), inputArray)) + throw(exceptions.invalidValue('Custom files cannot be used in a standard layers model. Did you forget to change the model type?')); + end + + if isempty(allowedNames.layerNames) + throw(exceptions.invalidValue('At least one layer must be added to the contrast model in a standard layers project. Did you forget to add the layers?')); end - elseif obj.domainsCalc && all(size(allowedNames.modelNames) == size(allowedNames.domainContrastNames)) && all(cellfun(@strcmp, allowedNames.modelNames, allowedNames.domainContrastNames)) - if length(inputArray) ~= 2 + + if obj.domainsCalc && length(inputArray) ~= 2 throw(exceptions.invalidValue('Exactly two model values are required for ''standard layers'' with domains')); end end @@ -260,7 +271,7 @@ for i = 1:modelSize found = strcmpi(inputArray{i}, modelNames); if ~any(found) - throw(exceptions.nameNotRecognised(sprintf('Model component name "%s" is not recognised. The allowed names are: "%s".', inputArray{i}, strjoin(allowedNames.modelNames, '", "')))); + throw(exceptions.nameNotRecognised(sprintf('Model component name "%s" is not recognised. The allowed names are: "%s".', inputArray{i}, strjoin(modelNames, '", "')))); end model{i} = modelNames{find(found, 1)}; end diff --git a/tests/testContrastsClass.m b/tests/testContrastsClass.m index b280417a5..f3f7f1e82 100644 --- a/tests/testContrastsClass.m +++ b/tests/testContrastsClass.m @@ -334,12 +334,18 @@ function testSetContrastModelStandardLayers(testCase) noDomainsClass.setContrastModel(1, testCase.allowedNames, testModel); testCase.verifyEqual(noDomainsClass.contrasts{1}.model, testModel, 'setContrastModel does not work correctly'); + + testCase.allowedNames.layerNames = {}; + testCase.verifyError(@() testCase.exampleClass.setContrastModel(1, testCase.allowedNames, {'Oxide Layer'}), exceptions.invalidValue.errorID); + testCase.verifyError(@() testCase.exampleClass.setContrastModel(1, testCase.allowedNames, {'DPPC Model'}), exceptions.invalidValue.errorID); + end function testSetContrastModelCustomLayers(testCase) % Test setting a model for a contrast from the contrasts class % for a "custom XY" model type noDomainsClass = contrastsClass(); + testCase.allowedNames = rmfield(testCase.allowedNames, 'layerNames'); noDomainsClass.addContrast(testCase.allowedNames); testModel = {'DPPC Model'}; @@ -350,6 +356,9 @@ function testSetContrastModelCustomLayers(testCase) % only one model value is allowed testCase.verifyError(@() testCase.exampleClass.setContrastModel(1, testCase.allowedNames, {'DPPC Model', 'DSPC Model'}), exceptions.invalidValue.errorID); + + testCase.allowedNames.customFileNames = {}; + testCase.verifyError(@() testCase.exampleClass.setContrastModel(1, testCase.allowedNames, {'DPPC Model'}), exceptions.invalidValue.errorID); end function testSetContrastModelInvalid(testCase)