Permalink
Browse files

ENH: Remove 'support' dir and tempfolder (#9)

For now helper functions for classdef syntax check do not
need to be shared, so them can go back to the test file itself.

Tempfolder usage can be avoided to make test simpler.

Additionally make whitespace consistent in test file.

OBS: The function name assertSringContains was written using
camelCase to be consistent with MOxUnit assertion helpers.
  • Loading branch information...
1 parent 08c3ba1 commit 25df341892ab8d406eda8bd1d751d9742baa83b8 Anderson Bravalheri committed with (none) Dec 24, 2016
View
@@ -7,11 +7,9 @@ MATLAB?=matlab
OCTAVE?=octave
TESTDIR=$(CURDIR)/tests
-SUPPORTDIR=$(TESTDIR)/support
ROOTDIR=$(CURDIR)/MOcov
ADDPATH=orig_dir=pwd();cd('$(ROOTDIR)');addpath(pwd);cd(orig_dir)
-ADDSUPPORT=orig_dir=pwd();cd('$(SUPPORTDIR)');addpath(pwd);cd(orig_dir)
RMPATH=rmpath('$(ROOTDIR)');
SAVEPATH=savepath();exit(0)
@@ -51,7 +49,7 @@ ifdef JUNIT_XML_FILE
endif
-TEST=$(ADDPATH);$(ADDSUPPORT);if(isempty(which('moxunit_runtests'))),error('MOxUnit is required; see https://github.com/MOxUnit/MOxUnit');end;success=moxunit_runtests($(RUNTESTS_ARGS));exit(~success);
+TEST=$(ADDPATH);if(isempty(which('moxunit_runtests'))),error('MOxUnit is required; see https://github.com/MOxUnit/MOxUnit');end;success=moxunit_runtests($(RUNTESTS_ARGS));exit(~success);
MATLAB_BIN=$(shell which $(MATLAB))
OCTAVE_BIN=$(shell which $(OCTAVE))
@@ -1,4 +0,0 @@
-function assertStringContains(text, subtext)
- assert(~isempty(strfind(text, subtext)), ...
- 'String ''%s'' should contain ''%s'', but it doesn''t.', text, subtext);
-end
@@ -1,13 +0,0 @@
-function filepath = create_tempfile(filename, contents)
- % Creates a temporary file with the specified content.
-
- filepath = ensure_path_in_tempdir(filename);
-
- % Make sure eventual folder exists
- tempfolder = fileparts(filepath);
- create_tempfolder(tempfolder);
-
- fid = fopen(filepath, 'w');
- fprintf(fid, contents);
- fclose(fid);
-end
@@ -1,11 +0,0 @@
-function folderpath = create_tempfolder(foldername)
- % Create a folder inside the default temporary director
- % and returns it path.
- %
- % If the folder already exists, do nothing...
-
- folderpath = ensure_path_in_tempdir(foldername);
-
- [unused, unused, unused] = mkdir(folderpath);
- % Avoid existing folder warnings by receiving all the 3 outputs of mkdir
-end
@@ -1,14 +0,0 @@
-function newpath = ensure_path_in_tempdir(oldpath)
- % Check if path start with the tempdir.
- % If not, prepend the tempdir to path.
- %
- % Returns a path that starts with tempdir.
-
- len = length(tempdir);
- if length(oldpath) >= len && strcmp(oldpath(1:len), tempdir)
- % Just ignore if oldpath already include the tempdir path
- newpath = oldpath;
- else
- newpath = fullfile(tempdir, oldpath);
- end
-end
@@ -2,9 +2,24 @@
initTestSuite;
end
+function assertStringContains(text, subtext)
+ assert(~isempty(strfind(text, subtext)), ...
+ 'String ''%s'' should contain ''%s'', but it doesn''t.', text, subtext);
+end
+
+function filepath = create_tempfile(filename, contents)
+ % Creates a temporary file with the specified content.
+
+ filepath = fullfile(tempdir, filename);
+ fid = fopen(filepath, 'w');
+ fprintf(fid, contents);
+ fclose(fid);
+end
+
function filepath = create_classdef(classname)
if nargin < 1
- classname = 'AClass';
+ % Use a random name to ensure uniqueness
+ classname = char(64 + ceil(26*rand(1, 20)));
end
filepath = create_tempfile([classname, '.m'], [ ...
@@ -56,9 +71,9 @@
method_opening = [8, 13];
for n = method_opening
- assertStringContains(lines{n}, 'methods');
- assert(~executable_lines(n), ...
- '`%s` line is wrongly classified as executable', lines{n});
+ assertStringContains(lines{n}, 'methods');
+ assert(~executable_lines(n), ...
+ '`%s` line is wrongly classified as executable', lines{n});
end
end
@@ -74,9 +89,9 @@
method_lines = [10, 15];
for n = method_lines
- assertStringContains(lines{n}, 'fprintf');
- assert(executable_lines(n), ...
- '`%s` line is wrongly classified as non-executable', lines{n});
+ assertStringContains(lines{n}, 'fprintf');
+ assert(executable_lines(n), ...
+ '`%s` line is wrongly classified as non-executable', lines{n});
end
end
@@ -93,53 +108,50 @@
properties_body = [3, 6];
for n = properties_opening
- assertStringContains(lines{n}, 'properties');
- assert(~executable_lines(n), ...
- '`%s` line is wrongly classified as executable', lines{n});
+ assertStringContains(lines{n}, 'properties');
+ assert(~executable_lines(n), ...
+ '`%s` line is wrongly classified as executable', lines{n});
end
for n = properties_body;
- assertStringContains(lines{n}, 'Prop;');
- assert(~executable_lines(n), ...
- '`%s` line is wrongly classified as executable', lines{n});
+ assertStringContains(lines{n}, 'Prop;');
+ assert(~executable_lines(n), ...
+ '`%s` line is wrongly classified as executable', lines{n});
end
end
function test_generate_valid_file
% Test subject: `write_lines_with_prefix` method
- originalPath = path;
- pathCleanup = onCleanup(@() path(originalPath));
+ original_path = path;
+ path_cleanup = onCleanup(@() path(original_path));
% Given:
% `AClass.m` file with a classdef declaration
- tempfile = create_classdef('AClass');
- tempfileCleanup = onCleanup(@() delete(tempfile));
-
- % a folder where mocov will store the decorated files
- tempfolder = create_tempfolder(['mocovtest', num2str(randi(99999999999))]);
- decorated = fullfile(tempfolder, 'AClass.m');
- tempfolderCleanup = onCleanup(@() rmdir(tempfolder, 's'));
-
+ classname = ['AClass', char(64 + ceil(26*rand(1, 20)))];
+ tempfile = create_classdef(classname);
+ teardown = onCleanup(@() delete(tempfile));
% a valid decorator
decorator = @(line_number) ...
- sprintf('fprintf(0, ''%s:%d'');', tempfile, line_number);
-
+ sprintf('fprintf(0, ''%s:%d'');', tempfile, line_number);
% When: the decorated file is generated
mfile = MOcovMFile(tempfile);
- write_lines_with_prefix(mfile, decorated, decorator);
-
+ write_lines_with_prefix(mfile, tempfile, decorator);
+ % ^ Here we just overwrite the original file, because we don't use it.
+ % In the real word, the new file should be saved to a
+ % different folder.
% Then: the decorated file should have a valid syntax
% Since Octave do not have a linter, run the code to check the syntax.
- addpath(tempfolder);
+ addpath(tempdir);
try
- aObject = AClass();
- aObject.aMethod();
+ constructor = str2func(classname);
+ aObject = constructor();
+ aObject.aMethod();
catch
- assert(false, ['Problems when running the decorated file: `%s` ', ...
- 'please check for syntax errors.'], decorated);
+ assert(false, ['Problems when running the decorated file: `%s` ', ...
+ 'please check for syntax errors.'], decorated);
end
end

0 comments on commit 25df341

Please sign in to comment.