FunctionPropertyTest.h 4.17 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
#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
90
    FunctionProperty prop("fun");
    std::string error;
91
92
    auto fun_p = FunctionFactory::Instance().createInitialized(
        createTestFunctionString());
93
94
    TS_ASSERT(fun_p);
    prop = fun_p;
95
    auto fun1_p = prop();
96
    TS_ASSERT(fun1_p);
97
98
99
    TS_ASSERT_EQUALS(fun_p, fun1_p);
    TS_ASSERT_EQUALS(fun1_p->asString(),
                     "name=FunctionPropertyTest_Function,A=3,B=2");
100
101
102
    TS_ASSERT_EQUALS(fun1_p->getParameter("A"), 3.0);
    TS_ASSERT_EQUALS(fun1_p->getParameter("B"), 2.0);
  }
103

104
105
106
  void test_SetValue_From_Json() {}

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

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