From b31fe35eb7a1301e1e6c5d3381b9f3d8054734dd Mon Sep 17 00:00:00 2001 From: Konstantin Varlamov Date: Tue, 6 Feb 2018 19:15:54 -0500 Subject: C++ port: port FSTFieldPath and FSTResourcePath to C++ (#749) Similar to Objective-C, FieldPath and ResourcePath share most of their interface (and implementation) by deriving from BasePath (using CRTP, so that factory methods in BasePath can return an instance of the derived class). --- .../firebase/firestore/model/resource_path_test.cc | 105 +++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 Firestore/core/test/firebase/firestore/model/resource_path_test.cc (limited to 'Firestore/core/test/firebase/firestore/model/resource_path_test.cc') diff --git a/Firestore/core/test/firebase/firestore/model/resource_path_test.cc b/Firestore/core/test/firebase/firestore/model/resource_path_test.cc new file mode 100644 index 0000000..317a1db --- /dev/null +++ b/Firestore/core/test/firebase/firestore/model/resource_path_test.cc @@ -0,0 +1,105 @@ +/* + * 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/model/resource_path.h" + +#include +#include +#include + +#include "gtest/gtest.h" + +namespace firebase { +namespace firestore { +namespace model { + +TEST(ResourcePath, Constructor) { + const ResourcePath empty_path; + EXPECT_TRUE(empty_path.empty()); + EXPECT_EQ(0, empty_path.size()); + EXPECT_TRUE(empty_path.begin() == empty_path.end()); + + const ResourcePath path_from_list{{"rooms", "Eros", "messages"}}; + EXPECT_FALSE(path_from_list.empty()); + EXPECT_EQ(3, path_from_list.size()); + EXPECT_TRUE(path_from_list.begin() + 3 == path_from_list.end()); + + std::vector segments{"rooms", "Eros", "messages"}; + const ResourcePath path_from_segments{segments.begin(), segments.end()}; + EXPECT_FALSE(path_from_segments.empty()); + EXPECT_EQ(3, path_from_segments.size()); + EXPECT_TRUE(path_from_segments.begin() + 3 == path_from_segments.end()); + + ResourcePath copied = path_from_list; + EXPECT_EQ(path_from_list, copied); + const ResourcePath moved = std::move(copied); + EXPECT_EQ(path_from_list, moved); + EXPECT_NE(copied, moved); + EXPECT_EQ(empty_path, copied); +} + +TEST(ResourcePath, Comparison) { + const ResourcePath abc{"a", "b", "c"}; + const ResourcePath abc2{"a", "b", "c"}; + const ResourcePath xyz{"x", "y", "z"}; + EXPECT_EQ(abc, abc2); + EXPECT_NE(abc, xyz); + + const ResourcePath empty; + const ResourcePath a{"a"}; + const ResourcePath b{"b"}; + const ResourcePath ab{"a", "b"}; + + EXPECT_TRUE(empty < a); + EXPECT_TRUE(a < b); + EXPECT_TRUE(a < ab); + + EXPECT_TRUE(a > empty); + EXPECT_TRUE(b > a); + EXPECT_TRUE(ab > a); +} + +TEST(ResourcePath, Parsing) { + const auto parse = [](const std::pair expected) { + const auto path = ResourcePath::Parse(expected.first); + return std::make_pair(path.CanonicalString(), path.size()); + }; + const auto make_expected = [](const std::string& str, const size_t size) { + return std::make_pair(str, size); + }; + + auto expected = make_expected("", 0); + EXPECT_EQ(expected, parse(expected)); + expected = make_expected("foo", 1); + EXPECT_EQ(expected, parse(expected)); + expected = make_expected("foo/bar", 2); + EXPECT_EQ(expected, parse(expected)); + expected = make_expected("foo/bar/baz", 3); + EXPECT_EQ(expected, parse(expected)); + expected = make_expected(R"(foo/__!?#@..`..\`/baz)", 3); + EXPECT_EQ(expected, parse(expected)); + + EXPECT_EQ(ResourcePath::Parse("/foo/").CanonicalString(), "foo"); +} + +TEST(ResourcePath, ParseFailures) { + ASSERT_DEATH_IF_SUPPORTED(ResourcePath::Parse("//"), ""); + ASSERT_DEATH_IF_SUPPORTED(ResourcePath::Parse("foo//bar"), ""); +} + +} // namespace model +} // namespace firestore +} // namespace firebase -- cgit v1.2.3