From b0af5e1d9fa81ef2eec4898760986c25e6547983 Mon Sep 17 00:00:00 2001 From: Anderson Bravalheri Date: Tue, 20 Dec 2016 14:03:12 -0200 Subject: [PATCH] ENH: Improve #9 by merging file tests --- tests/test_MOcovMFile_recognizes_classdef_syntax.m | 65 ++++++++++++++++++--- ...nes_with_prefix_generate_valid_classdef_files.m | 68 ---------------------- 2 files changed, 58 insertions(+), 75 deletions(-) delete mode 100644 tests/test_write_lines_with_prefix_generate_valid_classdef_files.m diff --git a/tests/test_MOcovMFile_recognizes_classdef_syntax.m b/tests/test_MOcovMFile_recognizes_classdef_syntax.m index bddd7ef..0cb62ec 100644 --- a/tests/test_MOcovMFile_recognizes_classdef_syntax.m +++ b/tests/test_MOcovMFile_recognizes_classdef_syntax.m @@ -33,9 +33,13 @@ fclose(fid); end -function filepath = create_classdef - filepath = create_tempfile('AClass.m', [ ... - 'classdef AClass < handle\n', ... +function filepath = create_classdef(classname) + if nargin < 1 + classname = 'AClass'; + end + + filepath = create_tempfile([classname, '.m'], [ ... + 'classdef ', classname, ' < handle\n', ... ' properties\n', ... ' aProp;\n', ... ' end\n', ... @@ -43,13 +47,13 @@ ' anotherProp;\n', ... ' end\n', ... ' methods\n', ... - ' function self = AClass\n', ... - ' fprintf(''hello world!'');\n', ... + ' function self = ', classname, ' \n', ... + ' fprintf(0, ''hello world!'');\n', ... ' end\n', ... ' end\n', ... - ' methods (Access = private)\n', ... + ' methods (Access = public)\n', ... ' function x = aMethod(self)\n', ... - ' fprintf(''hello world!'');\n', ... + ' fprintf(0, ''hello world!'');\n', ... ' end\n', ... ' end\n', ... 'end\n' ... @@ -62,6 +66,8 @@ function assertStringContains(text, subtext) end function test_classdef_line_not_executable + % Test subject: `MOcovMFile` constructor + tempfile = create_classdef; teardown = onCleanup(@() delete(tempfile)); @@ -75,6 +81,8 @@ function assertStringContains(text, subtext) end function test_methods_opening_section_not_executable + % Test subject: `MOcovMFile` constructor + tempfile = create_classdef; teardown = onCleanup(@() delete(tempfile)); @@ -91,6 +99,8 @@ function assertStringContains(text, subtext) end function test_method_body_executable + % Test subject: `MOcovMFile` constructor + tempfile = create_classdef; teardown = onCleanup(@() delete(tempfile)); @@ -107,6 +117,8 @@ function assertStringContains(text, subtext) end function test_properties_line_not_executable + % Test subject: `MOcovMFile` constructor + tempfile = create_classdef; teardown = onCleanup(@() delete(tempfile)); @@ -121,3 +133,42 @@ function assertStringContains(text, subtext) '`%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)); + + % 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')); + + % a valid decorator + decorator = @(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); + + + % 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); + try + aObject = AClass(); + aObject.aMethod(); + catch + assert(false, ['Problems when running the decorated file: `%s` ', ... + 'please check for syntax errors.'], decorated); + end +end diff --git a/tests/test_write_lines_with_prefix_generate_valid_classdef_files.m b/tests/test_write_lines_with_prefix_generate_valid_classdef_files.m deleted file mode 100644 index e0f5032..0000000 --- a/tests/test_write_lines_with_prefix_generate_valid_classdef_files.m +++ /dev/null @@ -1,68 +0,0 @@ -function test_suite = test_write_lines_with_prefix_generate_valid_classdef_files - initTestSuite; -end - -function fullname = tempfile(filename, contents) - tempfolder = fullfile(tempdir, 'mocov_fixtures'); - [~, ~, ~] = mkdir(tempfolder); - fullname = fullfile(tempfolder, filename); - fid = fopen(fullname, 'w'); - fprintf(fid, contents); - fclose(fid); -end - -function filename = create_classdef - filename = tempfile('AClass.m', [ ... - 'classdef AClass < handle\n', ... - ' properties\n', ... - ' aProp = 1;\n', ... - ' end\n', ... - ' properties (SetAccess = private, Dependent)\n', ... - ' anotherProp;\n', ... - ' end\n', ... - ' methods\n', ... - ' function self = AClass\n', ... - ' self.anotherProp = 2;\n', ... - ' end\n', ... - ' end\n', ... - ' methods (Access = public)\n', ... - ' function aMethod(self, x)\n', ... - ' self.aProp = x;\n', ... - ' end\n', ... - ' end\n', ... - 'end\n' ... - ]); -end - -function test_generate_valid_file - originalPath = path; % setup - cleaner = onCleanup(@() path(originalPath)); % teardown - - % Given: - % `AClass.m` file with a classdef declaration - filename = create_classdef; - % a folder where mocov will store the decorated files - foldername = fullfile(tempdir, 'mocov_decorated'); - [~,~,~] = mkdir(foldername); - decorated = fullfile(foldername, 'AClass.m'); - % a valid decorator - decorator = @(line_number) ... - sprintf('fprintf(0, ''%s:%d'');', filename, line_number); - - - % When: the decorated file is generated - mfile = MOcovMFile(filename); - write_lines_with_prefix(mfile, decorated, decorator); - - - % Then: the decorated file should have a valid syntax - % Since Octave do not have a linter, run the code to check the syntax. - addpath(foldername); - try - aObject = AClass(); - aObject.aMethod(4); - catch - assert(false, ['Problems when running the decorated file: `%s` ', ... - 'please check for syntax errors.'], decorated); - end -end