Merge remote-tracking branch 'origin/master' into YaSp
[scilab.git] / scilab / modules / development_tools / macros / example_run.sci
1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2012 - Michael Baudin
3 // Copyright (C) 2012 - Scilab Enterprises - Vincent COUVERT
4 //
5 // This file must be used under the terms of the CeCILL.
6 // This source file is licensed as described in the file COPYING, which
7 // you should have received as part of this distribution.  The terms
8 // are also available at
9 // http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
10
11 function boolStatus = example_run(modulesName, functionsName, helpLanguage, varargin)
12
13     rhs = argn(2);
14
15     if rhs < 1 then
16         modulesName = getmodules();
17     end
18     if rhs < 2 then
19         functionsName = [];
20     end
21     if rhs < 3 then
22         helpLanguage = "en_US";
23     end
24
25     if typeof(modulesName)<>"string" then
26         error(msprintf(gettext("%s: Wrong type for input argument #%d: A row array of strings expected.\n"), "example_run", 1));
27     end
28     if size(modulesName, 2)<>1 then
29         error(msprintf(gettext("%s: Wrong size for input argument #%d: A row array of strings expected.\n"), "example_run", 1));
30     end
31
32     if (typeof(functionsName)=="constant" & ~isempty(functionsName)) & typeof(functionsName)<>"string" then
33         error(msprintf(gettext("%s: Wrong type for input argument #%d: A row array of strings or empty matrix expected.\n"), "example_run", 2));
34     end
35     if ~isempty(functionsName) & size(functionsName, 2)<>1 then
36         error(msprintf(gettext("%s: Wrong size for input argument #%d: A row array of strings or empty matrix expected.\n"), "example_run", 2));
37     end
38
39     if typeof(helpLanguage)<>"string" then
40         error(msprintf(gettext("%s: Wrong type for input argument #%d: A string expected.\n"), "example_run", 3));
41     end
42     if size(helpLanguage, "*")<>1 then
43         error(msprintf(gettext("%s: Wrong size for input argument #%d: A string expected.\n"), "example_run", 3));
44     end
45
46     for kMod = 1:size(modulesName, "*")
47
48         moduleName = modulesName(kMod);
49
50         if (isdir(moduleName)) then
51             moduleDir = fullfile(moduleName,"help");
52         else
53             moduleDir = fullfile(SCI,"modules",moduleName,"help");
54             if ~isdir(moduleDir) then
55                 error(msprintf(gettext("%s: Wrong value for input argument #%d: A Scilab module name expected.\n"), "example_run", 1));
56                 return
57             end
58         end
59
60         moduleDir = fullfile(moduleDir,helpLanguage);
61         if ~isdir(moduleDir) then
62             error(msprintf(gettext("%s: Wrong value for input argument #%d: A valid language expected.\n"), "example_run", 3));
63             return
64         end
65
66         tempModuleName = basename(moduleName) + "_help";
67
68         workDir = fullfile(TMPDIR,tempModuleName,"tests","unit_tests");
69         if isdir(workDir) then
70             rmdir(workDir, "s");
71         end
72
73         if isempty(functionsName) then
74             xmlFiles = findHelpFiles(moduleDir, []);
75
76             for kFile = 1:size(xmlFiles, "*")
77                 xmlFile = xmlFiles(kFile);
78                 createTestsFromHelp(moduleDir, workDir, xmlFile)
79             end
80         else
81             for kFunction = 1:size(functionsName, "*")
82                 xmlFiles = findHelpFiles(moduleDir, functionsName(kFunction));
83                 if isempty(xmlFiles) then // The user wants to test a specific file
84                     error(msprintf(gettext("%s: Wrong value for input argument #%d: A ''%s'' module function name expected.\n"), "example_run", 2, moduleName));
85                     return
86                 end
87
88                 for kFile = 1:size(xmlFiles, "*")
89                     xmlFile = xmlFiles(kFile);
90                     createTestsFromHelp(moduleDir, workDir, xmlFile)
91                 end
92             end
93         end
94
95         testDir=fullfile(TMPDIR,tempModuleName)
96         if rhs > 3 then
97             boolStatus = test_run(testDir, [], varargin(:))
98         else
99             boolStatus = test_run(testDir, [])
100         end
101     end
102
103 endfunction
104
105 function createTestsFromHelp(baseDirectory, outputDirectory, helpFile)
106
107     mkdir(outputDirectory);
108
109     helpFileDoc = xmlRead(helpFile);
110
111     // Find all programlisting tag in the file
112     exampleTags = xmlXPath(helpFileDoc, "//scilabhelpfile:programlisting", ["scilabhelpfile" "http://docbook.org/ns/docbook"]);
113
114     for kExample = 1:size(exampleTags, "*")
115
116         programListing = [];
117
118         if exampleTags(kExample).attributes.role == "interactive-example" then
119             programListing = ["// <-- INTERACTIVE TEST -->" ; programListing];
120         end
121
122         ignoredRoles = ["no-scilab-exec"];
123         ignoreExample = or(exampleTags(kExample).attributes.role==ignoredRoles) | isempty(exampleTags(kExample).attributes.role);
124
125         programListing = ["// <-- NO CHECK REF -->" ; programListing; exampleTags(kExample).content];
126
127         if ~ignoreExample then
128             functionName = fileparts(helpFile, "fname");
129             relPath = dirname(getrelativefilename(baseDirectory, helpFile));
130
131             mkdir(fullfile(outputDirectory,relPath))
132
133             if size(exampleTags, "*")>1 then
134                 filebase=functionName + "_" + string(kExample) + ".tst"
135                 destfile=fullfile(outputDirectory,relPath,filebase);
136                 mputl(programListing,destfile);
137             else
138                 filebase=functionName + ".tst"
139                 destfile=fullfile(outputDirectory,relPath,filebase)
140                 mputl(programListing,destfile);
141             end
142         end
143     end
144
145 endfunction
146
147 function xmlFilenames = findHelpFiles(directory, functionName)
148
149     xmlFilenames = [];
150
151     allFiles = ls(directory);
152
153     for kFile = 1:size(allFiles, "*")
154         if isfile(allFiles(kFile)) then
155             if fileparts(allFiles(kFile), "extension")==".xml" then
156                 if isempty(functionName) then
157                     xmlFilenames($+1) = allFiles(kFile);
158                 elseif fileparts(allFiles(kFile), "fname")==functionName then
159                     xmlFilenames($+1) = allFiles(kFile);
160                 end
161             end
162         else // Directory
163             subDir=fullfile(directory,allFiles(kFile))
164             subxmlfiles=findHelpFiles(subDir, functionName)
165             xmlFilenames = [xmlFilenames;subxmlfiles]
166         end
167     end
168
169 endfunction