Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: multiple-values and separator #75

Merged
merged 3 commits into from
Mar 15, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 67 additions & 39 deletions Sources/API/Debug.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,119 +19,147 @@ public class Debug {
// MARK: Standard API

extension Debug {
/// Output `target` to console.
/// Output `targets` to console.
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/target/targets/g

/// - Parameters:
/// - target: target
/// - targets: targets
/// - separator: A string to print between each item.
public static func print(
_ target: Any
_ targets: Any...,
separator: String = " "
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This default value " " is similar to standard library.
https://developer.apple.com/documentation/swift/1541053-print

) {
Swift.print(_print(target))
Swift.print(_print(targets, separator: separator))
}

/// Output `target` to `output`.
/// Output `targets` to `output`.
/// - Parameters:
/// - target: target
/// - target: targets
/// - separator: A string to print between each item.
/// - output: output
public static func print<Target: TextOutputStream>(
_ target: Any,
_ targets: Any...,
separator: String = " ",
to output: inout Target
) {
Swift.print(_print(target), to: &output)
Swift.print(_print(targets, separator: separator), to: &output)
}

/// Output pretty-formatted `target` to console.
/// Output pretty-formatted `targets` to console.
/// - Parameters:
/// - target: target
/// - targets: targets
/// - separator: A string to print between each item.
/// - option: option (default: `Debug.sharedOption`)
public static func prettyPrint(
_ target: Any,
_ targets: Any...,
separator: String = "\n",
option: Option = Debug.sharedOption
) {
Swift.print(_prettyPrint(target, option: option))
Swift.print(_prettyPrint(targets, separator: separator, option: option))
}

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

/// Output debuggable `target` to console.
/// Output debuggable `targets` to console.
/// - Parameters:
/// - target: target
/// - targets: targets
/// - separator: A string to print between each item.
public static func debugPrint(
_ target: Any
_ targets: Any...,
separator: String = " "
) {
Swift.print(_debugPrint(target))
Swift.print(_debugPrint(targets, separator: separator))
}

/// Output debuggable `target` to console.
/// Output debuggable `targets` to console.
/// - Parameters:
/// - target: target
/// - targets: targets
/// - separator: A string to print between each item.
/// - output: output
public static func debugPrint<Target: TextOutputStream>(
_ target: Any,
_ targets: Any...,
separator: String = " ",
to output: inout Target
) {
Swift.print(_debugPrint(target), to: &output)
Swift.print(_debugPrint(targets, separator: separator), to: &output)
}

/// Output debuggable and pretty-formatted `target` to console.
/// Output debuggable and pretty-formatted `targets` to console.
/// - Parameters:
/// - target: target
/// - targets: targets
/// - separator: A string to print between each item.
/// - option: option (default: `Debug.sharedOption`)
public static func debugPrettyPrint(
_ target: Any,
_ targets: Any...,
separator: String = "\n",
option: Option = Debug.sharedOption
) {
Swift.print(_debugPrettyPrint(target, option: option))
Swift.print(_debugPrettyPrint(targets, separator: separator, option: option))
}

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

// MARK: - private

private static func _print(
_ target: Any
_ targets: [Any],
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't use targets: Any... because targets are passed to one array of argument instead of multiple values called from print() in this source.

Copy link
Collaborator

@sahara-ooga sahara-ooga Mar 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is due to Swift language design😎

separator: String
) -> String {
Pretty(formatter: SinglelineFormatter()).string(target, debug: false)
targets.map {
Pretty(formatter: SinglelineFormatter()).string($0, debug: false)
}.joined(separator: separator)
}

private static func _prettyPrint(
_ target: Any,
_ targets: [Any],
separator: String,
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: separator)
}

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

private static func _debugPrettyPrint(
_ target: Any,
_ targets: [Any],
separator: String,
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: separator)
}
}
72 changes: 70 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,72 @@ class DebugTests: XCTestCase {
]
""" + "\n")
}

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

//
// not specify `separator` (default)
//

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")

//
// specify `separator`
//

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

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

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

result = ""
Debug.debugPrettyPrint(array, 42, separator: "!!\n", 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)()
}
}