-
Notifications
You must be signed in to change notification settings - Fork 122
/
GeneratePythonScriptTest.h
120 lines (104 loc) · 5.38 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 "MantidDataHandling/Load.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()
{
// Load test file into workspace
Mantid::DataHandling::Load loader;
loader.initialize();
// Here we make the assumption that IRS26173_ipg.nxs will not change, and so is
// a valid choice to test the output of the Algorithm against.
loader.setPropertyValue("Filename", "IRS26173_ipg.nxs");
loader.setPropertyValue("OutputWorkspace","LoadedWorkspace");
loader.setPropertyValue("SpectrumMax","1");
loader.setRethrows(true);
std::string result[] = {
"######################################################################",
"#Python Script Generated by GeneratePythonScript Algorithm",
"######################################################################",
"LoadRaw(Filename=r'G:/Spencer/Science/Raw/irs26173.raw',OutputWorkspace='IPG',SpectrumMin='3',SpectrumMax='53')", // Not tested.
"ConvertUnits(InputWorkspace='IPG',OutputWorkspace='Spec',Target='Wavelength')",
"LoadRaw(Filename=r'G:/Spencer/Science/Raw/irs26173.raw',OutputWorkspace='Mon_in',SpectrumMax='1')", // Not tested.
"Unwrap(InputWorkspace='Mon_in',OutputWorkspace='Mon',LRef='37.86')",
"RemoveBins(InputWorkspace='Mon',OutputWorkspace='Mon',XMin='6.14600063416',XMax='6.14800063416',Interpolation='Linear')",
"FFTSmooth(InputWorkspace='Mon',OutputWorkspace='Mon')",
"RebinToWorkspace(WorkspaceToRebin='Spec',WorkspaceToMatch='Mon',OutputWorkspace='Spec')",
"LoadRaw(Filename=r'G:/Spencer/Science/Raw/irs26173.raw',OutputWorkspace='Mon_in',SpectrumMax='1')", // Not tested.
"Unwrap(InputWorkspace='Mon_in',OutputWorkspace='Mon',LRef='37.86')",
"RemoveBins(InputWorkspace='Mon',OutputWorkspace='Mon',XMin='6.14600063416',XMax='6.14800063416',Interpolation='Linear')",
"FFTSmooth(InputWorkspace='Mon',OutputWorkspace='Mon')",
"Divide(LHSWorkspace='Spec',RHSWorkspace='Mon',OutputWorkspace='Spec')",
"ConvertUnits(InputWorkspace='Spec',OutputWorkspace='Spec',Target='DeltaE',EMode='Indirect',EFixed='1.84')",
"GroupDetectors(InputWorkspace='Spec',OutputWorkspace='IPG_3',MapFile=r'G:/Spencer/Science/Mantid/IRIS/PG1op3.map')", // Not tested.
"Load(Filename=r'C:/Mantid/Test/AutoTestData/IRS26173_ipg.nxs',OutputWorkspace='IRS26173_ipg',SpectrumMax='1')", // Not tested.
""
};
TS_ASSERT_THROWS_NOTHING(loader.execute());
TS_ASSERT_EQUALS(loader.isExecuted(), true);
MatrixWorkspace_sptr ws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>("LoadedWorkspace");
TS_ASSERT(NULL != ws);
// 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", ws->getName()) );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("Filename", "GeneratePythonScriptTest.py") );
TS_ASSERT_THROWS_NOTHING( alg.execute(); );
TS_ASSERT( alg.isExecuted() );
// Compare the contents of the file to the expected result line-by-line.
// If the line contains Filename or MapFile then just check that the string
// is prefixed with r to convert it to a Python raw string and not the actual content
// as the file paths are different
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))
{
std::string::size_type filenamePos = scriptLine.find("Filename=");
std::string::size_type mapfilePos = scriptLine.find("MapFile=");
if(filenamePos != std::string::npos)
{
TS_ASSERT_EQUALS(scriptLine[filenamePos+9], 'r');
if(scriptLine.find("Load(") != std::string::npos)
{
// Check Load call has SpectrumMax
TS_ASSERT(scriptLine.find("SpectrumMax='1'") != std::string::npos);
}
}
else if(mapfilePos != std::string::npos)
{
TS_ASSERT_EQUALS(scriptLine[mapfilePos+8], 'r');
}
else
{
TS_ASSERT_EQUALS(scriptLine,result[lineCount]);
}
lineCount++;
}
file.close();
//if (Poco::File(filename).exists()) Poco::File(filename).remove();
}
};
#endif /* MANTID_ALGORITHMS_GENERATEPYTHONSCRIPTTEST_H_ */