FTestExpectations.m 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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
  42. withBlock:^(FIRDataSnapshot* snapshot) {
  43. exp.snap = snapshot;
  44. }];
  45. [expectations addObject:exp];
  46. }
  47. - (BOOL)isReady {
  48. for (FExpectation* exp in expectations) {
  49. if (!exp.snap) {
  50. return NO;
  51. }
  52. // Note that a failure here will end up triggering the timeout
  53. FIRDataSnapshot* snap = exp.snap;
  54. NSDictionary* result = snap.value;
  55. NSDictionary* expected = exp.expectation;
  56. if ([result isEqual:[NSNull null]] || ![result isEqualToDictionary:expected]) {
  57. return NO;
  58. }
  59. }
  60. return YES;
  61. }
  62. - (void)validate {
  63. for (FExpectation* exp in expectations) {
  64. FIRDataSnapshot* snap = exp.snap;
  65. NSDictionary* result = [snap value];
  66. NSDictionary* expected = exp.expectation;
  67. XCTAssertTrue([result isEqualToDictionary:expected], @"Expectation mismatch: %@ should be %@",
  68. result, expected);
  69. }
  70. }
  71. - (void)failWithException:(NSException*)anException {
  72. @throw anException;
  73. // TODO: fix
  74. //[from failWithException:anException];
  75. }
  76. @end