Skip to content

Commit

Permalink
Merge pull request scottrhoyt#20 from scottrhoyt/array_function
Browse files Browse the repository at this point in the history
Array function
  • Loading branch information
scottrhoyt authored Jan 1, 2017
2 parents 493a6a7 + 3ec8060 commit 747969e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* Added Documentation.
* Re-enabled console formatting support for Linux.
* Improved compilation time.
* Added convenience method for rendering tables from an array of
`TextTableRepresentable`s (`renderTextTable`).

##### Bug Fixes

Expand Down
12 changes: 12 additions & 0 deletions Source/SwiftyTextTable/TextTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,18 @@ private func fence(strings: [String], separator: String) -> String {
return separator + strings.joined(separator: separator) + separator
}

public extension Array where Element: TextTableRepresentable {

/**
Returns a rendered text table containing the data in the array.
- returns: A `String` containing the rendered text table.
*/
public func renderTextTable() -> String {
let table = TextTable(objects: self)
return table.render()
}
}

// MARK: - Helper Extensions

private extension String {
Expand Down
12 changes: 4 additions & 8 deletions Tests/SwiftyTextTableTests/SwiftyTextTableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ class SwiftyTextTableTests: XCTestCase {
TableObject(foo: 111, bar: "222", baz: 333)
]

let output = TextTable(objects: objects).render()
let expected = "+-----+-----+-------+\n" +
"| foo | bar | baz |\n" +
"+-----+-----+-------+\n" +
Expand All @@ -143,15 +142,14 @@ class SwiftyTextTableTests: XCTestCase {
"| 111 | 222 | 333.0 |\n" +
"+-----+-----+-------+"

XCTAssertEqual(output, expected)
XCTAssertEqual(objects.renderTextTable(), expected)

let emptyOutput = TextTable(objects: [TableObject]()).render()
let emptyExpected = "+-----+-----+-----+\n" +
"| foo | bar | baz |\n" +
"+-----+-----+-----+\n" +
"\n" +
"+-----+-----+-----+"
XCTAssertEqual(emptyOutput, emptyExpected)
XCTAssertEqual([TableObject]().renderTextTable(), emptyExpected)
}

func testTextTableRepresentablesWithHeader() {
Expand Down Expand Up @@ -179,7 +177,6 @@ class SwiftyTextTableTests: XCTestCase {
TableObject(foo: 111, bar: "222", baz: 333)
]

let output = TextTable(objects: objects).render()
let expected = "+-------------------+\n" +
"| foo table |\n" +
"+-------------------+\n" +
Expand All @@ -190,17 +187,16 @@ class SwiftyTextTableTests: XCTestCase {
"| 111 | 222 | 333.0 |\n" +
"+-----+-----+-------+"

XCTAssertEqual(output, expected)
XCTAssertEqual(objects.renderTextTable(), expected)

let emptyOutput = TextTable(objects: [TableObject]()).render()
let emptyExpected = "+-----------------+\n" +
"| foo table |\n" +
"+-----------------+\n" +
"| foo | bar | baz |\n" +
"+-----+-----+-----+\n" +
"\n" +
"+-----+-----+-----+"
XCTAssertEqual(emptyOutput, emptyExpected)
XCTAssertEqual([TableObject]().renderTextTable(), emptyExpected)
}
}

Expand Down

0 comments on commit 747969e

Please sign in to comment.