FTestExpectations.m 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 "FirebaseDatabase/Tests/Helpers/FTestExpectations.h"
  17. #import "FirebaseDatabase/Sources/Public/FirebaseDatabase/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]] && [expected count] == 0) {
  57. continue;
  58. }
  59. if ([result isEqual:[NSNull null]] || ![result isEqualToDictionary:expected]) {
  60. return NO;
  61. }
  62. }
  63. return YES;
  64. }
  65. - (void)validate {
  66. for (FExpectation* exp in expectations) {
  67. FIRDataSnapshot* snap = exp.snap;
  68. NSDictionary* result = [snap value];
  69. NSDictionary* expected = exp.expectation;
  70. if ([expected count] == 0) {
  71. XCTAssertTrue([result isEqual:[NSNull null]], @"Expectation mismatch: %@ should be %@",
  72. result, expected);
  73. continue;
  74. }
  75. XCTAssertTrue([result isEqualToDictionary:expected], @"Expectation mismatch: %@ should be %@",
  76. result, expected);
  77. }
  78. }
  79. - (void)failWithException:(NSException*)anException {
  80. @throw anException;
  81. // TODO: fix
  82. //[from failWithException:anException];
  83. }
  84. @end