FunctionPropertyTest.h 4.16 KB
Newer Older
1
2
3
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4
5
//   NScD Oak Ridge National Laboratory, European Spallation Source,
//   Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6
// SPDX - License - Identifier: GPL - 3.0 +
7
#pragma once
8
9
10
11

#include <cxxtest/TestSuite.h>

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

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

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

31
DECLARE_FUNCTION(FunctionPropertyTest_Function)
32

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

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

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

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

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

69
70
71
72
73
74
75
76
77
78
  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() {
79
80
    FunctionProperty prop("fun");
    std::string error;
81
82
    TS_ASSERT_THROWS_NOTHING(
        error = prop.setValue("name=FunctionDoesnotExist,A=3"));
83
84
85
86
87
    TS_ASSERT(!error.empty());
    TS_ASSERT_THROWS_NOTHING(error = prop.setValue("ghvjhgvjhgcjh"));
    TS_ASSERT(!error.empty());
  }

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

103
104
105
  void test_SetValue_From_Json() {}

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

121
private:
122
123
124
  std::string createTestFunctionString() {
    return "name=FunctionPropertyTest_Function,A=3";
  }
125
};