Skip to content

Commit 793b2bb

Browse files
committed
add the most basid test of them all and make sure it passes
1 parent 156b4f2 commit 793b2bb

File tree

2 files changed

+56
-12
lines changed

2 files changed

+56
-12
lines changed

saveEventsFile.m

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
end
4848

4949
if nargin < 3 || isempty(logFile)
50-
logFile = struct();
50+
logFile = struct('filename', [], 'extraColumns', cell(1));
51+
logFile(1).filename = '';
5152
end
5253

5354
switch action
@@ -157,7 +158,7 @@
157158
logFile(1).extraColumns = tmp;
158159
end
159160

160-
if isfield(logFile, 'extraColumns')
161+
if isfield(logFile, 'extraColumns') && ~isempty(logFile(1).extraColumns)
161162
namesExtraColumns = fieldnames(logFile(1).extraColumns);
162163
end
163164

@@ -171,7 +172,7 @@
171172
for iExtraColumn = 1:numel(namesExtraColumns)
172173

173174
nbCol = returnNbColumns(logFile, namesExtraColumns{iExtraColumn});
174-
175+
175176
if ~isfield(logFile(1).extraColumns.(namesExtraColumns{iExtraColumn}), 'length')
176177
logFile(1).extraColumns.(namesExtraColumns{iExtraColumn}).length = nbCol;
177178
end
@@ -193,11 +194,11 @@
193194
end
194195

195196
function nbCol = returnNbColumns(logFile, nameExtraColumn)
196-
197+
197198
thisExtraColumn = logFile(1).extraColumns.(nameExtraColumn);
198199

199200
nbCol = 1;
200-
201+
201202
if isfield(thisExtraColumn, 'length')
202203
nbCol = thisExtraColumn.length;
203204
end

tests/test_saveEventsFileOpen.m

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function test_saveEventsFileOpen()
22

3-
%% check header writing with extra columns
3+
%% Initialize file
44
fprintf('\n\n--------------------------------------------------------------------\n\n');
55

66
clear;
@@ -16,15 +16,12 @@ function test_saveEventsFileOpen()
1616

1717
cfg.testingDevice = 'mri';
1818

19-
% define the extra columns: they will be added to the tsv files in the order the user input them
20-
logFile.extraColumns = {'Speed', 'is_Fixation'};
21-
2219
%%% do stuff
2320

2421
[cfg, expParameters] = createFilename(cfg, expParameters);
2522

2623
% create the events file and header
27-
logFile = saveEventsFile('open', expParameters, logFile);
24+
logFile = saveEventsFile('open', expParameters);
2825

2926
% close the file
3027
saveEventsFile('close', expParameters, logFile);
@@ -37,9 +34,8 @@ function test_saveEventsFileOpen()
3734
expParameters.date '.tsv'];
3835

3936
% open the file
40-
nbExtraCol = 2;
4137
FID = fopen(fullfile(funcDir, eventFilename), 'r');
42-
C = textscan(FID, repmat('%s', 1, nbExtraCol + 3), 'Delimiter', '\t', 'EndOfLine', '\n');
38+
C = textscan(FID, repmat('%s', 1, 3), 'Delimiter', '\t', 'EndOfLine', '\n');
4339

4440
% check that the file has the right path and name
4541
assert(exist(fullfile(funcDir, eventFilename), 'file') == 2);
@@ -48,6 +44,53 @@ function test_saveEventsFileOpen()
4844
assert(isequal(C{1}{1}, 'onset'));
4945
assert(isequal(C{2}{1}, 'trial_type'));
5046
assert(isequal(C{3}{1}, 'duration'));
47+
48+
49+
50+
%% check header writing with extra columns
51+
fprintf('\n\n--------------------------------------------------------------------\n\n');
52+
53+
clear;
54+
55+
outputDir = fullfile(fileparts(mfilename('fullpath')), '..', 'output');
56+
57+
%%% set up
58+
59+
expParameters.subjectNb = 1;
60+
expParameters.runNb = 1;
61+
expParameters.task = 'testtask';
62+
expParameters.outputDir = outputDir;
63+
64+
cfg.testingDevice = 'mri';
65+
66+
% define the extra columns: they will be added to the tsv files in the order the user input them
67+
logFile.extraColumns = {'Speed', 'is_Fixation'};
68+
69+
%%% do stuff
70+
71+
[cfg, expParameters] = createFilename(cfg, expParameters);
72+
73+
% create the events file and header
74+
logFile = saveEventsFile('open', expParameters, logFile);
75+
76+
% close the file
77+
saveEventsFile('close', expParameters, logFile);
78+
79+
%%% test section
80+
81+
% open the file
82+
nbExtraCol = 2;
83+
FID = fopen(fullfile( ...
84+
expParameters.subjectOutputDir, ...
85+
expParameters.modality, ...
86+
expParameters.fileName.events), ...
87+
'r');
88+
C = textscan(FID, repmat('%s', 1, nbExtraCol + 3), 'Delimiter', '\t', 'EndOfLine', '\n');
89+
90+
% check the extra columns of the header
91+
assert(isequal(C{1}{1}, 'onset'));
92+
assert(isequal(C{2}{1}, 'trial_type'));
93+
assert(isequal(C{3}{1}, 'duration'));
5194
assert(isequal(C{4}{1}, 'Speed'));
5295
assert(isequal(C{5}{1}, 'is_Fixation'));
5396

0 commit comments

Comments
 (0)