FunctionPropertyTest.h 3.88 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 testConstructor() {
44
45
46
    TS_ASSERT_THROWS_NOTHING(FunctionProperty prop("fun"));
  }

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

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

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

71
  void testBadValue() {
72
73
    FunctionProperty prop("fun");
    std::string error;
74
75
    TS_ASSERT_THROWS_NOTHING(
        error = prop.setValue("name=FunctionDoesnotExist,A=3"));
76
77
78
79
80
    TS_ASSERT(!error.empty());
    TS_ASSERT_THROWS_NOTHING(error = prop.setValue("ghvjhgvjhgcjh"));
    TS_ASSERT(!error.empty());
  }

81
  void testSetValue() {
82
83
    FunctionProperty prop("fun");
    std::string error;
84
85
86
    boost::shared_ptr<IFunction> fun_p(
        FunctionFactory::Instance().createInitialized(
            "name=FunctionPropertyTest_Function,A=3"));
87
88
    TS_ASSERT(fun_p);
    prop = fun_p;
89
    boost::shared_ptr<IFunction> fun1_p = prop;
90
    TS_ASSERT(fun1_p);
91
92
93
    TS_ASSERT_EQUALS(fun_p, fun1_p);
    TS_ASSERT_EQUALS(fun1_p->asString(),
                     "name=FunctionPropertyTest_Function,A=3,B=2");
94
95
96
    TS_ASSERT_EQUALS(fun1_p->getParameter("A"), 3.0);
    TS_ASSERT_EQUALS(fun1_p->getParameter("B"), 2.0);
  }
97

98
  void testSharedPointer() {
99
100
    FunctionProperty prop("fun");
    std::string error;
101
102
    boost::shared_ptr<FunctionPropertyTest_Function> fun_p(
        new FunctionPropertyTest_Function);
103
    TS_ASSERT(fun_p);
104
    fun_p->setParameter("A", 3);
105
106
107
    prop = fun_p;
    boost::shared_ptr<IFunction> fun1_p = prop;
    TS_ASSERT(fun1_p);
108
109
110
    TS_ASSERT_EQUALS(fun_p, fun1_p);
    TS_ASSERT_EQUALS(fun1_p->asString(),
                     "name=FunctionPropertyTest_Function,A=3,B=2");
111
112
113
114
    TS_ASSERT_EQUALS(fun1_p->getParameter("A"), 3.0);
    TS_ASSERT_EQUALS(fun1_p->getParameter("B"), 2.0);
  }

115
116
117
118
private:
};

#endif /*FUNCTIONPROPERTYTEST_H_*/