@@ -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