|
| 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 |
0 commit comments