20
20
#include < bsoncxx/document/view.hpp>
21
21
#include < bsoncxx/document/view_or_value.hpp>
22
22
#include < bsoncxx/json.hpp>
23
+ #include < bsoncxx/string/view_or_value.hpp>
24
+
25
+ using namespace bsoncxx ;
23
26
24
27
using bsoncxx::to_json;
25
- using bsoncxx::builder::stream::document;
26
28
using bsoncxx::builder::stream::finalize;
27
- using bsoncxx::document::view_or_value;
28
29
29
- TEST_CASE (" view_or_value" , " [bsoncxx::document::view_or_value]" ) {
30
- auto empty = document{} << finalize;
31
- auto doc = document{} << " a" << 1 << finalize;
30
+ TEST_CASE (" document:: view_or_value" , " [bsoncxx::document::view_or_value]" ) {
31
+ auto empty = builder::stream:: document{} << finalize;
32
+ auto doc = builder::stream:: document{} << " a" << 1 << finalize;
32
33
auto json = to_json (doc.view ());
33
34
34
35
SECTION (" can be default-constructed" ) {
35
- view_or_value variant{};
36
+ document:: view_or_value variant{};
36
37
REQUIRE (to_json (variant) == to_json (empty));
37
38
}
38
39
39
40
SECTION (" can be constructed with a view" ) {
40
- view_or_value variant{doc.view ()};
41
+ document:: view_or_value variant{doc.view ()};
41
42
42
43
SECTION (" can be used as a view" ) {
43
44
REQUIRE (to_json (variant) == json);
44
45
}
45
46
46
47
SECTION (" can be copy constructed" ) {
47
- view_or_value copied{variant};
48
+ document:: view_or_value copied{variant};
48
49
REQUIRE (to_json (copied) == json);
49
50
}
50
51
51
52
SECTION (" can be copy assigned" ) {
52
- view_or_value copied{empty.view ()};
53
+ document:: view_or_value copied{empty.view ()};
53
54
{
54
- view_or_value temp{doc.view ()};
55
+ document:: view_or_value temp{doc.view ()};
55
56
copied = temp;
56
57
}
57
58
REQUIRE (to_json (copied) == json);
58
59
}
59
60
60
61
SECTION (" can be move constructed" ) {
61
- view_or_value temp{doc.view ()};
62
- view_or_value moved{std::move (temp)};
62
+ document:: view_or_value temp{doc.view ()};
63
+ document:: view_or_value moved{std::move (temp)};
63
64
REQUIRE (to_json (moved) == json);
64
65
REQUIRE (to_json (temp) == to_json (empty));
65
66
}
66
67
67
68
SECTION (" can be move assigned" ) {
68
- view_or_value moved{variant.view ()};
69
+ document:: view_or_value moved{variant.view ()};
69
70
{
70
- view_or_value temp{doc.view ()};
71
+ document:: view_or_value temp{doc.view ()};
71
72
moved = std::move (temp);
72
73
REQUIRE (to_json (temp) == to_json (empty));
73
74
}
@@ -77,43 +78,152 @@ TEST_CASE("view_or_value", "[bsoncxx::document::view_or_value]") {
77
78
78
79
SECTION (" can be constructed with a value" ) {
79
80
auto move_doc = doc;
80
- view_or_value variant{std::move (move_doc)};
81
+ document:: view_or_value variant{std::move (move_doc)};
81
82
82
83
SECTION (" can be used as a view" ) {
83
84
REQUIRE (to_json (variant) == json);
84
85
}
85
86
86
87
SECTION (" can be copy constructed" ) {
87
- view_or_value copied{variant};
88
+ document:: view_or_value copied{variant};
88
89
REQUIRE (to_json (copied) == json);
89
90
}
90
91
91
92
SECTION (" can be copy assigned" ) {
92
- view_or_value copied{empty};
93
+ document:: view_or_value copied{empty};
93
94
{
94
95
auto temp_doc = doc;
95
- view_or_value temp{std::move (temp_doc)};
96
+ document:: view_or_value temp{std::move (temp_doc)};
96
97
copied = temp;
97
98
}
98
99
REQUIRE (to_json (copied) == json);
99
100
}
100
101
101
102
SECTION (" can be move constructed" ) {
102
- view_or_value temp{doc.view ()};
103
- view_or_value moved{std::move (temp)};
103
+ document:: view_or_value temp{doc.view ()};
104
+ document:: view_or_value moved{std::move (temp)};
104
105
REQUIRE (to_json (moved) == json);
105
106
REQUIRE (to_json (temp) == to_json (empty));
106
107
}
107
108
108
109
SECTION (" can be move assigned" ) {
109
- view_or_value moved{empty};
110
+ document:: view_or_value moved{empty};
110
111
{
111
112
auto temp_doc = doc;
112
- view_or_value temp{std::move (temp_doc)};
113
+ document:: view_or_value temp{std::move (temp_doc)};
113
114
moved = std::move (temp);
114
115
REQUIRE (to_json (temp) == to_json (empty));
115
116
}
116
117
REQUIRE (to_json (moved) == json);
117
118
}
118
119
}
120
+
121
+ SECTION (" Can be compared to another view_or_value" ) {
122
+ SECTION (" Compares equal with equal views, regardless of ownership" ) {
123
+ document::value temp{doc};
124
+ document::view_or_value a{std::move (temp)};
125
+ document::view_or_value b{doc.view ()};
126
+
127
+ REQUIRE (b == a);
128
+ }
129
+
130
+ SECTION (" Compares inequal with different views" ) {
131
+ auto temp_a = builder::stream::document{} << " a" << 1 << finalize;
132
+ auto temp_b = builder::stream::document{} << " b" << 1 << finalize;
133
+ document::view_or_value a{std::move (temp_a)};
134
+ document::view_or_value b{std::move (temp_b)};
135
+
136
+ REQUIRE (a != b);
137
+ }
138
+ }
139
+
140
+ SECTION (" Can be compared to a plain View" ) {
141
+ auto bad_doc = builder::stream::document{} << " blah" << 1 << finalize;
142
+ document::view_or_value variant (doc.view ());
143
+ REQUIRE (variant == doc.view ());
144
+ REQUIRE (doc.view () == variant);
145
+ REQUIRE (variant != bad_doc.view ());
146
+ REQUIRE (bad_doc.view () != variant);
147
+ }
148
+
149
+ SECTION (" Can be compared to a plain Value" ) {
150
+ auto bad_doc = builder::stream::document{} << " blah" << 1 << finalize;
151
+ document::view_or_value variant (doc.view ());
152
+ REQUIRE (variant == doc);
153
+ REQUIRE (doc == variant);
154
+ REQUIRE (variant != bad_doc);
155
+ REQUIRE (bad_doc != variant);
156
+ }
157
+ }
158
+
159
+ TEST_CASE (" string::document::view_or_value" , " [bsoncxx::string::view_or_value]" ) {
160
+ SECTION (" can be constructed with a moved-in std::string" ) {
161
+ std::string name{" Sally" };
162
+ string::view_or_value sally{std::move (name)};
163
+ }
164
+
165
+ SECTION (" can be constructed with a const std::string&" ) {
166
+ std::string name{" Jonny" };
167
+ string::view_or_value jonny{name};
168
+
169
+ SECTION (" is non-owning" ) {
170
+ REQUIRE (jonny == " Jonny" );
171
+ name[1 ] = ' e' ;
172
+ REQUIRE (jonny == " Jenny" );
173
+ }
174
+ }
175
+
176
+ SECTION (" can be constructed with a const char*" ) {
177
+ std::string name = " Julia" ;
178
+ string::view_or_value julia{name.c_str ()};
179
+
180
+ SECTION (" is non-owning" ) {
181
+ REQUIRE (julia == " Julia" );
182
+ name[4 ] = ' o' ;
183
+ REQUIRE (julia == " Julio" );
184
+ }
185
+ }
186
+
187
+ SECTION (" can be constructed with a stdx::string_view" ) {
188
+ std::string name{" Mike" };
189
+ stdx::string_view name_view{name};
190
+ string::view_or_value mike{name_view};
191
+
192
+ SECTION (" is non-owning" ) {
193
+ REQUIRE (mike == " Mike" );
194
+ name[3 ] = ' a' ;
195
+ REQUIRE (mike == " Mika" );
196
+ }
197
+ }
198
+
199
+ SECTION (" can be compared to a std::string" ) {
200
+ std::string name{" Theo" };
201
+ std::string other_name{" Tad" };
202
+ string::view_or_value theo{name};
203
+
204
+ REQUIRE (theo == name);
205
+ REQUIRE (name == theo);
206
+ REQUIRE (theo != other_name);
207
+ REQUIRE (other_name != theo);
208
+ }
209
+
210
+ SECTION (" can be compared to a const char*" ) {
211
+ string::view_or_value bess{" Bess" };
212
+
213
+ REQUIRE (bess == " Bess" );
214
+ REQUIRE (" Bess" == bess);
215
+ REQUIRE (bess != " Betty" );
216
+ REQUIRE (" Betty" != bess);
217
+ }
218
+
219
+ SECTION (" can be compared to a stdx::string_view" ) {
220
+ stdx::string_view name{" Carlin" };
221
+ stdx::string_view other_name{" Cailin" };
222
+ string::view_or_value carlin{name};
223
+
224
+ REQUIRE (carlin == name);
225
+ REQUIRE (name == carlin);
226
+ REQUIRE (carlin != other_name);
227
+ REQUIRE (other_name != carlin);
228
+ }
119
229
}
0 commit comments