Newer
Older
#ifndef SIMPLEPYTHONAPITEST_H_
#define SIMPLEPYTHONAPITEST_H_
//--------------------------------
// Includes
//--------------------------------
#include <fstream>
#include <cxxtest/TestSuite.h>
Gigg, Martyn Anthony
committed
#include "Poco/File.h"
#include "MantidPythonAPI/SimplePythonAPI.h"
#include "MantidAPI/FrameworkManager.h"
class SimplePythonAPITest : public CxxTest::TestSuite
{
public:
SimplePythonAPITest()
{
Mantid::API::FrameworkManager::Instance();
}
void testCreateModule()
{
using namespace Mantid::PythonAPI;
//first call the function to create the module file
Gigg, Martyn Anthony
committed
SimplePythonAPI::createModule(false);
Gigg, Martyn Anthony
committed
Poco::File apimodule(SimplePythonAPI::getModuleName());
Gigg, Martyn Anthony
committed
TS_ASSERT(apimodule.exists());
//does it contain what we expect
Gigg, Martyn Anthony
committed
std::ifstream is(apimodule.path().c_str());
Gigg, Martyn Anthony
committed
std::string line;
getline(is, line);
TS_ASSERT_EQUALS(line, "import sys");
Gigg, Martyn Anthony
committed
//skip lines containing append commands
//This will leave line containing the next either non-blank or sys cmd line
while( getline(is, line) && (line.empty() || line.find("sys") != std::string::npos) )
{
}
Gigg, Martyn Anthony
committed
//Next should import main Python API
std::string modline("");
#ifdef _WIN32
Gigg, Martyn Anthony
committed
modline = "from MantidPythonAPI import *";
Gigg, Martyn Anthony
committed
modline = "from libMantidPythonAPI import *";
Gigg, Martyn Anthony
committed
TS_ASSERT_EQUALS(line, modline);
Gigg, Martyn Anthony
committed
//next line should be os import
getline( is, line );
TS_ASSERT_EQUALS(line, std::string("import os"));
//next line should be string import
getline( is, line );
TS_ASSERT_EQUALS(line, std::string("import string"));
getline( is, line );
getline( is, line );
TS_ASSERT_EQUALS(line, std::string("PYTHONAPIINMANTIDPLOT = False"));
Gigg, Martyn Anthony
committed
//next non-blank line should be API objects
//eat blank lines
while( getline(is, line) && ( line.empty() || line[0] == '#' ) )
{
}
TS_ASSERT_EQUALS(line, "mantid = FrameworkManager()");
getline(is, line);
getline(is, line);
Gigg, Martyn Anthony
committed
TS_ASSERT_EQUALS(line, "Mantid = mantid");
getline(is, line);
Gigg, Martyn Anthony
committed
TS_ASSERT_EQUALS(line, "mtd = mantid");
Gigg, Martyn Anthony
committed
getline(is, line);
TS_ASSERT_EQUALS(line, "Mtd = mantid");
Gigg, Martyn Anthony
committed
Gigg, Martyn Anthony
committed
//next non-blank line should be setWorkingDirectory()
//eat blank lines
while( getline(is, line) && line.empty() )
Gigg, Martyn Anthony
committed
{
}
Gigg, Martyn Anthony
committed
getline(is, line);
TS_ASSERT_EQUALS(line, "def setWorkingDirectory(path):");
getline(is, line);
TS_ASSERT_EQUALS(line, "\tos.chdir(path)");
//next non-blank line should be help()
//eat blank lines
while( getline(is, line) && line.empty() )
{
}
modline = "def mtdGlobalHelp():";
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
getline(is, line);
TS_ASSERT_EQUALS(line, modline);
//skip over help
while( getline(is, line) && !line.empty() )
{
}
//it should be an algorithm definition but first is a comment line
getline(is,line);
getline(is,line);
bool found(false);
if( line.find("def") == 0 ) found = true;
TS_ASSERT_EQUALS(found, true);
getline(is, line);
found = false;
if( line.find("createAlgorithm") != std::string::npos ) found = true;
TS_ASSERT_EQUALS(found, true);
int setprop(0);
while( getline(is, line) )
{
if( line.find("setPropertyValue") != std::string::npos )
++setprop;
}
TS_ASSERT( setprop > 0 );
is.close();
// remove
Gigg, Martyn Anthony
committed
TS_ASSERT_THROWS_NOTHING( apimodule.remove() );
TS_ASSERT( !apimodule.exists() );
}
};
#endif //SIMPLEPYTHONAPITEST_H_