FTestExpectations.m 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 "FTestExpectations.h"
  17. #import "FIRDataSnapshot.h"
  18. @interface FExpectation : NSObject
  19. @property (strong, nonatomic) FIRDatabaseQuery * query;
  20. @property (strong, nonatomic) id expectation;
  21. @property (strong, nonatomic) FIRDataSnapshot * snap;
  22. @end
  23. @implementation FExpectation
  24. @synthesize query;
  25. @synthesize expectation;
  26. @synthesize snap;
  27. @end
  28. @implementation FTestExpectations
  29. - (id) initFrom:(XCTestCase *)other {
  30. self = [super init];
  31. if (self) {
  32. expectations = [[NSMutableArray alloc] init];
  33. from = other;
  34. }
  35. return self;
  36. }
  37. - (void)addQuery:(FIRDatabaseQuery *)query withExpectation:(id)expectation {
  38. FExpectation* exp = [[FExpectation alloc] init];
  39. exp.query = query;
  40. exp.expectation = expectation;
  41. [query observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  42. exp.snap = snapshot;
  43. }];
  44. [expectations addObject:exp];
  45. }
  46. - (BOOL) isReady {
  47. for (FExpectation* exp in expectations) {
  48. if (!exp.snap) {
  49. return NO;
  50. }
  51. // Note that a failure here will end up triggering the timeout
  52. FIRDataSnapshot * snap = exp.snap;
  53. NSDictionary* result = snap.value;
  54. NSDictionary* expected = exp.expectation;
  55. if ([result isEqual:[NSNull null]] || ![result isEqualToDictionary:expected]) {
  56. return NO;
  57. }
  58. }
  59. return YES;
  60. }
  61. - (void) validate {
  62. for (FExpectation* exp in expectations) {
  63. FIRDataSnapshot * snap = exp.snap;
  64. NSDictionary* result = [snap value];
  65. NSDictionary* expected = exp.expectation;
  66. XCTAssertTrue([result isEqualToDictionary:expected], @"Expectation mismatch: %@ should be %@", result, expected);
  67. }
  68. }
  69. - (void) failWithException:(NSException *) anException {
  70. @throw anException;
  71. // TODO: fix
  72. //[from failWithException:anException];
  73. }
  74. @end