TreeTests.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // This source file is part of the Swift Argument Parser open source project
  4. //
  5. // Copyright (c) 2020 Apple Inc. and the Swift project authors
  6. // Licensed under Apache License v2.0 with Runtime Library Exception
  7. //
  8. // See https://swift.org/LICENSE.txt for license information
  9. //
  10. //===----------------------------------------------------------------------===//
  11. import XCTest
  12. @testable import ArgumentParser
  13. final class TreeTests: XCTestCase {
  14. }
  15. // MARK: -
  16. func generateTree() -> Tree<Int> {
  17. let tree = Tree(1)
  18. for x in 11...13 {
  19. let node = Tree(x)
  20. tree.addChild(node)
  21. for y in 1...3 {
  22. let subnode = Tree(x * 10 + y)
  23. node.addChild(subnode)
  24. }
  25. }
  26. return tree
  27. }
  28. extension TreeTests {
  29. func testHierarchy() {
  30. let tree = generateTree()
  31. XCTAssertEqual(tree.element, 1)
  32. XCTAssertEqual(tree.children.map { $0.element }, [11, 12, 13])
  33. XCTAssertEqual(
  34. tree.children.flatMap { $0.children.map { $0.element } },
  35. [111, 112, 113, 121, 122, 123, 131, 132, 133])
  36. }
  37. func testSearch() {
  38. let tree = generateTree()
  39. XCTAssertEqual(
  40. tree.path(toFirstWhere: { $0 == 1 }).map { $0.element },
  41. [1])
  42. XCTAssertEqual(
  43. tree.path(toFirstWhere: { $0 == 13 }).map { $0.element },
  44. [1, 13])
  45. XCTAssertEqual(
  46. tree.path(toFirstWhere: { $0 == 133 }).map { $0.element },
  47. [1, 13, 133])
  48. XCTAssertTrue(tree.path(toFirstWhere: { $0 < 0 }).isEmpty)
  49. }
  50. }
  51. extension TreeTests {
  52. struct A: ParsableCommand {
  53. static let configuration = CommandConfiguration(subcommands: [A.self])
  54. }
  55. struct Root: ParsableCommand {
  56. static let configuration = CommandConfiguration(subcommands: [Sub.self])
  57. }
  58. struct Sub: ParsableCommand {
  59. static let configuration = CommandConfiguration(subcommands: [Sub.self])
  60. }
  61. struct RootWithNamedNestedSub: ParsableCommand {
  62. static let configuration = CommandConfiguration(subcommands: [
  63. NestedSub.self
  64. ])
  65. struct NestedSub: ParsableCommand {
  66. static let configuration = CommandConfiguration(
  67. commandName: "sub", aliases: ["sub"])
  68. }
  69. }
  70. struct RootWithNestedSub: ParsableCommand {
  71. static let configuration = CommandConfiguration(subcommands: [
  72. NestedSub.self
  73. ])
  74. struct NestedSub: ParsableCommand {
  75. static let configuration = CommandConfiguration(aliases: ["nested-sub"])
  76. }
  77. }
  78. func testInitializationWithRecursiveSubcommand() {
  79. XCTAssertThrowsError(try Tree(root: A.asCommand))
  80. XCTAssertThrowsError(try Tree(root: Root.asCommand))
  81. }
  82. func testInitializationWithMatchingAliases() {
  83. XCTAssertThrowsError(try Tree(root: RootWithNamedNestedSub.asCommand))
  84. XCTAssertThrowsError(try Tree(root: RootWithNestedSub.asCommand))
  85. }
  86. }