-
Notifications
You must be signed in to change notification settings - Fork 121
/
GeneratePythonScriptTest.h
120 lines (100 loc) · 4.47 KB
/
GeneratePythonScriptTest.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#ifndef MANTID_ALGORITHMS_GENERATEPYTHONSCRIPTTEST_H_
#define MANTID_ALGORITHMS_GENERATEPYTHONSCRIPTTEST_H_
#include <cxxtest/TestSuite.h>
#include "MantidKernel/Timer.h"
#include "MantidKernel/System.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include "MantidAlgorithms/GeneratePythonScript.h"
#include "MantidAlgorithms/CreateWorkspace.h"
#include "MantidAlgorithms/CropWorkspace.h"
#include "MantidAlgorithms/Power.h"
#include "MantidAPI/AlgorithmManager.h"
#include <Poco/File.h>
using namespace Mantid;
using namespace Mantid::Algorithms;
using namespace Mantid::API;
class GeneratePythonScriptTest : public CxxTest::TestSuite
{
public:
void test_Init()
{
GeneratePythonScript alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
}
void test_exec()
{
// Create test workspace
std::string workspaceName = "testGeneratePython";
create_test_workspace( workspaceName );
std::string result[] = {
"######################################################################",
"#Python Script Generated by GeneratePythonScript Algorithm",
"######################################################################",
"CreateWorkspace(OutputWorkspace='testGeneratePython',DataX='1,2,3,5,6',DataY='7,9,16,4,3',DataE='2,3,4,2,1',WorkspaceTitle='Test Workspace')",
"CropWorkspace(InputWorkspace='testGeneratePython',OutputWorkspace='testGeneratePython',XMin='2',XMax='5')",
"Power(InputWorkspace='testGeneratePython',OutputWorkspace='testGeneratePython',Exponent='1.5')",
""
};
// Set up and execute the algorithm.
GeneratePythonScript alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() );
TS_ASSERT( alg.isInitialized() );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("InputWorkspace", workspaceName) );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("Filename", "GeneratePythonScriptTest.py") );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("ScriptText", "") );
TS_ASSERT_THROWS_NOTHING( alg.execute(); );
TS_ASSERT( alg.isExecuted() );
// Compare the contents of the file to the expected result line-by-line.
std::string filename = alg.getProperty("Filename");
std::ifstream file(filename.c_str(), std::ifstream::in);
std::string scriptLine;
int lineCount(0);
while(std::getline(file, scriptLine))
{
TS_ASSERT_EQUALS(scriptLine,result[lineCount]);
lineCount++;
}
// Verify that if we set the content of ScriptText that it is set correctly.
alg.setPropertyValue("ScriptText", result[4]);
TS_ASSERT_EQUALS(alg.getPropertyValue("ScriptText"), "CropWorkspace(InputWorkspace='testGeneratePython',OutputWorkspace='testGeneratePython',XMin='2',XMax='5')");
file.close();
if (Poco::File(filename).exists()) Poco::File(filename).remove();
}
void create_test_workspace( const std::string& wsName )
{
Mantid::Algorithms::CreateWorkspace creator;
Mantid::Algorithms::CropWorkspace cropper;
Mantid::Algorithms::Power powerer;
// Set up and execute creation of the workspace
creator.initialize();
creator.setPropertyValue("OutputWorkspace",wsName);
creator.setPropertyValue("DataX","1,2,3,5,6");
creator.setPropertyValue("DataY","7,9,16,4,3");
creator.setPropertyValue("DataE","2,3,4,2,1");
creator.setPropertyValue("WorkspaceTitle","Test Workspace");
creator.setRethrows(true);
TS_ASSERT_THROWS_NOTHING(creator.execute());
TS_ASSERT_EQUALS(creator.isExecuted(), true);
// Set up and execute the cropping of the workspace
cropper.initialize();
cropper.setPropertyValue("InputWorkspace",wsName);
cropper.setPropertyValue("OutputWorkspace",wsName);
cropper.setPropertyValue("XMin","2");
cropper.setPropertyValue("XMax","5");
cropper.setRethrows(true);
TS_ASSERT_THROWS_NOTHING(cropper.execute());
TS_ASSERT_EQUALS(cropper.isExecuted(), true);
// Set up and execute Power algorithm on the workspace
powerer.initialize();
powerer.setPropertyValue("InputWorkspace",wsName);
powerer.setPropertyValue("OutputWorkspace",wsName);
powerer.setPropertyValue("Exponent","1.5");
powerer.setRethrows(true);
TS_ASSERT_THROWS_NOTHING(powerer.execute());
TS_ASSERT_EQUALS(powerer.isExecuted(), true);
}
};
#endif /* MANTID_ALGORITHMS_GENERATEPYTHONSCRIPTTEST_H_ */