Create .m file programmatically with fitting metadata
3 次查看(过去 30 天)
显示 更早的评论
Hi@all,
I'm trying to build a unit test generator, but I dstruggle a little bit :/
planned steps:
- Find the file to create the test for (input is filename)
- Check if test folder exist or create them (based on package name)
- copy and rename a prepared template file
- rename the file name in the content
- add all public methods as test methods with special naming and prefilled content
- finsh
My problem is at the moment, if I change the content by using sth like this:
function renameFileNameContent(testPath, testName)
[~, name, ~] = fileparts(fullfile(testPath, testName));
fid = fopen(fullfile(testPath, testName), 'r');
fileContent = rot90(StringConverter.ensureCharString(fread(fid, '*char')));
fclose(fid);
fileContent = regexprep(char(fileContent), testing.UnitTestSupport.TemplateName, name);
fid = fopen(fullfile(testPath, testName), 'w');
fprintf(fid, '%s', fileContent);
fclose(fid);
end
The problem here is it's seems that the .m has meta data which are emtpy after this file recreation ...
My second try was sth like this:
function renameFileNameContent(testPath, testName)
fid = fopen(fullfile(testPath, testName), 'r');
fileContent = rot90(StringConverter.ensureCharString(fread(fid, '*char')));
fclose(fid);
fileContent = regexprep(char(fileContent), testing.UnitTestSupport.TemplateName, name);
editorService = com.mathworks.mlservices.MLEditorServices;
editorApplication = editorService.getEditorApplication();
editorApplication.newEditor(fileContent);
openFiles = matlab.desktop.editor.getAll();
%save(fullfile(testPath, testName), openFiles(length(openFiles)).Filename);
end
Now I have the file open as new untiteled file and can save it, but still the same issue :/
Did someone solved this problem, or can give me a hint to handle this?
Best regards
0 个评论
采纳的回答
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Testing Frameworks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!