Skip to content

Commit 17db0bf

Browse files
CXX-739 Tests for string::view_or_value and new comparison operators
1 parent 5bb06a3 commit 17db0bf

File tree

1 file changed

+132
-22
lines changed

1 file changed

+132
-22
lines changed

src/bsoncxx/test/view_or_value.cpp

Lines changed: 132 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,54 +20,55 @@
2020
#include <bsoncxx/document/view.hpp>
2121
#include <bsoncxx/document/view_or_value.hpp>
2222
#include <bsoncxx/json.hpp>
23+
#include <bsoncxx/string/view_or_value.hpp>
24+
25+
using namespace bsoncxx;
2326

2427
using bsoncxx::to_json;
25-
using bsoncxx::builder::stream::document;
2628
using bsoncxx::builder::stream::finalize;
27-
using bsoncxx::document::view_or_value;
2829

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;
3233
auto json = to_json(doc.view());
3334

3435
SECTION("can be default-constructed") {
35-
view_or_value variant{};
36+
document::view_or_value variant{};
3637
REQUIRE(to_json(variant) == to_json(empty));
3738
}
3839

3940
SECTION("can be constructed with a view") {
40-
view_or_value variant{doc.view()};
41+
document::view_or_value variant{doc.view()};
4142

4243
SECTION("can be used as a view") {
4344
REQUIRE(to_json(variant) == json);
4445
}
4546

4647
SECTION("can be copy constructed") {
47-
view_or_value copied{variant};
48+
document::view_or_value copied{variant};
4849
REQUIRE(to_json(copied) == json);
4950
}
5051

5152
SECTION("can be copy assigned") {
52-
view_or_value copied{empty.view()};
53+
document::view_or_value copied{empty.view()};
5354
{
54-
view_or_value temp{doc.view()};
55+
document::view_or_value temp{doc.view()};
5556
copied = temp;
5657
}
5758
REQUIRE(to_json(copied) == json);
5859
}
5960

6061
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)};
6364
REQUIRE(to_json(moved) == json);
6465
REQUIRE(to_json(temp) == to_json(empty));
6566
}
6667

6768
SECTION("can be move assigned") {
68-
view_or_value moved{variant.view()};
69+
document::view_or_value moved{variant.view()};
6970
{
70-
view_or_value temp{doc.view()};
71+
document::view_or_value temp{doc.view()};
7172
moved = std::move(temp);
7273
REQUIRE(to_json(temp) == to_json(empty));
7374
}
@@ -77,43 +78,152 @@ TEST_CASE("view_or_value", "[bsoncxx::document::view_or_value]") {
7778

7879
SECTION("can be constructed with a value") {
7980
auto move_doc = doc;
80-
view_or_value variant{std::move(move_doc)};
81+
document::view_or_value variant{std::move(move_doc)};
8182

8283
SECTION("can be used as a view") {
8384
REQUIRE(to_json(variant) == json);
8485
}
8586

8687
SECTION("can be copy constructed") {
87-
view_or_value copied{variant};
88+
document::view_or_value copied{variant};
8889
REQUIRE(to_json(copied) == json);
8990
}
9091

9192
SECTION("can be copy assigned") {
92-
view_or_value copied{empty};
93+
document::view_or_value copied{empty};
9394
{
9495
auto temp_doc = doc;
95-
view_or_value temp{std::move(temp_doc)};
96+
document::view_or_value temp{std::move(temp_doc)};
9697
copied = temp;
9798
}
9899
REQUIRE(to_json(copied) == json);
99100
}
100101

101102
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)};
104105
REQUIRE(to_json(moved) == json);
105106
REQUIRE(to_json(temp) == to_json(empty));
106107
}
107108

108109
SECTION("can be move assigned") {
109-
view_or_value moved{empty};
110+
document::view_or_value moved{empty};
110111
{
111112
auto temp_doc = doc;
112-
view_or_value temp{std::move(temp_doc)};
113+
document::view_or_value temp{std::move(temp_doc)};
113114
moved = std::move(temp);
114115
REQUIRE(to_json(temp) == to_json(empty));
115116
}
116117
REQUIRE(to_json(moved) == json);
117118
}
118119
}
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+
}
119229
}

0 commit comments

Comments
 (0)