Skip to content

Commit df97911

Browse files
authored
CXX-3082 use from_json and make_array to simplify expectation expressions (#1232)
1 parent cd536b3 commit df97911

27 files changed

+84
-158
lines changed

examples/api/bsoncxx/examples/bson_documents/create_array/builder_append.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <bsoncxx/array/value.hpp>
1818
#include <bsoncxx/array/view.hpp>
1919
#include <bsoncxx/builder/basic/array.hpp>
20-
#include <bsoncxx/types.hpp>
2120

2221
#include <examples/api/runner.hh>
2322
#include <examples/macros.hh>
@@ -30,12 +29,9 @@ void example() {
3029
builder.append(std::int32_t{1});
3130
builder.append(2.0);
3231
builder.append("three");
33-
bsoncxx::array::value owner = builder.extract();
34-
bsoncxx::array::view arr = owner.view();
32+
bsoncxx::array::value arr = builder.extract();
3533

36-
EXPECT(arr[0].get_int32().value == 1);
37-
EXPECT(arr[1].get_double().value == 2.0);
38-
EXPECT(arr[2].get_string().value.compare("three") == 0);
34+
EXPECT(arr.view() == bsoncxx::builder::basic::make_array(1, 2.0, "three"));
3935
}
4036
// [Example]
4137

examples/api/bsoncxx/examples/bson_documents/create_array/builder_basic.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <bsoncxx/array/value.hpp>
1818
#include <bsoncxx/array/view.hpp>
1919
#include <bsoncxx/builder/basic/array.hpp>
20-
#include <bsoncxx/types.hpp>
2120

2221
#include <examples/api/runner.hh>
2322
#include <examples/macros.hh>
@@ -28,12 +27,9 @@ namespace {
2827
void example() {
2928
bsoncxx::builder::basic::array builder;
3029
builder.append(std::int32_t{1}, 2.0, "three");
31-
bsoncxx::array::value owner = builder.extract();
32-
bsoncxx::array::view arr = owner.view();
30+
bsoncxx::array::value arr = builder.extract();
3331

34-
EXPECT(arr[0].get_int32().value == 1);
35-
EXPECT(arr[1].get_double().value == 2.0);
36-
EXPECT(arr[2].get_string().value.compare("three") == 0);
32+
EXPECT(arr.view() == bsoncxx::builder::basic::make_array(1, 2.0, "three"));
3733
}
3834
// [Example]
3935

examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_type.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,9 @@ void example() {
2828
bsoncxx::types::b_double b{2.0};
2929
bsoncxx::types::b_string c{"three"};
3030

31-
bsoncxx::array::value owner = bsoncxx::builder::basic::make_array(a, b, c);
32-
bsoncxx::array::view arr = owner.view();
31+
bsoncxx::array::value arr = bsoncxx::builder::basic::make_array(a, b, c);
3332

34-
EXPECT(arr[0].type() == bsoncxx::type::k_int32);
35-
EXPECT(arr[1].type() == bsoncxx::type::k_double);
36-
EXPECT(arr[2].type() == bsoncxx::type::k_string);
37-
38-
EXPECT(arr[0].get_int32().value == 1);
39-
EXPECT(arr[1].get_double().value == 2.0);
40-
EXPECT(arr[2].get_string().value.compare("three") == 0);
33+
EXPECT(arr.view() == bsoncxx::builder::basic::make_array(1, 2.0, "three"));
4134
}
4235
// [Example]
4336

examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_value.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include <bsoncxx/array/view.hpp>
1919
#include <bsoncxx/builder/basic/array.hpp>
2020
#include <bsoncxx/builder/basic/kvp.hpp>
21-
#include <bsoncxx/types.hpp>
2221
#include <bsoncxx/types/bson_value/value.hpp>
2322

2423
#include <examples/api/runner.hh>
@@ -36,17 +35,10 @@ void example() {
3635
"three",
3736
};
3837

39-
bsoncxx::array::value owner =
38+
bsoncxx::array::value arr =
4039
bsoncxx::builder::basic::make_array(values[0], values[1], values[2]);
41-
bsoncxx::array::view arr = owner.view();
4240

43-
EXPECT(arr[0].type() == bsoncxx::type::k_int32);
44-
EXPECT(arr[1].type() == bsoncxx::type::k_double);
45-
EXPECT(arr[2].type() == bsoncxx::type::k_string);
46-
47-
EXPECT(arr[0].get_value() == values[0]);
48-
EXPECT(arr[1].get_value() == values[1]);
49-
EXPECT(arr[2].get_value() == values[2]);
41+
EXPECT(arr.view() == bsoncxx::builder::basic::make_array(1, 2.0, "three"));
5042
}
5143
// [Example]
5244

examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_value.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include <bsoncxx/array/view.hpp>
2121
#include <bsoncxx/builder/basic/array.hpp>
2222
#include <bsoncxx/document/value.hpp>
23-
#include <bsoncxx/types.hpp>
2423

2524
#include <examples/api/runner.hh>
2625
#include <examples/macros.hh>
@@ -36,11 +35,9 @@ void example(const std::uint8_t* data, std::size_t length) {
3635
std::copy_n(data, length, raw);
3736

3837
deleter_type deleter = [](std::uint8_t* data) { delete[] data; };
39-
bsoncxx::array::value owner{raw, length, deleter};
40-
bsoncxx::array::view arr = owner.view();
38+
bsoncxx::array::value arr{raw, length, deleter};
4139

42-
EXPECT(arr[0].get_int32().value == 1);
43-
EXPECT(arr[1].get_int32().value == 2);
40+
EXPECT(arr.view() == bsoncxx::builder::basic::make_array(1, 2));
4441
}
4542
// [Example]
4643

examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_view.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include <bsoncxx/array/view.hpp>
1919
#include <bsoncxx/builder/basic/array.hpp>
2020
#include <bsoncxx/document/value.hpp>
21-
#include <bsoncxx/types.hpp>
2221

2322
#include <examples/api/runner.hh>
2423
#include <examples/macros.hh>
@@ -30,8 +29,7 @@ namespace {
3029
void example(const std::uint8_t* data, std::size_t length) {
3130
bsoncxx::array::view arr{data, length};
3231

33-
EXPECT(arr[0].get_int32().value == 1);
34-
EXPECT(arr[1].get_int32().value == 2);
32+
EXPECT(arr == bsoncxx::builder::basic::make_array(1, 2));
3533
}
3634
// [Example]
3735

examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_array.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,12 @@ namespace {
2727
void example() {
2828
using bsoncxx::builder::basic::make_array;
2929

30-
bsoncxx::array::value owner = make_array(make_array(std::int32_t{1}, std::int64_t{2}));
31-
bsoncxx::array::view v = owner.view()[0].get_array().value;
30+
bsoncxx::array::value arr = make_array(1, 2.0, "three");
3231

33-
EXPECT(v[0].type() == bsoncxx::type::k_int32);
34-
EXPECT(v[1].type() == bsoncxx::type::k_int64);
32+
bsoncxx::array::value owner = make_array(arr.view());
33+
bsoncxx::array::view v = owner.view()[0].get_array().value;
3534

36-
EXPECT(v[0].get_int32().value == 1);
37-
EXPECT(v[1].get_int64().value == 2);
35+
EXPECT(v == arr.view());
3836
}
3937
// [Example]
4038

examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_array_append.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,18 @@ namespace {
2525

2626
// [Example]
2727
void example() {
28+
using bsoncxx::builder::basic::make_array;
29+
2830
std::int32_t values[] = {1, 2, 3};
2931

30-
bsoncxx::array::value owner =
31-
bsoncxx::builder::basic::make_array([&](bsoncxx::builder::basic::sub_array arr) {
32-
for (int i = 0; i < 3; ++i) {
33-
arr.append(values[i]);
34-
}
35-
});
32+
bsoncxx::array::value owner = make_array([&](bsoncxx::builder::basic::sub_array arr) {
33+
for (int i = 0; i < 3; ++i) {
34+
arr.append(values[i]);
35+
}
36+
});
3637
bsoncxx::array::view v = owner.view()[0].get_array().value;
3738

38-
EXPECT(v[0].get_int32().value == 1);
39-
EXPECT(v[1].get_int32().value == 2);
40-
EXPECT(v[2].get_int32().value == 3);
39+
EXPECT(v == make_array(1, 2, 3));
4140
}
4241
// [Example]
4342

examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include <bsoncxx/builder/basic/document.hpp>
1717
#include <bsoncxx/builder/basic/kvp.hpp>
1818
#include <bsoncxx/document/view.hpp>
19+
#include <bsoncxx/json.hpp>
20+
#include <bsoncxx/types.hpp>
1921

2022
#include <examples/api/runner.hh>
2123
#include <examples/macros.hh>
@@ -28,10 +30,12 @@ void example() {
2830
using bsoncxx::builder::basic::make_array;
2931
using bsoncxx::builder::basic::make_document;
3032

31-
bsoncxx::array::value owner = make_array(make_document(kvp("key", "value")));
33+
bsoncxx::document::value subdoc = make_document(kvp("key", "value"));
34+
35+
bsoncxx::array::value owner = make_array(subdoc.view());
3236
bsoncxx::document::view v = owner.view()[0].get_document().value;
3337

34-
EXPECT(v["key"].get_string().value.compare("value") == 0);
38+
EXPECT(v == subdoc.view());
3539
}
3640
// [Example]
3741

examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document_append.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <bsoncxx/builder/basic/kvp.hpp>
2020
#include <bsoncxx/builder/basic/sub_document.hpp>
2121
#include <bsoncxx/document/view.hpp>
22+
#include <bsoncxx/json.hpp>
2223

2324
#include <examples/api/runner.hh>
2425
#include <examples/macros.hh>
@@ -40,9 +41,7 @@ void example() {
4041
});
4142
bsoncxx::document::view v = owner.view()[0].get_document().value;
4243

43-
EXPECT(v["a"].get_int32().value == 1);
44-
EXPECT(v["b"].get_int32().value == 2);
45-
EXPECT(v["c"].get_int32().value == 3);
44+
EXPECT(v == bsoncxx::from_json(R"({"a": 1, "b": 2, "c": 3})"));
4645
}
4746
// [Example]
4847

examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_array.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
#include <bsoncxx/array/view.hpp>
16+
#include <bsoncxx/builder/basic/array.hpp>
1617
#include <bsoncxx/document/value.hpp>
1718
#include <bsoncxx/json.hpp>
1819
#include <bsoncxx/types.hpp>
@@ -24,15 +25,14 @@ namespace {
2425

2526
// [Example]
2627
void example() {
27-
bsoncxx::document::value owner = bsoncxx::from_json(R"(
28+
bsoncxx::document::value doc = bsoncxx::from_json(R"(
2829
[
29-
[1, 2]
30+
[1, 2.0, "three"]
3031
]
3132
)");
32-
bsoncxx::array::view sub = owner.view()["0"].get_array().value;
33+
bsoncxx::array::view sub = doc.view()["0"].get_array().value;
3334

34-
EXPECT(sub[0].get_int32().value == 1);
35-
EXPECT(sub[1].get_int32().value == 2);
35+
EXPECT(sub == bsoncxx::builder::basic::make_array(1, 2.0, "three"));
3636
}
3737
// [Example]
3838

examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_document.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ namespace {
2424

2525
// [Example]
2626
void example() {
27-
bsoncxx::document::value owner = bsoncxx::from_json(R"(
27+
bsoncxx::document::value doc = bsoncxx::from_json(R"(
2828
[
2929
{"key": "value"}
3030
]
3131
)");
32-
bsoncxx::document::view v = owner.view()["0"].get_document().value;
32+
bsoncxx::document::view v = doc.view()["0"].get_document().value;
3333

34-
EXPECT(v["key"].get_string().value.compare("value") == 0);
34+
EXPECT(v == bsoncxx::from_json(R"({"key": "value"})"));
3535
}
3636
// [Example]
3737

examples/api/bsoncxx/examples/bson_documents/create_doc/builder_append.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
#include <bsoncxx/builder/basic/document.hpp>
1818
#include <bsoncxx/builder/basic/kvp.hpp>
1919
#include <bsoncxx/document/value.hpp>
20-
#include <bsoncxx/document/view.hpp>
21-
#include <bsoncxx/types.hpp>
20+
#include <bsoncxx/json.hpp>
2221

2322
#include <examples/api/runner.hh>
2423
#include <examples/macros.hh>
@@ -33,12 +32,9 @@ void example() {
3332
builder.append(kvp("a", std::int32_t{1}));
3433
builder.append(kvp("b", 2.0));
3534
builder.append(kvp("c", "three"));
36-
bsoncxx::document::value owner = builder.extract();
37-
bsoncxx::document::view doc = owner.view();
35+
bsoncxx::document::value doc = builder.extract();
3836

39-
EXPECT(doc["a"].get_int32().value == 1);
40-
EXPECT(doc["b"].get_double().value == 2.0);
41-
EXPECT(doc["c"].get_string().value.compare("three") == 0);
37+
EXPECT(doc == bsoncxx::from_json(R"({"a": 1, "b": 2.0, "c": "three"})"));
4238
}
4339
// [Example]
4440

examples/api/bsoncxx/examples/bson_documents/create_doc/builder_basic.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
#include <bsoncxx/builder/basic/document.hpp>
1818
#include <bsoncxx/builder/basic/kvp.hpp>
1919
#include <bsoncxx/document/value.hpp>
20-
#include <bsoncxx/document/view.hpp>
21-
#include <bsoncxx/types.hpp>
20+
#include <bsoncxx/json.hpp>
2221

2322
#include <examples/api/runner.hh>
2423
#include <examples/macros.hh>
@@ -31,12 +30,9 @@ void example() {
3130

3231
bsoncxx::builder::basic::document builder;
3332
builder.append(kvp("a", std::int32_t{1}), kvp("b", 2.0), kvp("c", "three"));
34-
bsoncxx::document::value owner = builder.extract();
35-
bsoncxx::document::view doc = owner.view();
33+
bsoncxx::document::value doc = builder.extract();
3634

37-
EXPECT(doc["a"].get_int32().value == 1);
38-
EXPECT(doc["b"].get_double().value == 2.0);
39-
EXPECT(doc["c"].get_string().value.compare("three") == 0);
35+
EXPECT(doc.view() == bsoncxx::from_json(R"({"a": 1, "b": 2.0, "c": "three"})"));
4036
}
4137
// [Example]
4238

examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_type.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <bsoncxx/builder/basic/document.hpp>
1818
#include <bsoncxx/builder/basic/kvp.hpp>
1919
#include <bsoncxx/document/value.hpp>
20-
#include <bsoncxx/document/view.hpp>
20+
#include <bsoncxx/json.hpp>
2121
#include <bsoncxx/types.hpp>
2222

2323
#include <examples/api/runner.hh>
@@ -33,17 +33,10 @@ void example() {
3333
bsoncxx::types::b_double b{2.0};
3434
bsoncxx::types::b_string c{"three"};
3535

36-
bsoncxx::document::value owner =
36+
bsoncxx::document::value doc =
3737
bsoncxx::builder::basic::make_document(kvp("a", a), kvp("b", b), kvp("c", c));
38-
bsoncxx::document::view doc = owner.view();
3938

40-
EXPECT(doc["a"].type() == bsoncxx::type::k_int32);
41-
EXPECT(doc["b"].type() == bsoncxx::type::k_double);
42-
EXPECT(doc["c"].type() == bsoncxx::type::k_string);
43-
44-
EXPECT(doc["a"].get_int32() == a);
45-
EXPECT(doc["b"].get_double() == b);
46-
EXPECT(doc["c"].get_string() == c);
39+
EXPECT(doc.view() == bsoncxx::from_json(R"({"a": 1, "b": 2.0, "c": "three"})"));
4740
}
4841
// [Example]
4942

examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_value.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
#include <bsoncxx/builder/basic/document.hpp>
1818
#include <bsoncxx/builder/basic/kvp.hpp>
1919
#include <bsoncxx/document/value.hpp>
20-
#include <bsoncxx/document/view.hpp>
21-
#include <bsoncxx/types.hpp>
20+
#include <bsoncxx/json.hpp>
2221
#include <bsoncxx/types/bson_value/value.hpp>
2322

2423
#include <examples/api/runner.hh>
@@ -36,17 +35,10 @@ void example() {
3635
"three",
3736
};
3837

39-
bsoncxx::document::value owner = bsoncxx::builder::basic::make_document(
38+
bsoncxx::document::value doc = bsoncxx::builder::basic::make_document(
4039
kvp("a", values[0]), kvp("b", values[1]), kvp("c", values[2]));
41-
bsoncxx::document::view doc = owner.view();
4240

43-
EXPECT(doc["a"].type() == bsoncxx::type::k_int32);
44-
EXPECT(doc["b"].type() == bsoncxx::type::k_double);
45-
EXPECT(doc["c"].type() == bsoncxx::type::k_string);
46-
47-
EXPECT(doc["a"].get_value() == values[0]);
48-
EXPECT(doc["b"].get_value() == values[1]);
49-
EXPECT(doc["c"].get_value() == values[2]);
41+
EXPECT(doc.view() == bsoncxx::from_json(R"({"a": 1, "b": 2.0, "c": "three"})"));
5042
}
5143
// [Example]
5244

0 commit comments

Comments
 (0)