Permalink
Browse files

ENH: Improve #9 by merging file tests

  • Loading branch information...
1 parent 6ab9539 commit b0af5e1d9fa81ef2eec4898760986c25e6547983 Anderson Bravalheri committed with (none) Dec 20, 2016
@@ -33,23 +33,27 @@
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', ...
' properties (SetAccess = private, Dependent)\n', ...
' 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
@@ -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

0 comments on commit b0af5e1

Please sign in to comment.