FSTHelpers.h 6.8 KB

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