classdef test_expcodes < SCDtest
  
  properties
    expcode_obj;
    SCDexps;
    shot;
  end
  
  properties(ClassSetupParameter)
    expcode = {'1','1005','1007','1008','1010'};        % list of expcodes to test
    shote   = {'68915','68915','70127','68915','68915'}; % different shot per exp code
    %expcode = {'1'};        % list of expcodes to test
    %shote   = {'68915'}; % different shot per exp code
  end
   
  methods(TestClassSetup,ParameterCombination='sequential')
    function setup_environment(testCase)
      testCase.addTeardown(@cd,pwd);
      testCase.addTeardown(@path,path);
      testCase.addTeardown(@() SCDclass_expcode.close_all(0))
      
      % Clean data dictionaries
      SCD.clean_dd;

      basePath = fullfile(fileparts(mfilename('fullpath')),'..');
      run(fullfile(basePath,'rtccode_paths'));

      % clear base workspace
      evalin('base','clear variables');
      
      % get SCD experimental code object container
      testCase.SCDexps = SCDconf_createexpcodes;
      
      SCDconf_setConf('SIM'); % set ConfigurationSettings for Simulation
    end
  
    function setup_expcode(testCase,expcode,shote)
      % get this expcode object from expcode object container
      fprintf('\n=== Testing expcode %s ===\n',expcode);
      testCase.expcode_obj = getbymaincode(testCase.SCDexps,str2double(expcode));
      testCase.shot = str2double(shote);
    end
    
  end
  
  methods(Test,TestTags={'expcodes'})
    
    function test_expcode_printinfo(testCase)
      testCase.expcode_obj.printinfo
    end
    
    function test_expcode_init(testCase)
        fprintf('\n=== Testing init for expcode %d: %s === \n',...
        testCase.expcode_obj.maincode,...
        testCase.expcode_obj.name);
      
        testCase.expcode_obj.init; % initialize
    end
    
    function test_expcode_setup(testCase)    
      fprintf('\n=== Testing setup for expcode %d: %s === \n',...
      testCase.expcode_obj.maincode,...
      testCase.expcode_obj.name);
    
      testCase.expcode_obj.setup; % run setup this exp code
    end
   
    function test_expcode_compile_nodes(testCase)
      % compile each node separately first
      nodes = testCase.expcode_obj.nodes;
      for inode = 1:numel(nodes)
        node = nodes{inode};
        if isempty(node)
          fprintf('skipping compilation for node %d since not configured\n',inode)
        elseif ~node.active
          fprintf('skipping compilation for node %d since not active\n',inode)
        else
          fprintf('\n === Testing Simulink compilation for node %02d of expcode %d: %s === \n',...
            inode,testCase.expcode_obj.maincode,testCase.expcode_obj.name);
          testCase.expcode_obj.compile(inode); % compile single node
        end
      end
    end
    
    function test_actualize(testCase)
      testCase.expcode_obj.actualize(testCase.shot);
    end
    
    function test_expcode_sim(testCase)
      testCase.assumeFail('Skipping all simulations tests temporarily to save time')
      fprintf('\n === Testing Simulink simulation of tcv.slx for expcode %d: === \n',testCase.expcode_obj.maincode)
      testCase.expcode_obj.sim; % simulate whole tcv.slx with this expcode
    end
    
    function test_build(testCase)
      testCase.assumeFail('Skipping all build tests temporarily')
      testCase.expcode_obj.build;
    end
  end
end
