Skip to content

Commit c85d0ed

Browse files
authored
operator== and operator!= for DocumentChange (#607)
* operator== and operator!= for DocumentChange. * delegate to underlying impl. * Address comments. * Update readme. * Use ASSERT_EQ in a few more instances. * rename third_change to change3.
1 parent 8925603 commit c85d0ed

File tree

8 files changed

+111
-6
lines changed

8 files changed

+111
-6
lines changed

firestore/integration_test_internal/src/document_change_test.cc

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
namespace firebase {
2828
namespace firestore {
2929

30-
#if defined(__ANDROID__)
31-
3230
using DocumentChangeTest = FirestoreIntegrationTest;
3331

32+
#if defined(__ANDROID__)
33+
3434
TEST_F(DocumentChangeTest, Construction) {
3535
testutil::AssertWrapperConstructionContract<DocumentChange,
3636
DocumentChangeInternal>();
@@ -59,7 +59,7 @@ TEST_F(DocumentChangeTest, TestDocumentChanges) {
5959
snapshot = listener.last_result();
6060

6161
std::vector<DocumentChange> changes = snapshot.DocumentChanges();
62-
EXPECT_EQ(changes.size(), 1);
62+
ASSERT_EQ(changes.size(), 1);
6363

6464
EXPECT_EQ(changes[0].type(), DocumentChange::Type::kAdded);
6565
EXPECT_EQ(changes[0].document().id(), doc1.id());
@@ -71,7 +71,7 @@ TEST_F(DocumentChangeTest, TestDocumentChanges) {
7171
snapshot = listener.last_result();
7272

7373
changes = snapshot.DocumentChanges();
74-
EXPECT_EQ(changes.size(), 1);
74+
ASSERT_EQ(changes.size(), 1);
7575
EXPECT_EQ(changes[0].type(), DocumentChange::Type::kAdded);
7676
EXPECT_EQ(changes[0].document().id(), doc2.id());
7777
EXPECT_EQ(changes[0].old_index(), DocumentChange::npos);
@@ -83,7 +83,7 @@ TEST_F(DocumentChangeTest, TestDocumentChanges) {
8383
snapshot = listener.last_result();
8484

8585
changes = snapshot.DocumentChanges();
86-
EXPECT_EQ(changes.size(), 1);
86+
ASSERT_EQ(changes.size(), 1);
8787
EXPECT_EQ(changes[0].type(), DocumentChange::Type::kModified);
8888
EXPECT_EQ(changes[0].document().id(), doc2.id());
8989
EXPECT_EQ(changes[0].old_index(), 1);
@@ -92,5 +92,67 @@ TEST_F(DocumentChangeTest, TestDocumentChanges) {
9292

9393
#endif // defined(__ANDROID__)
9494

95+
TEST_F(DocumentChangeTest, Equality) {
96+
DocumentChange invalid_change_1 = DocumentChange();
97+
DocumentChange invalid_change_2 = DocumentChange();
98+
99+
EXPECT_TRUE(invalid_change_1 == invalid_change_2);
100+
EXPECT_FALSE(invalid_change_1 != invalid_change_2);
101+
102+
CollectionReference collection = Collection();
103+
Query query = collection.OrderBy("a");
104+
105+
DocumentReference doc1 = collection.Document("1");
106+
DocumentReference doc2 = collection.Document("2");
107+
108+
TestEventListener<QuerySnapshot> listener("TestDocumentChanges");
109+
ListenerRegistration registration = listener.AttachTo(&query);
110+
Await(listener);
111+
QuerySnapshot snapshot = listener.last_result();
112+
EXPECT_EQ(snapshot.size(), 0);
113+
114+
WriteDocument(doc1, MapFieldValue{{"a", FieldValue::Integer(1)}});
115+
Await(listener);
116+
snapshot = listener.last_result();
117+
118+
std::vector<DocumentChange> changes = snapshot.DocumentChanges();
119+
ASSERT_EQ(changes.size(), 1);
120+
// First change: Added doc1.
121+
auto change1 = changes[0];
122+
EXPECT_TRUE(change1 == change1);
123+
EXPECT_TRUE(change1 != invalid_change_1);
124+
EXPECT_FALSE(change1 != change1);
125+
EXPECT_FALSE(change1 == invalid_change_1);
126+
127+
WriteDocument(doc2, MapFieldValue{{"a", FieldValue::Integer(2)}});
128+
Await(listener, 2);
129+
snapshot = listener.last_result();
130+
131+
changes = snapshot.DocumentChanges();
132+
ASSERT_EQ(changes.size(), 1);
133+
// Second change: Added doc2.
134+
auto change2 = changes[0];
135+
EXPECT_TRUE(change2 != change1);
136+
EXPECT_TRUE(change2 != invalid_change_1);
137+
EXPECT_FALSE(change2 == change1);
138+
EXPECT_FALSE(change2 == invalid_change_1);
139+
140+
// Make doc2 ordered before doc1.
141+
WriteDocument(doc2, MapFieldValue{{"a", FieldValue::Integer(0)}});
142+
Await(listener, 3);
143+
snapshot = listener.last_result();
144+
145+
changes = snapshot.DocumentChanges();
146+
ASSERT_EQ(changes.size(), 1);
147+
// Third change: Modified doc2.
148+
auto change3 = changes[0];
149+
EXPECT_TRUE(change3 != change1);
150+
EXPECT_TRUE(change3 != change2);
151+
EXPECT_TRUE(change3 != invalid_change_1);
152+
EXPECT_FALSE(change3 == change1);
153+
EXPECT_FALSE(change3 == change2);
154+
EXPECT_FALSE(change3 == invalid_change_1);
155+
}
156+
95157
} // namespace firestore
96158
} // namespace firebase

firestore/src/android/document_change_android.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "firestore/src/android/document_change_type_android.h"
2020
#include "firestore/src/android/document_snapshot_android.h"
21+
#include "firestore/src/jni/compare.h"
2122
#include "firestore/src/jni/env.h"
2223
#include "firestore/src/jni/loader.h"
2324

@@ -69,5 +70,10 @@ std::size_t DocumentChangeInternal::new_index() const {
6970
return env.Call(obj_, kNewIndex);
7071
}
7172

73+
bool operator==(const DocumentChangeInternal& lhs,
74+
const DocumentChangeInternal& rhs) {
75+
return jni::EqualityCompareJni(lhs, rhs);
76+
}
77+
7278
} // namespace firestore
7379
} // namespace firebase

firestore/src/android/document_change_android.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ class DocumentChangeInternal : public Wrapper {
3838
std::size_t new_index() const;
3939
};
4040

41+
bool operator==(const DocumentChangeInternal& lhs,
42+
const DocumentChangeInternal& rhs);
43+
44+
inline bool operator!=(const DocumentChangeInternal& lhs,
45+
const DocumentChangeInternal& rhs) {
46+
return !(lhs == rhs);
47+
}
48+
4149
} // namespace firestore
4250
} // namespace firebase
4351

firestore/src/common/document_change.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "firestore/src/common/cleanup.h"
2222
#include "firestore/src/common/hard_assert_common.h"
23+
#include "firestore/src/common/util.h"
2324
#include "firestore/src/include/firebase/firestore/document_snapshot.h"
2425
#if defined(__ANDROID__)
2526
#include "firestore/src/android/document_change_android.h"
@@ -118,5 +119,9 @@ std::size_t DocumentChange::new_index() const {
118119
return internal_->new_index();
119120
}
120121

122+
bool operator==(const DocumentChange& lhs, const DocumentChange& rhs) {
123+
return EqualityCompare(lhs.internal_, rhs.internal_);
124+
}
125+
121126
} // namespace firestore
122127
} // namespace firebase

firestore/src/include/firebase/firestore/document_change.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ class DocumentChange {
173173
bool is_valid() const { return internal_ != nullptr; }
174174

175175
private:
176+
friend bool operator==(const DocumentChange& lhs, const DocumentChange& rhs);
177+
176178
friend class FirestoreInternal;
177179
friend class Wrapper;
178180
friend struct ConverterImpl;
@@ -184,6 +186,14 @@ class DocumentChange {
184186
mutable DocumentChangeInternal* internal_ = nullptr;
185187
};
186188

189+
/** Checks `lhs` and `rhs` for equality. */
190+
bool operator==(const DocumentChange& lhs, const DocumentChange& rhs);
191+
192+
/** Checks `lhs` and `rhs` for inequality. */
193+
inline bool operator!=(const DocumentChange& lhs, const DocumentChange& rhs) {
194+
return !(lhs == rhs);
195+
}
196+
187197
} // namespace firestore
188198
} // namespace firebase
189199

firestore/src/main/document_change_main.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,10 @@ std::size_t DocumentChangeInternal::new_index() const {
6060
return change_.new_index();
6161
}
6262

63+
bool operator==(const DocumentChangeInternal& lhs,
64+
const DocumentChangeInternal& rhs) {
65+
return lhs.change_ == rhs.change_;
66+
}
67+
6368
} // namespace firestore
6469
} // namespace firebase

firestore/src/main/document_change_main.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,18 @@ class DocumentChangeInternal {
4141
std::size_t old_index() const;
4242
std::size_t new_index() const;
4343

44+
friend bool operator==(const DocumentChangeInternal& lhs,
45+
const DocumentChangeInternal& rhs);
46+
4447
private:
4548
api::DocumentChange change_;
4649
};
4750

51+
inline bool operator!=(const DocumentChangeInternal& lhs,
52+
const DocumentChangeInternal& rhs) {
53+
return !(lhs == rhs);
54+
}
55+
4856
} // namespace firestore
4957
} // namespace firebase
5058

release_build_files/readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,8 @@ code.
571571
- Changes
572572
- General: Updating Android and iOS dependencies to the latest.
573573
- Firestore: Added `operator==` and `operator!=` for `SnapshotMetadata`,
574-
`Settings`, `QuerySnapshot`, `DocumentSnapshot`, and `SetOptions`.
574+
`Settings`, `QuerySnapshot`, `DocumentSnapshot`, `SetOptions`, and
575+
`DocumentChange`.
575576

576577
### 8.3.0
577578
- Changes

0 commit comments

Comments
 (0)