FSTHelpers.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 <unordered_map>
  19. #include <vector>
  20. #include "Firestore/core/src/firebase/firestore/core/filter.h"
  21. #include "Firestore/core/src/firebase/firestore/core/view.h"
  22. #include "Firestore/core/src/firebase/firestore/core/view_snapshot.h"
  23. #include "Firestore/core/src/firebase/firestore/local/local_view_changes.h"
  24. #include "Firestore/core/src/firebase/firestore/local/query_data.h"
  25. #include "Firestore/core/src/firebase/firestore/model/delete_mutation.h"
  26. #include "Firestore/core/src/firebase/firestore/model/document.h"
  27. #include "Firestore/core/src/firebase/firestore/model/document_map.h"
  28. #include "Firestore/core/src/firebase/firestore/model/document_set.h"
  29. #include "Firestore/core/src/firebase/firestore/model/field_path.h"
  30. #include "Firestore/core/src/firebase/firestore/model/field_value.h"
  31. #include "Firestore/core/src/firebase/firestore/model/maybe_document.h"
  32. #include "Firestore/core/src/firebase/firestore/model/mutation.h"
  33. #include "Firestore/core/src/firebase/firestore/model/no_document.h"
  34. #include "Firestore/core/src/firebase/firestore/model/patch_mutation.h"
  35. #include "Firestore/core/src/firebase/firestore/model/resource_path.h"
  36. #include "Firestore/core/src/firebase/firestore/model/set_mutation.h"
  37. #include "Firestore/core/src/firebase/firestore/model/transform_mutation.h"
  38. #include "Firestore/core/src/firebase/firestore/model/types.h"
  39. #include "Firestore/core/src/firebase/firestore/model/unknown_document.h"
  40. #include "Firestore/core/src/firebase/firestore/remote/remote_event.h"
  41. #include "absl/strings/string_view.h"
  42. #include "absl/types/optional.h"
  43. @class FIRGeoPoint;
  44. @class FIRTimestamp;
  45. @class FSTDocumentKeyReference;
  46. @class FSTUserDataConverter;
  47. namespace firebase {
  48. namespace firestore {
  49. namespace remote {
  50. class RemoteEvent;
  51. } // namespace remote
  52. } // namespace firestore
  53. } // namespace firebase
  54. namespace core = firebase::firestore::core;
  55. namespace local = firebase::firestore::local;
  56. namespace model = firebase::firestore::model;
  57. NS_ASSUME_NONNULL_BEGIN
  58. /**
  59. * Takes an array of "equality group" arrays and asserts that the compare: selector returns the
  60. * same as compare: on the indexes of the "equality groups" (NSOrderedSame for items in the same
  61. * group).
  62. */
  63. #define FSTAssertComparisons(values) \
  64. do { \
  65. for (NSUInteger i = 0; i < [values count]; i++) { \
  66. for (id left in values[i]) { \
  67. for (NSUInteger j = 0; j < [values count]; j++) { \
  68. for (id right in values[j]) { \
  69. NSComparisonResult expected = [@(i) compare:@(j)]; \
  70. NSComparisonResult result = [left compare:right]; \
  71. NSComparisonResult inverseResult = [right compare:left]; \
  72. XCTAssertEqual(result, expected, @"comparing %@ with %@ at (%lu, %lu)", left, right, \
  73. (unsigned long)i, (unsigned long)j); \
  74. XCTAssertEqual(inverseResult, -expected, @"comparing %@ with %@ at (%lu, %lu)", right, \
  75. left, (unsigned long)j, (unsigned long)i); \
  76. } \
  77. } \
  78. } \
  79. } \
  80. } while (0)
  81. static NSString *kExceptionPrefix = @"FIRESTORE INTERNAL ASSERTION FAILED: ";
  82. // Remove possible exception-prefix.
  83. inline NSString *FSTRemoveExceptionPrefix(NSString *exception) {
  84. if ([exception hasPrefix:kExceptionPrefix]) {
  85. return [exception substringFromIndex:kExceptionPrefix.length];
  86. } else {
  87. return exception;
  88. }
  89. }
  90. // Helper for validating API exceptions.
  91. #define FSTAssertThrows(expression, exceptionReason, ...) \
  92. do { \
  93. BOOL didThrow = NO; \
  94. @try { \
  95. (void)(expression); \
  96. } @catch (NSException * exception) { \
  97. didThrow = YES; \
  98. XCTAssertEqualObjects(FSTRemoveExceptionPrefix(exception.reason), \
  99. FSTRemoveExceptionPrefix(exceptionReason)); \
  100. } \
  101. XCTAssertTrue(didThrow, ##__VA_ARGS__); \
  102. } while (0)
  103. /** Creates a new NSDate from components. Note that year, month, and day are all one-based. */
  104. NSDate *FSTTestDate(int year, int month, int day, int hour, int minute, int second);
  105. /**
  106. * Creates a new NSData from the var args of bytes, must be terminated with a negative byte
  107. */
  108. NSData *FSTTestData(int bytes, ...);
  109. // Note that FIRGeoPoint is a model class in addition to an API class, so we put this helper here
  110. // instead of FSTAPIHelpers.h
  111. /** Creates a new GeoPoint from the latitude and longitude values */
  112. FIRGeoPoint *FSTTestGeoPoint(double latitude, double longitude);
  113. /** Creates a user data converter set up for a generic project. */
  114. FSTUserDataConverter *FSTTestUserDataConverter();
  115. /**
  116. * Creates a new NSDateComponents from components. Note that year, month, and day are all
  117. * one-based.
  118. */
  119. NSDateComponents *FSTTestDateComponents(
  120. int year, int month, int day, int hour, int minute, int second);
  121. /** Wraps a plain value into an FieldValue instance. */
  122. model::FieldValue FSTTestFieldValue(id _Nullable value);
  123. /** Wraps a NSDictionary value into an ObjectValue instance. */
  124. model::ObjectValue FSTTestObjectValue(NSDictionary<NSString *, id> *data);
  125. /** A convenience method for creating document keys for tests. */
  126. firebase::firestore::model::DocumentKey FSTTestDocKey(NSString *path);
  127. /** Allow tests to just use an int literal for versions. */
  128. typedef int64_t FSTTestSnapshotVersion;
  129. /**
  130. * A convenience method for creating a document reference from a path string.
  131. */
  132. FSTDocumentKeyReference *FSTTestRef(std::string projectID, std::string databaseID, NSString *path);
  133. /** Creates a set mutation for the document key at the given path. */
  134. model::SetMutation FSTTestSetMutation(NSString *path, NSDictionary<NSString *, id> *values);
  135. /** Creates a patch mutation for the document key at the given path. */
  136. model::PatchMutation FSTTestPatchMutation(
  137. absl::string_view path,
  138. NSDictionary<NSString *, id> *values,
  139. const std::vector<firebase::firestore::model::FieldPath> &updateMask);
  140. /**
  141. * Creates a TransformMutation by parsing any FIRFieldValue sentinels in the provided data. The
  142. * data is expected to use dotted-notation for nested fields (i.e.
  143. * @{ @"foo.bar": [FIRFieldValue ...] } and must not contain any non-sentinel data.
  144. */
  145. model::TransformMutation FSTTestTransformMutation(NSString *path,
  146. NSDictionary<NSString *, id> *data);
  147. /** Creates a delete mutation for the document key at the given path. */
  148. model::DeleteMutation FSTTestDeleteMutation(NSString *path);
  149. NS_ASSUME_NONNULL_END