Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
test_variant.cpp File Reference
#include <boost/test/included/unit_test.hpp>
#include <fc/variant_object.hpp>
#include <fc/exception/exception.hpp>
#include <fc/crypto/base64.hpp>
#include <string>
Include dependency graph for test_variant.cpp:

Go to the source code of this file.

Macros

#define BOOST_TEST_MODULE   variant
 

Functions

 BOOST_AUTO_TEST_CASE (mutable_variant_object_test)
 
 BOOST_AUTO_TEST_CASE (variant_format_string_limited)
 

Macro Definition Documentation

◆ BOOST_TEST_MODULE

#define BOOST_TEST_MODULE   variant

Definition at line 1 of file test_variant.cpp.

Function Documentation

◆ BOOST_AUTO_TEST_CASE() [1/2]

BOOST_AUTO_TEST_CASE ( mutable_variant_object_test )

Definition at line 12 of file test_variant.cpp.

13{
14 // no BOOST_CHECK / BOOST_REQUIRE, just see that this compiles on all supported platforms
15 try {
16 variant v(42);
19 variants vs;
20 vs.push_back(mutable_variant_object("level", "debug")("color", v));
21 vs.push_back(mutable_variant_object()("level", "debug")("color", v));
22 vs.push_back(mutable_variant_object("level", "debug")("color", "green"));
23 vs.push_back(mutable_variant_object()("level", "debug")("color", "green"));
24 vs.push_back(mutable_variant_object("level", "debug")(vo));
25 vs.push_back(mutable_variant_object()("level", "debug")(mvo));
26 vs.push_back(mutable_variant_object("level", "debug").set("color", v));
27 vs.push_back(mutable_variant_object()("level", "debug").set("color", v));
28 vs.push_back(mutable_variant_object("level", "debug").set("color", "green"));
29 vs.push_back(mutable_variant_object()("level", "debug").set("color", "green"));
30 }
32}
An order-preserving dictionary of variants.
An order-preserving dictionary of variants.
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition variant.hpp:191
#define FC_LOG_AND_RETHROW()
std::vector< fc::variant > variants
Definition variant.hpp:173
bool set

◆ BOOST_AUTO_TEST_CASE() [2/2]

BOOST_AUTO_TEST_CASE ( variant_format_string_limited )

Definition at line 34 of file test_variant.cpp.

35{
36 constexpr size_t long_rep_char_num = 1024;
37 const std::string a_long_list = std::string(long_rep_char_num, 'a');
38 const std::string b_long_list = std::string(long_rep_char_num, 'b');
39 {
40 const string format = "${a} ${b} ${c}";
42 mu( "a", string( long_rep_char_num, 'a' ) );
43 mu( "b", string( long_rep_char_num, 'b' ) );
44 mu( "c", string( long_rep_char_num, 'c' ) );
45 const string result = fc::format_string( format, mu, true );
46 BOOST_CHECK_LT(0, mu.size());
47 const auto arg_limit_size = (1024 - format.size()) / mu.size();
48 BOOST_CHECK_EQUAL( result, string(arg_limit_size, 'a' ) + "... " + string(arg_limit_size, 'b' ) + "... " + string(arg_limit_size, 'c' ) + "..." );
49 BOOST_CHECK_LT(result.size(), 1024 + 3 * mu.size());
50 }
51 { // verify object, array, blob, and string, all exceeds limits, fold display for each
53 mu( "str", a_long_list );
54 mu( "obj", variant_object(mutable_variant_object()("a", a_long_list)("b", b_long_list)) );
55 mu( "arr", variants{variant(a_long_list), variant(b_long_list)} );
56 mu( "blob", blob({std::vector<char>(a_long_list.begin(), a_long_list.end())}) );
57 const string format_prefix = "Format string test: ";
58 const string format_str = format_prefix + "${str} ${obj} ${arr} {blob}";
59 const string result = fc::format_string( format_str, mu, true );
60 BOOST_CHECK_LT(0, mu.size());
61 const auto arg_limit_size = (1024 - format_str.size()) / mu.size();
62 BOOST_CHECK_EQUAL( result, format_prefix + a_long_list.substr(0, arg_limit_size) + "..." + " ${obj} ${arr} {blob}");
63 BOOST_CHECK_LT(result.size(), 1024 + 3 * mu.size());
64 }
65 { // verify object, array can be displayed properly
66 const string format_prefix = "Format string test: ";
67 const string format_str = format_prefix + "${str} ${obj} ${arr} ${blob} ${var}";
68 BOOST_CHECK_LT(format_str.size(), 1024);
69 const size_t short_rep_char_num = (1024 - format_str.size()) / 5 - 1;
70 const std::string a_short_list = std::string(short_rep_char_num, 'a');
71 const std::string b_short_list = std::string(short_rep_char_num / 3, 'b');
72 const std::string c_short_list = std::string(short_rep_char_num / 3, 'c');
73 const std::string d_short_list = std::string(short_rep_char_num / 3, 'd');
74 const std::string e_short_list = std::string(short_rep_char_num / 3, 'e');
75 const std::string f_short_list = std::string(short_rep_char_num, 'f');
76 const std::string g_short_list = std::string(short_rep_char_num, 'g');
78 const variant_object vo(mutable_variant_object()("b", b_short_list)("c", c_short_list));
79 const variants variant_list{variant(d_short_list), variant(e_short_list)};
80 const blob a_blob({std::vector<char>(f_short_list.begin(), f_short_list.end())});
81 const variant a_variant(g_short_list);
82 mu( "str", a_short_list );
83 mu( "obj", vo);
84 mu( "arr", variant_list);
85 mu( "blob", a_blob);
86 mu( "var", a_variant);
87 const string result = fc::format_string( format_str, mu, true );
88 const string target_result = format_prefix + a_short_list + " " +
89 "{" + "\"b\":\"" + b_short_list + "\",\"c\":\"" + c_short_list + "\"}" + " " +
90 "[\"" + d_short_list + "\",\"" + e_short_list + "\"]" + " " +
91 base64_encode( a_blob.data.data(), a_blob.data.size() ) + "=" + " " +
92 g_short_list;
93
94 BOOST_CHECK_EQUAL( result, target_result);
95 BOOST_CHECK_LT(result.size(), 1024 + 3 * mu.size());
96 }
97}
std::string base64_encode(unsigned char const *bytes_to_encode, unsigned int in_len)
Definition base64.cpp:92
fc::string format_string(const fc::string &, const variant_object &, bool minimize=false)
Definition variant.cpp:773
cmd_format format
Here is the call graph for this function: