Skip to content

Commit

Permalink
[WIP] Tests for apple#658
Browse files Browse the repository at this point in the history
These tests are very poorly named and aren't necessarily in the right
file. They should be re-written with the actual fix.
  • Loading branch information
porglezomp committed Aug 5, 2024
1 parent e3d8a3d commit 5e37672
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//===----------------------------------------------------------*- swift -*-===//
//
// This source file is part of the Swift Argument Parser open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

import XCTest
import ArgumentParserTestHelpers
import ArgumentParser

// MARK: Arguments at multiple layers, all unrecognized arguments
fileprivate struct OuterArgCommand_1: ParsableCommand {
static let configuration = CommandConfiguration(subcommands: [InnerArgCommand.self])

@Argument() var argument: String

struct InnerArgCommand: ParsableCommand {
@OptionGroup() var outer: OuterArgCommand_1

@Argument(parsing: .allUnrecognized)
var query: [String] = []
}
}

extension NestedCommandEndToEndTests {
func testParsing_multipleLayersAllUnrecognizedInner() throws {
AssertParseCommand(OuterArgCommand_1.self, OuterArgCommand_1.InnerArgCommand.self, ["outer", "inner-arg-command"]) { inner in
XCTAssertEqual(inner.outer.argument, "outer")
XCTAssertEqual(inner.query, [])
}
}

func testParsing_multipleLayersAllUnrecognizedInner_2() throws {
AssertParseCommand(OuterArgCommand_1.self, OuterArgCommand_1.InnerArgCommand.self, ["outer", "inner-arg-command", "a", "b", "c"]) { inner in
XCTAssertEqual(inner.outer.argument, "outer")
XCTAssertEqual(inner.query, ["a", "b", "c"])
}
}

func testParsing_multipleLayersAllUnrecognizedInner_3() throws {
AssertParseCommand(OuterArgCommand_1.self, OuterArgCommand_1.InnerArgCommand.self, ["outer", "inner-arg-command", "--include", "a", "--exclude", "b"]) { inner in
XCTAssertEqual(inner.outer.argument, "outer")
XCTAssertEqual(inner.query, ["--include", "a", "--exclude", "b"])
}
}
}

// MARK: Arguments at multiple layers, all unrecognized arguments mixed with an argument and options
fileprivate struct OuterArgCommand_2: ParsableCommand {
static let configuration = CommandConfiguration(subcommands: [InnerArgCommand.self])

@Argument() var argument: String

struct InnerArgCommand: ParsableCommand {
@OptionGroup() var outer: OuterArgCommand_2

@Argument
var first: Int

@Option
var feature: [Int] = []

@Argument(parsing: .allUnrecognized)
var query: [String] = []
}
}

extension NestedCommandEndToEndTests {
func testParsing_multipleLayersAllUnrecognizedInnerWithExtraArgs() throws {
AssertParseCommand(OuterArgCommand_2.self, OuterArgCommand_2.InnerArgCommand.self, ["outer", "inner-arg-command", "24"]) { inner in
XCTAssertEqual(inner.outer.argument, "outer")
XCTAssertEqual(inner.first, 24)
XCTAssertEqual(inner.query, [])
}
}

func testParsing_multipleLayersAllUnrecognizedInnerWithExtraArgs_2() throws {
AssertParseCommand(OuterArgCommand_2.self, OuterArgCommand_2.InnerArgCommand.self, ["outer", "inner-arg-command", "24", "--feature", "1", "--feature", "2"]) { inner in
XCTAssertEqual(inner.outer.argument, "outer")
XCTAssertEqual(inner.first, 24)
XCTAssertEqual(inner.feature, [1, 2])
XCTAssertEqual(inner.query, [])
}
}

func testParsing_multipleLayersAllUnrecognizedInnerWithExtraArgs_3() throws {
AssertParseCommand(OuterArgCommand_2.self, OuterArgCommand_2.InnerArgCommand.self, ["outer", "inner-arg-command", "24", "--enable", "example", "--feature", "1", "--disable", "another", "--feature", "2", "--final"]) { inner in
XCTAssertEqual(inner.outer.argument, "outer")
XCTAssertEqual(inner.first, 24)
XCTAssertEqual(inner.feature, [1, 2])
XCTAssertEqual(inner.query, ["--enable", "example", "--disable", "another", "--final"])
}
}
}
22 changes: 22 additions & 0 deletions Tests/ArgumentParserEndToEndTests/NestedCommandEndToEndTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,25 @@ extension NestedCommandEndToEndTests {
}
}
}

// MARK: Arguments at multiple layers
private struct OuterArgCommand: ParsableCommand {
static let configuration = CommandConfiguration(subcommands: [InnerArgCommand.self])

@Argument() var argument: String

struct InnerArgCommand: ParsableCommand {
@OptionGroup() var outer: OuterArgCommand

@Argument() var argument: String
}
}

extension NestedCommandEndToEndTests {
func testParsing_multipleLayersArgs() throws {
AssertParseCommand(OuterArgCommand.self, OuterArgCommand.InnerArgCommand.self, ["outer", "inner-arg-command", "inner"]) { inner in
XCTAssertEqual(inner.outer.argument, "outer")
XCTAssertEqual(inner.argument, "inner")
}
}
}

0 comments on commit 5e37672

Please sign in to comment.