Skip to content

Commit

Permalink
feature: multiple-values to output
Browse files Browse the repository at this point in the history
  • Loading branch information
YusukeHosonuma committed Mar 15, 2020
1 parent bf83e00 commit 3bd821a
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 41 deletions.
86 changes: 47 additions & 39 deletions Sources/API/Debug.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,119 +19,127 @@ public class Debug {
// MARK: Standard API

extension Debug {
/// Output `target` to console.
/// Output `targets` to console.
/// - Parameters:
/// - target: target
/// - targets: targets
public static func print(
_ target: Any
_ targets: Any...
) {
Swift.print(_print(target))
Swift.print(_print(targets))
}

/// Output `target` to `output`.
/// Output `targets` to `output`.
/// - Parameters:
/// - target: target
/// - target: targets
/// - output: output
public static func print<Target: TextOutputStream>(
_ target: Any,
_ targets: Any...,
to output: inout Target
) {
Swift.print(_print(target), to: &output)
Swift.print(_print(targets), to: &output)
}

/// Output pretty-formatted `target` to console.
/// Output pretty-formatted `targets` to console.
/// - Parameters:
/// - target: target
/// - targets: targets
/// - option: option (default: `Debug.sharedOption`)
public static func prettyPrint(
_ target: Any,
_ targets: Any...,
option: Option = Debug.sharedOption
) {
Swift.print(_prettyPrint(target, option: option))
Swift.print(_prettyPrint(targets, option: option))
}

/// Output pretty-formatted `target` to console.
/// Output pretty-formatted `targets` to console.
/// - Parameters:
/// - target: target
/// - targets: targets
/// - option: option (default: `Debug.sharedOption`)
/// - output: output
public static func prettyPrint<Target: TextOutputStream>(
_ target: Any,
_ targets: Any...,
option: Option = Debug.sharedOption,
to output: inout Target
) {
Swift.print(_prettyPrint(target, option: option), to: &output)
Swift.print(_prettyPrint(targets, option: option), to: &output)
}

/// Output debuggable `target` to console.
/// Output debuggable `targets` to console.
/// - Parameters:
/// - target: target
/// - targets: targets
public static func debugPrint(
_ target: Any
_ targets: Any...
) {
Swift.print(_debugPrint(target))
Swift.print(_debugPrint(targets))
}

/// Output debuggable `target` to console.
/// Output debuggable `targets` to console.
/// - Parameters:
/// - target: target
/// - targets: targets
/// - output: output
public static func debugPrint<Target: TextOutputStream>(
_ target: Any,
_ targets: Any...,
to output: inout Target
) {
Swift.print(_debugPrint(target), to: &output)
Swift.print(_debugPrint(targets), to: &output)
}

/// Output debuggable and pretty-formatted `target` to console.
/// Output debuggable and pretty-formatted `targets` to console.
/// - Parameters:
/// - target: target
/// - targets: targets
/// - option: option (default: `Debug.sharedOption`)
public static func debugPrettyPrint(
_ target: Any,
_ targets: Any...,
option: Option = Debug.sharedOption
) {
Swift.print(_debugPrettyPrint(target, option: option))
Swift.print(_debugPrettyPrint(targets, option: option))
}

/// Output debuggable and pretty-formatted `target` to console.
/// - Parameters:
/// - target: target
/// - targets: targets
/// - option: option (default: `Debug.sharedOption`)
/// - output: output
public static func debugPrettyPrint<Target: TextOutputStream>(
_ target: Any,
_ targets: Any...,
option: Option = Debug.sharedOption,
to output: inout Target
) {
Swift.print(_debugPrettyPrint(target, option: option), to: &output)
Swift.print(_debugPrettyPrint(targets, option: option), to: &output)
}

// MARK: - private

private static func _print(
_ target: Any
_ targets: [Any]
) -> String {
Pretty(formatter: SinglelineFormatter()).string(target, debug: false)
targets.map {
Pretty(formatter: SinglelineFormatter()).string($0, debug: false)
}.joined(separator: ", ")
}

private static func _prettyPrint(
_ target: Any,
_ targets: [Any],
option: Option
) -> String {
Pretty(formatter: MultilineFormatter(option: option)).string(target, debug: false)
targets.map {
Pretty(formatter: MultilineFormatter(option: option)).string($0, debug: false)
}.joined(separator: ",\n")
}

private static func _debugPrint(
_ target: Any
_ targets: [Any]
) -> String {
Pretty(formatter: SinglelineFormatter()).string(target, debug: true)
targets.map {
Pretty(formatter: SinglelineFormatter()).string($0, debug: true)
}.joined(separator: ", ")
}

private static func _debugPrettyPrint(
_ target: Any,
_ targets: [Any],
option: Option
) -> String {
Pretty(formatter: MultilineFormatter(option: option)).string(target, debug: true)
targets.map {
Pretty(formatter: MultilineFormatter(option: option)).string($0, debug: true)
}.joined(separator: ",\n")
}
}
36 changes: 34 additions & 2 deletions Tests/API/DebugTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ class DebugTests: XCTestCase {
override func setUp() {}

override func tearDown() {}

func testPrint() {
let expectString =
#"Dog(id: "pochi", name: "ポチ", nickname: nil, age: 3, homepage: https://www.google.com/)"#

let expectDebugString =
#"Dog(id: DogId(rawValue: "pochi"), name: Optional("ポチ"), nickname: nil, age: 3, homepage: Optional(https://www.google.com/))"#

//
// Struct
//
Expand Down Expand Up @@ -191,4 +191,36 @@ class DebugTests: XCTestCase {
]
""" + "\n")
}

func testMultipleValues() {
let array = ["Hello", "World"]

var result = ""
Debug.print(array, 42, to: &result)
XCTAssertEqual(result, #"["Hello", "World"], 42"# + "\n")

result = ""
Debug.debugPrint(array, 42, to: &result)
XCTAssertEqual(result, #"["Hello", "World"], 42"# + "\n")

result = ""
Debug.prettyPrint(array, 42, to: &result)
XCTAssertEqual(result, """
[
"Hello",
"World"
],
42
""" + "\n")

result = ""
Debug.debugPrettyPrint(array, 42, to: &result)
XCTAssertEqual(result, """
[
"Hello",
"World"
],
42
""" + "\n")
}
}
5 changes: 5 additions & 0 deletions Tests/Helper/Util.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ func uncurry<T1, T2, R>(_ f: @escaping (T1) -> (T2) -> R) -> (T1, T2) -> R {
f(t1)(t2)
}
}
func uncurry<T1, R>(_ f: @escaping (T1) -> () -> R) -> (T1) -> R {
{ (t1) -> R in
f(t1)()
}
}

0 comments on commit 3bd821a

Please sign in to comment.