diff options
author | Davis Haupt <davis.haupt@mongodb.com> | 2023-03-07 15:01:45 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2023-03-07 15:57:19 +0000 |
commit | a54d49ec73af71f576b4f74d61b9ad2420979e03 (patch) | |
tree | 9fe63ad9a2cf39a83a37e1d8b4aa30ddb4f4b66f /src/mongo | |
parent | caeb2cb757c619159ef95b0b76426c141c1591c6 (diff) | |
download | mongo-a54d49ec73af71f576b4f74d61b9ad2420979e03.tar.gz |
SERVER-74655 Add ASSERT_BSONOBJ_EQ_AUTO auto-updating assertion
Diffstat (limited to 'src/mongo')
-rw-r--r-- | src/mongo/unittest/SConscript | 1 | ||||
-rw-r--r-- | src/mongo/unittest/bson_test_util.cpp | 10 | ||||
-rw-r--r-- | src/mongo/unittest/bson_test_util.h | 9 | ||||
-rw-r--r-- | src/mongo/unittest/bson_test_util_test.cpp | 63 | ||||
-rw-r--r-- | src/mongo/unittest/inline_auto_update.cpp | 15 | ||||
-rw-r--r-- | src/mongo/unittest/inline_auto_update.h | 3 |
6 files changed, 89 insertions, 12 deletions
diff --git a/src/mongo/unittest/SConscript b/src/mongo/unittest/SConscript index e03c2067369..0d1dfc44e67 100644 --- a/src/mongo/unittest/SConscript +++ b/src/mongo/unittest/SConscript @@ -127,6 +127,7 @@ env.CppUnitTest( 'thread_assertion_monitor_test.cpp', 'assert_that_test.cpp', 'inline_auto_update_test.cpp', + 'bson_test_util_test.cpp', ], ) diff --git a/src/mongo/unittest/bson_test_util.cpp b/src/mongo/unittest/bson_test_util.cpp index 11f504a8f99..a4aed598d55 100644 --- a/src/mongo/unittest/bson_test_util.cpp +++ b/src/mongo/unittest/bson_test_util.cpp @@ -30,6 +30,7 @@ #include "mongo/platform/basic.h" #include "mongo/unittest/bson_test_util.h" +#include "mongo/unittest/inline_auto_update.h" namespace mongo { namespace unittest { @@ -83,5 +84,14 @@ GENERATE_BSON_CMP_FUNC(BSONElement, GT, SimpleBSONElementComparator::kInstance, GENERATE_BSON_CMP_FUNC(BSONElement, GTE, SimpleBSONElementComparator::kInstance, >=); GENERATE_BSON_CMP_FUNC(BSONElement, NE, SimpleBSONElementComparator::kInstance, !=); +std::string makeJsonStr(const BSONObj& obj) { + const static JsonStringFormat format = JsonStringFormat::ExtendedRelaxedV2_0_0; + std::string str = obj.jsonString(format); + if (str.size() > kAutoUpdateMaxLineLength) { + str = obj.jsonString(format, 1); + } + return str::stream() << "fromjson(R\"(" << str << ")\")"; +} + } // namespace unittest } // namespace mongo diff --git a/src/mongo/unittest/bson_test_util.h b/src/mongo/unittest/bson_test_util.h index fb8bfe4ce3b..e75c4da0058 100644 --- a/src/mongo/unittest/bson_test_util.h +++ b/src/mongo/unittest/bson_test_util.h @@ -32,6 +32,7 @@ #include "mongo/bson/simple_bsonelement_comparator.h" #include "mongo/bson/simple_bsonobj_comparator.h" #include "mongo/unittest/assert.h" +#include "mongo/unittest/inline_auto_update.h" /** * BSON comparison utility macro. Do not use directly. @@ -90,5 +91,13 @@ DECLARE_BSON_CMP_FUNC(BSONElement, GTE); DECLARE_BSON_CMP_FUNC(BSONElement, NE); #undef DECLARE_BSON_CMP_FUNC +/** + * Given a BSONObj, return a string that wraps the json form of the BSONObj with + * `fromjson(R"(<>)")`. + */ +std::string makeJsonStr(const BSONObj& obj); + +#define ASSERT_BSONOBJ_EQ_AUTO(expected, actual) \ + ASSERT(AUTO_UPDATE_HELPER(makeJsonStr(expected), makeJsonStr(actual), false)) } // namespace unittest } // namespace mongo diff --git a/src/mongo/unittest/bson_test_util_test.cpp b/src/mongo/unittest/bson_test_util_test.cpp new file mode 100644 index 00000000000..666dc1a37a6 --- /dev/null +++ b/src/mongo/unittest/bson_test_util_test.cpp @@ -0,0 +1,63 @@ +/** + * Copyright (C) 2023-present MongoDB, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the Server Side Public License, version 1, + * as published by MongoDB, Inc. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Server Side Public License for more details. + * + * You should have received a copy of the Server Side Public License + * along with this program. If not, see + * <http://www.mongodb.com/licensing/server-side-public-license>. + * + * As a special exception, the copyright holders give permission to link the + * code of portions of this program with the OpenSSL library under certain + * conditions as described in each individual source file and distribute + * linked combinations including the program with the OpenSSL library. You + * must comply with the Server Side Public License in all respects for + * all of the code used other than as permitted herein. If you modify file(s) + * with this exception, you may extend this exception to your version of the + * file(s), but you are not obligated to do so. If you do not wish to do so, + * delete this exception statement from your version. If you delete this + * exception statement from all source files in the program, then also delete + * it in the license file. + */ + +#include "mongo/bson/json.h" +#include "mongo/unittest/unittest.h" + +namespace mongo::unittest { +namespace { +TEST(AutoUpdateAssertion, BSONTest) { + // BSONObj that fits on a single line. + auto actual = fromjson("{hello: 'world'}"); + ASSERT_BSONOBJ_EQ_AUTO( // NOLINT + fromjson(R"({"hello":"world"})"), + actual); + + // BSONObj that spills over to multiple lines. + actual = fromjson( + "{hello: 'world', nested: {key1: 1, nested: {key2: 3, key4: 'some string here', nested: " + "{anotherLevel: true}}}}"); + ASSERT_BSONOBJ_EQ_AUTO( // NOLINT + fromjson(R"({ + "hello": "world", + "nested": { + "key1": 1, + "nested": { + "key2": 3, + "key4": "some string here", + "nested": { + "anotherLevel": true + } + } + } + })"), + actual); +} +} // namespace +} // namespace mongo::unittest diff --git a/src/mongo/unittest/inline_auto_update.cpp b/src/mongo/unittest/inline_auto_update.cpp index a179ecf9d69..1e6739168bf 100644 --- a/src/mongo/unittest/inline_auto_update.cpp +++ b/src/mongo/unittest/inline_auto_update.cpp @@ -58,9 +58,6 @@ std::vector<std::string> formatStr(const std::string& str, const bool needsEscap std::vector<std::string> replacementLines; const bool endsWithNewline = str.back() == '\n'; - // Account for maximum line length after linting. We need to indent, add quotes, etc. - static constexpr size_t kEscapedLength = 88; - std::vector<std::string> inputLines; std::istringstream inputStream(str); std::string line; @@ -75,15 +72,15 @@ std::vector<std::string> formatStr(const std::string& str, const bool needsEscap for (;;) { // If the line is estimated to exceed the maximum length allowed by the linter, break it // up and make sure to insert '\n' only at the end of the last segment. - const bool breakupLine = needsEscaping && escaped.size() > kEscapedLength; + const bool breakupLine = needsEscaping && escaped.size() > kAutoUpdateMaxLineLength; - size_t lineLength = kEscapedLength; + size_t lineLength = needsEscaping ? kAutoUpdateMaxLineLength : escaped.size(); if (breakupLine) { // Attempt to break the line at the last space. lineLength = escaped.find_last_of(' ', lineLength); if (lineLength == std::string::npos) { // Line does not have any spaces. - lineLength = kEscapedLength; + lineLength = kAutoUpdateMaxLineLength; } else { lineLength++; } @@ -119,12 +116,6 @@ std::vector<std::string> formatStr(const std::string& str, const bool needsEscap // Account for the fact that we need an extra comma after the string constant in the macro. auto& lastLine = replacementLines.back(); lastLine.insert(lastLine.size() - 1, ","); - - if (replacementLines.size() == 1) { - // For single lines, add a 'nolint' comment to prevent the linter from inlining the - // single line with the macro itself. - lastLine.insert(lastLine.size() - 1, " // NOLINT (test auto-update)"); - } } return replacementLines; diff --git a/src/mongo/unittest/inline_auto_update.h b/src/mongo/unittest/inline_auto_update.h index e16f214d902..535b9cef8e4 100644 --- a/src/mongo/unittest/inline_auto_update.h +++ b/src/mongo/unittest/inline_auto_update.h @@ -49,6 +49,9 @@ bool handleAutoUpdate(const std::string& expected, size_t lineNumber, bool needsEscaping); +// Account for maximum line length after linting. We need to indent, add quotes, etc. +static constexpr size_t kAutoUpdateMaxLineLength = 88; + /** * Auto update result back in the source file if the assert fails. * The expected result must be a multi-line string in the following form: |