MatrixBerryCore
TestCase.m
Go to the documentation of this file.
1 classdef TestCase<matlab.unittest.TestCase
2  properties (Access=private)
3  PROF_MODE='off';
4  PROF_DIR='';
5  end
6  methods
7  function runAndCheckError(self,commandStr,expIdentifierList,varargin)
8  import mxberry.core.checkmultvar;
9  import mxberry.core.throwerror;
10  import mxberry.core.check.lib.*;
11  %
12  isNoIdentPatternSpec=false;
13  if ischar(expIdentifierList)
14  expIdentifierList={expIdentifierList};
15  elseif isempty(expIdentifierList)
16  isNoIdentPatternSpec=true;
17  end
18  %
19  nExpIdentifiers=length(expIdentifierList);
20  %
21  [reg,isRegSpec,causeCheckDepth,reportStr,~,isRepStrSpec]=...
22  mxberry.core.parseparext(varargin,...
23  {'causeCheckDepth','reportStr';...
24  0,'successful execution when failure is expected';...
25  'isscalar(x)&&isnumeric(x)',@isstring},...
26  [0,1],...
27  'regDefList',{[]},...
28  'regCheckList',{@(x)(iscellstr(x)||isstring(x))});
29  %
30  if isRegSpec
31  expMsgCodeList=reg{1};
32  if ischar(expMsgCodeList)
33  expMsgCodeList={expMsgCodeList};
34  end
35  isNoMsgPatternSpecVec=false(1,numel(expMsgCodeList));
36  else
37  isNoMsgPatternSpecVec=true(1,nExpIdentifiers);
38  expMsgCodeList=repmat({''},1,nExpIdentifiers);
39  end
40  nMsgCodes=numel(expMsgCodeList);
41  if isNoIdentPatternSpec
42  expIdentifierList=repmat({''},1,nMsgCodes);
43  isNoIdentPatternSpecVec=true(1,nMsgCodes);
44  else
45  isNoIdentPatternSpecVec=false(1,nMsgCodes);
46  end
47  %
48  checkmultvar(@(x,y) size(y,2) == 0 || ...
49  size(y,2) == size(x,2),2,expIdentifierList,expMsgCodeList);
50  %
51  try
52  if ischar(commandStr)
53  evalin('caller',commandStr);
54  else
55  feval(commandStr);
56  end
57  catch meObj
58  errMsg='';
59  [isIdentMatchVec,identPatternStr] =checkCode(meObj,...
60  'identifier',expIdentifierList);
61  [isMsgMatchVec,msgPatternStr] =checkCode(meObj,...
62  'message',expMsgCodeList);
63  %
64  isOk=any((isIdentMatchVec|isNoIdentPatternSpecVec)&...
65  (isMsgMatchVec|isNoMsgPatternSpecVec));
66  patternStr=['identifier(',identPatternStr,')',...
67  'message(',msgPatternStr,')'];
68  %
69  if isRepStrSpec
70  addSuffix=', %s';
71  addArgList={reportStr};
72  else
73  addSuffix='';
74  addArgList={};
75  end
76 
77  self.verifyEqual(true,isOk,...
78  sprintf(...
79  ['\n no match found for pattern %s',...
80  ' exception details: \n %s',addSuffix],...
81  patternStr,errMsg,addArgList{:}))
82  return;
83  end
84  self.verifyEqual(true,false,reportStr);
85  function [isMatchVec,patternsStr]=checkCode(inpMeObj,fieldName,codeList)
86  errMsg=strrep(...
87  mxberry.core.MExceptionUtils.me2HyperString(...
88  inpMeObj),...
89  '%','%%');
90  isMatchVec=cellfun(...
91  @(x)getIsCodeMatch(...
92  inpMeObj,causeCheckDepth,fieldName,x),codeList);
93  %
94  patternsStr=mxberry.core.string.catwithsep(codeList,', ');
95  %
96 
97  end
98  function isPositive=getIsCodeMatch(inpMeObj,checkDepth,fieldName,codeStr)
99  fieldValue = inpMeObj.(fieldName);
100  if isempty(fieldValue)
101  isPositive= isempty(codeStr);
102  else
103  isPositive=~isempty(strfind(fieldValue,codeStr));
104  end
105  causeList=inpMeObj.cause;
106  nCauses=length(causeList);
107  if checkDepth>0&&nCauses>0
108  for iCause=1:nCauses
109  isPositive=isPositive||getIsCodeMatch(...
110  causeList{iCause},checkDepth-1,...
111  fieldName,codeStr);
112  end
113  end
114  end
115  end
116  function resTime=runAndMeasureTime(self,varargin)
117  profileMgr=mxberry.dev.prof.ProfileManager(...
118  self.PROF_MODE,self.PROF_DIR);
119  %
120  resTime=profileMgr.runAndProcess(varargin{:});
121  end
122  end
123 end
function throwerror(in msgTag, in varargin)
THROWERROR works similarly to built-in ERROR function in case when there is no output arguments but s...
function checkmultvar(in typeSpec, in nPlaceHolders, in varargin)
CHECKMULTVAR checks a generic condition provided by typeSpec string in the following format: &#39;isnumer...
function repmat(in inpArray, in varargin)
function isstring(in inpArray)
function runAndProcess(in self, in fRun, in varargin)
RUNANDPROCESS executes the specified command and generates a profiling report using the specified nam...