Skip to content

Commit

Permalink
issue #31: added tests from mxberry-core, fixed appeared errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dimamih authored and irublev committed Feb 23, 2018
1 parent 67a1b9e commit 9392b46
Show file tree
Hide file tree
Showing 138 changed files with 1,782 additions and 1,163 deletions.
18 changes: 18 additions & 0 deletions lib/+modgen/+dev/+test/+mlunit/TestSuite.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
% $Author: Peter Gagarinov, PhD <[email protected]> $
% $Copyright: 2015-2016 Peter Gagarinov, PhD
% 2012-2015 Moscow State University,
% Faculty of Applied Mathematics and Computer Science,
% System Analysis Department$
classdef TestSuite < mlunitext.test_case
methods
function self = TestSuite(varargin)
self = [email protected]_case(varargin{:});
end
%
function testTouch(~)
import com.mathworks.mlservices.MatlabDesktopServices;
modgen.selfmnt.MCodeQualityUtils.mlintScanAll();
MatlabDesktopServices.getDesktop.closeGroup('Web Browser');
end
end
end
8 changes: 8 additions & 0 deletions lib/+modgen/+dev/+test/+mlunit/run_tests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function results=run_tests(varargin)
%
runner = mlunitext.text_test_runner(1, 1);
loader = mlunitext.test_loader;
suite = loader.load_tests_from_test_case(...
'modgen.dev.test.mlunit.TestSuite');
%
results=runner.run(suite);
13 changes: 13 additions & 0 deletions lib/+modgen/+dev/@MLintScanner/MLintScanner.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
% $Author: Peter Gagarinov, PhD <[email protected]> $
% $Copyright: 2015-2016 Peter Gagarinov, PhD
% 2012-2015 Moscow State University,
% Faculty of Applied Mathematics and Computer Science,
% System Analysis Department$
classdef MLintScanner
methods (Static)
[fileList,reportList]=scan(dirNameList,...
pathPatternToExclude)
[htmlOut,fullFileNameList,reportList]=...
scanWithHtmlReport(dirList,patternToExclude)
end
end
35 changes: 35 additions & 0 deletions lib/+modgen/+dev/@MLintScanner/scan.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
% $Author: Peter Gagarinov, PhD <[email protected]> $
% $Copyright: 2015-2016 Peter Gagarinov, PhD
% 2012-2015 Moscow State University,
% Faculty of Applied Mathematics and Computer Science,
% System Analysis Department$
function [fileList,reportList]=scan(dirNameList,...
pathPatternToExclude)
pathList=modgen.io.PathUtils.genPathByRootList(dirNameList,...
pathPatternToExclude);
%
[reportListOfList,fileListOfList]=cellfun(@(x)scanDir(x),pathList,...
'UniformOutput',false);
isnEmptyVec=~cellfun('isempty',reportListOfList);
fileListOfList=fileListOfList(isnEmptyVec);
reportListOfList=reportListOfList(isnEmptyVec);
fileList=vertcat(fileListOfList{:});
reportList=vertcat(reportListOfList{:});
if isempty(fileList)
fileList={};
reportList={};
end
end
%
function [reportList,localFilenames]=scanDir(dirName)
reportName = getString(message(...
'MATLAB:codetools:reports:CodeAnalyzerReportName'));
fileList = internal.matlab.codetools.reports.matlabFiles(dirName,...
reportName);
localFilenames = strcat(dirName,filesep,fileList);
reportList=mlint(localFilenames,'-struct');
%
isnEmptyVec=~cellfun('isempty',reportList);
reportList=reportList(isnEmptyVec);
localFilenames=localFilenames(isnEmptyVec);
end
141 changes: 141 additions & 0 deletions lib/+modgen/+dev/@MLintScanner/scanWithHtmlReport.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
% $Author: Peter Gagarinov, PhD <[email protected]> $
% $Copyright: 2015-2016 Peter Gagarinov, PhD
% 2012-2015 Moscow State University,
% Faculty of Applied Mathematics and Computer Science,
% System Analysis Department$
function [htmlOut,fullFileNameList,reportList]=...
scanWithHtmlReport(dirList,patternToExclude)
[fullFileNameList,reportList]=...
modgen.dev.MLintScanner.scan(dirList,...
patternToExclude);
[~,fileList]=cellfun(@fileparts,fullFileNameList,'UniformOutput',false);
%
%
SHtmlProxyReportVec = [];
name='';
reportName = getString(message(...
'MATLAB:codetools:reports:CodeAnalyzerReportName'));
%
nFiles=numel(fullFileNameList);
for iFile = 1:nFiles
%
SHtmlProxyReportVec(iFile).fileName = fileList{iFile}; %#ok<*AGROW>
SHtmlProxyReportVec(iFile).fullFileName = fullFileNameList{iFile};
%
SHtmlProxyReportVec(iFile).lineNumber = [];
SHtmlProxyReportVec(iFile).lineMessage = {};
%
SMlintProblemVec = reportList{iFile};
for iProblem = 1:numel(SMlintProblemVec)
ln = SMlintProblemVec(iProblem).message;
ln = code2html(ln);
for iLine = 1:numel(SMlintProblemVec(iProblem).line)
SHtmlProxyReportVec(iFile).lineNumber(end+1) = ...
SMlintProblemVec(iProblem).line(iLine);
SHtmlProxyReportVec(iFile).lineMessage{end+1} = ln;
end
end
%
% Now sort the list by line number
if ~isempty(SHtmlProxyReportVec(iFile).lineNumber)
lineNum = [SHtmlProxyReportVec(iFile).lineNumber];
lineMsg = SHtmlProxyReportVec(iFile).lineMessage;
[~, ndx] = sort(lineNum);
lineNum = lineNum(ndx);
lineMsg = lineMsg(ndx);
SHtmlProxyReportVec(iFile).lineNumber = lineNum;
SHtmlProxyReportVec(iFile).lineMessage = lineMsg;
end

end
%
% Limit the number of messages displayed to keep from being overwhelmed by
% large pathological files.
displayLimit = 500;
%
% Now generate the HTML
help = [getString(message('MATLAB:codetools:reports:MLintReportDescription')) ' '];
doc = 'matlab_env_mlint';
dirListExpr=modgen.cell.cellstr2expression(dirList);
rerunAction = sprintf(...
'modgen.dev.MLintScanner.scanWithHtmlReport(%s,''%s'')',...
dirListExpr,patternToExclude);
runOnThisDirAction = 'mlintrpt';
s = internal.matlab.codetools.reports.makeReportHeader(reportName, ...
help, doc, rerunAction, runOnThisDirAction);

s{end+1} = '<p>';
%
s{end+1} = getString(message(...
'MATLAB:codetools:reports:ReportForSpecificFolder',name));
%
s{end+1} = '<p>';
%
s{end+1} = '<table cellspacing="0" cellpadding="2" border="0">';
for n = 1:length(SHtmlProxyReportVec)
encodedFileName = urlencode(SHtmlProxyReportVec(n).fullFileName);
decodedFileName = urldecode(encodedFileName);
%
s{end+1} = '<tr><td valign="top" class="td-linetop">';
openInEditor = sprintf('edit(''%s'')',decodedFileName);
regExpRep = sprintf('%s', SHtmlProxyReportVec(n).fileName);
%
s{end+1} = ['<a href="matlab:' openInEditor '">'];
s{end+1} = sprintf('<span class="mono">');
s{end+1} = regExpRep;
s{end+1} = sprintf('</span> </a> </br>');
%
if isempty(SHtmlProxyReportVec(n).lineNumber)
msgStr = ['<span class="soft">' getString(...
message('MATLAB:codetools:reports:NoMessages')) '</span>'];
elseif length(SHtmlProxyReportVec(n).lineNumber)==1
msgStr = ['<span class="warning">' getString(...
message('MATLAB:codetools:reports:OneMessage')) '</span>'];
elseif length(SHtmlProxyReportVec(n).lineNumber) < displayLimit
msgStr = ['<span class="warning">' getString(...
message('MATLAB:codetools:reports:SpecifiedNumberOfMessages',...
length(SHtmlProxyReportVec(n).lineNumber))) '</span>'];
else
% Truncate the list of messages if there are too many.
msgStr = ['<span class="warning">' ...
getString(...
message('MATLAB:codetools:reports:SpecifiedNumberOfMessages',...
length(SHtmlProxyReportVec(n).lineNumber))) ...
'\n<br/>' ...
getString(...
message('MATLAB:codetools:reports:ShowingOnlyFirstAmountOfMessages',...
displayLimit)) ...
'</span>'];
end
s{end+1} = sprintf('%s</td><td valign="top" class="td-linetopleft">',msgStr);
%
if ~isempty(SHtmlProxyReportVec(n).lineNumber)
for m = 1:min(length(SHtmlProxyReportVec(n).lineNumber),displayLimit)

openMessageLine = sprintf('opentoline(''%s'',%d)',...
decodedFileName, SHtmlProxyReportVec(n).lineNumber(m));
%
lineNum = sprintf('%d', SHtmlProxyReportVec(n).lineNumber(m));
lineMsg = sprintf('%s',SHtmlProxyReportVec(n).lineMessage{m});
%
s{end+1} = sprintf('<span class="mono">');
s{end+1} = ['<a href="matlab:' openMessageLine '">'];
s{end+1} = lineNum;
s{end+1} = sprintf('</a> ');
s{end+1} = lineMsg;
s{end+1} = sprintf('</span> <br/>');
end
end
s{end+1} = '</td></tr>';
end
%
s{end+1} = '</table>';
s{end+1} = '</body></html>';
%
if nargout==0
sOut = [s{:}];
web(['text://' sOut],'-noaddressbox');
else
htmlOut = s;
end
end
60 changes: 60 additions & 0 deletions lib/+modgen/+io/PathUtils.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
classdef PathUtils
methods (Static)
function pathStr=rmLastPathParts(pathStr,nPartsToRemove)
% RMLASTPATHPARTS removes a specified number of path parts
% (directory names) starting from the tail
%
% Input:
% regular:
% pathStr: char[1,] - directory name
% nPartsToRemove: double[1,1] - number of path parts to
% remove
%
% Output:
% pathStr: char[1,] - resulting path
% % $Author: Peter Gagarinov, PhD <[email protected]> $
% $Copyright: 2015-2016 Peter Gagarinov, PhD
% 2009-2015 Moscow State University
% Faculty of Computational Mathematics and Computer Science
% System Analysis Department$
%
if isempty(pathStr)
modgen.common.throwerror('wrongInput',...
'pathStr cannot be empty');
end
indVec=regexp(pathStr,filesep);
nPartsInTotal=numel(indVec);
if nPartsToRemove>0
pathStr=pathStr(1:(indVec(nPartsInTotal-nPartsToRemove+1)-1));
end
end
function pathList=genPathByRootList(pathToIncludeCVec,...
pathPatternToExclude)
% GENPATHBYROOTLIST recursively generates a list of path based
% on a list of root path and a regular expression exclusion
% pattern.
%
% Input:
% regular:
% pathToIncludeCVec: cell[1,] of char[1,] - list of
% directory names to scan recursively for
% generating a path list
% pathPatternToExclude: char[1,] - regex pattern for
% directory names to exclude (".git|.svn" for instance)
%
% Output:
% pathList: cell[1,] of char[1,] - list of generated
% directory names
% % $Author: Peter Gagarinov, PhD <[email protected]> $
% $Copyright: 2015-2016 Peter Gagarinov, PhD
% 2009-2015 Moscow State University
% Faculty of Computational Mathematics and Computer Science
% System Analysis Department$

pathListOfLists=cellfun(@(x)modgen.io.listdirsrecursive(x,...
['regex:^(?:(?!(',pathPatternToExclude,')).)*$'],Inf),...
pathToIncludeCVec,'UniformOutput',false);
pathList=vertcat(pathListOfLists{:}).';
end
end
end
Expand Down
55 changes: 55 additions & 0 deletions lib/+modgen/+io/listdirsrecursive.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
function fileNameList=listdirsrecursive(dirName,patternStr,maxDepth)
% LISTDIRSRECURSIVE returns a list of directories with names matching
% a specified pattern in all the subdirectories of a directory
% up to the specified depth
%
% Input:
% regular:
% dirName: char[1,] - directory name to scan
%
% patternStr: char[1,] - either Glob expression with "glob:" prefix
% ("glob:**.m" for instance) or Regex expression with "regex:"
% prefix.
% -------Glob syntax description--------
% *.txt Matches all files that has extension as txt.
% *.{html,htm} Matches all files that has extension as
% html or htm. { } are used to group patterns and ,
% comma is used to separate patterns.
% ?.txt Matches all files that has any single charcter as
% name and extension as txt.
% *.* Matches all files that has . in its name.
% C:\\Users\\* Matches any files in C: "Users" directory
% in Windows file system. Backslash is used to escape a
% special character.
% /home/** Matches /home/foo and /home/foo/bar on UNIX
% platforms. ** matches strings of characters corssing
% directory boundaries.
% [xyz].txt Matches a file name with single character "x"
% or "y" or "z" and extension as txt. Square brackets [ ]
% are used to sepcify a character set.
% [a-c].txt Matches a file name with single character "a" or
% "b" or "c" and extension as txt. Hypehen ÿ is used to
% specify a range and used in [ ]
% [!a].txt Matches a file name with single character that
% is not "a". ! is used for negation.
%
% maxDepth: double[1,1] - maximum depth, use Inf means for no depth
% liminations
% Output:
% dirNameList: cell[nFiles,1] of char[1,] - resuling list of directories
%
% $Author: Peter Gagarinov, PhD <[email protected]> $
% $Copyright: 2015-2016 Peter Gagarinov, PhD
% 2015 Moscow State University,
% Faculty of Computational Mathematics and Computer Science,
% System Analysis Department$
%
modgen.common.checkvar(maxDepth,'fix(x)==x&&x>=0');
if maxDepth==Inf
maxDepth=-1;
else
maxDepth=maxDepth+1;
end
%
fileNameList=cell(modgen.io.FileUtils.listDirsRecursive(dirName,...
patternStr,maxDepth));
52 changes: 52 additions & 0 deletions lib/+modgen/+io/listfilesrecursive.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
function fileNameList=listfilesrecursive(dirName,patternStr,maxDepth)
% LISTFILESRECURSIVE returns a list of files with names matching
% a specified pattern in all the subdirectories of a directory
% up to the specified depth
%
% Input:
% regular:
% dirName: char[1,] - directory name to scan
%
% patternStr: char[1,] - either Glob expression with "glob:" prefix
% ("glob:**.m" for instance) or Regex expression with "regex:"
% prefix.
% -------Glob syntax description--------
% *.txt Matches all files that has extension as txt.
% *.{html,htm} Matches all files that has extension as
% html or htm. { } are used to group patterns and ,
% comma is used to separate patterns.
% ?.txt Matches all files that has any single charcter as
% name and extension as txt.
% *.* Matches all files that has . in its name.
% C:\\Users\\* Matches any files in C: "Users" directory
% in Windows file system. Backslash is used to escape a
% special character.
% /home/** Matches /home/foo and /home/foo/bar on UNIX
% platforms. ** matches strings of characters corssing
% directory boundaries.
% [xyz].txt Matches a file name with single character "x"
% or "y" or "z" and extension as txt. Square brackets [ ]
% are used to sepcify a character set.
% [a-c].txt Matches a file name with single character "a" or
% "b" or "c" and extension as txt. Hypehen ÿ is used to
% specify a range and used in [ ]
% [!a].txt Matches a file name with single character that
% is not "a". ! is used for negation.
%
% maxDepth: double[1,1] - maximum depth, use Inf means for no depth
% liminations
% Output:
% fileNameList: cell[nFiles,1] of char[1,] - resuling list of files
%% $Author: Peter Gagarinov, PhD <[email protected]> $
% $Copyright: 2015-2016 Peter Gagarinov, PhD
% 2015 Moscow State University,
% Faculty of Computational Mathematics and Computer Science,
% System Analysis Department$
%
modgen.common.checkvar(maxDepth,'fix(x)==x&&x>0');
if maxDepth==Inf
maxDepth=-1;
end
%
fileNameList=cell(modgen.io.FileUtils.listFilesRecursive(dirName,...
patternStr,maxDepth));
Expand Down
Loading

0 comments on commit 9392b46

Please sign in to comment.