Newer
Older
Gigg, Martyn Anthony
committed
#ifndef MANTID_API_ALGORITHMPROPERTYTEST_H_
#define MANTID_API_ALGORITHMPROPERTYTEST_H_
#include <cxxtest/TestSuite.h>
#include "MantidKernel/Timer.h"
#include "MantidKernel/System.h"
#include "MantidAPI/AlgorithmProperty.h"
#include "MantidAPI/AlgorithmHasProperty.h"
#include "MantidAPI/Algorithm.h"
using namespace Mantid::API;
using namespace Mantid::Kernel;
class AlgorithmPropertyTest : public CxxTest::TestSuite {
Gigg, Martyn Anthony
committed
private:
/// Use a fake algorithm object instead of a dependency on a real one.
class SimpleSum : public Algorithm {
Gigg, Martyn Anthony
committed
public:
SimpleSum() : Algorithm() {}
~SimpleSum() override {}
const std::string name() const override { return "SimpleSum"; }
int version() const override { return 1; }
const std::string category() const override { return "Dummy"; }
const std::string summary() const override { return "Test summary"; }
Gigg, Martyn Anthony
committed
declareProperty("Input1", 2);
declareProperty("Input2", 1);
declareProperty("Output1", -1, Direction::Output);
Gigg, Martyn Anthony
committed
}
Gigg, Martyn Anthony
committed
const int lhs = getProperty("Input1");
const int rhs = getProperty("Input2");
Gigg, Martyn Anthony
committed
setProperty("Output1", sum);
}
};
class HasAlgProp : public Algorithm {
Gigg, Martyn Anthony
committed
public:
const std::string name() const override { return "HasAlgProp"; }
int version() const override { return 1; }
const std::string category() const override { return "Dummy"; }
const std::string summary() const override { return "Test summary"; }
void init() override {
declareProperty(make_unique<AlgorithmProperty>("CalculateStep"));
}
Gigg, Martyn Anthony
committed
};
class HasAlgPropAndValidator : public Algorithm {
Gigg, Martyn Anthony
committed
public:
const std::string name() const override { return "HasAlgPropAndValidator"; }
int version() const override { return 1; }
const std::string category() const override { return "Dummy"; }
const std::string summary() const override { return "Test summary"; }
void init() override {
declareProperty(make_unique<AlgorithmProperty>(
"CalculateStep",
boost::make_shared<AlgorithmHasProperty>("Output1")));
Gigg, Martyn Anthony
committed
}
Gigg, Martyn Anthony
committed
};
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static AlgorithmPropertyTest *createSuite() {
return new AlgorithmPropertyTest();
}
static void destroySuite(AlgorithmPropertyTest *suite) { delete suite; }
Gigg, Martyn Anthony
committed
Gigg, Martyn Anthony
committed
Mantid::API::AlgorithmFactory::Instance().subscribe<SimpleSum>();
Mantid::API::AlgorithmFactory::Instance().subscribe<HasAlgProp>();
Mantid::API::AlgorithmFactory::Instance()
.subscribe<HasAlgPropAndValidator>();
Gigg, Martyn Anthony
committed
}
~AlgorithmPropertyTest() override {
Mantid::API::AlgorithmFactory::Instance().unsubscribe("SimpleSum", 1);
Mantid::API::AlgorithmFactory::Instance().unsubscribe("HasAlgProp1", 1);
Mantid::API::AlgorithmFactory::Instance().unsubscribe(
"HasAlgPropAndValidator1", 1);
Gigg, Martyn Anthony
committed
}
void test_A_Valid_Alg_String_Is_Accepted() {
Gigg, Martyn Anthony
committed
SimpleSum adder;
adder.initialize();
adder.execute();
TS_ASSERT_EQUALS(adder.getPropertyValue("Output1"), "3");
AlgorithmProperty testProp("CalculateStep");
TS_ASSERT_EQUALS(testProp.setValue(adder.toString()), "");
Gigg, Martyn Anthony
committed
}
void test_An_Invalid_String_Returns_An_Appropriate_Error() {
AlgorithmProperty testProp("CalculateStep");
TS_ASSERT_EQUALS(testProp.setValue("{\"name\":\"ComplexSum\"}"),
"Algorithm not registered ComplexSum");
Gigg, Martyn Anthony
committed
}
void test_Alg_With_An_AlgorithmProperty_Accepts_Another_Algorithm() {
Gigg, Martyn Anthony
committed
HasAlgProp testAlg;
testAlg.initialize();
IAlgorithm_sptr adder =
Mantid::API::AlgorithmFactory::Instance().create("SimpleSum", 1);
Gigg, Martyn Anthony
committed
adder->initialize();
adder->execute();
TS_ASSERT_THROWS_NOTHING(testAlg.setProperty("CalculateStep", adder));
// Can we retrieve it again
TS_ASSERT_THROWS_NOTHING(IAlgorithm_sptr calcStep =
testAlg.getProperty("CalculateStep"));
Gigg, Martyn Anthony
committed
// (And const) Can we retrieve it again
TS_ASSERT_THROWS_NOTHING(IAlgorithm_const_sptr calcStep =
testAlg.getProperty("CalculateStep"));
Gigg, Martyn Anthony
committed
// Is it correct?
IAlgorithm_sptr calcStep = testAlg.getProperty("CalculateStep");
TS_ASSERT_EQUALS(calcStep->getPropertyValue("Output1"), "3");
}
void
test_Alg_With_AlgorithmProperty_And_Validator_Fails_If_Input_Is_Invalid() {
Gigg, Martyn Anthony
committed
HasAlgPropAndValidator testAlg;
testAlg.initialize();
// Without initialize it has no properties
IAlgorithm_sptr adder =
Mantid::API::AlgorithmFactory::Instance().create("SimpleSum", 1);
TS_ASSERT_THROWS(testAlg.setProperty("CalculateStep", adder),
std::invalid_argument);
Gigg, Martyn Anthony
committed
// Add the required property so now it should pass
adder->initialize();
TS_ASSERT_THROWS_NOTHING(testAlg.setProperty("CalculateStep", adder));
}
};
#endif /* MANTID_API_ALGORITHMPROPERTYTEST_H_ */