|
2 | 2 | // Please see LICENSE for license or visit https://github.com/taocpp/json/ |
3 | 3 |
|
4 | 4 | #include <limits> |
| 5 | +#include <memory> |
| 6 | +#include <string> |
5 | 7 |
|
6 | 8 | #include "test.hpp" |
7 | 9 | #include "test_events.hpp" |
@@ -101,6 +103,38 @@ namespace tao::json |
101 | 103 | TEST_ASSERT( n != v ); |
102 | 104 | } |
103 | 105 |
|
| 106 | + struct default_type |
| 107 | + : std::string |
| 108 | + { |
| 109 | + using std::string::string; |
| 110 | + }; |
| 111 | + |
| 112 | + template<> |
| 113 | + struct traits< default_type > |
| 114 | + : traits< std::string > |
| 115 | + { |
| 116 | + TAO_JSON_DEFAULT_KEY( "default_key" ); |
| 117 | + }; |
| 118 | + |
| 119 | + void test_default() |
| 120 | + { |
| 121 | + const default_type a = "a"; |
| 122 | + const value v1 = { a }; |
| 123 | + TEST_ASSERT( v1.get_object().size() == 1 ); |
| 124 | + TEST_ASSERT( v1.get_object().begin()->first == "default_key" ); |
| 125 | + TEST_ASSERT( v1.get_object().begin()->second == "a" ); |
| 126 | + const auto b = std::make_unique< default_type >( "b" ); |
| 127 | + const value v2 = { b }; |
| 128 | + TEST_ASSERT( v2.get_object().size() == 1 ); |
| 129 | + TEST_ASSERT( v2.get_object().begin()->first == "default_key" ); |
| 130 | + TEST_ASSERT( v2.get_object().begin()->second == "b" ); |
| 131 | + const auto c = std::make_shared< default_type >( "c" ); |
| 132 | + const value v3 = { c }; |
| 133 | + TEST_ASSERT( v3.get_object().size() == 1 ); |
| 134 | + TEST_ASSERT( v3.get_object().begin()->first == "default_key" ); |
| 135 | + TEST_ASSERT( v3.get_object().begin()->second == "c" ); |
| 136 | + } |
| 137 | + |
104 | 138 | void test_deque() |
105 | 139 | { |
106 | 140 | const std::deque< std::uint64_t > f = { 1, 2, 3, 4 }; |
@@ -284,6 +318,7 @@ namespace tao::json |
284 | 318 |
|
285 | 319 | test_shared(); |
286 | 320 | test_unique(); |
| 321 | + test_default(); |
287 | 322 |
|
288 | 323 | test_deque(); |
289 | 324 | test_list(); |
|
0 commit comments