Skip to content

Commit f3fdc12

Browse files
authored
Merge pull request #162 from Remi-Gau/remi-update_tests
[INFRA] update tests
2 parents 0445396 + 16c8a42 commit f3fdc12

File tree

11 files changed

+239
-74
lines changed

11 files changed

+239
-74
lines changed

.github/workflows/moxunit.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on:
44
push:
55
branches:
66
- master
7+
- dev
78
pull_request:
89
branches: '*'
910

miss_hit.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ copyright_entity: "CPP_BIDS developers"
66

77
# metrics limit for the code quality (https://florianschanda.github.io/miss_hit/metrics.html)
88
metric "cnest": limit 4
9-
metric "file_length": limit 550
9+
metric "file_length": limit 500
1010
metric "cyc": limit 15
1111
metric "parameters": limit 6

src/saveEventsFile.m

Lines changed: 5 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ function printHeaderExtraColumns(logFile)
285285
namesExtraColumns{iExtraColumn});
286286

287287
end
288-
warningSaveEventsFile(cfg, 'missingData', warningMessage);
288+
throwWarning(cfg, 'saveEventsFile:missingData', warningMessage);
289289

290290
if cfg.verbose > 1
291291
disp(logFile(iEvent));
@@ -297,50 +297,6 @@ function printHeaderExtraColumns(logFile)
297297

298298
end
299299

300-
function data = checkInput(data)
301-
% check the data to write
302-
% default will be 'n/a' for chars and NaN for numeric data
303-
% for numeric data that don't have the expected length, it will be padded with NaNs
304-
if islogical(data) && data
305-
data = 'true';
306-
elseif islogical(data) && ~data
307-
data = 'false';
308-
end
309-
310-
if ischar(data) && isempty(data) || strcmp(data, ' ')
311-
data = 'n/a';
312-
elseif isempty(data)
313-
% important to not set this to n/a as we still need to check if this
314-
% numeric valur has the right length and needs to be nan padded
315-
data = nan;
316-
end
317-
318-
end
319-
320-
function data = nanPadding(cfg, data, expectedLength)
321-
322-
if nargin < 2
323-
expectedLength = [];
324-
end
325-
326-
if ~isempty(expectedLength) && isnumeric(data)
327-
328-
if max(size(data)) < expectedLength
329-
padding = expectedLength - max(size(data));
330-
data(end + 1:end + padding) = nan(1, padding);
331-
332-
elseif max(size(data)) > expectedLength
333-
warningMessage = ['A field for this event is longer than expected.', ...
334-
'Truncating extra values.'];
335-
warningSaveEventsFile(cfg, 'arrayTooLong', warningMessage);
336-
337-
data = data(1:expectedLength);
338-
339-
end
340-
end
341-
342-
end
343-
344300
function logFile = saveToLogFile(logFile, cfg)
345301

346302
% appends to the logfile all the data stored in the structure
@@ -367,7 +323,7 @@ function printHeaderExtraColumns(logFile)
367323

368324
skipEvent = true;
369325

370-
warningMessageID = 'emptyEvent';
326+
warningMessageID = 'saveEventsFile:emptyEvent';
371327
warningMessage = sprintf(['Skipping saving this event. \n '...
372328
'onset: %s \n duration: %s \n'], ...
373329
onset, ...
@@ -391,21 +347,21 @@ function printHeaderExtraColumns(logFile)
391347
if all(~isValid)
392348
skipEvent = true;
393349

394-
warningMessageID = 'emptyEvent';
350+
warningMessageID = 'saveEventsFile:emptyEvent';
395351
warningMessage = sprintf(['Skipping saving this event. \n', ...
396352
'No values defined. \n']);
397353

398354
elseif any(~isValid)
399355
skipEvent = false;
400356

401-
warningMessageID = 'missingData';
357+
warningMessageID = 'saveEventsFile:missingData';
402358
warningMessage = sprintf('Missing some %s data for this event. \n', ...
403359
namesExtraColumns{find(isValid)});
404360
end
405361
end
406362

407363
% now save the event to log file (if not skipping)
408-
warningSaveEventsFile(cfg, warningMessageID, warningMessage);
364+
throwWarning(cfg, warningMessageID, warningMessage);
409365

410366
printToFile(cfg, logFile, skipEvent, iEvent);
411367

@@ -504,14 +460,3 @@ function errorSaveEventsFile(identifier)
504460
error(errorStruct);
505461

506462
end
507-
508-
function warningSaveEventsFile(cfg, identifier, warningMessage)
509-
if cfg.verbose > 0 && ...
510-
nargin == 3 && ...
511-
~isempty(identifier) && ...
512-
~isempty(warningMessage)
513-
514-
warningMessageID = ['saveEventsFile:' identifier];
515-
warning(warningMessageID, warningMessage);
516-
end
517-
end

src/utils/checkInput.m

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
% (C) Copyright 2020 CPP_BIDS developers
2+
3+
function data = checkInput(data)
4+
%
5+
% Check the data to write and convert to the proper format if needed.
6+
%
7+
% Default will be 'n/a' for chars and NaN for numeric data.
8+
%
9+
% USAGE::
10+
%
11+
% data = checkInput(data)
12+
%
13+
14+
if islogical(data) && data
15+
data = 'true';
16+
elseif islogical(data) && ~data
17+
data = 'false';
18+
end
19+
20+
if ischar(data) && isempty(data) || strcmp(data, ' ')
21+
data = 'n/a';
22+
elseif isempty(data)
23+
% Important to not set this to n/a as we might still need to check if this
24+
% numeric value has the right length and needs to be nan padded
25+
data = nan;
26+
end
27+
28+
end

src/utils/nanPadding.m

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
% (C) Copyright 2020 CPP_BIDS developers
2+
3+
function data = nanPadding(cfg, data, expectedLength)
4+
%
5+
% For numeric data that don't have the expected length, it will be padded
6+
% with NaNs. If the vector is too long it will be truncated
7+
%
8+
% USAGE::
9+
%
10+
% data = nanPadding(cfg, data, expectedLength)
11+
%
12+
%
13+
14+
if nargin < 2
15+
expectedLength = [];
16+
end
17+
18+
if ~isempty(expectedLength) && isnumeric(data)
19+
20+
if max(size(data)) < expectedLength
21+
padding = expectedLength - max(size(data));
22+
data(end + 1:end + padding) = nan(1, padding);
23+
24+
elseif max(size(data)) > expectedLength
25+
warningMessage = ['A field for this event is longer than expected.', ...
26+
'Truncating extra values.'];
27+
throwWarning(cfg, 'nanPadding:arrayTooLong', warningMessage);
28+
29+
data = data(1:expectedLength);
30+
31+
end
32+
end
33+
34+
end

src/utils/throwWarning.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
% (C) Copyright 2020 CPP_BIDS developers
2+
3+
function throwWarning(cfg, identifier, warningMessage)
4+
if cfg.verbose > 0 && ...
5+
nargin == 3 && ...
6+
~isempty(identifier) && ...
7+
~isempty(warningMessage)
8+
9+
warning(identifier, warningMessage);
10+
end
11+
end

tests/test_checkInput.m

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
% (C) Copyright 2020 CPP_BIDS developers
2+
3+
function test_suite = test_checkInput %#ok<*STOUT>
4+
try % assignment of 'localfunctions' is necessary in Matlab >= 2016
5+
test_functions = localfunctions(); %#ok<*NASGU>
6+
catch % no problem; early Matlab versions can use initTestSuite fine
7+
end
8+
initTestSuite;
9+
end
10+
11+
function test_checkInputBasic()
12+
13+
data = checkInput(1);
14+
assertEqual(data, 1);
15+
16+
data = checkInput('test');
17+
assertEqual(data, 'test');
18+
19+
data = checkInput('');
20+
assertEqual(data, 'n/a');
21+
22+
data = checkInput(' ');
23+
assertEqual(data, 'n/a');
24+
25+
data = checkInput([]);
26+
assertEqual(data, nan);
27+
28+
data = checkInput(true());
29+
assertEqual(data, 'true');
30+
31+
data = checkInput(false());
32+
assertEqual(data, 'false');
33+
34+
end

tests/test_createDataDictionary.m

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,18 @@ function test_createDataDictionaryBasic()
5757
% jsondecode:27 (/github/workspace/lib/bids-matlab/+bids/+util/jsondecode.m)
5858
% test_createDataDictionary>test_createDataDictionaryBasic:48
5959
% (/github/workspace/tests/test_createDataDictionary.m)
60+
%
61+
% failure: fileread: cannot open file
62+
% fileread:37 (/octave/share/octave/5.2.0/m/io/fileread.m)
63+
% jsondecode:27 (/github/workspace/lib/bids-matlab/+bids/+util/jsondecode.m)
64+
% test_createDataDictionary>test_createDataDictionaryBasic:62
6065

6166
actualStruct = bids.util.jsondecode(fullfile(funcDir, jsonFilename));
6267

6368
% data to test against
6469
expectedStruct = bids.util.jsondecode( ...
6570
fullfile( ...
66-
pwd, '..', 'tests', ...
71+
pwd, ...
6772
'testData', ...
6873
'eventsDataDictionary.json'));
6974

@@ -125,15 +130,18 @@ function test_createDataDictionaryStim()
125130
% jsondecode:27 (/github/workspace/lib/bids-matlab/+bids/+util/jsondecode.m)
126131
% test_createDataDictionary>test_createDataDictionaryBasic:48
127132
% (/github/workspace/tests/test_createDataDictionary.m)
133+
%
134+
% failure: fileread: cannot open file
135+
% fileread:37 (/octave/share/octave/5.2.0/m/io/fileread.m)
136+
% jsondecode:27 (/github/workspace/lib/bids-matlab/+bids/+util/jsondecode.m)
137+
% test_createDataDictionary>test_createDataDictionaryStim:128
128138

129139
actualStruct = bids.util.jsondecode(fullfile(funcDir, jsonFilename));
130140

131141
% data to test against
132142
expectedStruct = bids.util.jsondecode( ...
133143
fullfile( ...
134144
pwd, ...
135-
'..', ...
136-
'tests', ...
137145
'testData', ...
138146
'stimDataDictionary.json'));
139147

tests/test_createJson.m

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -157,26 +157,25 @@ function test_createJsonExtra()
157157
fileName = strrep(cfg.fileName.events, '_events', '_bold');
158158
fileName = strrep(fileName, '.tsv', '.json');
159159

160-
return
161-
162160
% TODO fix error in CI
163161
% failure: /github/workspace/lib/JSONio/jsonread.mex: failed to load: liboctinterp.so.4:
164162
% cannot open shared object file: No such file or directory
165163
% jsondecode:27 (/github/workspace/lib/bids-matlab/+bids/+util/jsondecode.m)
166164
% test_createJson>test_createJsonExtra:158 (/github/workspace/tests/test_createJson.m)
165+
%
166+
% failure: fileread: cannot open file
167+
% fileread:37 (/octave/share/octave/5.2.0/m/io/fileread.m)
168+
% jsondecode:27 (/github/workspace/lib/bids-matlab/+bids/+util/jsondecode.m)
169+
% test_createJson>test_createJsonExtra:180 (/github/workspace/tests/test_createJson.m)
167170

168171
actualStruct = bids.util.jsondecode(fullfile( ...
169172
cfg.dir.outputSubject, ...
170173
cfg.fileName.modality, ...
171174
fileName));
172175

173-
% data to test against
174-
expectedStruct = bids.util.jsondecode( ...
175-
fullfile(pwd, ...
176-
'..', ...
177-
'tests', ...
178-
'testData', ...
179-
'extra_bold.json'));
176+
return
177+
178+
expectedStruct = bids.util.jsondecode(fullfile(pwd, 'testData', 'extra_bold.json'));
180179

181180
% test
182181
assertEqual(expectedStruct, actualStruct);

tests/test_nanPadding.m

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
% (C) Copyright 2020 CPP_BIDS developers
2+
3+
function test_suite = test_nanPadding %#ok<*STOUT>
4+
try % assignment of 'localfunctions' is necessary in Matlab >= 2016
5+
test_functions = localfunctions(); %#ok<*NASGU>
6+
catch % no problem; early Matlab versions can use initTestSuite fine
7+
end
8+
initTestSuite;
9+
end
10+
11+
function test_nanPaddingBasic()
12+
13+
cfg.verbose = 1;
14+
15+
%%
16+
data = 'test';
17+
expectedLength = 2;
18+
19+
data = nanPadding(cfg, data, expectedLength);
20+
21+
assertEqual(data, 'test');
22+
23+
%%
24+
data = [1 0];
25+
expectedLength = 2;
26+
27+
data = nanPadding(cfg, data, expectedLength);
28+
29+
assertEqual(data, [1 0]);
30+
31+
%%
32+
data = [1 0];
33+
expectedLength = 3;
34+
35+
data = nanPadding(cfg, data, expectedLength);
36+
37+
assertEqual(data, [1 0 NaN]);
38+
39+
%%
40+
%%
41+
data = [1 0 1];
42+
expectedLength = 2;
43+
44+
data = nanPadding(cfg, data, expectedLength);
45+
46+
assertEqual(data, [1 0]);
47+
48+
end

0 commit comments

Comments
 (0)