function [passed,results] = run_gdat_tests(test_case)
% test runner for gdat

% F. Felici, EPFL federico.felici@epfl.ch

if nargin==0 || isempty(test_case)
  test_case = 'basic'; % default
end
test_case = lower(test_case);

%% Import some classes we need
import matlab.unittest.selectors.HasTag;
import matlab.unittest.constraints.ContainsSubstring;
import matlab.unittest.selectors.HasName;

%% populate suite
% add path with tests
addpath(genpath(fullfile(fileparts(mfilename('fullpath')),'tests')));
addpath(genpath(fullfile(fileparts(mfilename('fullpath')),'TCV_IMAS')));

suite_all = [matlab.unittest.TestSuite.fromClass(?test_requestnames_tcv),...
  matlab.unittest.TestSuite.fromClass(?test_requestnames_aug),...
  matlab.unittest.TestSuite.fromClass(?test_tcv_get_ids)];

switch test_case
  case 'all'
    suite = suite_all; % run all
  case 'basic'
    s = ~HasTag('slow');
    suite = suite_all.selectIf(s);
  case 'imas'
    s = HasTag('imas');
    suite = suite_all.selectIf(s);
  case 'tcv'
    s = HasName(ContainsSubstring('tcv'));
    suite = suite_all.selectIf(s);
  case 'aug'
    s = HasName(ContainsSubstring('aug'));
    suite = suite_all.selectIf(s);

  otherwise
    error('not yet implemented')
end

%% run it
fprintf('Start test case: %s\n%s\n\n',test_case,datestr(now));
results = run(suite);
disp(table(results));
fprintf('\nTotal test duration: %5.2fs\n',sum(table(results).Duration))

%% display results
if all([results.Passed])
  fprintf('\nPassed all tests\n')
  passed = true;
else
  fprintf('\nSome tests Failed or Incomplete\n')
  if any([results.Incomplete])
    fprintf('\nIncomplete:\n')
    disp(table(results([results.Incomplete])))
  end
  if any([results.Failed])
    fprintf('\nFailed:\n')
    disp(table(results([results.Failed])));
  end
  passed = false;
end
