Skip to content

[INFRA] add tests and refactor #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,15 @@ jobs:
make -C MOxUnit install
git clone https://github.com/MOcov/MOcov.git --depth 1
make -C MOcov install

- name: Add bids-matlab
run: |
git clone https://github.com/bids-standard/bids-matlab.git --depth 1

- name: Update octave path
run: |
octave $OCTFLAGS --eval "initCppRoi; savepath();"
octave $OCTFLAGS --eval "addpath(fullfile(pwd, 'tests', 'utils')); savepath();"
octave $OCTFLAGS --eval "addpath(fullfile(pwd, 'bids-matlab')); savepath();"

- name: Run tests
run: |
Expand Down
30 changes: 30 additions & 0 deletions src/utils/converToValidCamelCase.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
% (C) Copyright 2021 CPP ROI developers

function str = converToValidCamelCase(str)
%
% Removes non alphanumeric characters and uppercase first letter of all
% words but the first
%
% USAGE::
%
% str = converToValidCamelCase(str)
%
% :param str:
% :type str: string
%
% :returns:
% :str: (string) returns the input with an upper case for first letter
% for all words but the first one (``camelCase``) and
% removes invalid characters (like spaces).
%
%

% camel case: upper case for first letter for all words but the first one
spaceIdx = regexp(str, '[a-zA-Z0-9]*', 'start');
str(spaceIdx(2:end)) = upper(str(spaceIdx(2:end)));

% remove invalid characters
[unvalidCharacters] = regexp(str, '[^a-zA-Z0-9]');
str(unvalidCharacters) = [];

end
16 changes: 4 additions & 12 deletions src/utils/createFilename.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,14 @@
function newName = createFilename(p)

entities = fieldnames(p);

if ~isfield(p, 'filename')
p.filename = '';
end

ext = p.ext;
suffix = p.type;

% create a new file name based on the remaining entities in the order they
% are in the structure
entities = setxor(entities, {'filename', 'type', 'ext'}, 'stable');

newName = '';
for iEntity = 1:numel(entities)

thisEntity = entities{iEntity};

if ~isempty(p.(thisEntity))
if ~any(strcmp(thisEntity, {'filename', 'type', 'ext'})) && ...
~isempty(p.(thisEntity))
thisLabel = converToValidCamelCase(p.(thisEntity));
newName = [newName '_' thisEntity '-' thisLabel]; %#ok<AGROW>
end
Expand All @@ -30,6 +20,8 @@
% remove lead '_'
newName(1) = [];

ext = p.ext;
suffix = p.type;
newName = [newName '_', suffix ext];

end
23 changes: 23 additions & 0 deletions tests/test_createFilename.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
% (C) Copyright 2021 CPP ROI developers

function test_suite = test_createFilename %#ok<*STOUT>
try % assignment of 'localfunctions' is necessary in Matlab >= 2016
test_functions = localfunctions(); %#ok<*NASGU>
catch % no problem; early Matlab versions can use initTestSuite fine
end
initTestSuite;
end

function test_createFilenameBasic()

p.ext = '.nii';
p.type = 'bold';
p.sub = '01';
p.task = 'face repetition';
p.run = '01';

newName = createFilename(p);

assertEqual(newName, 'sub-01_task-faceRepetition_run-01_bold.nii');

end