FunctionPropertyTest.h 4.25 KB
Newer Older
1
2
3
4
5
6
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
//     NScD Oak Ridge National Laboratory, European Spallation Source
//     & Institut Laue - Langevin
// SPDX - License - Identifier: GPL - 3.0 +
7
8
9
10
11
12
#ifndef FUNCTIONPROPERTYTEST_H_
#define FUNCTIONPROPERTYTEST_H_

#include <cxxtest/TestSuite.h>

#include "MantidAPI/FunctionFactory.h"
LamarMoore's avatar
LamarMoore committed
13
#include "MantidAPI/FunctionProperty.h"
14
15
#include "MantidAPI/ParamFunction.h"
#include <boost/shared_ptr.hpp>
16
#include <json/value.h>
17
18
19
20

using namespace Mantid::Kernel;
using namespace Mantid::API;

21
22
class FunctionPropertyTest_Function : public virtual ParamFunction,
                                      public virtual IFunction {
23
public:
24
25
26
  FunctionPropertyTest_Function() {
    this->declareParameter("A", 1.0);
    this->declareParameter("B", 2.0);
27
  }
28
29
  std::string name() const override { return "FunctionPropertyTest_Function"; }
  void function(const FunctionDomain &, FunctionValues &) const override {}
30
31
};

32
DECLARE_FUNCTION(FunctionPropertyTest_Function)
33

34
class FunctionPropertyTest : public CxxTest::TestSuite {
35
36
37
public:
  // This pair of boilerplate methods prevent the suite being created statically
  // This means the constructor isn't called when running other tests
38
39
40
41
  static FunctionPropertyTest *createSuite() {
    return new FunctionPropertyTest();
  }
  static void destroySuite(FunctionPropertyTest *suite) { delete suite; }
42

43
  void test_Constructor() {
44
45
46
    TS_ASSERT_THROWS_NOTHING(FunctionProperty prop("fun"));
  }

47
  void test_Value() {
48
    FunctionProperty prop("fun");
49
50
    TS_ASSERT_EQUALS("", prop.value());

51
    std::string error;
52
    TS_ASSERT_THROWS_NOTHING(error = prop.setValue(createTestFunctionString()));
53
    TS_ASSERT(error.empty());
54
    boost::shared_ptr<IFunction> fun_p = prop;
55
56
    TS_ASSERT_EQUALS(fun_p->asString(),
                     "name=FunctionPropertyTest_Function,A=3,B=2");
57
58
    TS_ASSERT_EQUALS(fun_p->getParameter("A"), 3.0);
    TS_ASSERT_EQUALS(fun_p->getParameter("B"), 2.0);
59
60
    TS_ASSERT_EQUALS(prop.value(),
                     "name=FunctionPropertyTest_Function,A=3,B=2");
61
62
  }

63
  void test_ValueAsJson() {
64
65
66
67
68
69
    FunctionProperty prop("fun");
    const std::string funcString("name=FunctionPropertyTest_Function,A=3,B=2");
    prop.setValue(funcString);
    TS_ASSERT_EQUALS(funcString, prop.valueAsJson().asString());
  }

70
71
72
73
74
75
76
77
78
79
  void test_SetValueFromJson() {
    FunctionProperty prop("fun");
    const std::string helpMessage{
        prop.setValueFromJson(Json::Value(createTestFunctionString()))};
    TS_ASSERT(helpMessage.empty());
    TS_ASSERT_EQUALS("name=FunctionPropertyTest_Function,A=3,B=2",
                     prop.value());
  }

  void test_Bad_Value() {
80
81
    FunctionProperty prop("fun");
    std::string error;
82
83
    TS_ASSERT_THROWS_NOTHING(
        error = prop.setValue("name=FunctionDoesnotExist,A=3"));
84
85
86
87
88
    TS_ASSERT(!error.empty());
    TS_ASSERT_THROWS_NOTHING(error = prop.setValue("ghvjhgvjhgcjh"));
    TS_ASSERT(!error.empty());
  }

89
  void test_Assignment_By_SharedPtr() {
90
91
    FunctionProperty prop("fun");
    std::string error;
92
93
    auto fun_p = FunctionFactory::Instance().createInitialized(
        createTestFunctionString());
94
95
    TS_ASSERT(fun_p);
    prop = fun_p;
96
    auto fun1_p = prop();
97
    TS_ASSERT(fun1_p);
98
99
100
    TS_ASSERT_EQUALS(fun_p, fun1_p);
    TS_ASSERT_EQUALS(fun1_p->asString(),
                     "name=FunctionPropertyTest_Function,A=3,B=2");
101
102
103
    TS_ASSERT_EQUALS(fun1_p->getParameter("A"), 3.0);
    TS_ASSERT_EQUALS(fun1_p->getParameter("B"), 2.0);
  }
104

105
106
107
  void test_SetValue_From_Json() {}

  void test_Shared_Pointer() {
108
109
    FunctionProperty prop("fun");
    std::string error;
110
111
    boost::shared_ptr<FunctionPropertyTest_Function> fun_p(
        new FunctionPropertyTest_Function);
112
    TS_ASSERT(fun_p);
113
    fun_p->setParameter("A", 3);
114
115
116
    prop = fun_p;
    boost::shared_ptr<IFunction> fun1_p = prop;
    TS_ASSERT(fun1_p);
117
118
119
    TS_ASSERT_EQUALS(fun_p, fun1_p);
    TS_ASSERT_EQUALS(fun1_p->asString(),
                     "name=FunctionPropertyTest_Function,A=3,B=2");
120
121
122
123
    TS_ASSERT_EQUALS(fun1_p->getParameter("A"), 3.0);
    TS_ASSERT_EQUALS(fun1_p->getParameter("B"), 2.0);
  }

124
private:
125
126
127
  std::string createTestFunctionString() {
    return "name=FunctionPropertyTest_Function,A=3";
  }
128
129
130
};

#endif /*FUNCTIONPROPERTYTEST_H_*/