aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firestore/Example/Tests/Util
diff options
context:
space:
mode:
authorGravatar zxu <zxu@google.com>2018-03-14 08:57:31 -0400
committerGravatar GitHub <noreply@github.com>2018-03-14 08:57:31 -0400
commit9e815620e9f7f43b42e03db4e5118d7ad03ddee7 (patch)
tree86f362bad99281325fca7ee3ae116b6a0f36d511 /Firestore/Example/Tests/Util
parentd4d73ea53ecdf1e8ade3d00921419645dd5d66f7 (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/Util')
-rw-r--r--Firestore/Example/Tests/Util/FSTHelpers.h62
-rw-r--r--Firestore/Example/Tests/Util/FSTHelpers.mm71
2 files changed, 67 insertions, 66 deletions
diff --git a/Firestore/Example/Tests/Util/FSTHelpers.h b/Firestore/Example/Tests/Util/FSTHelpers.h
index cc9f2ec..d86201b 100644
--- a/Firestore/Example/Tests/Util/FSTHelpers.h
+++ b/Firestore/Example/Tests/Util/FSTHelpers.h
@@ -16,10 +16,16 @@
#import <Foundation/Foundation.h>
+#include <map>
+#include <vector>
+
#import "Firestore/Source/Core/FSTTypes.h"
#import "Firestore/Source/Model/FSTDocumentDictionary.h"
#import "Firestore/Source/Model/FSTDocumentKeySet.h"
+#include "Firestore/core/src/firebase/firestore/model/field_path.h"
+#include "Firestore/core/src/firebase/firestore/model/field_value.h"
+#include "Firestore/core/src/firebase/firestore/model/resource_path.h"
#include "absl/strings/string_view.h"
@class FIRGeoPoint;
@@ -28,13 +34,11 @@
@class FSTDocument;
@class FSTDocumentKeyReference;
@class FSTDocumentSet;
-@class FSTFieldPath;
@class FSTFieldValue;
@class FSTLocalViewChanges;
@class FSTPatchMutation;
@class FSTQuery;
@class FSTRemoteEvent;
-@class FSTResourcePath;
@class FSTSetMutation;
@class FSTSnapshotVersion;
@class FSTSortOrder;
@@ -121,18 +125,30 @@ extern "C" {
} \
} while (0)
+static NSString *kExceptionPrefix = @"FIRESTORE INTERNAL ASSERTION FAILED: ";
+
+// Remove possible exception-prefix.
+inline NSString *FSTRemoveExceptionPrefix(NSString *exception) {
+ if ([exception hasPrefix:kExceptionPrefix]) {
+ return [exception substringFromIndex:kExceptionPrefix.length];
+ } else {
+ return exception;
+ }
+}
+
// Helper for validating API exceptions.
-#define FSTAssertThrows(expression, exceptionReason, ...) \
- ({ \
- BOOL __didThrow = NO; \
- @try { \
- (void)(expression); \
- } @catch (NSException * exception) { \
- __didThrow = YES; \
- XCTAssertEqualObjects(exception.reason, exceptionReason); \
- } \
- XCTAssertTrue(__didThrow, ##__VA_ARGS__); \
- })
+#define FSTAssertThrows(expression, exceptionReason, ...) \
+ do { \
+ BOOL didThrow = NO; \
+ @try { \
+ (void)(expression); \
+ } @catch (NSException * exception) { \
+ didThrow = YES; \
+ XCTAssertEqualObjects(FSTRemoveExceptionPrefix(exception.reason), \
+ FSTRemoveExceptionPrefix(exceptionReason)); \
+ } \
+ XCTAssertTrue(didThrow, ##__VA_ARGS__); \
+ } while (0)
/** Creates a new FIRTimestamp from components. Note that year, month, and day are all one-based. */
FIRTimestamp *FSTTestTimestamp(int year, int month, int day, int hour, int minute, int second);
@@ -157,8 +173,6 @@ FIRGeoPoint *FSTTestGeoPoint(double latitude, double longitude);
NSDateComponents *FSTTestDateComponents(
int year, int month, int day, int hour, int minute, int second);
-FSTFieldPath *FSTTestFieldPath(NSString *field);
-
/** Wraps a plain value into an FSTFieldValue instance. */
FSTFieldValue *FSTTestFieldValue(id _Nullable value);
@@ -186,9 +200,6 @@ FSTDocument *FSTTestDoc(NSString *path,
/** A convenience method for creating deleted docs for tests. */
FSTDeletedDocument *FSTTestDeletedDoc(NSString *path, FSTTestSnapshotVersion version);
-/** A convenience method for creating resource paths from a path string. */
-FSTResourcePath *FSTTestPath(NSString *path);
-
/**
* A convenience method for creating a document reference from a path string.
*/
@@ -197,22 +208,22 @@ FSTDocumentKeyReference *FSTTestRef(const absl::string_view projectID,
NSString *path);
/** A convenience method for creating a query for the given path (without any other filters). */
-FSTQuery *FSTTestQuery(NSString *path);
+FSTQuery *FSTTestQuery(const absl::string_view path);
/**
* A convenience method to create a FSTFilter using a string representation for both field
* and operator (<, <=, ==, >=, >).
*/
-id<FSTFilter> FSTTestFilter(NSString *field, NSString *op, id value);
+id<FSTFilter> FSTTestFilter(const absl::string_view field, NSString *op, id value);
/** A convenience method for creating sort orders. */
-FSTSortOrder *FSTTestOrderBy(NSString *field, NSString *direction);
+FSTSortOrder *FSTTestOrderBy(const absl::string_view field, NSString *direction);
/**
* Creates an NSComparator that will compare FSTDocuments by the given fieldPath string then by
* key.
*/
-NSComparator FSTTestDocComparator(NSString *fieldPath);
+NSComparator FSTTestDocComparator(const absl::string_view fieldPath);
/**
* Creates a FSTDocumentSet based on the given comparator, initially containing the given
@@ -229,9 +240,10 @@ FSTViewSnapshot *_Nullable FSTTestApplyChanges(FSTView *view,
FSTSetMutation *FSTTestSetMutation(NSString *path, NSDictionary<NSString *, id> *values);
/** Creates a patch mutation for the document key at the given path. */
-FSTPatchMutation *FSTTestPatchMutation(NSString *path,
- NSDictionary<NSString *, id> *values,
- NSArray<FSTFieldPath *> *_Nullable updateMask);
+FSTPatchMutation *FSTTestPatchMutation(
+ const absl::string_view path,
+ NSDictionary<NSString *, id> *values,
+ const std::vector<firebase::firestore::model::FieldPath> &updateMask);
FSTTransformMutation *FSTTestTransformMutation(NSString *path,
NSArray<NSString *> *serverTimestampFields);
diff --git a/Firestore/Example/Tests/Util/FSTHelpers.mm b/Firestore/Example/Tests/Util/FSTHelpers.mm
index 00464bc..be02002 100644
--- a/Firestore/Example/Tests/Util/FSTHelpers.mm
+++ b/Firestore/Example/Tests/Util/FSTHelpers.mm
@@ -18,6 +18,8 @@
#include <inttypes.h>
#include <list>
+#include <map>
+#include <vector>
#import <FirebaseFirestore/FIRFieldPath.h>
#import <FirebaseFirestore/FIRGeoPoint.h>
@@ -36,18 +38,22 @@
#import "Firestore/Source/Model/FSTDocumentSet.h"
#import "Firestore/Source/Model/FSTFieldValue.h"
#import "Firestore/Source/Model/FSTMutation.h"
-#import "Firestore/Source/Model/FSTPath.h"
#import "Firestore/Source/Remote/FSTRemoteEvent.h"
#import "Firestore/Source/Remote/FSTWatchChange.h"
#import "Firestore/Source/Util/FSTAssert.h"
#include "Firestore/core/src/firebase/firestore/model/database_id.h"
+#include "Firestore/core/src/firebase/firestore/model/field_value.h"
+#include "Firestore/core/src/firebase/firestore/model/resource_path.h"
#include "Firestore/core/src/firebase/firestore/util/string_apple.h"
#include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
namespace util = firebase::firestore::util;
namespace testutil = firebase::firestore::testutil;
using firebase::firestore::model::DatabaseId;
+using firebase::firestore::model::FieldPath;
+using firebase::firestore::model::FieldValue;
+using firebase::firestore::model::ResourcePath;
NS_ASSUME_NONNULL_BEGIN
@@ -103,10 +109,6 @@ NSDateComponents *FSTTestDateComponents(
return comps;
}
-FSTFieldPath *FSTTestFieldPath(NSString *field) {
- return [FIRFieldPath pathWithDotSeparatedString:field].internalValue;
-}
-
FSTFieldValue *FSTTestFieldValue(id _Nullable value) {
// This owns the DatabaseIds since we do not have FirestoreClient instance to own them.
static DatabaseId database_id{"project", DatabaseId::kDefault};
@@ -162,18 +164,6 @@ FSTDeletedDocument *FSTTestDeletedDoc(NSString *path, FSTTestSnapshotVersion ver
return [FSTDeletedDocument documentWithKey:key version:FSTTestVersion(version)];
}
-static NSArray<NSString *> *FSTTestSplitPath(NSString *path) {
- if ([path isEqualToString:@""]) {
- return @[];
- } else {
- return [path componentsSeparatedByString:@"/"];
- }
-}
-
-FSTResourcePath *FSTTestPath(NSString *path) {
- return [FSTResourcePath pathWithSegments:FSTTestSplitPath(path)];
-}
-
FSTDocumentKeyReference *FSTTestRef(const absl::string_view projectID,
const absl::string_view database,
NSString *path) {
@@ -184,12 +174,12 @@ FSTDocumentKeyReference *FSTTestRef(const absl::string_view projectID,
databaseID:&database_ids.back()];
}
-FSTQuery *FSTTestQuery(NSString *path) {
- return [FSTQuery queryWithPath:[FSTTestPath(path) toCPPResourcePath]];
+FSTQuery *FSTTestQuery(const absl::string_view path) {
+ return [FSTQuery queryWithPath:testutil::Resource(path)];
}
-id<FSTFilter> FSTTestFilter(NSString *field, NSString *opString, id value) {
- FSTFieldPath *path = FSTTestFieldPath(field);
+id<FSTFilter> FSTTestFilter(const absl::string_view field, NSString *opString, id value) {
+ const FieldPath path = testutil::Field(field);
FSTRelationFilterOperator op;
if ([opString isEqualToString:@"<"]) {
op = FSTRelationFilterOperatorLessThan;
@@ -208,17 +198,17 @@ id<FSTFilter> FSTTestFilter(NSString *field, NSString *opString, id value) {
FSTFieldValue *data = FSTTestFieldValue(value);
if ([data isEqual:[FSTDoubleValue nanValue]]) {
FSTCAssert(op == FSTRelationFilterOperatorEqual, @"Must use == with NAN.");
- return [[FSTNanFilter alloc] initWithField:[path toCPPFieldPath]];
+ return [[FSTNanFilter alloc] initWithField:path];
} else if ([data isEqual:[FSTNullValue nullValue]]) {
FSTCAssert(op == FSTRelationFilterOperatorEqual, @"Must use == with Null.");
- return [[FSTNullFilter alloc] initWithField:[path toCPPFieldPath]];
+ return [[FSTNullFilter alloc] initWithField:path];
} else {
- return [FSTRelationFilter filterWithField:[path toCPPFieldPath] filterOperator:op value:data];
+ return [FSTRelationFilter filterWithField:path filterOperator:op value:data];
}
}
-FSTSortOrder *FSTTestOrderBy(NSString *field, NSString *direction) {
- FSTFieldPath *path = FSTTestFieldPath(field);
+FSTSortOrder *FSTTestOrderBy(const absl::string_view field, NSString *direction) {
+ const FieldPath path = testutil::Field(field);
BOOL ascending;
if ([direction isEqualToString:@"asc"]) {
ascending = YES;
@@ -227,14 +217,13 @@ FSTSortOrder *FSTTestOrderBy(NSString *field, NSString *direction) {
} else {
FSTCFail(@"Unsupported direction: %@", direction);
}
- return [FSTSortOrder sortOrderWithFieldPath:[path toCPPFieldPath] ascending:ascending];
+ return [FSTSortOrder sortOrderWithFieldPath:path ascending:ascending];
}
-NSComparator FSTTestDocComparator(NSString *fieldPath) {
- FSTQuery *query = [FSTTestQuery(@"docs")
- queryByAddingSortOrder:[FSTSortOrder
- sortOrderWithFieldPath:[FSTTestFieldPath(fieldPath) toCPPFieldPath]
- ascending:YES]];
+NSComparator FSTTestDocComparator(const absl::string_view fieldPath) {
+ FSTQuery *query = [FSTTestQuery("docs")
+ queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:testutil::Field(fieldPath)
+ ascending:YES]];
return [query comparator];
}
@@ -252,23 +241,23 @@ FSTSetMutation *FSTTestSetMutation(NSString *path, NSDictionary<NSString *, id>
precondition:[FSTPrecondition none]];
}
-FSTPatchMutation *FSTTestPatchMutation(NSString *path,
+FSTPatchMutation *FSTTestPatchMutation(const absl::string_view path,
NSDictionary<NSString *, id> *values,
- NSArray<FSTFieldPath *> *_Nullable updateMask) {
- BOOL merge = updateMask != nil;
+ const std::vector<FieldPath> &updateMask) {
+ BOOL merge = !updateMask.empty();
__block FSTObjectValue *objectValue = [FSTObjectValue objectValue];
- NSMutableArray<FSTFieldPath *> *fieldMaskPaths = [NSMutableArray array];
+ __block std::vector<FieldPath> fieldMaskPaths{};
[values enumerateKeysAndObjectsUsingBlock:^(NSString *key, id value, BOOL *stop) {
- FSTFieldPath *path = FSTTestFieldPath(key);
- [fieldMaskPaths addObject:path];
+ const FieldPath path = testutil::Field(util::MakeStringView(key));
+ fieldMaskPaths.push_back(path);
if (![value isEqual:kDeleteSentinel]) {
FSTFieldValue *parsedValue = FSTTestFieldValue(value);
objectValue = [objectValue objectBySettingValue:parsedValue forPath:path];
}
}];
- FSTDocumentKey *key = [FSTDocumentKey keyWithPath:testutil::Resource([path UTF8String])];
+ FSTDocumentKey *key = [FSTDocumentKey keyWithPath:testutil::Resource(path)];
FSTFieldMask *mask = [[FSTFieldMask alloc] initWithFields:merge ? updateMask : fieldMaskPaths];
return [[FSTPatchMutation alloc] initWithKey:key
fieldMask:mask
@@ -279,10 +268,10 @@ FSTPatchMutation *FSTTestPatchMutation(NSString *path,
// For now this only creates TransformMutations with server timestamps.
FSTTransformMutation *FSTTestTransformMutation(NSString *path,
NSArray<NSString *> *serverTimestampFields) {
- FSTDocumentKey *key = [FSTDocumentKey keyWithPath:testutil::Resource([path UTF8String])];
+ FSTDocumentKey *key = [FSTDocumentKey keyWithPath:testutil::Resource(util::MakeStringView(path))];
NSMutableArray<FSTFieldTransform *> *fieldTransforms = [NSMutableArray array];
for (NSString *field in serverTimestampFields) {
- FSTFieldPath *fieldPath = FSTTestFieldPath(field);
+ const FieldPath fieldPath = testutil::Field(util::MakeStringView(field));
id<FSTTransformOperation> transformOp = [FSTServerTimestampTransform serverTimestampTransform];
FSTFieldTransform *transform =
[[FSTFieldTransform alloc] initWithPath:fieldPath transform:transformOp];