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 <test/jtx/basic_prop.h>
27 #include <test/jtx/requires.h>
28 
29 #include <functional>
30 #include <memory>
31 #include <vector>
32 
33 namespace ripple {
34 namespace test {
35 namespace jtx {
36 
37 class Env;
38 
42 struct JTx
43 {
47  bool fill_fee = true;
48  bool fill_seq = true;
49  bool fill_sig = true;
52 
53  JTx() = default;
54  JTx(JTx const&) = default;
55  JTx&
56  operator=(JTx const&) = default;
57  JTx(JTx&&) = default;
58  JTx&
59  operator=(JTx&&) = default;
60 
61  JTx(Json::Value&& jv_) : jv(std::move(jv_))
62  {
63  }
64 
65  JTx(Json::Value const& jv_) : jv(jv_)
66  {
67  }
68 
69  template <class Key>
71  operator[](Key const& key)
72  {
73  return jv[key];
74  }
75 
81  template <class Prop>
82  Prop*
83  get()
84  {
85  for (auto& prop : props_.list)
86  {
87  if (auto test = dynamic_cast<prop_type<Prop>*>(prop.get()))
88  return &test->t;
89  }
90  return nullptr;
91  }
92 
93  template <class Prop>
94  Prop const*
95  get() const
96  {
97  for (auto& prop : props_.list)
98  {
99  if (auto test = dynamic_cast<prop_type<Prop> const*>(prop.get()))
100  return &test->t;
101  }
102  return nullptr;
103  }
111  void
113  {
114  for (auto& prop : props_.list)
115  {
116  if (prop->assignable(p.get()))
117  {
118  prop = std::move(p);
119  return;
120  }
121  }
122  props_.list.emplace_back(std::move(p));
123  }
124 
125  template <class Prop, class... Args>
126  void
127  set(Args&&... args)
128  {
129  set(std::make_unique<prop_type<Prop>>(std::forward<Args>(args)...));
130  }
133 private:
134  struct prop_list
135  {
136  prop_list() = default;
137 
138  prop_list(prop_list const& other)
139  {
140  for (auto const& prop : other.list)
141  list.emplace_back(prop->clone());
142  }
143 
144  prop_list&
145  operator=(prop_list const& other)
146  {
147  if (this != &other)
148  {
149  list.clear();
150  for (auto const& prop : other.list)
151  list.emplace_back(prop->clone());
152  }
153  return *this;
154  }
155 
156  prop_list(prop_list&& src) = default;
157  prop_list&
158  operator=(prop_list&& src) = default;
159 
161  };
162 
164 };
165 
166 } // namespace jtx
167 } // namespace test
168 } // namespace ripple
169 
170 #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:95
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:145
vector
ripple::test::jtx::JTx::stx
std::shared_ptr< STTx const > stx
Definition: JTx.h:50
std::unique_ptr::get
T get(T... args)
std::function
ripple::test::jtx::JTx::operator[]
Json::Value & operator[](Key const &key)
Definition: JTx.h:71
ripple::test::jtx::JTx::require
requires_t require
Definition: JTx.h:45
ripple::test::jtx::JTx::JTx
JTx(Json::Value &&jv_)
Definition: JTx.h:61
ripple::test::jtx::JTx::operator=
JTx & operator=(JTx const &)=default
ripple::test::jtx::JTx::props_
prop_list props_
Definition: JTx.h:163
ripple::test::jtx::JTx::prop_list
Definition: JTx.h:134
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:112
ripple::TERSubset
Definition: TER.h:340
ripple::test::jtx::JTx
Execution context for applying a JSON transaction.
Definition: JTx.h:42
ripple::test::jtx::JTx::jv
Json::Value jv
Definition: JTx.h:44
ripple::test::jtx::JTx::JTx
JTx(Json::Value const &jv_)
Definition: JTx.h:65
ripple::test::jtx::prop_type
Definition: basic_prop.h:37
ripple::test::jtx::JTx::signer
std::function< void(Env &, JTx &)> signer
Definition: JTx.h:51
memory
ripple::test::jtx::JTx::get
Prop * get()
Return a property if it exists.
Definition: JTx.h:83
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:48
ripple::test::jtx::JTx::fill_sig
bool fill_sig
Definition: JTx.h:49
std
STL namespace.
ripple::test::jtx::JTx::prop_list::list
std::vector< std::unique_ptr< basic_prop > > list
Definition: JTx.h:160
std::optional
ripple::test::jtx::JTx::prop_list::prop_list
prop_list(prop_list const &other)
Definition: JTx.h:138
std::unique_ptr
STL class.
ripple::test::jtx::JTx::fill_fee
bool fill_fee
Definition: JTx.h:47
ripple::test::jtx::JTx::set
void set(Args &&... args)
Definition: JTx.h:127
ripple::tesSUCCESS
@ tesSUCCESS
Definition: TER.h:222
ripple::test::jtx::Env
A transaction testing environment.
Definition: Env.h:116
Json::Value
Represents a JSON value.
Definition: json_value.h:145