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 <test/jtx/requires.h>
24 #include <test/jtx/basic_prop.h>
25 #include <ripple/json/json_value.h>
26 #include <ripple/protocol/STTx.h>
27 #include <ripple/protocol/TER.h>
28 #include <functional>
29 #include <memory>
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& operator=(JTx const&) = default;
55  JTx(JTx&&) = default;
56  JTx& operator=(JTx&&) = default;
57 
58  JTx (Json::Value&& jv_)
59  : jv(std::move(jv_))
60  {
61  }
62 
63  JTx (Json::Value const& jv_)
64  : 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<
88  prop.get()))
89  return &test->t;
90  }
91  return nullptr;
92  }
93 
94  template <class Prop>
95  Prop const*
96  get() const
97  {
98  for (auto& prop : props_.list)
99  {
100  if (auto test = dynamic_cast<
101  prop_type<Prop> const*>(
102  prop.get()))
103  return &test->t;
104  }
105  return nullptr;
106  }
114  void
116  {
117  for (auto& prop : props_.list)
118  {
119  if (prop->assignable(p.get()))
120  {
121  prop = std::move(p);
122  return;
123  }
124  }
125  props_.list.emplace_back(std::move(p));
126  }
127 
128  template <class Prop, class... Args>
129  void
130  set(Args&&... args)
131  {
134  std::forward <Args> (
135  args)...));
136  }
139 private:
140  struct prop_list
141  {
142  prop_list() = default;
143 
144  prop_list(prop_list const& other)
145  {
146  for (auto const& prop : other.list)
147  list.emplace_back(prop->clone());
148  }
149 
151  {
152  if (this != &other)
153  {
154  list.clear();
155  for (auto const& prop : other.list)
156  list.emplace_back(prop->clone());
157  }
158  return *this;
159  }
160 
161  prop_list(prop_list&& src) = default;
162  prop_list& operator=(prop_list&& src) = default;
163 
166  };
167 
169 };
170 
171 } // jtx
172 } // test
173 } // ripple
174 
175 #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:96
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:150
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:58
ripple::test::jtx::JTx::operator=
JTx & operator=(JTx const &)=default
ripple::test::jtx::JTx::props_
prop_list props_
Definition: JTx.h:168
ripple::test::jtx::JTx::prop_list
Definition: JTx.h:140
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:115
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:63
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::test::jtx::basic_prop
Definition: basic_prop.h:27
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::prop_list
prop_list(prop_list const &other)
Definition: JTx.h:144
ripple::test::jtx::JTx::prop_list::list
std::vector< std::unique_ptr< basic_prop > > list
Definition: JTx.h:165
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:130
ripple::tesSUCCESS
@ tesSUCCESS
Definition: TER.h:219
ripple::test::jtx::Env
A transaction testing environment.
Definition: Env.h:117
Json::Value
Represents a JSON value.
Definition: json_value.h:141