FSTHelpers.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <Foundation/Foundation.h>
  17. #include <string>
  18. #include <vector>
  19. #include "Firestore/core/src/model/model_fwd.h"
  20. #include "absl/strings/string_view.h"
  21. @class FIRGeoPoint;
  22. @class FSTDocumentKeyReference;
  23. @class FSTUserDataConverter;
  24. namespace model = firebase::firestore::model;
  25. NS_ASSUME_NONNULL_BEGIN
  26. /**
  27. * Takes an array of "equality group" arrays and asserts that the compare: selector returns the
  28. * same as compare: on the indexes of the "equality groups" (NSOrderedSame for items in the same
  29. * group).
  30. */
  31. #define FSTAssertComparisons(values) \
  32. do { \
  33. for (NSUInteger i = 0; i < [values count]; i++) { \
  34. for (id left in values[i]) { \
  35. for (NSUInteger j = 0; j < [values count]; j++) { \
  36. for (id right in values[j]) { \
  37. NSComparisonResult expected = [@(i) compare:@(j)]; \
  38. NSComparisonResult result = [left compare:right]; \
  39. NSComparisonResult inverseResult = [right compare:left]; \
  40. XCTAssertEqual(result, expected, @"comparing %@ with %@ at (%lu, %lu)", left, right, \
  41. (unsigned long)i, (unsigned long)j); \
  42. XCTAssertEqual(inverseResult, -expected, @"comparing %@ with %@ at (%lu, %lu)", right, \
  43. left, (unsigned long)j, (unsigned long)i); \
  44. } \
  45. } \
  46. } \
  47. } \
  48. } while (0)
  49. static NSString *kExceptionPrefix = @"FIRESTORE INTERNAL ASSERTION FAILED: ";
  50. // Remove possible exception-prefix.
  51. inline NSString *FSTRemoveExceptionPrefix(NSString *exception) {
  52. if ([exception hasPrefix:kExceptionPrefix]) {
  53. return [exception substringFromIndex:kExceptionPrefix.length];
  54. } else {
  55. return exception;
  56. }
  57. }
  58. // Helper for validating API exceptions.
  59. #define FSTAssertThrows(expression, exceptionReason, ...) \
  60. do { \
  61. BOOL didThrow = NO; \
  62. @try { \
  63. (void)(expression); \
  64. } @catch (NSException * exception) { \
  65. didThrow = YES; \
  66. XCTAssertEqualObjects(FSTRemoveExceptionPrefix(exception.reason), \
  67. FSTRemoveExceptionPrefix(exceptionReason)); \
  68. } \
  69. XCTAssertTrue(didThrow, ##__VA_ARGS__); \
  70. } while (0)
  71. /** Creates a new NSDate from components. Note that year, month, and day are all one-based. */
  72. NSDate *FSTTestDate(int year, int month, int day, int hour, int minute, int second);
  73. /**
  74. * Creates a new NSData from the var args of bytes, must be terminated with a negative byte
  75. */
  76. NSData *FSTTestData(int bytes, ...);
  77. // Note that FIRGeoPoint is a model class in addition to an API class, so we put this helper here
  78. // instead of FSTAPIHelpers.h
  79. /** Creates a new GeoPoint from the latitude and longitude values */
  80. FIRGeoPoint *FSTTestGeoPoint(double latitude, double longitude);
  81. /** Creates a user data converter set up for a generic project. */
  82. FSTUserDataConverter *FSTTestUserDataConverter();
  83. /**
  84. * Creates a new NSDateComponents from components. Note that year, month, and day are all
  85. * one-based.
  86. */
  87. NSDateComponents *FSTTestDateComponents(
  88. int year, int month, int day, int hour, int minute, int second);
  89. /** Wraps a plain value into an FieldValue instance. */
  90. model::FieldValue FSTTestFieldValue(id _Nullable value);
  91. /** Wraps a NSDictionary value into an ObjectValue instance. */
  92. model::ObjectValue FSTTestObjectValue(NSDictionary<NSString *, id> *data);
  93. /** A convenience method for creating document keys for tests. */
  94. model::DocumentKey FSTTestDocKey(NSString *path);
  95. /** Allow tests to just use an int literal for versions. */
  96. typedef int64_t FSTTestSnapshotVersion;
  97. /**
  98. * A convenience method for creating a document reference from a path string.
  99. */
  100. FSTDocumentKeyReference *FSTTestRef(std::string projectID, std::string databaseID, NSString *path);
  101. /** Creates a set mutation for the document key at the given path. */
  102. model::SetMutation FSTTestSetMutation(NSString *path, NSDictionary<NSString *, id> *values);
  103. /** Creates a patch mutation for the document key at the given path. */
  104. model::PatchMutation FSTTestPatchMutation(
  105. absl::string_view path,
  106. NSDictionary<NSString *, id> *values,
  107. const std::vector<firebase::firestore::model::FieldPath> &updateMask);
  108. /**
  109. * Creates a TransformMutation by parsing any FIRFieldValue sentinels in the provided data. The
  110. * data is expected to use dotted-notation for nested fields (i.e.
  111. * @{ @"foo.bar": [FIRFieldValue ...] } and must not contain any non-sentinel data.
  112. */
  113. model::TransformMutation FSTTestTransformMutation(NSString *path,
  114. NSDictionary<NSString *, id> *data);
  115. /** Creates a delete mutation for the document key at the given path. */
  116. model::DeleteMutation FSTTestDeleteMutation(NSString *path);
  117. NS_ASSUME_NONNULL_END