1 
2 /**
3  *    Copyright (C) 2018-present MongoDB, Inc.
4  *
5  *    This program is free software: you can redistribute it and/or modify
6  *    it under the terms of the Server Side Public License, version 1,
7  *    as published by MongoDB, Inc.
8  *
9  *    This program is distributed in the hope that it will be useful,
10  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *    Server Side Public License for more details.
13  *
14  *    You should have received a copy of the Server Side Public License
15  *    along with this program. If not, see
16  *    <http://www.mongodb.com/licensing/server-side-public-license>.
17  *
18  *    As a special exception, the copyright holders give permission to link the
19  *    code of portions of this program with the OpenSSL library under certain
20  *    conditions as described in each individual source file and distribute
21  *    linked combinations including the program with the OpenSSL library. You
22  *    must comply with the Server Side Public License in all respects for
23  *    all of the code used other than as permitted herein. If you modify file(s)
24  *    with this exception, you may extend this exception to your version of the
25  *    file(s), but you are not obligated to do so. If you do not wish to do so,
26  *    delete this exception statement from your version. If you delete this
27  *    exception statement from all source files in the program, then also delete
28  *    it in the license file.
29  */
30 
31 #include <iosfwd>
32 
33 #include "mongo/bson/mutable/document.h"
34 
35 namespace mongo {
36 
37 class BSONObj;
38 
39 namespace mutablebson {
40 
41 //
42 // Utilities for mutable BSON unit tests.
43 //
44 
45 /**
46  * Catch all comparator between a mutable 'doc' and the expected BSON 'exp'. It compares
47  * (a) 'doc's generated object, (b) 'exp', the expected object, and (c) 'doc(exp)', a
48  * document created from 'exp'. Returns true if all three are equal, otherwise false.
49  */
50 bool checkDoc(const Document& lhs, const BSONObj& rhs);
51 bool checkDoc(const Document& lhs, const Document& rhs);
52 
53 inline bool operator==(const Document& lhs, const Document& rhs) {
54     return checkDoc(lhs, rhs);
55 }
56 
57 inline bool operator==(const BSONObj& lhs, const Document& rhs) {
58     return checkDoc(rhs, lhs);
59 }
60 
61 inline bool operator==(const Document& lhs, const BSONObj& rhs) {
62     return checkDoc(lhs, rhs);
63 }
64 
65 /** Stream out an constelement; useful within ASSERT calls */
66 std::ostream& operator<<(std::ostream& stream, const ConstElement& elt);
67 
68 /** Stream out a document; useful within ASSERT calls */
69 std::ostream& operator<<(std::ostream& stream, const Document& doc);
70 
71 /** Stream out an element; useful within ASSERT calls */
72 std::ostream& operator<<(std::ostream& stream, const Element& elt);
73 
74 /** Check that the two provided Documents are equivalent modulo field ordering in Object
75  *  Elements. Leaf values are considered equal via woCompare.
76  */
77 bool checkEqualNoOrdering(const Document& lhs, const Document& rhs);
78 
79 struct UnorderedWrapper_Obj {
UnorderedWrapper_ObjUnorderedWrapper_Obj80     inline explicit UnorderedWrapper_Obj(const BSONObj& o) : obj(o) {}
81     const BSONObj& obj;
82 };
83 
84 struct UnorderedWrapper_Doc {
UnorderedWrapper_DocUnorderedWrapper_Doc85     inline explicit UnorderedWrapper_Doc(const Document& d) : doc(d) {}
86     const Document& doc;
87 };
88 
unordered(const Document & d)89 inline UnorderedWrapper_Doc unordered(const Document& d) {
90     return UnorderedWrapper_Doc(d);
91 }
92 
unordered(const BSONObj & o)93 inline UnorderedWrapper_Obj unordered(const BSONObj& o) {
94     return UnorderedWrapper_Obj(o);
95 }
96 
97 inline bool operator==(const UnorderedWrapper_Doc& lhs, const UnorderedWrapper_Doc& rhs) {
98     return checkEqualNoOrdering(lhs.doc, rhs.doc);
99 }
100 
101 inline bool operator!=(const UnorderedWrapper_Doc& lhs, const UnorderedWrapper_Doc& rhs) {
102     return !(lhs == rhs);
103 }
104 
105 
106 inline bool operator==(const UnorderedWrapper_Obj& lhs, const UnorderedWrapper_Obj& rhs) {
107     const Document dlhs(lhs.obj);
108     const Document drhs(rhs.obj);
109     return checkEqualNoOrdering(dlhs, drhs);
110 }
111 
112 inline bool operator!=(const UnorderedWrapper_Obj& lhs, const UnorderedWrapper_Obj& rhs) {
113     return !(lhs == rhs);
114 }
115 
116 
117 inline bool operator==(const UnorderedWrapper_Doc& lhs, const UnorderedWrapper_Obj& rhs) {
118     const Document drhs(rhs.obj);
119     return checkEqualNoOrdering(lhs.doc, drhs);
120 }
121 
122 inline bool operator!=(const UnorderedWrapper_Doc& lhs, const UnorderedWrapper_Obj& rhs) {
123     return !(lhs == rhs);
124 }
125 
126 
127 inline bool operator==(const UnorderedWrapper_Obj& lhs, const UnorderedWrapper_Doc& rhs) {
128     const Document dlhs(lhs.obj);
129     return checkEqualNoOrdering(dlhs, rhs.doc);
130 }
131 
132 inline bool operator!=(const UnorderedWrapper_Obj& lhs, const UnorderedWrapper_Doc& rhs) {
133     return !(lhs == rhs);
134 }
135 
136 std::ostream& operator<<(std::ostream& stream, const UnorderedWrapper_Doc& uw_d);
137 std::ostream& operator<<(std::ostream& stream, const UnorderedWrapper_Obj& uw_o);
138 
139 }  // namespace mutablebson
140 }  // namespace mongo
141