Skip to content

Commit 78da4c7

Browse files
committed
Added test for StimulusControl and fixed err id in expServer test
1 parent 2e99f3f commit 78da4c7

File tree

3 files changed

+141
-32
lines changed

3 files changed

+141
-32
lines changed

tests/Contents.m

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,38 @@
66
% check the coverage of a given test, use `checkCoverage`.
77
%
88
% Files:
9-
% runall - gathers and runs all tests in Rigbox
10-
% checkCoverage - Check the coverage of a given test
11-
% AlyxPanel_test - Tests for eui.AlyxPanel
12-
% ParamEditor_test - Tests for eui.ParamEditor
13-
% Parameters_test - Tests for exp.Parameters
14-
% calibrate_test - Tests for hw.calibrate
15-
% catStructs_test - Tests for catStructs
16-
% cellflat_test - Tests for cellflat
17-
% cellsprintf_test - Tests for cellsprintf
18-
% dat_test - Tests for the +dat package
19-
% emptyElems_test - Tests for emptyElems
20-
% ensureCell_test - Tests for ensureCell
21-
% expServer_test - Tests for srv.expServer
22-
% fileFunction_test - Tests for fileFunction
23-
% file_test - Tests for the +file package
24-
% fun_test - Tests for the +fun package
25-
% iff_test - Tests for iff
26-
% inferParams_test - inferParams test
27-
% loadVar_test - loadVar test
28-
% mapToCell_test - Tests for mapToCell function
29-
% mergeStructs_test - mergeStructs test
30-
% namedArg_test - namedArg test
31-
% nop_test - Tests for nop
32-
% num2cellstr_test - Tests for num2cellstr
33-
% obj2json_test - Tests for obj2struct
34-
% pick_test - pick test
35-
% repelems_test - repelems test
36-
% structAssign_test - structAssign test
37-
% superSave_test - superSave test
38-
% tabulateArgs_test - Tests for tabulateArgs
39-
% varName_test - varName test
9+
% runall - gathers and runs all tests in Rigbox
10+
% checkCoverage - Check the coverage of a given test
11+
% AlyxPanel_test - Tests for eui.AlyxPanel
12+
% ParamEditor_test - Tests for eui.ParamEditor
13+
% Parameters_test - Tests for exp.Parameters
14+
% StimulusControl_test - Tests for srv.StimulusControl and
15+
% srv.stimulusControllers
16+
% calibrate_test - Tests for hw.calibrate
17+
% catStructs_test - Tests for catStructs
18+
% cellflat_test - Tests for cellflat
19+
% cellsprintf_test - Tests for cellsprintf
20+
% dat_test - Tests for the +dat package
21+
% emptyElems_test - Tests for emptyElems
22+
% ensureCell_test - Tests for ensureCell
23+
% expServer_test - Tests for srv.expServer
24+
% fileFunction_test - Tests for fileFunction
25+
% file_test - Tests for the +file package
26+
% fun_test - Tests for the +fun package
27+
% iff_test - Tests for iff
28+
% inferParams_test - inferParams test
29+
% loadVar_test - loadVar test
30+
% mapToCell_test - Tests for mapToCell function
31+
% mergeStructs_test - mergeStructs test
32+
% namedArg_test - namedArg test
33+
% nop_test - Tests for nop
34+
% num2cellstr_test - Tests for num2cellstr
35+
% obj2json_test - Tests for obj2struct
36+
% pick_test - pick test
37+
% repelems_test - repelems test
38+
% structAssign_test - structAssign test
39+
% superSave_test - superSave test
40+
% tabulateArgs_test - Tests for tabulateArgs
41+
% varName_test - varName test
42+
4043

tests/StimulusControl_test.m

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
%STIMULUSCONTROL_TEST Tests for srv.StimulusControl and
2+
%srv.stimulusControllers
3+
% TODO Create tests for remaining methods
4+
classdef (SharedTestFixtures={ % add 'fixtures' folder as test fixture
5+
matlab.unittest.fixtures.PathFixture('fixtures'),...
6+
matlab.unittest.fixtures.PathFixture(['fixtures' filesep 'util'])})...
7+
StimulusControl_test < matlab.unittest.TestCase & matlab.mock.TestCase
8+
9+
properties (SetAccess = protected)
10+
% An experiment reference for the test
11+
Ref
12+
% A list of StimulusControl names for testing the remote file
13+
RemoteNames = {'Rig1', 'Sevvan', 'Reet'}
14+
end
15+
16+
methods (TestClassSetup)
17+
function setupFolder(testCase)
18+
% SETUPFOLDER Set up subject, queue and config folders for test
19+
% Creates a few folders for saving parameters and hardware. Adds
20+
% teardowns for deletion of these folders. Also creates a custom
21+
% paths file to deactivate Alyx.
22+
%
23+
% TODO Make into shared fixture
24+
25+
assert(endsWith(which('dat.paths'),...
26+
fullfile('tests', 'fixtures', '+dat', 'paths.m')));
27+
28+
% Set INTEST flag to true
29+
testCase.setTestFlag(true)
30+
testCase.addTeardown(@testCase.setTestFlag, false)
31+
32+
% Create a rig config folder
33+
configDir = getOr(dat.paths, 'rigConfig');
34+
assert(mkdir(configDir), 'Failed to create config directory')
35+
36+
% Clear loadVar cache
37+
addTeardown(testCase, @clearCBToolsCache)
38+
39+
% Create a remote file for one of the tests
40+
globalConfigDir = getOr(dat.paths, 'globalConfig');
41+
stimulusControllers = cellfun(@srv.StimulusControl.create, testCase.RemoteNames);
42+
save(fullfile(globalConfigDir, 'remote'), 'stimulusControllers')
43+
assert(file.exists(fullfile(globalConfigDir, 'remote.mat')))
44+
45+
% Add teardown to remove folders
46+
rmFcn = @()assert(rmdir(globalConfigDir, 's'), ...
47+
'Failed to remove test config folder');
48+
addTeardown(testCase, rmFcn)
49+
50+
% Set some default behaviours for some of the objects; create a ref
51+
testCase.Ref = dat.constructExpRef('test', now, randi(10000));
52+
end
53+
54+
end
55+
56+
methods (Test)
57+
58+
function test_create(testCase)
59+
% Test for constructor defaults
60+
name = testCase.RemoteNames{1};
61+
sc = srv.StimulusControl.create(name);
62+
testCase.verifyMatches(sc.Name, name, 'Failed to set Name')
63+
testCase.verifyMatches(sc.Uri, ['.*',name,':',num2str(sc.DefaultPort)])
64+
65+
uri = 'ws://rig:1428';
66+
sc = srv.StimulusControl.create(name, uri);
67+
testCase.verifyMatches(sc.Name, name, 'Failed to set Name')
68+
testCase.verifyEqual(sc.Uri, uri, 'Failed to set provided uri')
69+
end
70+
71+
function test_errorOnFail(testCase)
72+
% A message array to test
73+
id = 'test:error:failSent';
74+
msg = 'Fail message';
75+
r = {'success', testCase.Ref, id, msg};
76+
77+
srv.StimulusControl.errorOnFail('This is no error') % Test char input
78+
srv.StimulusControl.errorOnFail(r) % Test array input without fail
79+
r{1} = 'fail'; % Change message to fail state
80+
testCase.verifyError(@()srv.StimulusControl.errorOnFail(r), id, ...
81+
'Failed to throw error with ID')
82+
ex.message = [];
83+
r(3) = []; % Remove ID
84+
try srv.StimulusControl.errorOnFail(r), catch ex, end
85+
testCase.verifyMatches(ex.message, msg, 'Failed to throw error without ID')
86+
end
87+
88+
function test_stimulusControllers(testCase)
89+
% Test the loading of saved StimulusControl objects via dat.paths
90+
sc = srv.stimulusControllers;
91+
testCase.verifyLength(sc, length(testCase.RemoteNames))
92+
testCase.verifyTrue(isa(sc, 'srv.StimulusControl'))
93+
testCase.verifyEqual({sc.Name}, sort(testCase.RemoteNames), ...
94+
'Failed to return array sorted by Name')
95+
end
96+
end
97+
98+
methods (Static)
99+
function setTestFlag(TF)
100+
% SETTESTFLAG Set global INTEST flag
101+
% Allows setting of test flag via callback function
102+
global INTEST
103+
INTEST = TF;
104+
end
105+
end
106+
end

tests/expServer_test.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ function test_quit(testCase)
224224
function test_devices_fail(testCase)
225225
% Set hw.devices to return empty
226226
clear devices;
227-
id = 'rigbox:srv:expServer:missingHardware';
227+
id = 'Rigbox:srv:expServer:missingHardware';
228228
testCase.verifyError(@srv.expServer, id, ...
229229
'Expected error for misconfigured hardware');
230230
end

0 commit comments

Comments
 (0)