[SERVER-36785] Add a way to convert STL containers and StringMaps to strings Created: 21/Aug/18  Updated: 29/Oct/23  Resolved: 31/Oct/22

Status: Closed
Project: Core Server
Component/s: Internal Code
Affects Version/s: None
Fix Version/s: 6.2.0-rc0

Type: Improvement Priority: Major - P3
Reporter: Charlie Swanson Assignee: Billy Donahue
Resolution: Fixed Votes: 1
Labels: devtools-to-servicearch
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
is duplicated by SERVER-36784 Add a helper to convert a StringMap t... Closed
Backwards Compatibility: Fully Compatible
Sprint: Service Arch 2022-11-14
Participants:

 Description   

This can help with debugging and logging. If we add an operator<<, I believe we could use things like ASSERT_EQ in test code (which really only makes sense for ordered types like vector or set).



 Comments   
Comment by Githook User [ 31/Oct/22 ]

Author:

{'name': 'Billy Donahue', 'email': 'billy.donahue@mongodb.com', 'username': 'BillyDonahue'}

Message: SERVER-36785 ASSERT pretty-print tuplelike types (and therefore maps)
Branch: master
https://github.com/mongodb/mongo/commit/b19d3e51735cf7fda9ba4aa0ed28e556ccf8cc3d

Comment by Billy Donahue [ 31/Oct/22 ]

Changed to use pythonic (a,b,...) for tuples. This distinguishes them from [a,b,...] sequences.

E.g.:

    ASSERT_EQ(stringifyForAssert(std::map<int, std::string>{{1, "a"}, {2, "b"}}),
              "[(1, a), (2, b)]");

Comment by Billy Donahue [ 29/Oct/22 ]

Confirmed maps don't print so good.

    // Map
    std::map<int, int> myMap{{101, 201}, {102, 202}};
    ASSERT_THAT(myMap,
                ElementsAre(StructuredBindingsAre(Eq(101), Eq(201)),
                            StructuredBindingsAre(Eq(102), Eq(-202))));

{"t":{"$date":"2022-10-29T17:13:50.954Z"},"s":"E",  "c":"TEST",     "id":23070,   "ctx":"main","msg":"Throwing exception","attr":{"exception":"value: myMap, actual: [[std::pair<int const, int>=65 00 00 00 c9 00 00 00], [std::pair<int const, int>=66 00 00 00 ca 00 00 00]], failed: [1:failed: [1:(Eq(-202))]], expected: ElementsAre(StructuredBindingsAre(Eq(101), Eq(201)), StructuredBindingsAre(Eq(102), Eq(-202))) @src/mongo/unittest/assert_that_test.cpp:237"}}

Yuck:

[[std::pair<int const, int>=65 00 00 00 c9 00 00 00], [std::pair<int const, int>=66 00 00 00 ca 00 00 00]]

Will fix.

Comment by Billy Donahue [ 29/Oct/22 ]

Hi spencer.jackson@mongodb.com. Though SERVER-70815 is phrased specifically abut enums, the fix was a little more broad than the ticket.

To get the nicer printing you used to have to use ASSERT_THAT but SERVER-70815 extended it to ASSERT_EQ and friends.

ASSERT_THAT has always been able to match types that don't have operator<< or anything else. It treats pretty printing as optional, and can fall back to ugly printing (hexdump), kind of akin to gdb.

I also made a prettier printer as a fallback for enum classes which might not have an operator<<, to more thoroughly address the problem from the ticket.

I know it supports pretty printing of sequences (anything with a .begin() and .end()). So e.g. std::vector<PRINTABLE> will show up as a nice delimited list. I don't remember if the std::pair and std::tuple are pretty printed nicely or not. It doesn't look like it, so StringMap might print as a delimited sequence of hexdumps. I should test that and fix it. Basically it's just adding another case to this big if/else trait dispatch ladder here:
https://github.com/10gen/mongo/blob/ab34541c92e3ee679ea155b6331253a4e031d451/src/mongo/unittest/stringify.h#L127-L141

Comment by Spencer Jackson [ 28/Oct/22 ]

blake.oler@mongodb.com does SERVER-70815 apply to arbitrary types like std::vector<std::string>, or just to enums? Alternatively, does ASSERT_THAT now allow you to compare types which don't have operator<< defined?

Comment by Lauren Lewis (Inactive) [ 24/Feb/22 ]

We haven’t heard back from you for at least one calendar year, so this issue is being closed. If this is still an issue for you, please provide additional information and we will reopen the ticket.

Comment by Spencer Jackson [ 31/Aug/20 ]

As of Structured Logging, the logging subsystem now knows how to record events from container objects. However, I believe the request still applies for unittests. Because I believe that is maintained by SA, I'm routing there.

Generated at Thu Feb 08 04:44:04 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.