diff options
author | zxu <zxu@google.com> | 2018-03-14 08:57:31 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-14 08:57:31 -0400 |
commit | 9e815620e9f7f43b42e03db4e5118d7ad03ddee7 (patch) | |
tree | 86f362bad99281325fca7ee3ae116b6a0f36d511 /Firestore/Example/Tests/Model | |
parent | d4d73ea53ecdf1e8ade3d00921419645dd5d66f7 (diff) |
grand PR to port the remaining paths (FieldPath and ResourcePath). (#865)
* naively remove FSTPath import and source/test files.
* port FieldPath, part I
* port FieldPath, part II
* port ResourcePath, part I
* port ResourcePath, part II
* the grand commit to fix build errors
* use testutil:: helper instead of those from FSTHelpers
* fix test and lint
* use c_str in errmsg directly
* fix
* fix
* make code clean
* fix integration test I missed
* fix to avoid naming collision in preprocessor
* address changes
* address changes
* address changes
* fix: fieldMask are actually shared with different context.
* address changes
* address changes
Diffstat (limited to 'Firestore/Example/Tests/Model')
-rw-r--r-- | Firestore/Example/Tests/Model/FSTDocumentSetTests.mm | 2 | ||||
-rw-r--r-- | Firestore/Example/Tests/Model/FSTDocumentTests.mm | 9 | ||||
-rw-r--r-- | Firestore/Example/Tests/Model/FSTFieldValueTests.mm | 43 | ||||
-rw-r--r-- | Firestore/Example/Tests/Model/FSTMutationTests.mm | 21 | ||||
-rw-r--r-- | Firestore/Example/Tests/Model/FSTPathTests.mm | 224 |
5 files changed, 40 insertions, 259 deletions
diff --git a/Firestore/Example/Tests/Model/FSTDocumentSetTests.mm b/Firestore/Example/Tests/Model/FSTDocumentSetTests.mm index fbaa5d6..d70c44d 100644 --- a/Firestore/Example/Tests/Model/FSTDocumentSetTests.mm +++ b/Firestore/Example/Tests/Model/FSTDocumentSetTests.mm @@ -37,7 +37,7 @@ NS_ASSUME_NONNULL_BEGIN - (void)setUp { [super setUp]; - _comp = FSTTestDocComparator(@"sort"); + _comp = FSTTestDocComparator("sort"); _doc1 = FSTTestDoc(@"docs/1", 0, @{ @"sort" : @2 }, NO); _doc2 = FSTTestDoc(@"docs/2", 0, @{ @"sort" : @3 }, NO); _doc3 = FSTTestDoc(@"docs/3", 0, @{ @"sort" : @1 }, NO); diff --git a/Firestore/Example/Tests/Model/FSTDocumentTests.mm b/Firestore/Example/Tests/Model/FSTDocumentTests.mm index 59f526d..0527852 100644 --- a/Firestore/Example/Tests/Model/FSTDocumentTests.mm +++ b/Firestore/Example/Tests/Model/FSTDocumentTests.mm @@ -21,10 +21,13 @@ #import "Firestore/Source/Core/FSTSnapshotVersion.h" #import "Firestore/Source/Model/FSTDocumentKey.h" #import "Firestore/Source/Model/FSTFieldValue.h" -#import "Firestore/Source/Model/FSTPath.h" #import "Firestore/Example/Tests/Util/FSTHelpers.h" +#include "Firestore/core/test/firebase/firestore/testutil/testutil.h" + +namespace testutil = firebase::firestore::testutil; + NS_ASSUME_NONNULL_BEGIN @interface FSTDocumentTests : XCTestCase @@ -55,9 +58,9 @@ NS_ASSUME_NONNULL_BEGIN FSTDocument *doc = [FSTDocument documentWithData:data key:key version:version hasLocalMutations:NO]; - XCTAssertEqualObjects([doc fieldForPath:FSTTestFieldPath(@"desc")], + XCTAssertEqualObjects([doc fieldForPath:testutil::Field("desc")], [FSTStringValue stringValue:@"Discuss all the project related stuff"]); - XCTAssertEqualObjects([doc fieldForPath:FSTTestFieldPath(@"owner.title")], + XCTAssertEqualObjects([doc fieldForPath:testutil::Field("owner.title")], [FSTStringValue stringValue:@"scallywag"]); } diff --git a/Firestore/Example/Tests/Model/FSTFieldValueTests.mm b/Firestore/Example/Tests/Model/FSTFieldValueTests.mm index 03dfa66..1a207f4 100644 --- a/Firestore/Example/Tests/Model/FSTFieldValueTests.mm +++ b/Firestore/Example/Tests/Model/FSTFieldValueTests.mm @@ -23,14 +23,15 @@ #import "Firestore/Source/API/FIRFirestore+Internal.h" #import "Firestore/Source/API/FSTUserDataConverter.h" #import "Firestore/Source/Model/FSTFieldValue.h" -#import "Firestore/Source/Model/FSTPath.h" #import "Firestore/Example/Tests/API/FSTAPIHelpers.h" #import "Firestore/Example/Tests/Util/FSTHelpers.h" #include "Firestore/core/src/firebase/firestore/model/database_id.h" #include "Firestore/core/src/firebase/firestore/util/string_apple.h" +#include "Firestore/core/test/firebase/firestore/testutil/testutil.h" +namespace testutil = firebase::firestore::testutil; namespace util = firebase::firestore::util; using firebase::firestore::model::DatabaseId; @@ -301,20 +302,20 @@ union DoubleBits { FSTObjectValue *obj = FSTTestObjectValue(@{ @"foo" : @{@"a" : @YES, @"b" : @"string"} }); FSTAssertIsKindOfClass(obj, FSTObjectValue); - FSTAssertIsKindOfClass([obj valueForPath:FSTTestFieldPath(@"foo")], FSTObjectValue); - XCTAssertEqualObjects([obj valueForPath:FSTTestFieldPath(@"foo.a")], [FSTBooleanValue trueValue]); - XCTAssertEqualObjects([obj valueForPath:FSTTestFieldPath(@"foo.b")], + FSTAssertIsKindOfClass([obj valueForPath:testutil::Field("foo")], FSTObjectValue); + XCTAssertEqualObjects([obj valueForPath:testutil::Field("foo.a")], [FSTBooleanValue trueValue]); + XCTAssertEqualObjects([obj valueForPath:testutil::Field("foo.b")], [FSTStringValue stringValue:@"string"]); - XCTAssertNil([obj valueForPath:FSTTestFieldPath(@"foo.a.b")]); - XCTAssertNil([obj valueForPath:FSTTestFieldPath(@"bar")]); - XCTAssertNil([obj valueForPath:FSTTestFieldPath(@"bar.a")]); + XCTAssertNil([obj valueForPath:testutil::Field("foo.a.b")]); + XCTAssertNil([obj valueForPath:testutil::Field("bar")]); + XCTAssertNil([obj valueForPath:testutil::Field("bar.a")]); } - (void)testOverwritesExistingFields { FSTObjectValue *old = FSTTestObjectValue(@{@"a" : @"old"}); FSTObjectValue *mod = - [old objectBySettingValue:FSTTestFieldValue(@"mod") forPath:FSTTestFieldPath(@"a")]; + [old objectBySettingValue:FSTTestFieldValue(@"mod") forPath:testutil::Field("a")]; // Should return a new object, leaving the old one unmodified. XCTAssertNotEqual(old, mod); @@ -325,13 +326,13 @@ union DoubleBits { - (void)testAddsNewFields { FSTObjectValue *empty = [FSTObjectValue objectValue]; FSTObjectValue *mod = - [empty objectBySettingValue:FSTTestFieldValue(@"mod") forPath:FSTTestFieldPath(@"a")]; + [empty objectBySettingValue:FSTTestFieldValue(@"mod") forPath:testutil::Field("a")]; XCTAssertNotEqual(empty, mod); XCTAssertEqualObjects(empty, FSTTestFieldValue(@{})); XCTAssertEqualObjects(mod, FSTTestFieldValue(@{@"a" : @"mod"})); FSTObjectValue *old = mod; - mod = [old objectBySettingValue:FSTTestFieldValue(@1) forPath:FSTTestFieldPath(@"b")]; + mod = [old objectBySettingValue:FSTTestFieldValue(@1) forPath:testutil::Field("b")]; XCTAssertNotEqual(old, mod); XCTAssertEqualObjects(old, FSTTestFieldValue(@{@"a" : @"mod"})); XCTAssertEqualObjects(mod, FSTTestFieldValue(@{ @"a" : @"mod", @"b" : @1 })); @@ -340,7 +341,7 @@ union DoubleBits { - (void)testImplicitlyCreatesObjects { FSTObjectValue *old = FSTTestObjectValue(@{@"a" : @"old"}); FSTObjectValue *mod = - [old objectBySettingValue:FSTTestFieldValue(@"mod") forPath:FSTTestFieldPath(@"b.c.d")]; + [old objectBySettingValue:FSTTestFieldValue(@"mod") forPath:testutil::Field("b.c.d")]; XCTAssertNotEqual(old, mod); XCTAssertEqualObjects(old, FSTTestFieldValue(@{@"a" : @"old"})); XCTAssertEqualObjects(mod, FSTTestFieldValue( @@ -351,7 +352,7 @@ union DoubleBits { - (void)testCanOverwritePrimitivesWithObjects { FSTObjectValue *old = FSTTestObjectValue(@{ @"a" : @{@"b" : @"old"} }); FSTObjectValue *mod = - [old objectBySettingValue:FSTTestFieldValue(@{@"b" : @"mod"}) forPath:FSTTestFieldPath(@"a")]; + [old objectBySettingValue:FSTTestFieldValue(@{@"b" : @"mod"}) forPath:testutil::Field("a")]; XCTAssertNotEqual(old, mod); XCTAssertEqualObjects(old, FSTTestFieldValue(@{ @"a" : @{@"b" : @"old"} })); XCTAssertEqualObjects(mod, FSTTestFieldValue(@{ @"a" : @{@"b" : @"mod"} })); @@ -360,7 +361,7 @@ union DoubleBits { - (void)testAddsToNestedObjects { FSTObjectValue *old = FSTTestObjectValue(@{ @"a" : @{@"b" : @"old"} }); FSTObjectValue *mod = - [old objectBySettingValue:FSTTestFieldValue(@"mod") forPath:FSTTestFieldPath(@"a.c")]; + [old objectBySettingValue:FSTTestFieldValue(@"mod") forPath:testutil::Field("a.c")]; XCTAssertNotEqual(old, mod); XCTAssertEqualObjects(old, FSTTestFieldValue(@{ @"a" : @{@"b" : @"old"} })); XCTAssertEqualObjects(mod, FSTTestFieldValue(@{ @"a" : @{@"b" : @"old", @"c" : @"mod"} })); @@ -368,12 +369,12 @@ union DoubleBits { - (void)testDeletesKeys { FSTObjectValue *old = FSTTestObjectValue(@{ @"a" : @1, @"b" : @2 }); - FSTObjectValue *mod = [old objectByDeletingPath:FSTTestFieldPath(@"a")]; + FSTObjectValue *mod = [old objectByDeletingPath:testutil::Field("a")]; XCTAssertNotEqual(old, mod); XCTAssertEqualObjects(old, FSTTestFieldValue(@{ @"a" : @1, @"b" : @2 })); XCTAssertEqualObjects(mod, FSTTestFieldValue(@{ @"b" : @2 })); - FSTObjectValue *empty = [mod objectByDeletingPath:FSTTestFieldPath(@"b")]; + FSTObjectValue *empty = [mod objectByDeletingPath:testutil::Field("b")]; XCTAssertNotEqual(mod, empty); XCTAssertEqualObjects(mod, FSTTestFieldValue(@{ @"b" : @2 })); XCTAssertEqualObjects(empty, FSTTestFieldValue(@{})); @@ -381,15 +382,15 @@ union DoubleBits { - (void)testDeletesHandleMissingKeys { FSTObjectValue *old = FSTTestObjectValue(@{ @"a" : @{@"b" : @1, @"c" : @2} }); - FSTObjectValue *mod = [old objectByDeletingPath:FSTTestFieldPath(@"b")]; + FSTObjectValue *mod = [old objectByDeletingPath:testutil::Field("b")]; XCTAssertEqualObjects(old, mod); XCTAssertEqualObjects(mod, FSTTestFieldValue(@{ @"a" : @{@"b" : @1, @"c" : @2} })); - mod = [old objectByDeletingPath:FSTTestFieldPath(@"a.d")]; + mod = [old objectByDeletingPath:testutil::Field("a.d")]; XCTAssertEqualObjects(old, mod); XCTAssertEqualObjects(mod, FSTTestFieldValue(@{ @"a" : @{@"b" : @1, @"c" : @2} })); - mod = [old objectByDeletingPath:FSTTestFieldPath(@"a.b.c")]; + mod = [old objectByDeletingPath:testutil::Field("a.b.c")]; XCTAssertEqualObjects(old, mod); XCTAssertEqualObjects(mod, FSTTestFieldValue(@{ @"a" : @{@"b" : @1, @"c" : @2} })); } @@ -397,19 +398,19 @@ union DoubleBits { - (void)testDeletesNestedKeys { FSTObjectValue *old = FSTTestObjectValue( @{ @"a" : @{@"b" : @1, @"c" : @{@"d" : @2, @"e" : @3}} }); - FSTObjectValue *mod = [old objectByDeletingPath:FSTTestFieldPath(@"a.c.d")]; + FSTObjectValue *mod = [old objectByDeletingPath:testutil::Field("a.c.d")]; XCTAssertNotEqual(old, mod); XCTAssertEqualObjects(old, FSTTestFieldValue( @{ @"a" : @{@"b" : @1, @"c" : @{@"d" : @2, @"e" : @3}} })); XCTAssertEqualObjects(mod, FSTTestFieldValue(@{ @"a" : @{@"b" : @1, @"c" : @{@"e" : @3}} })); old = mod; - mod = [old objectByDeletingPath:FSTTestFieldPath(@"a.c")]; + mod = [old objectByDeletingPath:testutil::Field("a.c")]; XCTAssertEqualObjects(old, FSTTestFieldValue(@{ @"a" : @{@"b" : @1, @"c" : @{@"e" : @3}} })); XCTAssertEqualObjects(mod, FSTTestFieldValue(@{ @"a" : @{@"b" : @1} })); old = mod; - mod = [old objectByDeletingPath:FSTTestFieldPath(@"a")]; + mod = [old objectByDeletingPath:testutil::Field("a")]; XCTAssertEqualObjects(old, FSTTestFieldValue(@{ @"a" : @{@"b" : @1} })); XCTAssertEqualObjects(mod, FSTTestFieldValue(@{})); } diff --git a/Firestore/Example/Tests/Model/FSTMutationTests.mm b/Firestore/Example/Tests/Model/FSTMutationTests.mm index 1c1375c..5b056fb 100644 --- a/Firestore/Example/Tests/Model/FSTMutationTests.mm +++ b/Firestore/Example/Tests/Model/FSTMutationTests.mm @@ -22,10 +22,13 @@ #import "Firestore/Source/Model/FSTDocument.h" #import "Firestore/Source/Model/FSTDocumentKey.h" #import "Firestore/Source/Model/FSTFieldValue.h" -#import "Firestore/Source/Model/FSTPath.h" #import "Firestore/Example/Tests/Util/FSTHelpers.h" +#include "Firestore/core/test/firebase/firestore/testutil/testutil.h" + +namespace testutil = firebase::firestore::testutil; + @interface FSTMutationTests : XCTestCase @end @@ -52,8 +55,7 @@ NSDictionary *docData = @{ @"foo" : @{@"bar" : @"bar-value"}, @"baz" : @"baz-value" }; FSTDocument *baseDoc = FSTTestDoc(@"collection/key", 0, docData, NO); - FSTMutation *patch = - FSTTestPatchMutation(@"collection/key", @{@"foo.bar" : @"new-bar-value"}, nil); + FSTMutation *patch = FSTTestPatchMutation("collection/key", @{@"foo.bar" : @"new-bar-value"}, {}); FSTMaybeDocument *patchedDoc = [patch applyTo:baseDoc baseDocument:baseDoc localWriteTime:_timestamp]; @@ -66,7 +68,7 @@ FSTDocument *baseDoc = FSTTestDoc(@"collection/key", 0, docData, NO); FSTDocumentKey *key = FSTTestDocKey(@"collection/key"); - FSTFieldMask *mask = [[FSTFieldMask alloc] initWithFields:@[ FSTTestFieldPath(@"foo.bar") ]]; + FSTFieldMask *mask = [[FSTFieldMask alloc] initWithFields:{testutil::Field("foo.bar")}]; FSTMutation *patch = [[FSTPatchMutation alloc] initWithKey:key fieldMask:mask value:[FSTObjectValue objectValue] @@ -82,8 +84,7 @@ NSDictionary *docData = @{@"foo" : @"foo-value", @"baz" : @"baz-value"}; FSTDocument *baseDoc = FSTTestDoc(@"collection/key", 0, docData, NO); - FSTMutation *patch = - FSTTestPatchMutation(@"collection/key", @{@"foo.bar" : @"new-bar-value"}, nil); + FSTMutation *patch = FSTTestPatchMutation("collection/key", @{@"foo.bar" : @"new-bar-value"}, {}); FSTMaybeDocument *patchedDoc = [patch applyTo:baseDoc baseDocument:baseDoc localWriteTime:_timestamp]; @@ -93,7 +94,7 @@ - (void)testPatchingDeletedDocumentsDoesNothing { FSTMaybeDocument *baseDoc = FSTTestDeletedDoc(@"collection/key", 0); - FSTMutation *patch = FSTTestPatchMutation(@"collection/key", @{@"foo" : @"bar"}, nil); + FSTMutation *patch = FSTTestPatchMutation("collection/key", @{@"foo" : @"bar"}, {}); FSTMaybeDocument *patchedDoc = [patch applyTo:baseDoc baseDocument:baseDoc localWriteTime:_timestamp]; XCTAssertEqualObjects(patchedDoc, baseDoc); @@ -115,7 +116,7 @@ [expectedData objectBySettingValue:[FSTServerTimestampValue serverTimestampValueWithLocalWriteTime:_timestamp previousValue:nil] - forPath:FSTTestFieldPath(@"foo.bar")]; + forPath:testutil::Field("foo.bar")]; FSTDocument *expectedDoc = [FSTDocument documentWithData:expectedData key:FSTTestDocKey(@"collection/key") @@ -176,7 +177,7 @@ NSDictionary *docData = @{@"foo" : @"bar"}; FSTDocument *baseDoc = FSTTestDoc(@"collection/key", 0, docData, NO); - FSTMutation *patch = FSTTestPatchMutation(@"collection/key", @{@"foo" : @"new-bar"}, nil); + FSTMutation *patch = FSTTestPatchMutation("collection/key", @{@"foo" : @"new-bar"}, {}); FSTMutationResult *mutationResult = [[FSTMutationResult alloc] initWithVersion:FSTTestVersion(4) transformResults:nil]; FSTMaybeDocument *patchedDoc = [patch applyTo:baseDoc @@ -210,7 +211,7 @@ FSTDeletedDocument *deletedV3 = FSTTestDeletedDoc(@"collection/key", 3); FSTMutation *setMutation = FSTTestSetMutation(@"collection/key", @{}); - FSTMutation *patchMutation = FSTTestPatchMutation(@"collection/key", @{}, nil); + FSTMutation *patchMutation = FSTTestPatchMutation("collection/key", {}, {}); FSTMutation *deleteMutation = FSTTestDeleteMutation(@"collection/key"); ASSERT_VERSION_TRANSITION(setMutation, docV3, docV3); diff --git a/Firestore/Example/Tests/Model/FSTPathTests.mm b/Firestore/Example/Tests/Model/FSTPathTests.mm deleted file mode 100644 index 388c5c3..0000000 --- a/Firestore/Example/Tests/Model/FSTPathTests.mm +++ /dev/null @@ -1,224 +0,0 @@ -/* - * 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. - */ - -#import "Firestore/Example/Tests/Util/FSTHelpers.h" -#import "Firestore/Source/Model/FSTPath.h" - -#import <XCTest/XCTest.h> - -NS_ASSUME_NONNULL_BEGIN - -@interface FSTFieldPathTests : XCTestCase -@end - -@implementation FSTFieldPathTests - -- (void)testConstructor { - FSTFieldPath *path = [FSTFieldPath pathWithSegments:@[ @"rooms", @"Eros", @"messages" ]]; - XCTAssertEqual(3, path.length); -} - -- (void)testIndexing { - FSTFieldPath *path = [FSTFieldPath pathWithSegments:@[ @"rooms", @"Eros", @"messages" ]]; - XCTAssertEqualObjects(@"rooms", path.firstSegment); - XCTAssertEqualObjects(@"rooms", [path segmentAtIndex:0]); - XCTAssertEqualObjects(@"rooms", path[0]); - - XCTAssertEqualObjects(@"Eros", [path segmentAtIndex:1]); - XCTAssertEqualObjects(@"Eros", path[1]); - - XCTAssertEqualObjects(@"messages", [path segmentAtIndex:2]); - XCTAssertEqualObjects(@"messages", path[2]); - XCTAssertEqualObjects(@"messages", path.lastSegment); -} - -- (void)testPathByRemovingFirstSegment { - FSTFieldPath *path = [FSTFieldPath pathWithSegments:@[ @"rooms", @"Eros", @"messages" ]]; - FSTFieldPath *same = [FSTFieldPath pathWithSegments:@[ @"rooms", @"Eros", @"messages" ]]; - FSTFieldPath *second = [FSTFieldPath pathWithSegments:@[ @"Eros", @"messages" ]]; - FSTFieldPath *third = [FSTFieldPath pathWithSegments:@[ @"messages" ]]; - FSTFieldPath *empty = [FSTFieldPath pathWithSegments:@[]]; - - XCTAssertEqualObjects(second, path.pathByRemovingFirstSegment); - XCTAssertEqualObjects(third, path.pathByRemovingFirstSegment.pathByRemovingFirstSegment); - XCTAssertEqualObjects( - empty, path.pathByRemovingFirstSegment.pathByRemovingFirstSegment.pathByRemovingFirstSegment); - // unmodified original - XCTAssertEqualObjects(same, path); -} - -- (void)testPathByRemovingLastSegment { - FSTFieldPath *path = [FSTFieldPath pathWithSegments:@[ @"rooms", @"Eros", @"messages" ]]; - FSTFieldPath *same = [FSTFieldPath pathWithSegments:@[ @"rooms", @"Eros", @"messages" ]]; - FSTFieldPath *second = [FSTFieldPath pathWithSegments:@[ @"rooms", @"Eros" ]]; - FSTFieldPath *third = [FSTFieldPath pathWithSegments:@[ @"rooms" ]]; - FSTFieldPath *empty = [FSTFieldPath pathWithSegments:@[]]; - - XCTAssertEqualObjects(second, path.pathByRemovingLastSegment); - XCTAssertEqualObjects(third, path.pathByRemovingLastSegment.pathByRemovingLastSegment); - XCTAssertEqualObjects( - empty, path.pathByRemovingLastSegment.pathByRemovingLastSegment.pathByRemovingLastSegment); - // unmodified original - XCTAssertEqualObjects(same, path); -} - -- (void)testPathByAppendingSegment { - FSTFieldPath *path = [FSTFieldPath pathWithSegments:@[ @"rooms" ]]; - FSTFieldPath *rooms = [FSTFieldPath pathWithSegments:@[ @"rooms" ]]; - FSTFieldPath *roomsEros = [FSTFieldPath pathWithSegments:@[ @"rooms", @"eros" ]]; - FSTFieldPath *roomsEros1 = [FSTFieldPath pathWithSegments:@[ @"rooms", @"eros", @"1" ]]; - - XCTAssertEqualObjects(roomsEros, [path pathByAppendingSegment:@"eros"]); - XCTAssertEqualObjects(roomsEros1, - [[path pathByAppendingSegment:@"eros"] pathByAppendingSegment:@"1"]); - // unmodified original - XCTAssertEqualObjects(rooms, path); - - FSTFieldPath *sub = [FSTTestFieldPath(@"rooms.eros.1") pathByRemovingFirstSegment]; - FSTFieldPath *appended = [sub pathByAppendingSegment:@"2"]; - XCTAssertEqualObjects(appended, FSTTestFieldPath(@"eros.1.2")); -} - -- (void)testPathComparison { - FSTFieldPath *path1 = [FSTFieldPath pathWithSegments:@[ @"a", @"b", @"c" ]]; - FSTFieldPath *path2 = [FSTFieldPath pathWithSegments:@[ @"a", @"b", @"c" ]]; - FSTFieldPath *path3 = [FSTFieldPath pathWithSegments:@[ @"x", @"y", @"z" ]]; - XCTAssertTrue([path1 isEqual:path2]); - XCTAssertFalse([path1 isEqual:path3]); - - FSTFieldPath *empty = [FSTFieldPath pathWithSegments:@[]]; - FSTFieldPath *a = [FSTFieldPath pathWithSegments:@[ @"a" ]]; - FSTFieldPath *b = [FSTFieldPath pathWithSegments:@[ @"b" ]]; - FSTFieldPath *ab = [FSTFieldPath pathWithSegments:@[ @"a", @"b" ]]; - - XCTAssertEqual(NSOrderedAscending, [empty compare:a]); - XCTAssertEqual(NSOrderedAscending, [a compare:b]); - XCTAssertEqual(NSOrderedAscending, [a compare:ab]); - - XCTAssertEqual(NSOrderedDescending, [a compare:empty]); - XCTAssertEqual(NSOrderedDescending, [b compare:a]); - XCTAssertEqual(NSOrderedDescending, [ab compare:a]); -} - -- (void)testIsPrefixOfPath { - FSTFieldPath *empty = [FSTFieldPath pathWithSegments:@[]]; - FSTFieldPath *a = [FSTFieldPath pathWithSegments:@[ @"a" ]]; - FSTFieldPath *ab = [FSTFieldPath pathWithSegments:@[ @"a", @"b" ]]; - FSTFieldPath *abc = [FSTFieldPath pathWithSegments:@[ @"a", @"b", @"c" ]]; - FSTFieldPath *b = [FSTFieldPath pathWithSegments:@[ @"b" ]]; - FSTFieldPath *ba = [FSTFieldPath pathWithSegments:@[ @"b", @"a" ]]; - - XCTAssertTrue([empty isPrefixOfPath:a]); - XCTAssertTrue([empty isPrefixOfPath:ab]); - XCTAssertTrue([empty isPrefixOfPath:abc]); - XCTAssertTrue([empty isPrefixOfPath:empty]); - XCTAssertTrue([empty isPrefixOfPath:b]); - XCTAssertTrue([empty isPrefixOfPath:ba]); - - XCTAssertTrue([a isPrefixOfPath:a]); - XCTAssertTrue([a isPrefixOfPath:ab]); - XCTAssertTrue([a isPrefixOfPath:abc]); - XCTAssertFalse([a isPrefixOfPath:empty]); - XCTAssertFalse([a isPrefixOfPath:b]); - XCTAssertFalse([a isPrefixOfPath:ba]); - - XCTAssertFalse([ab isPrefixOfPath:a]); - XCTAssertTrue([ab isPrefixOfPath:ab]); - XCTAssertTrue([ab isPrefixOfPath:abc]); - XCTAssertFalse([ab isPrefixOfPath:empty]); - XCTAssertFalse([ab isPrefixOfPath:b]); - XCTAssertFalse([ab isPrefixOfPath:ba]); - - XCTAssertFalse([abc isPrefixOfPath:a]); - XCTAssertFalse([abc isPrefixOfPath:ab]); - XCTAssertTrue([abc isPrefixOfPath:abc]); - XCTAssertFalse([abc isPrefixOfPath:empty]); - XCTAssertFalse([abc isPrefixOfPath:b]); - XCTAssertFalse([abc isPrefixOfPath:ba]); -} - -- (void)testInvalidPaths { - XCTAssertThrows(FSTTestFieldPath(@"")); - XCTAssertThrows(FSTTestFieldPath(@".")); - XCTAssertThrows(FSTTestFieldPath(@".foo")); - XCTAssertThrows(FSTTestFieldPath(@"foo.")); - XCTAssertThrows(FSTTestFieldPath(@"foo..bar")); -} - -#define ASSERT_ROUND_TRIP(str, segments) \ - do { \ - FSTFieldPath *path = [FSTFieldPath pathWithServerFormat:str]; \ - XCTAssertEqual([path length], segments); \ - NSString *canonical = [path canonicalString]; \ - XCTAssertEqualObjects(canonical, str); \ - } while (0); - -- (void)testCanonicalString { - ASSERT_ROUND_TRIP(@"foo", 1); - ASSERT_ROUND_TRIP(@"foo.bar", 2); - ASSERT_ROUND_TRIP(@"foo.bar.baz", 3); - ASSERT_ROUND_TRIP(@"`.foo\\\\`", 1); - ASSERT_ROUND_TRIP(@"`.foo\\\\`.`.foo`", 2); - ASSERT_ROUND_TRIP(@"foo.`\\``.bar", 3); - - FSTFieldPath *path = [FSTFieldPath pathWithServerFormat:@"foo\\.bar"]; - XCTAssertEqualObjects([path canonicalString], @"`foo.bar`"); - XCTAssertEqual(path.length, 1); -} - -#undef ASSERT_ROUND_TRIP - -- (void)testCanonicalStringOfSubstring { - FSTFieldPath *path = [FSTFieldPath pathWithServerFormat:@"foo.bar.baz"]; - XCTAssertEqualObjects([path canonicalString], @"foo.bar.baz"); - - FSTFieldPath *pathTail = [path pathByRemovingFirstSegment]; - XCTAssertEqualObjects([pathTail canonicalString], @"bar.baz"); - - FSTFieldPath *pathHead = [path pathByRemovingLastSegment]; - XCTAssertEqualObjects([pathHead canonicalString], @"foo.bar"); - - XCTAssertEqualObjects([[pathTail pathByRemovingLastSegment] canonicalString], @"bar"); - XCTAssertEqualObjects([[pathHead pathByRemovingFirstSegment] canonicalString], @"bar"); -} - -- (void)testRoundTrip { - FSTFieldPath *path = [FSTFieldPath pathWithSegments:@[ @"rooms", @"Eros", @"messages" ]]; - XCTAssertEqualObjects(path, [FSTFieldPath fieldPathWithCPPFieldPath:[path toCPPFieldPath]]); - - const firebase::firestore::model::FieldPath cppPath{"rooms", "Eros", "messages"}; - XCTAssertEqual(cppPath, [[FSTFieldPath fieldPathWithCPPFieldPath:cppPath] toCPPFieldPath]); -} - -@end - -@interface FSTResourcePathTests : XCTestCase -@end - -@implementation FSTResourcePathTests - -- (void)testRoundTrip { - FSTResourcePath *path = [FSTResourcePath pathWithSegments:@[ @"rooms", @"Eros", @"messages" ]]; - XCTAssertEqualObjects(path, - [FSTResourcePath resourcePathWithCPPResourcePath:[path toCPPResourcePath]]); - - const firebase::firestore::model::ResourcePath cppPath{"rooms", "Eros", "messages"}; - XCTAssertEqual(cppPath, - [[FSTResourcePath resourcePathWithCPPResourcePath:cppPath] toCPPResourcePath]); -} - -@end -NS_ASSUME_NONNULL_END |