% UTP_PLIST_FIND a set of UTPs for the plist/find method
%
% M Hewitson 06-08-08
%
% $Id: utp_plist_find.m,v 1.3 2009/07/22 14:02:26 ingo Exp $
%

% <MethodDescription>
%
% The find method of the plist class returns the value corresponding to
% the first parameters in the list with the search-key. 
%
% </MethodDescription>

function results = utp_plist_find(varargin)
  
  % Check the inputs
  if nargin == 0
    
    % Some keywords
    class   = 'plist';
    mthd    = 'find';
    
    results = [];
    disp('******************************************************');
    disp(['****  Running UTPs for ' class '/' mthd]);
    disp('******************************************************');
    
    % Test PLIST objects
    [pl1, pl2, pl3, pl4, plv, plm] = get_test_objects_plist;
    
    % Exception list for the UTPs:
    [ple1,ple2,ple3,ple4,ple5,ple6] = get_test_ples();

    % Run the tests
    results = [results utp_01];    % getInfo call
    results = [results utp_02];    % Test the modify call works
    results = [results utp_03];    % Test with different inputs for the key/value pair
    results = [results utp_04];    % Test negative case.
    
    disp('Done.');
    disp('******************************************************');
    
  elseif nargin == 1 % Check for UTP functions
    if strcmp(varargin{1}, 'isutp')
      results = 1;
    else
      results = 0;
    end
  else
    error('### Incorrect inputs')
  end
  
  %% UTP_01
  
  % <TestDescription>
  %
  % Tests that the getInfo call works for this method.
  %
  % </TestDescription>
  function result = utp_01
    
    
    % <SyntaxDescription>
    %
    % Test that the getInfo call works for no sets, all sets, and each set
    % individually.
    %
    % </SyntaxDescription>
    
    try
      % <SyntaxCode>
      % Call for no sets
      io(1) = eval([class '.getInfo(''' mthd ''', ''None'')']);
      % Call for all sets
      io(2) = eval([class '.getInfo(''' mthd ''')']);
      % Call for each set
      for kk=1:numel(io(2).sets)
        io(kk+2) = eval([class '.getInfo(''' mthd ''', ''' io(2).sets{kk} ''')']);
      end
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end
    
    % <AlgoDescription>
    %
    % 1) Check that getInfo call returned an minfo object in all cases.
    % 2) Check that all plists have the correct parameters.
    %
    % </AlgoDescription>
    
    atest = true;
    if stest
      % <AlgoCode>
      % check we have minfo objects
      if isa(io, 'minfo')
        %%% SET 'None'
        if ~isempty(io(1).sets), atest = false; end
        if ~isempty(io(1).plists), atest = false; end
        %%% Check all Sets
        if ~any(strcmpi(io(2).sets, 'Default')), atest = false; end
        if numel(io(2).plists) ~= numel(io(2).sets), atest = false; end
        %%%%%%%%%%   SET 'Default'
        if io(3).plists.nparams ~= 1, atest = false; end
        % Check key
        if ~io(3).plists.isparam('key'), atest = false; end
        % Check default value
        if ~isEmptyChar(io(3).plists.find('key')), atest = false; end
        % Check options
        if ~isequal(io(3).plists.getOptionsForParam('key'), {''}), atest = false; end
      end
      % </AlgoCode>
    else
      atest = false;
    end
    
    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_01
  
  %% UTP_02
  
  % <TestDescription>
  %
  % Tests that the find method applies the modify command.
  %
  % </TestDescription>
  function result = utp_02
    
    % <SyntaxDescription>
    %
    % Test that it is possible to use modify command and show that this
    % method ignoring case.
    %
    % </SyntaxDescription>
    
    try
      % <SyntaxCode>
      % modify ain
      out1 = pl4.find('spec');
      out2 = pl4.find('sPeC');
      out3 = pl4.find('SPEC');
      pl4.find('spec')
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end
    
    % <AlgoDescription>
    %
    % 1) Check that out1..3 have all the same object.
    %
    % </AlgoDescription>
    
    atest = true;
    if stest
      % <AlgoCode>
      % Find the right object
      for ii = 1:pl4.nparams
        if strcmpi(pl4.params(ii).key, 'SPEC')
          obj = pl4.params(ii).getVal;
          break
        end
      end
      if ~eq(obj, out1), atest = false; end
      if ~eq(obj, out2), atest = false; end
      if ~eq(obj, out3), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end
    
    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_02
  
  %% UTP_03
  
  % <TestDescription>
  %
  % Test that the find method accepts a plist.
  %
  % </TestDescription>
  function result = utp_03
    
    % <SyntaxDescription>
    %
    % Test that the find method take the 'key' from a 'search' plist. 
    %
    % </SyntaxDescription>
    
    try
      % <SyntaxCode>
      out = pl4.find(plist('key', 'spec'));
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end
    
    % <AlgoDescription>
    %
    % 1) Check that the output have the correct value.
    %
    % </AlgoDescription>
    
    atest = true;
    if stest
      % <AlgoCode>
      for ii = 1:pl4.nparams
        if strcmpi(pl4.params(ii).key, 'SPEC')
          obj = pl4.params(ii).getVal;
          break
        end
      end
      if ~eq(obj, out), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end
    
    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_03
  
  %% UTP_04
  
  % <TestDescription>
  %
  % Test the find method returns an empty array if the 'key' doesn't
  % exist in the PLIST.
  %
  % </TestDescription>
  function result = utp_04
    
    % <SyntaxDescription>
    %
    % Test the find method returns an empty array if the 'key' doesn't
    % exist in the PLIST.
    %
    % </SyntaxDescription>
    
    try
      % <SyntaxCode>
      out = pl4.find('non_existing_key');
      stest = true;
      % </SyntaxCode>
    catch err
      disp(err.message)
      stest = false;
    end
    
    % <AlgoDescription>
    %
    % 1) Check that the output is an empty array.
    %
    % </AlgoDescription>
    
    atest = true;
    if stest
      % <AlgoCode>
      if ~isempty(out), atest = false; end;
      % </AlgoCode>
    else
      atest = false;
    end
    
    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_04
  
end
