Bladeren bron

Change some tests to validate against message prefix only (#8839)

wu-hui 4 jaren geleden
bovenliggende
commit
554ad41247

+ 6 - 4
Firestore/Example/Tests/Integration/API/FIRValidationTests.mm

@@ -556,8 +556,9 @@ namespace testutil = firebase::firestore::testutil;
       [collection queryWhereFieldPath:[FIRFieldPath documentID] isEqualTo:@"foo/bar/baz"], reason);
 
   reason = @"Invalid query. When querying by document ID you must provide a valid string or "
-            "DocumentReference, but it was of type: __NSCFNumber";
-  FSTAssertThrows([collection queryWhereFieldPath:[FIRFieldPath documentID] isEqualTo:@1], reason);
+            "DocumentReference, but it was of type:";
+  FSTAssertExceptionPrefix([collection queryWhereFieldPath:[FIRFieldPath documentID] isEqualTo:@1],
+                           reason);
 
   reason = @"Invalid query. When querying a collection group by document ID, the value "
             "provided must result in a valid document path, but 'foo/bar/baz' is not because it "
@@ -586,9 +587,10 @@ namespace testutil = firebase::firestore::testutil;
                   reason);
 
   reason = @"Invalid query. When querying by document ID you must provide a valid string or "
-            "DocumentReference, but it was of type: __NSArrayI";
+            "DocumentReference, but it was of type:";
   NSArray *value = @[ @1, @2 ];
-  FSTAssertThrows([collection queryWhereFieldPath:[FIRFieldPath documentID] in:value], reason);
+  FSTAssertExceptionPrefix([collection queryWhereFieldPath:[FIRFieldPath documentID] in:value],
+                           reason);
 
   reason = @"Invalid query. When querying a collection group by document ID, the value "
             "provided must result in a valid document path, but 'foo' is not because it "

+ 21 - 0
Firestore/Example/Tests/Util/FSTHelpers.h

@@ -67,6 +67,10 @@ inline NSString *FSTRemoveExceptionPrefix(NSString *exception) {
   }
 }
 
+inline NSString *FSTTakeMessagePrefix(NSString *exception, NSInteger length) {
+  return [exception substringToIndex:length];
+}
+
 // Helper for validating API exceptions.
 #define FSTAssertThrows(expression, exceptionReason, ...)               \
   do {                                                                  \
@@ -81,6 +85,23 @@ inline NSString *FSTRemoveExceptionPrefix(NSString *exception) {
     XCTAssertTrue(didThrow, ##__VA_ARGS__);                             \
   } while (0)
 
+// Helper for validating API exceptions.
+#define FSTAssertExceptionPrefix(expression, prefix, ...)                   \
+  do {                                                                      \
+    BOOL didThrow = NO;                                                     \
+    @try {                                                                  \
+      (void)(expression);                                                   \
+    } @catch (NSException * exception) {                                    \
+      didThrow = YES;                                                       \
+      NSString *expectedMessage = FSTRemoveExceptionPrefix(prefix);         \
+      NSString *actualMessage = FSTRemoveExceptionPrefix(exception.reason); \
+      NSInteger length = expectedMessage.length;                            \
+      XCTAssertEqualObjects(FSTTakeMessagePrefix(actualMessage, length),    \
+                            FSTTakeMessagePrefix(expectedMessage, length)); \
+    }                                                                       \
+    XCTAssertTrue(didThrow, ##__VA_ARGS__);                                 \
+  } while (0)
+
 /** Creates a new NSDate from components. Note that year, month, and day are all one-based. */
 NSDate *FSTTestDate(int year, int month, int day, int hour, int minute, int second);