rippled
JTx.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef RIPPLE_TEST_JTX_JTX_H_INCLUDED
21 #define RIPPLE_TEST_JTX_JTX_H_INCLUDED
22 
23 #include <ripple/json/json_value.h>
24 #include <ripple/protocol/STTx.h>
25 #include <ripple/protocol/TER.h>
26 #include <functional>
27 #include <memory>
28 #include <test/jtx/basic_prop.h>
29 #include <test/jtx/requires.h>
30 #include <vector>
31 
32 namespace ripple {
33 namespace test {
34 namespace jtx {
35 
36 class Env;
37 
41 struct JTx
42 {
45  boost::optional<TER> ter = TER{tesSUCCESS};
46  bool fill_fee = true;
47  bool fill_seq = true;
48  bool fill_sig = true;
51 
52  JTx() = default;
53  JTx(JTx const&) = default;
54  JTx&
55  operator=(JTx const&) = default;
56  JTx(JTx&&) = default;
57  JTx&
58  operator=(JTx&&) = default;
59 
60  JTx(Json::Value&& jv_) : jv(std::move(jv_))
61  {
62  }
63 
64  JTx(Json::Value const& jv_) : jv(jv_)
65  {
66  }
67 
68  template <class Key>
70  operator[](Key const& key)
71  {
72  return jv[key];
73  }
74 
80  template <class Prop>
81  Prop*
82  get()
83  {
84  for (auto& prop : props_.list)
85  {
86  if (auto test = dynamic_cast<prop_type<Prop>*>(prop.get()))
87  return &test->t;
88  }
89  return nullptr;
90  }
91 
92  template <class Prop>
93  Prop const*
94  get() const
95  {
96  for (auto& prop : props_.list)
97  {
98  if (auto test = dynamic_cast<prop_type<Prop> const*>(prop.get()))
99  return &test->t;
100  }
101  return nullptr;
102  }
110  void
112  {
113  for (auto& prop : props_.list)
114  {
115  if (prop->assignable(p.get()))
116  {
117  prop = std::move(p);
118  return;
119  }
120  }
121  props_.list.emplace_back(std::move(p));
122  }
123 
124  template <class Prop, class... Args>
125  void
126  set(Args&&... args)
127  {
128  set(std::make_unique<prop_type<Prop>>(std::forward<Args>(args)...));
129  }
132 private:
133  struct prop_list
134  {
135  prop_list() = default;
136 
137  prop_list(prop_list const& other)
138  {
139  for (auto const& prop : other.list)
140  list.emplace_back(prop->clone());
141  }
142 
143  prop_list&
144  operator=(prop_list const& other)
145  {
146  if (this != &other)
147  {
148  list.clear();
149  for (auto const& prop : other.list)
150  list.emplace_back(prop->clone());
151  }
152  return *this;
153  }
154 
155  prop_list(prop_list&& src) = default;
156  prop_list&
157  operator=(prop_list&& src) = default;
158 
160  };
161 
163 };
164 
165 } // namespace jtx
166 } // namespace test
167 } // namespace ripple
168 
169 #endif
ripple::test::jtx::JTx::prop_list::prop_list
prop_list()=default
std::shared_ptr
STL class.
ripple::test::jtx::JTx::get
Prop const * get() const
Definition: JTx.h:94
ripple::test::jtx::ter
Set the expected result code for a JTx The test will fail if the code doesn't match.
Definition: ter.h:33
functional
ripple::test::jtx::prop
Set a property on a JTx.
Definition: prop.h:32
std::make_unique
T make_unique(T... args)
ripple::test::jtx::JTx::prop_list::operator=
prop_list & operator=(prop_list const &other)
Definition: JTx.h:144
vector
ripple::test::jtx::JTx::stx
std::shared_ptr< STTx const > stx
Definition: JTx.h:49
std::unique_ptr::get
T get(T... args)
std::function
ripple::test::jtx::JTx::operator[]
Json::Value & operator[](Key const &key)
Definition: JTx.h:70
ripple::test::jtx::JTx::JTx
JTx(Json::Value &&jv_)
Definition: JTx.h:60
ripple::test::jtx::JTx::operator=
JTx & operator=(JTx const &)=default
ripple::test::jtx::JTx::props_
prop_list props_
Definition: JTx.h:162
ripple::test::jtx::JTx::prop_list
Definition: JTx.h:133
ripple::test::jtx::JTx::set
void set(std::unique_ptr< basic_prop > p)
Set a property If the property already exists, it is replaced.
Definition: JTx.h:111
ripple::TERSubset< CanCvtToTER >
ripple::test::jtx::JTx
Execution context for applying a JSON transaction.
Definition: JTx.h:41
ripple::test::jtx::JTx::jv
Json::Value jv
Definition: JTx.h:43
ripple::test::jtx::JTx::JTx
JTx(Json::Value const &jv_)
Definition: JTx.h:64
ripple::test::jtx::prop_type
Definition: basic_prop.h:37
ripple::test::jtx::JTx::signer
std::function< void(Env &, JTx &)> signer
Definition: JTx.h:50
memory
ripple::test::jtx::JTx::get
Prop * get()
Return a property if it exists.
Definition: JTx.h:82
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::jtx::JTx::JTx
JTx()=default
ripple::test::jtx::JTx::fill_seq
bool fill_seq
Definition: JTx.h:47
ripple::test::jtx::JTx::fill_sig
bool fill_sig
Definition: JTx.h:48
ripple::test::jtx::JTx::requires
requires_t requires
Definition: JTx.h:44
std
STL namespace.
ripple::test::jtx::JTx::prop_list::list
std::vector< std::unique_ptr< basic_prop > > list
Definition: JTx.h:159
ripple::test::jtx::JTx::prop_list::prop_list
prop_list(prop_list const &other)
Definition: JTx.h:137
std::unique_ptr
STL class.
ripple::test::jtx::JTx::fill_fee
bool fill_fee
Definition: JTx.h:46
ripple::test::jtx::JTx::set
void set(Args &&... args)
Definition: JTx.h:126
ripple::tesSUCCESS
@ tesSUCCESS
Definition: TER.h:213
ripple::test::jtx::Env
A transaction testing environment.
Definition: Env.h:115
Json::Value
Represents a JSON value.
Definition: json_value.h:145