FSTHelpers.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. // Helper for validating API exceptions.
  61. #define FSTAssertThrows(expression, exceptionReason, ...) \
  62. do { \
  63. BOOL didThrow = NO; \
  64. @try { \
  65. (void)(expression); \
  66. } @catch (NSException * exception) { \
  67. didThrow = YES; \
  68. XCTAssertEqualObjects(FSTRemoveExceptionPrefix(exception.reason), \
  69. FSTRemoveExceptionPrefix(exceptionReason)); \
  70. } \
  71. XCTAssertTrue(didThrow, ##__VA_ARGS__); \
  72. } while (0)
  73. /** Creates a new NSDate from components. Note that year, month, and day are all one-based. */
  74. NSDate *FSTTestDate(int year, int month, int day, int hour, int minute, int second);
  75. /**
  76. * Creates a new NSData from the var args of bytes, must be terminated with a negative byte
  77. */
  78. NSData *FSTTestData(int bytes, ...);
  79. // Note that FIRGeoPoint is a model class in addition to an API class, so we put this helper here
  80. // instead of FSTAPIHelpers.h
  81. /** Creates a new GeoPoint from the latitude and longitude values */
  82. FIRGeoPoint *FSTTestGeoPoint(double latitude, double longitude);
  83. /** Creates a user data converter set up for a generic project. */
  84. FSTUserDataReader *FSTTestUserDataReader();
  85. /**
  86. * Creates a new NSDateComponents from components. Note that year, month, and day are all
  87. * one-based.
  88. */
  89. NSDateComponents *FSTTestDateComponents(
  90. int year, int month, int day, int hour, int minute, int second);
  91. /** Wraps a plain value into a Message proto. */
  92. firebase::firestore::nanopb::Message<firebase::firestore::google_firestore_v1_Value>
  93. FSTTestFieldValue(id _Nullable value);
  94. /** Wraps a NSDictionary value into an ObjectValue instance. */
  95. model::ObjectValue FSTTestObjectValue(NSDictionary<NSString *, id> *data);
  96. /** A convenience method for creating document keys for tests. */
  97. model::DocumentKey FSTTestDocKey(NSString *path);
  98. /** Allow tests to just use an int literal for versions. */
  99. typedef int64_t FSTTestSnapshotVersion;
  100. /**
  101. * A convenience method for creating a document reference from a path string.
  102. */
  103. FSTDocumentKeyReference *FSTTestRef(std::string projectID, std::string databaseID, NSString *path);
  104. /** Creates a set mutation for the document key at the given path. */
  105. model::SetMutation FSTTestSetMutation(NSString *path, NSDictionary<NSString *, id> *values);
  106. /** Creates a patch mutation for the document key at the given path. */
  107. model::PatchMutation FSTTestPatchMutation(
  108. NSString *path,
  109. NSDictionary<NSString *, id> *values,
  110. const std::vector<firebase::firestore::model::FieldPath> &updateMask);
  111. /** Creates a delete mutation for the document key at the given path. */
  112. model::DeleteMutation FSTTestDeleteMutation(NSString *path);
  113. NS_ASSUME_NONNULL_END