FPruneForestTest.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 <Foundation/Foundation.h>
  17. #import <XCTest/XCTest.h>
  18. #import "FPath.h"
  19. #import "FPruneForest.h"
  20. @interface FPruneForestTest : XCTestCase
  21. @end
  22. @implementation FPruneForestTest
  23. - (void)testEmptyDoesNotAffectAnyPaths {
  24. FPruneForest *forest = [FPruneForest empty];
  25. XCTAssertFalse([forest affectsPath:[FPath empty]]);
  26. XCTAssertFalse([forest affectsPath:[FPath pathWithString:@"foo"]]);
  27. }
  28. - (void)testPruneAffectsPath {
  29. FPruneForest *forest = [FPruneForest empty];
  30. forest = [forest prunePath:[FPath pathWithString:@"foo/bar"]];
  31. forest = [forest keepPath:[FPath pathWithString:@"foo/bar/baz"]];
  32. XCTAssertTrue([forest affectsPath:[FPath pathWithString:@"foo"]]);
  33. XCTAssertFalse([forest affectsPath:[FPath pathWithString:@"baz"]]);
  34. XCTAssertFalse([forest affectsPath:[FPath pathWithString:@"baz/bar"]]);
  35. XCTAssertTrue([forest affectsPath:[FPath pathWithString:@"foo/bar"]]);
  36. XCTAssertTrue([forest affectsPath:[FPath pathWithString:@"foo/bar/baz"]]);
  37. XCTAssertTrue([forest affectsPath:[FPath pathWithString:@"foo/bar/qux"]]);
  38. }
  39. - (void)testPruneAnythingWorks {
  40. FPruneForest *empty = [FPruneForest empty];
  41. XCTAssertFalse([empty prunesAnything]);
  42. XCTAssertTrue([[empty prunePath:[FPath pathWithString:@"foo"]] prunesAnything]);
  43. XCTAssertFalse([[[empty prunePath:[FPath pathWithString:@"foo/bar"]]
  44. keepPath:[FPath pathWithString:@"foo"]] prunesAnything]);
  45. XCTAssertTrue([[[empty prunePath:[FPath pathWithString:@"foo"]]
  46. keepPath:[FPath pathWithString:@"foo/bar"]] prunesAnything]);
  47. }
  48. - (void)testKeepUnderPruneWorks {
  49. FPruneForest *forest = [FPruneForest empty];
  50. forest = [forest prunePath:[FPath pathWithString:@"foo/bar"]];
  51. forest = [forest keepPath:[FPath pathWithString:@"foo/bar/baz"]];
  52. [forest keepAll:[NSSet setWithArray:@[ @"qux", @"quu" ]]
  53. atPath:[FPath pathWithString:@"foo/bar"]];
  54. }
  55. - (void)testPruneUnderKeepThrows {
  56. FPruneForest *forest = [FPruneForest empty];
  57. forest = [forest prunePath:[FPath pathWithString:@"foo"]];
  58. forest = [forest keepPath:[FPath pathWithString:@"foo/bar"]];
  59. XCTAssertThrows([forest prunePath:[FPath pathWithString:@"foo/bar/baz"]]);
  60. NSSet *children = [NSSet setWithArray:@[ @"qux", @"quu" ]];
  61. XCTAssertThrows([forest pruneAll:children atPath:[FPath pathWithString:@"foo/bar"]]);
  62. }
  63. - (void)testChildKeepsPruneInfo {
  64. FPruneForest *forest = [FPruneForest empty];
  65. forest = [forest keepPath:[FPath pathWithString:@"foo/bar"]];
  66. XCTAssertTrue([[forest child:@"foo"] affectsPath:[FPath pathWithString:@"bar"]]);
  67. XCTAssertTrue([[[forest child:@"foo"] child:@"bar"] affectsPath:[FPath pathWithString:@""]]);
  68. XCTAssertTrue(
  69. [[[[forest child:@"foo"] child:@"bar"] child:@"baz"] affectsPath:[FPath pathWithString:@""]]);
  70. forest = [[FPruneForest empty] prunePath:[FPath pathWithString:@"foo/bar"]];
  71. XCTAssertTrue([[forest child:@"foo"] affectsPath:[FPath pathWithString:@"bar"]]);
  72. XCTAssertTrue([[[forest child:@"foo"] child:@"bar"] affectsPath:[FPath pathWithString:@""]]);
  73. XCTAssertTrue(
  74. [[[[forest child:@"foo"] child:@"bar"] child:@"baz"] affectsPath:[FPath pathWithString:@""]]);
  75. XCTAssertFalse([[forest child:@"non-existent"] affectsPath:[FPath pathWithString:@""]]);
  76. }
  77. - (void)testShouldPruneWorks {
  78. FPruneForest *forest = [FPruneForest empty];
  79. forest = [forest prunePath:[FPath pathWithString:@"foo"]];
  80. forest = [forest keepPath:[FPath pathWithString:@"foo/bar/baz"]];
  81. XCTAssertTrue([forest shouldPruneUnkeptDescendantsAtPath:[FPath pathWithString:@"foo"]]);
  82. XCTAssertTrue([forest shouldPruneUnkeptDescendantsAtPath:[FPath pathWithString:@"foo/bar"]]);
  83. XCTAssertFalse([forest shouldPruneUnkeptDescendantsAtPath:[FPath pathWithString:@"foo/bar/baz"]]);
  84. XCTAssertFalse([forest shouldPruneUnkeptDescendantsAtPath:[FPath pathWithString:@"qux"]]);
  85. }
  86. @end