FPruneForestTest.m 4.4 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 <Foundation/Foundation.h>
  17. #import <XCTest/XCTest.h>
  18. #import "FPruneForest.h"
  19. #import "FPath.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"]] keepPath:[FPath pathWithString:@"foo"]] prunesAnything]);
  44. XCTAssertTrue([[[empty prunePath:[FPath pathWithString:@"foo"]] keepPath:[FPath pathWithString:@"foo/bar"]] prunesAnything]);
  45. }
  46. - (void) testKeepUnderPruneWorks {
  47. FPruneForest *forest = [FPruneForest empty];
  48. forest = [forest prunePath:[FPath pathWithString:@"foo/bar"]];
  49. forest = [forest keepPath:[FPath pathWithString:@"foo/bar/baz"]];
  50. forest = [forest keepAll:[NSSet setWithArray:@[@"qux", @"quu"]] atPath:[FPath pathWithString:@"foo/bar"]];
  51. }
  52. - (void) testPruneUnderKeepThrows {
  53. FPruneForest *forest = [FPruneForest empty];
  54. forest = [forest prunePath:[FPath pathWithString:@"foo"]];
  55. forest = [forest keepPath:[FPath pathWithString:@"foo/bar"]];
  56. XCTAssertThrows([forest prunePath:[FPath pathWithString:@"foo/bar/baz"]]);
  57. NSSet *children = [NSSet setWithArray:@[@"qux", @"quu"]];
  58. XCTAssertThrows([forest pruneAll:children atPath:[FPath pathWithString:@"foo/bar"]]);
  59. }
  60. - (void) testChildKeepsPruneInfo {
  61. FPruneForest *forest = [FPruneForest empty];
  62. forest = [forest keepPath:[FPath pathWithString:@"foo/bar"]];
  63. XCTAssertTrue([[forest child:@"foo"] affectsPath:[FPath pathWithString:@"bar"]]);
  64. XCTAssertTrue([[[forest child:@"foo"] child:@"bar"] affectsPath:[FPath pathWithString:@""]]);
  65. XCTAssertTrue([[[[forest child:@"foo"] child:@"bar"] child:@"baz"] affectsPath:[FPath pathWithString:@""]]);
  66. forest = [[FPruneForest empty] prunePath:[FPath pathWithString:@"foo/bar"]];
  67. XCTAssertTrue([[forest child:@"foo"] affectsPath:[FPath pathWithString:@"bar"]]);
  68. XCTAssertTrue([[[forest child:@"foo"] child:@"bar"] affectsPath:[FPath pathWithString:@""]]);
  69. XCTAssertTrue([[[[forest child:@"foo"] child:@"bar"] child:@"baz"] affectsPath:[FPath pathWithString:@""]]);
  70. XCTAssertFalse([[forest child:@"non-existent"] affectsPath:[FPath pathWithString:@""]]);
  71. }
  72. - (void) testShouldPruneWorks {
  73. FPruneForest *forest = [FPruneForest empty];
  74. forest = [forest prunePath:[FPath pathWithString:@"foo"]];
  75. forest = [forest keepPath:[FPath pathWithString:@"foo/bar/baz"]];
  76. XCTAssertTrue([forest shouldPruneUnkeptDescendantsAtPath:[FPath pathWithString:@"foo"]]);
  77. XCTAssertTrue([forest shouldPruneUnkeptDescendantsAtPath:[FPath pathWithString:@"foo/bar"]]);
  78. XCTAssertFalse([forest shouldPruneUnkeptDescendantsAtPath:[FPath pathWithString:@"foo/bar/baz"]]);
  79. XCTAssertFalse([forest shouldPruneUnkeptDescendantsAtPath:[FPath pathWithString:@"qux"]]);
  80. }
  81. @end