FIRMutableDataTests.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 "FIRMutableDataTests.h"
  17. #import "FIRMutableData_Private.h"
  18. #import "FSnapshotUtilities.h"
  19. @implementation FIRMutableDataTests
  20. - (FIRMutableData*)dataFor:(id)input {
  21. id<FNode> node = [FSnapshotUtilities nodeFrom:input];
  22. return [[FIRMutableData alloc] initWithNode:node];
  23. }
  24. - (void)testDataForInWorksAlphaPriorities {
  25. FIRMutableData* data = [self dataFor:@{
  26. @"a" : @{@".value" : @1, @".priority" : @"first"},
  27. @"z" : @{@".value" : @26, @".priority" : @"second"},
  28. @"m" : @{@".value" : @13, @".priority" : @"third"},
  29. @"n" : @{@".value" : @14, @".priority" : @"fourth"},
  30. @"c" : @{@".value" : @3, @".priority" : @"fifth"},
  31. @"b" : @{@".value" : @2, @".priority" : @"sixth"},
  32. @"e" : @{@".value" : @5, @".priority" : @"seventh"},
  33. }];
  34. NSMutableString* output = [[NSMutableString alloc] init];
  35. NSMutableArray* priorities = [[NSMutableArray alloc] init];
  36. for (FIRMutableData* child in data.children) {
  37. [output appendFormat:@"%@:%@:", child.key, child.value];
  38. [priorities addObject:child.priority];
  39. }
  40. XCTAssertTrue([output isEqualToString:@"c:3:a:1:n:14:z:26:e:5:b:2:m:13:"], @"Proper order");
  41. NSArray* expected = @[ @"fifth", @"first", @"fourth", @"second", @"seventh", @"sixth", @"third" ];
  42. XCTAssertTrue([priorities isEqualToArray:expected], @"Correct priorities");
  43. XCTAssertTrue(data.childrenCount == 7, @"Got correct children count");
  44. }
  45. - (void)testWritingMutableData {
  46. FIRMutableData* data = [self dataFor:@{}];
  47. data.value = @{@"a" : @1, @"b" : @2};
  48. XCTAssertTrue([data hasChildren], @"Should have children node");
  49. XCTAssertTrue(data.childrenCount == 2, @"Counts both children");
  50. XCTAssertTrue([data hasChildAtPath:@"a"], @"Can see the children individually");
  51. FIRMutableData* childData = [data childDataByAppendingPath:@"b"];
  52. XCTAssertTrue([childData.value isEqualToNumber:@2], @"Get the correct child data");
  53. childData.value = @3;
  54. NSDictionary* expected = @{@"a" : @1, @"b" : @3};
  55. XCTAssertTrue([data.value isEqualToDictionary:expected], @"Updates the parent");
  56. int count = 0;
  57. for (FIRDataSnapshot* __unused child in data.children) {
  58. count++;
  59. if (count == 1) {
  60. [data childDataByAppendingPath:@"c"].value = @4;
  61. }
  62. }
  63. XCTAssertTrue(count == 2, @"Should not iterate nodes added while iterating");
  64. XCTAssertTrue(data.childrenCount == 3, @"Got the new node we added while iterating");
  65. XCTAssertTrue([[data childDataByAppendingPath:@"c"].value isEqualToNumber:@4],
  66. @"Can see the value of the new node");
  67. }
  68. - (void)testMutableDataNavigation {
  69. FIRMutableData* data = [self dataFor:@{@"a" : @1, @"b" : @2}];
  70. XCTAssertNil(data.key, @"Root data has no key");
  71. // Can get a child
  72. FIRMutableData* childData = [data childDataByAppendingPath:@"b"];
  73. XCTAssertTrue([childData.key isEqualToString:@"b"], @"Child has correct key");
  74. // Can get a non-existent child
  75. childData = [data childDataByAppendingPath:@"c"];
  76. XCTAssertTrue(childData != nil, @"Wrapper should not be nil");
  77. XCTAssertTrue([childData.key isEqualToString:@"c"], @"Child should have correct key");
  78. XCTAssertTrue(childData.value == [NSNull null], @"Non-existent data has no value");
  79. childData.value = @{@"d" : @4};
  80. NSDictionary* expected = @{@"a" : @1, @"b" : @2, @"c" : @{@"d" : @4}};
  81. XCTAssertTrue([data.value isEqualToDictionary:expected],
  82. @"Setting non-existent child updates parent");
  83. }
  84. - (void)testPriorities {
  85. FIRMutableData* data = [self dataFor:@{@"a" : @1, @"b" : @2}];
  86. XCTAssertTrue(data.priority == [NSNull null], @"Should not be a priority");
  87. data.priority = @"foo";
  88. XCTAssertTrue([data.priority isEqualToString:@"foo"], @"Should now have a priority");
  89. data.value = @3;
  90. XCTAssertTrue(data.priority == [NSNull null], @"Setting a value overrides a priority");
  91. data.priority = @4;
  92. data.value = nil;
  93. XCTAssertTrue(data.priority == [NSNull null], @"Removing the value does remove the priority");
  94. }
  95. @end