From bb546e19885ae084823e0315e93564a44c0a8257 Mon Sep 17 00:00:00 2001 From: Gil Date: Fri, 1 Jun 2018 13:42:47 -0700 Subject: Fix Firestore compilation under Xcode < 9.2 (#1367) * Don't rely on specialization failure to determine when std::hash is unavailable. Instead manually declare the conditions under which std::hash should be defined. * Fix detection of Objective-C classes in Xcode < 9.2 std::is_base_of{} is false there so the overloads defined for Objective-C types weren't getting enabled. * Add explicit tests for StringFormat using Objective-C objects * Add explicit tests for HasStdHash --- .../test/firebase/firestore/util/CMakeLists.txt | 9 ++++ .../test/firebase/firestore/util/hashing_test.cc | 18 +++++++ .../firestore/util/string_format_apple_test.mm | 60 ++++++++++++++++++++++ .../firestore/util/type_traits_apple_test.mm | 50 ++++++++++++++++++ 4 files changed, 137 insertions(+) create mode 100644 Firestore/core/test/firebase/firestore/util/string_format_apple_test.mm create mode 100644 Firestore/core/test/firebase/firestore/util/type_traits_apple_test.mm (limited to 'Firestore/core/test') diff --git a/Firestore/core/test/firebase/firestore/util/CMakeLists.txt b/Firestore/core/test/firebase/firestore/util/CMakeLists.txt index bcb1c84..c07a4ea 100644 --- a/Firestore/core/test/firebase/firestore/util/CMakeLists.txt +++ b/Firestore/core/test/firebase/firestore/util/CMakeLists.txt @@ -137,3 +137,12 @@ cc_test( firebase_firestore_util gmock ) + +if(APPLE) + target_sources( + firebase_firestore_util_test + PUBLIC + string_format_apple_test.mm + type_traits_apple_test.mm + ) +endif() diff --git a/Firestore/core/test/firebase/firestore/util/hashing_test.cc b/Firestore/core/test/firebase/firestore/util/hashing_test.cc index e5d9ff8..2c5c2f7 100644 --- a/Firestore/core/test/firebase/firestore/util/hashing_test.cc +++ b/Firestore/core/test/firebase/firestore/util/hashing_test.cc @@ -16,6 +16,9 @@ #include "Firestore/core/src/firebase/firestore/util/hashing.h" +#include +#include + #include "absl/strings/string_view.h" #include "gtest/gtest.h" @@ -29,6 +32,21 @@ struct HasHashMember { } }; +TEST(HashingTest, HasStdHash) { + EXPECT_TRUE(impl::has_std_hash::value); + EXPECT_TRUE(impl::has_std_hash::value); + EXPECT_TRUE(impl::has_std_hash::value); + EXPECT_TRUE(impl::has_std_hash::value); + EXPECT_TRUE(impl::has_std_hash::value); + EXPECT_TRUE(impl::has_std_hash::value); + EXPECT_TRUE(impl::has_std_hash::value); + + struct Foo {}; + EXPECT_FALSE(impl::has_std_hash::value); + EXPECT_FALSE(impl::has_std_hash::value); + EXPECT_FALSE((impl::has_std_hash>::value)); +} + TEST(HashingTest, Int) { ASSERT_EQ(std::hash{}(0), Hash(0)); } diff --git a/Firestore/core/test/firebase/firestore/util/string_format_apple_test.mm b/Firestore/core/test/firebase/firestore/util/string_format_apple_test.mm new file mode 100644 index 0000000..f0bcd35 --- /dev/null +++ b/Firestore/core/test/firebase/firestore/util/string_format_apple_test.mm @@ -0,0 +1,60 @@ +/* + * Copyright 2018 Google + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Firestore/core/src/firebase/firestore/util/string_format.h" + +#import + +#include "gtest/gtest.h" + +@interface FSTDescribable : NSObject +@end + +@implementation FSTDescribable + +- (NSString*)description { + return @"description"; +} + +@end + +namespace firebase { +namespace firestore { +namespace util { + +TEST(StringFormatTest, NSString) { + EXPECT_EQ("Hello World", StringFormat("Hello %s", @"World")); + + NSString* hello = [NSString stringWithUTF8String:"Hello"]; + EXPECT_EQ("Hello World", StringFormat("%s World", hello)); + + // NOLINTNEXTLINE false positive on "string" + NSMutableString* world = [NSMutableString string]; + [world appendString:@"World"]; + EXPECT_EQ("Hello World", StringFormat("Hello %s", world)); +} + +TEST(StringFormatTest, FSTDescribable) { + FSTDescribable* desc = [[FSTDescribable alloc] init]; + EXPECT_EQ("Hello description", StringFormat("Hello %s", desc)); + + id desc_id = desc; + EXPECT_EQ("Hello description", StringFormat("Hello %s", desc_id)); +} + +} // namespace util +} // namespace firestore +} // namespace firebase diff --git a/Firestore/core/test/firebase/firestore/util/type_traits_apple_test.mm b/Firestore/core/test/firebase/firestore/util/type_traits_apple_test.mm new file mode 100644 index 0000000..dfb03bb --- /dev/null +++ b/Firestore/core/test/firebase/firestore/util/type_traits_apple_test.mm @@ -0,0 +1,50 @@ +/* + * Copyright 2018 Google + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Firestore/core/src/firebase/firestore/util/type_traits.h" + +#import +#import +#import + +#include "gtest/gtest.h" + +namespace firebase { +namespace firestore { +namespace util { + +TEST(TypeTraitsTest, IsObjectiveCPointer) { + static_assert(is_objective_c_pointer{}, "NSObject"); + static_assert(is_objective_c_pointer{}, "NSString"); + static_assert(is_objective_c_pointer*>{}, + "NSArray"); + + static_assert(is_objective_c_pointer{}, "id"); + static_assert(is_objective_c_pointer>{}, "id"); + + static_assert(!is_objective_c_pointer{}, "int*"); + static_assert(!is_objective_c_pointer{}, "void*"); + static_assert(!is_objective_c_pointer{}, "int"); + static_assert(!is_objective_c_pointer{}, "void"); + + struct Foo {}; + static_assert(!is_objective_c_pointer{}, "Foo"); + static_assert(!is_objective_c_pointer{}, "Foo"); +} + +} // namespace util +} // namespace firestore +} // namespace firebase -- cgit v1.2.3