From 25df341892ab8d406eda8bd1d751d9742baa83b8 Mon Sep 17 00:00:00 2001 From: Anderson Bravalheri Date: Sat, 24 Dec 2016 13:01:48 +0000 Subject: [PATCH] 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. --- Makefile | 4 +- tests/support/assertStringContains.m | 4 -- tests/support/create_tempfile.m | 13 ---- tests/support/create_tempfolder.m | 11 ---- tests/support/ensure_path_in_tempdir.m | 14 ---- tests/test_MOcovMFile_recognizes_classdef_syntax.m | 76 +++++++++++++--------- 6 files changed, 45 insertions(+), 77 deletions(-) delete mode 100644 tests/support/assertStringContains.m delete mode 100644 tests/support/create_tempfile.m delete mode 100644 tests/support/create_tempfolder.m delete mode 100644 tests/support/ensure_path_in_tempdir.m diff --git a/Makefile b/Makefile index 9166cb6..1575b9f 100644 --- a/Makefile +++ b/Makefile @@ -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)) diff --git a/tests/support/assertStringContains.m b/tests/support/assertStringContains.m deleted file mode 100644 index 556c752..0000000 --- a/tests/support/assertStringContains.m +++ /dev/null @@ -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 diff --git a/tests/support/create_tempfile.m b/tests/support/create_tempfile.m deleted file mode 100644 index 152f093..0000000 --- a/tests/support/create_tempfile.m +++ /dev/null @@ -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 diff --git a/tests/support/create_tempfolder.m b/tests/support/create_tempfolder.m deleted file mode 100644 index d788600..0000000 --- a/tests/support/create_tempfolder.m +++ /dev/null @@ -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 diff --git a/tests/support/ensure_path_in_tempdir.m b/tests/support/ensure_path_in_tempdir.m deleted file mode 100644 index 867107d..0000000 --- a/tests/support/ensure_path_in_tempdir.m +++ /dev/null @@ -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 diff --git a/tests/test_MOcovMFile_recognizes_classdef_syntax.m b/tests/test_MOcovMFile_recognizes_classdef_syntax.m index 27e8bab..5157e68 100644 --- a/tests/test_MOcovMFile_recognizes_classdef_syntax.m +++ b/tests/test_MOcovMFile_recognizes_classdef_syntax.m @@ -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