Skip to content

operator== and operator!= for SnapshotMetadata and Settings #596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions firestore/integration_test_internal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ set(FIREBASE_INTEGRATION_TEST_PORTABLE_TEST_SRCS
src/query_test.cc
src/sanity_test.cc
src/server_timestamp_test.cc
src/settings_test.cc
src/smoke_test.cc
src/source_test.cc
src/transaction_test.cc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ namespace {

using jni::Env;

using SettingsTest = FirestoreIntegrationTest;
using SettingsTestAndroid = FirestoreIntegrationTest;

TEST_F(SettingsTest, ConverterBoolsAllTrue) {
TEST_F(SettingsTestAndroid, ConverterBoolsAllTrue) {
Env env;

Settings settings;
Expand All @@ -34,7 +34,7 @@ TEST_F(SettingsTest, ConverterBoolsAllTrue) {
EXPECT_EQ(result.cache_size_bytes(), five_mb);
}

TEST_F(SettingsTest, ConverterBoolsAllFalse) {
TEST_F(SettingsTestAndroid, ConverterBoolsAllFalse) {
Env env;

Settings settings;
Expand Down
14 changes: 10 additions & 4 deletions firestore/integration_test_internal/src/cleanup_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ void ExpectEqualityToWork(T* ptr) {
EXPECT_FALSE(*ptr != T());
}

// Checks that `operator==` and `operator!=` work correctly by comparing to
// a default-constructed instance.
template <typename T>
void ExpectEqualityToWork(const T& ref) {
EXPECT_TRUE(ref == T());
EXPECT_FALSE(ref != T());
}

// `ExpectAllMethodsAreNoOps` calls all the public API methods on the given
// `ptr` and checks that the calls don't crash and, where applicable, return
// value-initialized values.
Expand Down Expand Up @@ -122,8 +130,7 @@ void ExpectAllMethodsAreNoOps(DocumentSnapshot* ptr) {
EXPECT_FALSE(ptr->exists());

EXPECT_EQ(ptr->reference(), DocumentReference());
// TODO(b/137966104): implement == on `SnapshotMetadata`
ptr->metadata();
ExpectEqualityToWork(ptr->metadata());

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

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

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

// TODO(b/137966104): implement == on `SnapshotMetadata`
ptr->metadata();
ExpectEqualityToWork(ptr->metadata());

EXPECT_TRUE(ptr->DocumentChanges().empty());
EXPECT_TRUE(ptr->documents().empty());
Expand Down
85 changes: 85 additions & 0 deletions firestore/integration_test_internal/src/settings_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2021 Google LLC

#include <stdint.h>

#include "firebase/firestore.h"

#include "gtest/gtest.h"

namespace firebase {
namespace firestore {
namespace {

TEST(SettingsTest, Equality) {
constexpr int64_t kFiveMb = 5 * 1024 * 1024;
constexpr int64_t kSixMb = 6 * 1024 * 1024;

Settings settings1;
settings1.set_host("foo");
settings1.set_ssl_enabled(true);
settings1.set_persistence_enabled(true);
settings1.set_cache_size_bytes(kFiveMb);

Settings settings2;
settings2.set_host("bar");
settings2.set_ssl_enabled(true);
settings2.set_persistence_enabled(true);
settings2.set_cache_size_bytes(kFiveMb);

Settings settings3;
settings3.set_host("foo");
settings3.set_ssl_enabled(false);
settings3.set_persistence_enabled(true);
settings3.set_cache_size_bytes(kFiveMb);

Settings settings4;
settings4.set_host("foo");
settings4.set_ssl_enabled(true);
settings4.set_persistence_enabled(false);
settings4.set_cache_size_bytes(kFiveMb);

Settings settings5;
settings5.set_host("foo");
settings5.set_ssl_enabled(true);
settings5.set_persistence_enabled(true);
settings5.set_cache_size_bytes(kSixMb);

// This is the same as settings4.
Settings settings6;
settings6.set_host("foo");
settings6.set_ssl_enabled(true);
settings6.set_persistence_enabled(false);
settings6.set_cache_size_bytes(kFiveMb);

EXPECT_TRUE(settings1 == settings1);
EXPECT_TRUE(settings6 == settings4);

EXPECT_FALSE(settings1 == settings2);
EXPECT_FALSE(settings1 == settings3);
EXPECT_FALSE(settings1 == settings4);
EXPECT_FALSE(settings1 == settings5);
EXPECT_FALSE(settings2 == settings3);
EXPECT_FALSE(settings2 == settings4);
EXPECT_FALSE(settings2 == settings5);
EXPECT_FALSE(settings3 == settings4);
EXPECT_FALSE(settings3 == settings5);
EXPECT_FALSE(settings4 == settings5);

EXPECT_FALSE(settings1 != settings1);
EXPECT_FALSE(settings6 != settings4);

EXPECT_TRUE(settings1 != settings2);
EXPECT_TRUE(settings1 != settings3);
EXPECT_TRUE(settings1 != settings4);
EXPECT_TRUE(settings1 != settings5);
EXPECT_TRUE(settings2 != settings3);
EXPECT_TRUE(settings2 != settings4);
EXPECT_TRUE(settings2 != settings5);
EXPECT_TRUE(settings3 != settings4);
EXPECT_TRUE(settings3 != settings5);
EXPECT_TRUE(settings4 != settings5);
}

} // namespace
} // namespace firestore
} // namespace firebase
13 changes: 13 additions & 0 deletions firestore/src/include/firebase/firestore/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,19 @@ class Settings final {
#endif
};

/** Checks `lhs` and `rhs` for equality. */
inline bool operator==(const Settings& lhs, const Settings& rhs) {
return lhs.host() == rhs.host() &&
lhs.is_ssl_enabled() == rhs.is_ssl_enabled() &&
lhs.is_persistence_enabled() == rhs.is_persistence_enabled() &&
lhs.cache_size_bytes() == rhs.cache_size_bytes();
}

/** Checks `lhs` and `rhs` for inequality. */
inline bool operator!=(const Settings& lhs, const Settings& rhs) {
return !(lhs == rhs);
}

} // namespace firestore
} // namespace firebase

Expand Down
13 changes: 13 additions & 0 deletions firestore/src/include/firebase/firestore/snapshot_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,19 @@ class SnapshotMetadata final {
bool is_from_cache_ = false;
};

/** Checks `lhs` and `rhs` for equality. */
inline bool operator==(const SnapshotMetadata& lhs,
const SnapshotMetadata& rhs) {
return lhs.has_pending_writes() == rhs.has_pending_writes() &&
lhs.is_from_cache() == rhs.is_from_cache();
}

/** Checks `lhs` and `rhs` for inequality. */
inline bool operator!=(const SnapshotMetadata& lhs,
const SnapshotMetadata& rhs) {
return !(lhs == rhs);
}

} // namespace firestore
} // namespace firebase

Expand Down
5 changes: 5 additions & 0 deletions release_build_files/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,11 @@ code.

## Release Notes

### 8.4.0
- Changes
- Firestore: Added `operator==` and `operator!=` for `SnapshotMetadata`
and `Settings`.

### 8.3.0
- Changes
- General: This release adds tvOS C++ libraries that wrap the
Expand Down