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