FSTHelpers.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 <map>
  18. #include <vector>
  19. #import "Firestore/Source/Core/FSTTypes.h"
  20. #import "Firestore/Source/Model/FSTDocumentDictionary.h"
  21. #include "Firestore/core/src/firebase/firestore/model/field_path.h"
  22. #include "Firestore/core/src/firebase/firestore/model/field_value.h"
  23. #include "Firestore/core/src/firebase/firestore/model/resource_path.h"
  24. #include "absl/strings/string_view.h"
  25. @class FIRGeoPoint;
  26. @class FSTDeleteMutation;
  27. @class FSTDeletedDocument;
  28. @class FSTDocument;
  29. @class FSTDocumentKeyReference;
  30. @class FSTDocumentSet;
  31. @class FSTFieldValue;
  32. @class FSTLocalViewChanges;
  33. @class FSTPatchMutation;
  34. @class FSTQuery;
  35. @class FSTRemoteEvent;
  36. @class FSTSetMutation;
  37. @class FSTSortOrder;
  38. @class FSTTargetChange;
  39. @class FIRTimestamp;
  40. @class FSTTransformMutation;
  41. @class FSTView;
  42. @class FSTViewSnapshot;
  43. @class FSTObjectValue;
  44. @protocol FSTFilter;
  45. NS_ASSUME_NONNULL_BEGIN
  46. #if __cplusplus
  47. extern "C" {
  48. #endif
  49. #define FSTAssertIsKindOfClass(value, classType) \
  50. do { \
  51. XCTAssertEqualObjects([value class], [classType class]); \
  52. } while (0);
  53. /**
  54. * Asserts that the given NSSet of FSTDocumentKeys contains exactly the given expected keys.
  55. * This is a macro instead of a method so that the failure shows up on the right line.
  56. *
  57. * @param actualSet An NSSet of FSTDocumentKeys.
  58. * @param expectedArray A sorted array of keys that actualSet must be equal to (after converting
  59. * to an array and sorting).
  60. */
  61. #define FSTAssertEqualSets(actualSet, expectedArray) \
  62. do { \
  63. NSArray<FSTDocumentKey *> *actual = [(actualSet)allObjects]; \
  64. actual = [actual sortedArrayUsingSelector:@selector(compare:)]; \
  65. XCTAssertEqualObjects(actual, (expectedArray)); \
  66. } while (0)
  67. /**
  68. * Takes an array of "equality group" arrays and asserts that the compare: selector returns the
  69. * same as compare: on the indexes of the "equality groups" (NSOrderedSame for items in the same
  70. * group).
  71. */
  72. #define FSTAssertComparisons(values) \
  73. do { \
  74. for (NSUInteger i = 0; i < [values count]; i++) { \
  75. for (id left in values[i]) { \
  76. for (NSUInteger j = 0; j < [values count]; j++) { \
  77. for (id right in values[j]) { \
  78. NSComparisonResult expected = [@(i) compare:@(j)]; \
  79. NSComparisonResult result = [left compare:right]; \
  80. NSComparisonResult inverseResult = [right compare:left]; \
  81. XCTAssertEqual(result, expected, @"comparing %@ with %@ at (%lu, %lu)", left, right, \
  82. i, j); \
  83. XCTAssertEqual(inverseResult, -expected, @"comparing %@ with %@ at (%lu, %lu)", right, \
  84. left, j, i); \
  85. } \
  86. } \
  87. } \
  88. } \
  89. } while (0)
  90. /**
  91. * Takes an array of "equality group" arrays and asserts that the isEqual: selector returns TRUE
  92. * if-and-only-if items are in the same group.
  93. *
  94. * Additionally checks that the hash: selector returns the same value for items in the same group.
  95. */
  96. #define FSTAssertEqualityGroups(values) \
  97. do { \
  98. for (NSUInteger i = 0; i < [values count]; i++) { \
  99. for (id left in values[i]) { \
  100. for (NSUInteger j = 0; j < [values count]; j++) { \
  101. for (id right in values[j]) { \
  102. if (i == j) { \
  103. XCTAssertEqualObjects(left, right); \
  104. XCTAssertEqual([left hash], [right hash], @"comparing hash of %@ with hash of %@", \
  105. left, right); \
  106. } else { \
  107. XCTAssertNotEqualObjects(left, right); \
  108. } \
  109. } \
  110. } \
  111. } \
  112. } \
  113. } while (0)
  114. static NSString *kExceptionPrefix = @"FIRESTORE INTERNAL ASSERTION FAILED: ";
  115. // Remove possible exception-prefix.
  116. inline NSString *FSTRemoveExceptionPrefix(NSString *exception) {
  117. if ([exception hasPrefix:kExceptionPrefix]) {
  118. return [exception substringFromIndex:kExceptionPrefix.length];
  119. } else {
  120. return exception;
  121. }
  122. }
  123. // Helper for validating API exceptions.
  124. #define FSTAssertThrows(expression, exceptionReason, ...) \
  125. do { \
  126. BOOL didThrow = NO; \
  127. @try { \
  128. (void)(expression); \
  129. } @catch (NSException * exception) { \
  130. didThrow = YES; \
  131. XCTAssertEqualObjects(FSTRemoveExceptionPrefix(exception.reason), \
  132. FSTRemoveExceptionPrefix(exceptionReason)); \
  133. } \
  134. XCTAssertTrue(didThrow, ##__VA_ARGS__); \
  135. } while (0)
  136. /** Creates a new FIRTimestamp from components. Note that year, month, and day are all one-based. */
  137. FIRTimestamp *FSTTestTimestamp(int year, int month, int day, int hour, int minute, int second);
  138. /** Creates a new NSDate from components. Note that year, month, and day are all one-based. */
  139. NSDate *FSTTestDate(int year, int month, int day, int hour, int minute, int second);
  140. /**
  141. * Creates a new NSData from the var args of bytes, must be terminated with a negative byte
  142. */
  143. NSData *FSTTestData(int bytes, ...);
  144. // Note that FIRGeoPoint is a model class in addition to an API class, so we put this helper here
  145. // instead of FSTAPIHelpers.h
  146. /** Creates a new GeoPoint from the latitude and longitude values */
  147. FIRGeoPoint *FSTTestGeoPoint(double latitude, double longitude);
  148. /**
  149. * Creates a new NSDateComponents from components. Note that year, month, and day are all
  150. * one-based.
  151. */
  152. NSDateComponents *FSTTestDateComponents(
  153. int year, int month, int day, int hour, int minute, int second);
  154. /** Wraps a plain value into an FSTFieldValue instance. */
  155. FSTFieldValue *FSTTestFieldValue(id _Nullable value);
  156. /** Wraps a NSDictionary value into an FSTObjectValue instance. */
  157. FSTObjectValue *FSTTestObjectValue(NSDictionary<NSString *, id> *data);
  158. /** A convenience method for creating document keys for tests. */
  159. FSTDocumentKey *FSTTestDocKey(NSString *path);
  160. /** Allow tests to just use an int literal for versions. */
  161. typedef int64_t FSTTestSnapshotVersion;
  162. /** A convenience method for creating docs for tests. */
  163. FSTDocument *FSTTestDoc(const absl::string_view path,
  164. FSTTestSnapshotVersion version,
  165. NSDictionary<NSString *, id> *data,
  166. BOOL hasMutations);
  167. /** A convenience method for creating deleted docs for tests. */
  168. FSTDeletedDocument *FSTTestDeletedDoc(const absl::string_view path, FSTTestSnapshotVersion version);
  169. /**
  170. * A convenience method for creating a document reference from a path string.
  171. */
  172. FSTDocumentKeyReference *FSTTestRef(const absl::string_view projectID,
  173. const absl::string_view databaseID,
  174. NSString *path);
  175. /** A convenience method for creating a query for the given path (without any other filters). */
  176. FSTQuery *FSTTestQuery(const absl::string_view path);
  177. /**
  178. * A convenience method to create a FSTFilter using a string representation for both field
  179. * and operator (<, <=, ==, >=, >, array_contains).
  180. */
  181. id<FSTFilter> FSTTestFilter(const absl::string_view field, NSString *op, id value);
  182. /** A convenience method for creating sort orders. */
  183. FSTSortOrder *FSTTestOrderBy(const absl::string_view field, NSString *direction);
  184. /**
  185. * Creates an NSComparator that will compare FSTDocuments by the given fieldPath string then by
  186. * key.
  187. */
  188. NSComparator FSTTestDocComparator(const absl::string_view fieldPath);
  189. /**
  190. * Creates a FSTDocumentSet based on the given comparator, initially containing the given
  191. * documents.
  192. */
  193. FSTDocumentSet *FSTTestDocSet(NSComparator comp, NSArray<FSTDocument *> *docs);
  194. /** Computes changes to the view with the docs and then applies them and returns the snapshot. */
  195. FSTViewSnapshot *_Nullable FSTTestApplyChanges(FSTView *view,
  196. NSArray<FSTMaybeDocument *> *docs,
  197. FSTTargetChange *_Nullable targetChange);
  198. /** Creates a set mutation for the document key at the given path. */
  199. FSTSetMutation *FSTTestSetMutation(NSString *path, NSDictionary<NSString *, id> *values);
  200. /** Creates a patch mutation for the document key at the given path. */
  201. FSTPatchMutation *FSTTestPatchMutation(
  202. const absl::string_view path,
  203. NSDictionary<NSString *, id> *values,
  204. const std::vector<firebase::firestore::model::FieldPath> &updateMask);
  205. /**
  206. * Creates a FSTTransformMutation by parsing any FIRFieldValue sentinels in the provided data. The
  207. * data is expected to use dotted-notation for nested fields (i.e.
  208. * @{ @"foo.bar": [FIRFieldValue ...] } and must not contain any non-sentinel data.
  209. */
  210. FSTTransformMutation *FSTTestTransformMutation(NSString *path, NSDictionary<NSString *, id> *data);
  211. /** Creates a delete mutation for the document key at the given path. */
  212. FSTDeleteMutation *FSTTestDeleteMutation(NSString *path);
  213. /** Converts a list of documents to a sorted map. */
  214. FSTMaybeDocumentDictionary *FSTTestDocUpdates(NSArray<FSTMaybeDocument *> *docs);
  215. /** Creates a remote event with changes to a document. */
  216. FSTRemoteEvent *FSTTestUpdateRemoteEvent(FSTMaybeDocument *doc,
  217. NSArray<NSNumber *> *updatedInTargets,
  218. NSArray<NSNumber *> *removedFromTargets);
  219. /** Creates a test view changes. */
  220. FSTLocalViewChanges *FSTTestViewChanges(FSTQuery *query,
  221. NSArray<NSString *> *addedKeys,
  222. NSArray<NSString *> *removedKeys);
  223. /** Creates a resume token to match the given snapshot version. */
  224. NSData *_Nullable FSTTestResumeTokenFromSnapshotVersion(FSTTestSnapshotVersion watchSnapshot);
  225. #if __cplusplus
  226. } // extern "C"
  227. #endif
  228. NS_ASSUME_NONNULL_END