Skip to content

Commit 6239baf

Browse files
authored
operator== and operator!= for QuerySnapshot. (#598)
* operator== and operator!= for QuerySnapshot. * Address comments. * Proper test name.
1 parent 21cb5de commit 6239baf

File tree

8 files changed

+104
-4
lines changed

8 files changed

+104
-4
lines changed

firestore/integration_test_internal/src/query_snapshot_test.cc

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "firebase/firestore.h"
66
#include "firestore/src/common/wrapper_assertions.h"
7+
#include "firestore_integration_test.h"
78
#if defined(__ANDROID__)
89
#include "firestore/src/android/query_snapshot_android.h"
910
#endif // defined(__ANDROID__)
@@ -14,9 +15,9 @@
1415
namespace firebase {
1516
namespace firestore {
1617

17-
#if defined(__ANDROID__)
18+
using QuerySnapshotTest = FirestoreIntegrationTest;
1819

19-
using QuerySnapshotTest = testing::Test;
20+
#if defined(__ANDROID__)
2021

2122
TEST_F(QuerySnapshotTest, Construction) {
2223
testutil::AssertWrapperConstructionContract<QuerySnapshot,
@@ -30,5 +31,59 @@ TEST_F(QuerySnapshotTest, Assignment) {
3031

3132
#endif // defined(__ANDROID__)
3233

34+
TEST_F(QuerySnapshotTest, Equality) {
35+
CollectionReference collection =
36+
Collection({{"a", {{"k", FieldValue::String("a")}}},
37+
{"b", {{"k", FieldValue::String("b")}}},
38+
{"c", {{"k", FieldValue::String("c")}}}});
39+
QuerySnapshot snapshot1 = ReadDocuments(collection.Limit(2));
40+
QuerySnapshot snapshot2 = ReadDocuments(collection.Limit(2));
41+
QuerySnapshot snapshot3 = ReadDocuments(collection.Limit(1));
42+
QuerySnapshot snapshot4 = ReadDocuments(collection);
43+
QuerySnapshot snapshot5 =
44+
ReadDocuments(collection.OrderBy("k", Query::Direction::kAscending));
45+
QuerySnapshot snapshot6 =
46+
ReadDocuments(collection.OrderBy("k", Query::Direction::kDescending));
47+
48+
QuerySnapshot snapshot7 = QuerySnapshot();
49+
QuerySnapshot snapshot8 = QuerySnapshot();
50+
51+
EXPECT_TRUE(snapshot1 == snapshot1);
52+
EXPECT_TRUE(snapshot1 == snapshot2);
53+
EXPECT_TRUE(snapshot1 != snapshot3);
54+
EXPECT_TRUE(snapshot1 != snapshot4);
55+
EXPECT_TRUE(snapshot1 != snapshot5);
56+
EXPECT_TRUE(snapshot1 != snapshot6);
57+
EXPECT_TRUE(snapshot3 != snapshot4);
58+
EXPECT_TRUE(snapshot3 != snapshot5);
59+
EXPECT_TRUE(snapshot3 != snapshot6);
60+
EXPECT_TRUE(snapshot5 != snapshot6);
61+
EXPECT_TRUE(snapshot1 != snapshot7);
62+
EXPECT_TRUE(snapshot2 != snapshot7);
63+
EXPECT_TRUE(snapshot3 != snapshot7);
64+
EXPECT_TRUE(snapshot4 != snapshot7);
65+
EXPECT_TRUE(snapshot5 != snapshot7);
66+
EXPECT_TRUE(snapshot6 != snapshot7);
67+
EXPECT_TRUE(snapshot7 == snapshot8);
68+
69+
EXPECT_FALSE(snapshot1 != snapshot1);
70+
EXPECT_FALSE(snapshot1 != snapshot2);
71+
EXPECT_FALSE(snapshot1 == snapshot3);
72+
EXPECT_FALSE(snapshot1 == snapshot4);
73+
EXPECT_FALSE(snapshot1 == snapshot5);
74+
EXPECT_FALSE(snapshot1 == snapshot6);
75+
EXPECT_FALSE(snapshot3 == snapshot4);
76+
EXPECT_FALSE(snapshot3 == snapshot5);
77+
EXPECT_FALSE(snapshot3 == snapshot6);
78+
EXPECT_FALSE(snapshot5 == snapshot6);
79+
EXPECT_FALSE(snapshot1 == snapshot7);
80+
EXPECT_FALSE(snapshot2 == snapshot7);
81+
EXPECT_FALSE(snapshot3 == snapshot7);
82+
EXPECT_FALSE(snapshot4 == snapshot7);
83+
EXPECT_FALSE(snapshot5 == snapshot7);
84+
EXPECT_FALSE(snapshot6 == snapshot7);
85+
EXPECT_FALSE(snapshot7 != snapshot8);
86+
}
87+
3388
} // namespace firestore
3489
} // namespace firebase

firestore/src/android/query_snapshot_android.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,11 @@ std::size_t QuerySnapshotInternal::size() const {
7272
return env.Call(obj_, kSize);
7373
}
7474

75+
bool operator==(const QuerySnapshotInternal& lhs,
76+
const QuerySnapshotInternal& rhs) {
77+
Env env = FirestoreInternal::GetEnv();
78+
return Object::Equals(env, lhs.ToJava(), rhs.ToJava());
79+
}
80+
7581
} // namespace firestore
7682
} // namespace firebase

firestore/src/android/query_snapshot_android.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ class QuerySnapshotInternal : public Wrapper {
6565
std::size_t size() const;
6666
};
6767

68+
bool operator==(const QuerySnapshotInternal& lhs,
69+
const QuerySnapshotInternal& rhs);
70+
71+
inline bool operator!=(const QuerySnapshotInternal& lhs,
72+
const QuerySnapshotInternal& rhs) {
73+
return !(lhs == rhs);
74+
}
75+
6876
} // namespace firestore
6977
} // namespace firebase
7078

firestore/src/common/query_snapshot.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,13 @@ std::size_t QuerySnapshot::size() const {
103103
return internal_->size();
104104
}
105105

106+
bool operator==(const QuerySnapshot& lhs, const QuerySnapshot& rhs) {
107+
if (!lhs.internal_ || !rhs.internal_) {
108+
return lhs.internal_ == rhs.internal_;
109+
}
110+
111+
return lhs.internal_ == rhs.internal_ || *lhs.internal_ == *rhs.internal_;
112+
}
113+
106114
} // namespace firestore
107115
} // namespace firebase

firestore/src/include/firebase/firestore/query_snapshot.h

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

166166
private:
167+
friend bool operator==(const QuerySnapshot& lhs, const QuerySnapshot& rhs);
168+
167169
friend class EventListenerInternal;
168170
friend class FirestoreInternal;
169171
friend struct ConverterImpl;
@@ -175,6 +177,14 @@ class QuerySnapshot {
175177
mutable QuerySnapshotInternal* internal_ = nullptr;
176178
};
177179

180+
/** Checks `lhs` and `rhs` for equality. */
181+
bool operator==(const QuerySnapshot& lhs, const QuerySnapshot& rhs);
182+
183+
/** Checks `lhs` and `rhs` for inequality. */
184+
inline bool operator!=(const QuerySnapshot& lhs, const QuerySnapshot& rhs) {
185+
return !(lhs == rhs);
186+
}
187+
178188
} // namespace firestore
179189
} // namespace firebase
180190

firestore/src/main/query_snapshot_main.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,10 @@ std::vector<DocumentSnapshot> QuerySnapshotInternal::documents() const {
6262
return documents_.value();
6363
}
6464

65+
bool operator==(const QuerySnapshotInternal& lhs,
66+
const QuerySnapshotInternal& rhs) {
67+
return lhs.snapshot_ == rhs.snapshot_;
68+
}
69+
6570
} // namespace firestore
6671
} // namespace firebase

firestore/src/main/query_snapshot_main.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ class QuerySnapshotInternal {
3838

3939
std::vector<DocumentSnapshot> documents() const;
4040

41+
friend bool operator==(const QuerySnapshotInternal& lhs,
42+
const QuerySnapshotInternal& rhs);
43+
4144
private:
4245
api::QuerySnapshot snapshot_;
4346

@@ -47,6 +50,11 @@ class QuerySnapshotInternal {
4750
mutable bool changes_include_metadata_ = false;
4851
};
4952

53+
inline bool operator!=(const QuerySnapshotInternal& lhs,
54+
const QuerySnapshotInternal& rhs) {
55+
return !(lhs == rhs);
56+
}
57+
5058
} // namespace firestore
5159
} // namespace firebase
5260

release_build_files/readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -569,8 +569,8 @@ code.
569569

570570
### 8.4.0
571571
- Changes
572-
- Firestore: Added `operator==` and `operator!=` for `SnapshotMetadata`
573-
and `Settings`.
572+
- Firestore: Added `operator==` and `operator!=` for `SnapshotMetadata`,
573+
`Settings`, and `QuerySnapshot`.
574574

575575
### 8.3.0
576576
- Changes

0 commit comments

Comments
 (0)