Skip to content

Commit a0ccc0a

Browse files
committed
add tests for user input
1 parent c6659f7 commit a0ccc0a

File tree

8 files changed

+207
-90
lines changed

8 files changed

+207
-90
lines changed

manualTests/test_userInput.m

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function cfg = askForGroupAndOrSession(cfg)
2+
3+
askGrpSess = [true true];
4+
5+
if isfield(cfg, 'subject') && ...
6+
isfield(cfg.subject, 'askGrpSess') && ...
7+
~isempty(cfg.subject.askGrpSess)
8+
9+
askGrpSess = cfg.subject.askGrpSess;
10+
11+
end
12+
13+
if numel(askGrpSess) < 2
14+
askGrpSess(2) = 1;
15+
end
16+
17+
cfg.subject.askGrpSess = askGrpSess;
18+
19+
end

src/subfun/createQuestionList.m

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function questions = createQuestionList(cfg)
2+
3+
cfg = askForGroupAndOrSession(cfg);
4+
5+
questions.group = 'Enter subject group (leave empty if none): ';
6+
questions.subject = 'Enter subject number (1-999): ';
7+
questions.session = 'Enter the session (i.e day - 1-999)) number: ';
8+
questions.run = 'Enter the run number (1-999): ';
9+
questions.mustBePositiveInteger = 'Please enter a positive integer: ';
10+
% questions.questionsToAsk is a cell array : second column is a boolean
11+
% set to true if we must run inputCheck on the response
12+
questions.questionsToAsk = cell(4,2);
13+
14+
% subject group
15+
if cfg.subject.askGrpSess(1)
16+
questions.questionsToAsk{1, 1} = questions.group;
17+
questions.questionsToAsk{1, 2} = false;
18+
end
19+
20+
% the subject number
21+
questions.questionsToAsk{2, 1} = questions.subject;
22+
questions.questionsToAsk{2, 2} = true;
23+
24+
% the session number
25+
if cfg.subject.askGrpSess(2)
26+
questions.questionsToAsk{3, 1} = questions.session;
27+
questions.questionsToAsk{3, 2} = true;
28+
end
29+
30+
% the run number
31+
questions.questionsToAsk{4, 1} = questions.run;
32+
questions.questionsToAsk{4, 2} = true;
33+
34+
end

src/subfun/isPositiveInteger.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function trueOrFalse = isPositiveInteger(input2check)
2+
3+
trueOrFalse = ~(...
4+
~isnumeric(input2check) || ...
5+
isnan(input2check) || ...
6+
fix(input2check) ~= input2check || ...
7+
input2check < 0);
8+
9+
end

src/userInputs.m

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -9,72 +9,75 @@
99
% - the first value set to false will skip asking for the participants
1010
% group
1111
% - the second value set to false will skip asking for the session
12-
12+
1313
if nargin < 1
14-
cfg = [];
14+
cfg = struct('debug', []);
1515
end
16+
1617
if isempty(cfg.debug)
1718
cfg.debug.do = false;
1819
end
19-
20-
askGrpSess = [true true];
21-
if isfield(cfg, 'subject') && ...
22-
isfield(cfg.subject, 'askGrpSess') && ...
23-
~isempty(cfg.subject.askGrpSess)
24-
25-
askGrpSess = cfg.subject.askGrpSess;
26-
27-
end
28-
if numel(askGrpSess) < 2
29-
askGrpSess(2) = 1;
30-
end
31-
32-
subjectGrp = '';
33-
subjectNb = []; %#ok<*NASGU>
34-
sessionNb = [];
35-
runNb = [];
20+
21+
responses{1,1} = ''; % subjectGrp
22+
responses{2,1} = []; % subjectNb
23+
responses{3,1} = 1; % runNb
24+
responses{4,1} = []; % sessionNb
3625

3726
% When in debug more this function returns some dummy values
3827
if cfg.debug.do
39-
subjectGrp = 'ctrl';
40-
subjectNb = 666;
41-
runNb = 666;
42-
sessionNb = 666;
43-
28+
29+
responses{1,1} = 'ctrl';
30+
responses{2,1} = 666;
31+
responses{3,1} = 666;
32+
responses{4,1} = 666;
33+
4434
% Otherwise it prompts the user for some information
4535
else
4636

47-
% subject group
48-
if askGrpSess(1)
49-
subjectGrp = lower(input('Enter subject group (leave empty if none): ', 's'));
50-
end
37+
questions = createQuestionList(cfg);
38+
39+
responses = askUserCli(questions);
40+
41+
end
42+
43+
cfg.subject.subjectGrp = responses{1,1};
44+
cfg.subject.subjectNb = responses{2,1};
45+
cfg.subject.sessionNb = responses{3,1};
46+
cfg.subject.runNb = responses{4,1};
47+
48+
end
5149

52-
% the subject number
53-
subjectNb = str2double(input('Enter subject number (1-999): ', 's'));
54-
subjectNb = checkInput(subjectNb);
5550

56-
% the session number
57-
if numel(askGrpSess) > 1 && askGrpSess(2)
58-
sessionNb = str2double(input('Enter the session (i.e day - 1-999)) number: ', 's'));
59-
sessionNb = checkInput(sessionNb);
51+
function responses = askUserCli(questions)
52+
% response = askUserCli(questions)
53+
%
54+
% command line interface to ask questions to user
55+
%
56+
57+
for iQuestion = 1:size(questions.questionsToAsk, 1)
58+
59+
if ~isempty(questions.questionsToAsk{iQuestion,1})
60+
61+
responses{iQuestion, 1} = ...
62+
input(questions.questionsToAsk{iQuestion,1}, 's'); %#ok<*AGROW>
63+
64+
if questions.questionsToAsk{iQuestion,2}
65+
responses{iQuestion, 1} = str2double(responses);
66+
responses{iQuestion, 1} = checkInput(responses, questions);
67+
end
68+
6069
end
61-
62-
% the run number
63-
runNb = str2double(input('Enter the run number (1-999): ', 's'));
64-
runNb = checkInput(runNb);
65-
70+
6671
end
67-
68-
cfg.subject.subjectGrp = subjectGrp;
69-
cfg.subject.subjectNb = subjectNb;
70-
cfg.subject.sessionNb = sessionNb;
71-
cfg.subject.runNb = runNb;
72-
72+
7373
end
7474

75-
function input2check = checkInput(input2check)
75+
76+
function input2check = checkInput(input2check, questions)
7677
% this function checks the input to makes sure the user enters a positive integer
77-
while isnan(input2check) || fix(input2check) ~= input2check || input2check < 0
78-
input2check = str2double(input('Please enter a positive integer: ', 's'));
78+
79+
while ~isPositiveInteger(input2check)
80+
input2check = str2double(input(questions.mustBePositiveInteger, 's'));
7981
end
82+
8083
end
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
function test_suite = test_askForGroupAndOrSession %#ok<*STOUT>
2+
try % assignment of 'localfunctions' is necessary in Matlab >= 2016
3+
test_functions = localfunctions(); %#ok<*NASGU>
4+
catch % no problem; early Matlab versions can use initTestSuite fine
5+
end
6+
initTestSuite;
7+
end
8+
9+
function test_askForGroupAndOrSessionBasic()
10+
11+
%% set up
12+
cfg = struct();
13+
cfg = askForGroupAndOrSession(cfg);
14+
15+
expectedStructure = struct('subject', struct('askGrpSess', [true true]));
16+
17+
assertEqual(expectedStructure, cfg)
18+
19+
end
20+
21+
function test_askForGroupAndOrSessionNoGroup()
22+
23+
cfg.subject.askGrpSess = 0;
24+
cfg = askForGroupAndOrSession(cfg);
25+
26+
expectedStructure = struct('subject', struct('askGrpSess', [false true]));
27+
28+
assertEqual(expectedStructure, cfg)
29+
30+
end
31+
32+
function test_askForGroupAndOrSessionNoGroupNoSession()
33+
34+
cfg.subject.askGrpSess = [0 0];
35+
cfg = askForGroupAndOrSession(cfg);
36+
37+
expectedStructure = struct('subject', struct('askGrpSess', [false false]));
38+
39+
assertEqual(expectedStructure, cfg)
40+
41+
end
42+
43+
function test_askForGroupAndOrSessionNoSession()
44+
45+
cfg.subject.askGrpSess = [1 0];
46+
cfg = askForGroupAndOrSession(cfg);
47+
48+
expectedStructure = struct('subject', struct('askGrpSess', [true false]));
49+
50+
assertEqual(expectedStructure, cfg)
51+
52+
end

tests/test_createQuestionList.m

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function test_suite = test_createQuestionList %#ok<*STOUT>
2+
try % assignment of 'localfunctions' is necessary in Matlab >= 2016
3+
test_functions = localfunctions(); %#ok<*NASGU>
4+
catch % no problem; early Matlab versions can use initTestSuite fine
5+
end
6+
initTestSuite;
7+
end
8+
9+
function test_createQuestionListBasic()
10+
11+
%% set up
12+
cfg = struct();
13+
14+
questions = createQuestionList(cfg);
15+
16+
expectedCell = { ...
17+
'Enter subject group (leave empty if none): ', false;
18+
'Enter subject number (1-999): ', true;
19+
'Enter the session (i.e day - 1-999)) number: ', true;
20+
'Enter the run number (1-999): ', true};
21+
22+
assertEqual(expectedCell, questions.questionsToAsk)
23+
24+
end

tests/test_isPositiveInteger.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function test_suite = test_isPositiveInteger %#ok<*STOUT>
2+
try % assignment of 'localfunctions' is necessary in Matlab >= 2016
3+
test_functions = localfunctions(); %#ok<*NASGU>
4+
catch % no problem; early Matlab versions can use initTestSuite fine
5+
end
6+
initTestSuite;
7+
end
8+
9+
function test_isPositiveIntegerBasic()
10+
11+
assertTrue(isPositiveInteger(1));
12+
assertFalse(isPositiveInteger(nan()))
13+
assertFalse(isPositiveInteger(0.3))
14+
assertFalse(isPositiveInteger(-1))
15+
assertFalse(isPositiveInteger('1'))
16+
17+
end

0 commit comments

Comments
 (0)