diff options
Diffstat (limited to 'Firestore/core/test/firebase/firestore/util')
6 files changed, 228 insertions, 2 deletions
diff --git a/Firestore/core/test/firebase/firestore/util/CMakeLists.txt b/Firestore/core/test/firebase/firestore/util/CMakeLists.txt index 223fa41..e51bb51 100644 --- a/Firestore/core/test/firebase/firestore/util/CMakeLists.txt +++ b/Firestore/core/test/firebase/firestore/util/CMakeLists.txt @@ -16,8 +16,31 @@ cc_test( firebase_firestore_util_test autoid_test.cc secure_random_test.cc + string_printf_test.cc ) target_link_libraries( firebase_firestore_util_test firebase_firestore_util ) + +if(APPLE) + cc_test( + firebase_firestore_util_apple_test + assert_test.cc + log_test.cc + ) + target_link_libraries( + firebase_firestore_util_apple_test + firebase_firestore_util_apple + ) +endif(APPLE) + +cc_test( + firebase_firestore_util_stdio_test + assert_test.cc + log_test.cc +) +target_link_libraries( + firebase_firestore_util_stdio_test + firebase_firestore_util_stdio +) diff --git a/Firestore/core/test/firebase/firestore/util/assert_test.cc b/Firestore/core/test/firebase/firestore/util/assert_test.cc new file mode 100644 index 0000000..7c49462 --- /dev/null +++ b/Firestore/core/test/firebase/firestore/util/assert_test.cc @@ -0,0 +1,64 @@ +/* + * 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/firebase_assert.h" + +#include <exception> + +#include "gtest/gtest.h" + +namespace firebase { +namespace firestore { +namespace util { + +namespace { + +void AssertWithExpression(bool condition) { + FIREBASE_ASSERT_WITH_EXPRESSION(condition, 1 + 2 + 3); +} + +void Assert(bool condition) { + FIREBASE_ASSERT(condition == true); +} + +void AssertMessageWithExpression(bool condition) { + FIREBASE_ASSERT_MESSAGE_WITH_EXPRESSION(condition, 1 + 2 + 3, "connection %s", + condition ? "succeeded" : "failed"); +} + +} // namespace + +TEST(Assert, WithExpression) { + AssertWithExpression(true); + + EXPECT_ANY_THROW(AssertWithExpression(false)); +} + +TEST(Assert, Vanilla) { + Assert(true); + + EXPECT_ANY_THROW(Assert(false)); +} + +TEST(Assert, WithMessageAndExpression) { + AssertMessageWithExpression(true); + + EXPECT_ANY_THROW(AssertMessageWithExpression(false)); +} + +} // namespace util +} // namespace firestore +} // namespace firebase diff --git a/Firestore/core/test/firebase/firestore/util/autoid_test.cc b/Firestore/core/test/firebase/firestore/util/autoid_test.cc index 730c683..e55a8e9 100644 --- a/Firestore/core/test/firebase/firestore/util/autoid_test.cc +++ b/Firestore/core/test/firebase/firestore/util/autoid_test.cc @@ -18,7 +18,7 @@ #include <ctype.h> -#include "gtest/gtest.h" +#include <gtest/gtest.h> using firebase::firestore::util::CreateAutoId; diff --git a/Firestore/core/test/firebase/firestore/util/log_test.cc b/Firestore/core/test/firebase/firestore/util/log_test.cc new file mode 100644 index 0000000..46cbc4e --- /dev/null +++ b/Firestore/core/test/firebase/firestore/util/log_test.cc @@ -0,0 +1,61 @@ +/* + * Copyright 2017 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/log.h" + +#include "gtest/gtest.h" + +namespace firebase { +namespace firestore { +namespace util { + +// When running against the log_apple.mm implementation (backed by FIRLogger) +// this test can fail if debug_mode gets persisted in the user defaults. Check +// for defaults getting in your way with +// +// defaults read firebase_firestore_util_log_apple_test +// +// You can fix it with: +// +// defaults write firebase_firestore_util_log_apple_test \ +// /google/firebase/debug_mode NO +TEST(Log, SetAndGet) { + LogSetLevel(kLogLevelVerbose); + + LogSetLevel(kLogLevelDebug); + EXPECT_EQ(kLogLevelDebug, LogGetLevel()); + + LogSetLevel(kLogLevelInfo); + EXPECT_EQ(kLogLevelInfo, LogGetLevel()); + + LogSetLevel(kLogLevelWarning); + EXPECT_EQ(kLogLevelWarning, LogGetLevel()); + + LogSetLevel(kLogLevelError); + EXPECT_EQ(kLogLevelError, LogGetLevel()); +} + +TEST(Log, LogAllKinds) { + LogDebug("test debug logging %d", 1); + LogInfo("test info logging %d", 2); + LogWarning("test warning logging %d", 3); + LogError("test error logging %d", 4); + LogMessage(kLogLevelError, "test va-args %s %c %d", "abc", ':', 123); +} + +} // namespace util +} // namespace firestore +} // namespace firebase diff --git a/Firestore/core/test/firebase/firestore/util/secure_random_test.cc b/Firestore/core/test/firebase/firestore/util/secure_random_test.cc index f96f3de..ee2ae00 100644 --- a/Firestore/core/test/firebase/firestore/util/secure_random_test.cc +++ b/Firestore/core/test/firebase/firestore/util/secure_random_test.cc @@ -16,7 +16,7 @@ #include "Firestore/core/src/firebase/firestore/util/secure_random.h" -#include "gtest/gtest.h" +#include <gtest/gtest.h> using firebase::firestore::util::SecureRandom; diff --git a/Firestore/core/test/firebase/firestore/util/string_printf_test.cc b/Firestore/core/test/firebase/firestore/util/string_printf_test.cc new file mode 100644 index 0000000..76f7cde --- /dev/null +++ b/Firestore/core/test/firebase/firestore/util/string_printf_test.cc @@ -0,0 +1,78 @@ +/* + * Copyright 2017 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_printf.h" + +#include <gtest/gtest.h> + +namespace firebase { +namespace firestore { +namespace util { + +TEST(StringPrintf, Empty) { + EXPECT_EQ("", StringPrintf("")); + EXPECT_EQ("", StringPrintf("%s", std::string().c_str())); + EXPECT_EQ("", StringPrintf("%s", "")); +} + +TEST(StringAppendFTest, Empty) { + std::string value("Hello"); + const char* empty = ""; + StringAppendF(&value, "%s", empty); + EXPECT_EQ("Hello", value); +} + +TEST(StringAppendFTest, EmptyString) { + std::string value("Hello"); + StringAppendF(&value, "%s", ""); + EXPECT_EQ("Hello", value); +} + +TEST(StringAppendFTest, String) { + std::string value("Hello"); + StringAppendF(&value, " %s", "World"); + EXPECT_EQ("Hello World", value); +} + +TEST(StringAppendFTest, Int) { + std::string value("Hello"); + StringAppendF(&value, " %d", 123); + EXPECT_EQ("Hello 123", value); +} + +TEST(StringPrintf, DontOverwriteErrno) { + // Check that errno isn't overwritten unless we're printing + // something significantly larger than what people are normally + // printing in their badly written PLOG() statements. + errno = ECHILD; + std::string value = StringPrintf("Hello, %s!", "World"); + EXPECT_EQ(ECHILD, errno); +} + +TEST(StringPrintf, LargeBuf) { + // Check that the large buffer is handled correctly. + int n = 2048; + char* buf = new char[n + 1]; + memset(buf, ' ', n); + buf[n] = 0; + std::string value = StringPrintf("%s", buf); + EXPECT_EQ(buf, value); + delete[] buf; +} + +} // namespace util +} // namespace firestore +} // namespace firebase |