FSTHelpers.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. #import "Firestore/Source/API/FIRDocumentReference+Internal.h"
  18. #import "Firestore/Source/Core/FSTTypes.h"
  19. #import "Firestore/Source/Model/FSTDocumentDictionary.h"
  20. #import "Firestore/Source/Model/FSTDocumentKeySet.h"
  21. @class FIRGeoPoint;
  22. @class FSTDeleteMutation;
  23. @class FSTDeletedDocument;
  24. @class FSTDocument;
  25. @class FSTDocumentKeyReference;
  26. @class FSTDocumentSet;
  27. @class FSTFieldPath;
  28. @class FSTFieldValue;
  29. @class FSTLocalViewChanges;
  30. @class FSTPatchMutation;
  31. @class FSTQuery;
  32. @class FSTRemoteEvent;
  33. @class FSTResourceName;
  34. @class FSTResourcePath;
  35. @class FSTSetMutation;
  36. @class FSTSnapshotVersion;
  37. @class FSTSortOrder;
  38. @class FSTTargetChange;
  39. @class FSTTimestamp;
  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. // Helper for validating API exceptions.
  115. #define FSTAssertThrows(expression, exceptionReason, ...) \
  116. ({ \
  117. BOOL __didThrow = NO; \
  118. @try { \
  119. (void)(expression); \
  120. } @catch (NSException * exception) { \
  121. __didThrow = YES; \
  122. XCTAssertEqualObjects(exception.reason, exceptionReason); \
  123. } \
  124. XCTAssertTrue(__didThrow, ##__VA_ARGS__); \
  125. })
  126. /** Creates a new FSTTimestamp from components. Note that year, month, and day are all one-based. */
  127. FSTTimestamp *FSTTestTimestamp(int year, int month, int day, int hour, int minute, int second);
  128. /** Creates a new NSDate from components. Note that year, month, and day are all one-based. */
  129. NSDate *FSTTestDate(int year, int month, int day, int hour, int minute, int second);
  130. /**
  131. * Creates a new NSData from the var args of bytes, must be terminated with a negative byte
  132. */
  133. NSData *FSTTestData(int bytes, ...);
  134. /** Creates a new GeoPoint from the latitude and longitude values */
  135. FIRGeoPoint *FSTTestGeoPoint(double latitude, double longitude);
  136. /**
  137. * Creates a new NSDateComponents from components. Note that year, month, and day are all
  138. * one-based.
  139. */
  140. NSDateComponents *FSTTestDateComponents(
  141. int year, int month, int day, int hour, int minute, int second);
  142. FSTFieldPath *FSTTestFieldPath(NSString *field);
  143. /** Wraps a plain value into an FSTFieldValue instance. */
  144. FSTFieldValue *FSTTestFieldValue(id _Nullable value);
  145. /** Wraps a NSDictionary value into an FSTObjectValue instance. */
  146. FSTObjectValue *FSTTestObjectValue(NSDictionary<NSString *, id> *data);
  147. /** A convenience method for creating document keys for tests. */
  148. FSTDocumentKey *FSTTestDocKey(NSString *path);
  149. /** A convenience method for creating a document key set for tests. */
  150. FSTDocumentKeySet *FSTTestDocKeySet(NSArray<FSTDocumentKey *> *keys);
  151. /** Allow tests to just use an int literal for versions. */
  152. typedef int64_t FSTTestSnapshotVersion;
  153. /** A convenience method for creating snapshot versions for tests. */
  154. FSTSnapshotVersion *FSTTestVersion(FSTTestSnapshotVersion version);
  155. /** A convenience method for creating docs for tests. */
  156. FSTDocument *FSTTestDoc(NSString *path,
  157. FSTTestSnapshotVersion version,
  158. NSDictionary<NSString *, id> *data,
  159. BOOL hasMutations);
  160. /** A convenience method for creating deleted docs for tests. */
  161. FSTDeletedDocument *FSTTestDeletedDoc(NSString *path, FSTTestSnapshotVersion version);
  162. /** A convenience method for creating resource paths from a path string. */
  163. FSTResourcePath *FSTTestPath(NSString *path);
  164. /**
  165. * A convenience method for creating a document reference from a path string.
  166. */
  167. FSTDocumentKeyReference *FSTTestRef(NSString *projectID, NSString *databaseID, NSString *path);
  168. /** A convenience method for creating a query for the given path (without any other filters). */
  169. FSTQuery *FSTTestQuery(NSString *path);
  170. /**
  171. * A convenience method to create a FSTFilter using a string representation for both field
  172. * and operator (<, <=, ==, >=, >).
  173. */
  174. id<FSTFilter> FSTTestFilter(NSString *field, NSString *op, id value);
  175. /** A convenience method for creating sort orders. */
  176. FSTSortOrder *FSTTestOrderBy(NSString *field, NSString *direction);
  177. /**
  178. * Creates an NSComparator that will compare FSTDocuments by the given fieldPath string then by
  179. * key.
  180. */
  181. NSComparator FSTTestDocComparator(NSString *fieldPath);
  182. /**
  183. * Creates a FSTDocumentSet based on the given comparator, initially containing the given
  184. * documents.
  185. */
  186. FSTDocumentSet *FSTTestDocSet(NSComparator comp, NSArray<FSTDocument *> *docs);
  187. /** Computes changes to the view with the docs and then applies them and returns the snapshot. */
  188. FSTViewSnapshot *_Nullable FSTTestApplyChanges(FSTView *view,
  189. NSArray<FSTMaybeDocument *> *docs,
  190. FSTTargetChange *_Nullable targetChange);
  191. /** Creates a set mutation for the document key at the given path. */
  192. FSTSetMutation *FSTTestSetMutation(NSString *path, NSDictionary<NSString *, id> *values);
  193. /** Creates a patch mutation for the document key at the given path. */
  194. FSTPatchMutation *FSTTestPatchMutation(NSString *path,
  195. NSDictionary<NSString *, id> *values,
  196. NSArray<FSTFieldPath *> *_Nullable updateMask);
  197. FSTTransformMutation *FSTTestTransformMutation(NSString *path,
  198. NSArray<NSString *> *serverTimestampFields);
  199. /** Creates a delete mutation for the document key at the given path. */
  200. FSTDeleteMutation *FSTTestDeleteMutation(NSString *path);
  201. /** Converts a list of documents to a sorted map. */
  202. FSTMaybeDocumentDictionary *FSTTestDocUpdates(NSArray<FSTMaybeDocument *> *docs);
  203. /** Creates a remote event with changes to a document. */
  204. FSTRemoteEvent *FSTTestUpdateRemoteEvent(FSTMaybeDocument *doc,
  205. NSArray<NSNumber *> *updatedInTargets,
  206. NSArray<NSNumber *> *removedFromTargets);
  207. /** Creates a test view changes. */
  208. FSTLocalViewChanges *FSTTestViewChanges(FSTQuery *query,
  209. NSArray<NSString *> *addedKeys,
  210. NSArray<NSString *> *removedKeys);
  211. /** Creates a resume token to match the given snapshot version. */
  212. NSData *_Nullable FSTTestResumeTokenFromSnapshotVersion(FSTTestSnapshotVersion watchSnapshot);
  213. #if __cplusplus
  214. } // extern "C"
  215. #endif
  216. NS_ASSUME_NONNULL_END