Skip to content

Commit 21cb5de

Browse files
authored
operator== and operator!= for SnapshotMetadata and Settings (#596)
* Add operator== and operator!= for SnapshotMetadata. * Add operator== and operator!= for Settings. * Address code review comments. * Add release notes. * Address comments. * Address comments. * Update Android-specific test suite name.
1 parent ffe2fb7 commit 21cb5de

File tree

7 files changed

+130
-7
lines changed

7 files changed

+130
-7
lines changed

firestore/integration_test_internal/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ set(FIREBASE_INTEGRATION_TEST_PORTABLE_TEST_SRCS
9595
src/query_test.cc
9696
src/sanity_test.cc
9797
src/server_timestamp_test.cc
98+
src/settings_test.cc
9899
src/smoke_test.cc
99100
src/source_test.cc
100101
src/transaction_test.cc

firestore/integration_test_internal/src/android/settings_android_test.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ namespace {
1414

1515
using jni::Env;
1616

17-
using SettingsTest = FirestoreIntegrationTest;
17+
using SettingsTestAndroid = FirestoreIntegrationTest;
1818

19-
TEST_F(SettingsTest, ConverterBoolsAllTrue) {
19+
TEST_F(SettingsTestAndroid, ConverterBoolsAllTrue) {
2020
Env env;
2121

2222
Settings settings;
@@ -34,7 +34,7 @@ TEST_F(SettingsTest, ConverterBoolsAllTrue) {
3434
EXPECT_EQ(result.cache_size_bytes(), five_mb);
3535
}
3636

37-
TEST_F(SettingsTest, ConverterBoolsAllFalse) {
37+
TEST_F(SettingsTestAndroid, ConverterBoolsAllFalse) {
3838
Env env;
3939

4040
Settings settings;

firestore/integration_test_internal/src/cleanup_test.cc

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ void ExpectEqualityToWork(T* ptr) {
4343
EXPECT_FALSE(*ptr != T());
4444
}
4545

46+
// Checks that `operator==` and `operator!=` work correctly by comparing to
47+
// a default-constructed instance.
48+
template <typename T>
49+
void ExpectEqualityToWork(const T& ref) {
50+
EXPECT_TRUE(ref == T());
51+
EXPECT_FALSE(ref != T());
52+
}
53+
4654
// `ExpectAllMethodsAreNoOps` calls all the public API methods on the given
4755
// `ptr` and checks that the calls don't crash and, where applicable, return
4856
// value-initialized values.
@@ -122,8 +130,7 @@ void ExpectAllMethodsAreNoOps(DocumentSnapshot* ptr) {
122130
EXPECT_FALSE(ptr->exists());
123131

124132
EXPECT_EQ(ptr->reference(), DocumentReference());
125-
// TODO(b/137966104): implement == on `SnapshotMetadata`
126-
ptr->metadata();
133+
ExpectEqualityToWork(ptr->metadata());
127134

128135
EXPECT_EQ(ptr->GetData(), MapFieldValue());
129136

@@ -236,8 +243,7 @@ void ExpectAllMethodsAreNoOps(QuerySnapshot* ptr) {
236243

237244
EXPECT_EQ(ptr->query(), Query());
238245

239-
// TODO(b/137966104): implement == on `SnapshotMetadata`
240-
ptr->metadata();
246+
ExpectEqualityToWork(ptr->metadata());
241247

242248
EXPECT_TRUE(ptr->DocumentChanges().empty());
243249
EXPECT_TRUE(ptr->documents().empty());
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright 2021 Google LLC
2+
3+
#include <stdint.h>
4+
5+
#include "firebase/firestore.h"
6+
7+
#include "gtest/gtest.h"
8+
9+
namespace firebase {
10+
namespace firestore {
11+
namespace {
12+
13+
TEST(SettingsTest, Equality) {
14+
constexpr int64_t kFiveMb = 5 * 1024 * 1024;
15+
constexpr int64_t kSixMb = 6 * 1024 * 1024;
16+
17+
Settings settings1;
18+
settings1.set_host("foo");
19+
settings1.set_ssl_enabled(true);
20+
settings1.set_persistence_enabled(true);
21+
settings1.set_cache_size_bytes(kFiveMb);
22+
23+
Settings settings2;
24+
settings2.set_host("bar");
25+
settings2.set_ssl_enabled(true);
26+
settings2.set_persistence_enabled(true);
27+
settings2.set_cache_size_bytes(kFiveMb);
28+
29+
Settings settings3;
30+
settings3.set_host("foo");
31+
settings3.set_ssl_enabled(false);
32+
settings3.set_persistence_enabled(true);
33+
settings3.set_cache_size_bytes(kFiveMb);
34+
35+
Settings settings4;
36+
settings4.set_host("foo");
37+
settings4.set_ssl_enabled(true);
38+
settings4.set_persistence_enabled(false);
39+
settings4.set_cache_size_bytes(kFiveMb);
40+
41+
Settings settings5;
42+
settings5.set_host("foo");
43+
settings5.set_ssl_enabled(true);
44+
settings5.set_persistence_enabled(true);
45+
settings5.set_cache_size_bytes(kSixMb);
46+
47+
// This is the same as settings4.
48+
Settings settings6;
49+
settings6.set_host("foo");
50+
settings6.set_ssl_enabled(true);
51+
settings6.set_persistence_enabled(false);
52+
settings6.set_cache_size_bytes(kFiveMb);
53+
54+
EXPECT_TRUE(settings1 == settings1);
55+
EXPECT_TRUE(settings6 == settings4);
56+
57+
EXPECT_FALSE(settings1 == settings2);
58+
EXPECT_FALSE(settings1 == settings3);
59+
EXPECT_FALSE(settings1 == settings4);
60+
EXPECT_FALSE(settings1 == settings5);
61+
EXPECT_FALSE(settings2 == settings3);
62+
EXPECT_FALSE(settings2 == settings4);
63+
EXPECT_FALSE(settings2 == settings5);
64+
EXPECT_FALSE(settings3 == settings4);
65+
EXPECT_FALSE(settings3 == settings5);
66+
EXPECT_FALSE(settings4 == settings5);
67+
68+
EXPECT_FALSE(settings1 != settings1);
69+
EXPECT_FALSE(settings6 != settings4);
70+
71+
EXPECT_TRUE(settings1 != settings2);
72+
EXPECT_TRUE(settings1 != settings3);
73+
EXPECT_TRUE(settings1 != settings4);
74+
EXPECT_TRUE(settings1 != settings5);
75+
EXPECT_TRUE(settings2 != settings3);
76+
EXPECT_TRUE(settings2 != settings4);
77+
EXPECT_TRUE(settings2 != settings5);
78+
EXPECT_TRUE(settings3 != settings4);
79+
EXPECT_TRUE(settings3 != settings5);
80+
EXPECT_TRUE(settings4 != settings5);
81+
}
82+
83+
} // namespace
84+
} // namespace firestore
85+
} // namespace firebase

firestore/src/include/firebase/firestore/settings.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,19 @@ class Settings final {
228228
#endif
229229
};
230230

231+
/** Checks `lhs` and `rhs` for equality. */
232+
inline bool operator==(const Settings& lhs, const Settings& rhs) {
233+
return lhs.host() == rhs.host() &&
234+
lhs.is_ssl_enabled() == rhs.is_ssl_enabled() &&
235+
lhs.is_persistence_enabled() == rhs.is_persistence_enabled() &&
236+
lhs.cache_size_bytes() == rhs.cache_size_bytes();
237+
}
238+
239+
/** Checks `lhs` and `rhs` for inequality. */
240+
inline bool operator!=(const Settings& lhs, const Settings& rhs) {
241+
return !(lhs == rhs);
242+
}
243+
231244
} // namespace firestore
232245
} // namespace firebase
233246

firestore/src/include/firebase/firestore/snapshot_metadata.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,19 @@ class SnapshotMetadata final {
140140
bool is_from_cache_ = false;
141141
};
142142

143+
/** Checks `lhs` and `rhs` for equality. */
144+
inline bool operator==(const SnapshotMetadata& lhs,
145+
const SnapshotMetadata& rhs) {
146+
return lhs.has_pending_writes() == rhs.has_pending_writes() &&
147+
lhs.is_from_cache() == rhs.is_from_cache();
148+
}
149+
150+
/** Checks `lhs` and `rhs` for inequality. */
151+
inline bool operator!=(const SnapshotMetadata& lhs,
152+
const SnapshotMetadata& rhs) {
153+
return !(lhs == rhs);
154+
}
155+
143156
} // namespace firestore
144157
} // namespace firebase
145158

release_build_files/readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,11 @@ code.
567567

568568
## Release Notes
569569

570+
### 8.4.0
571+
- Changes
572+
- Firestore: Added `operator==` and `operator!=` for `SnapshotMetadata`
573+
and `Settings`.
574+
570575
### 8.3.0
571576
- Changes
572577
- General: This release adds tvOS C++ libraries that wrap the

0 commit comments

Comments
 (0)