Skip to content

Commit

Permalink
Add header for TableObject. Renamed tableHeaders to columnHeaders.
Browse files Browse the repository at this point in the history
  • Loading branch information
scottrhoyt committed Sep 14, 2016
1 parent 350946e commit a033948
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 9 deletions.
13 changes: 10 additions & 3 deletions Source/SwiftyTextTable/TextTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,17 @@ private extension Array where Element: CustomStringConvertible {
// MARK: - TextTable Protocols

public protocol TextTableObject {
static var tableHeaders: [String] { get }
static var tableHeader: String? { get }
static var columnHeaders: [String] { get }
var tableValues: [CustomStringConvertible] { get }
}

public extension TextTableObject {
static var tableHeader: String? {
return nil
}
}

private func fence(strings: [String], separator: String) -> String {
return separator + strings.joined(separator: separator) + separator
}
Expand Down Expand Up @@ -96,8 +103,8 @@ public struct TextTable {
}

public init<T: TextTableObject>(objects: [T], header: String? = nil) {
self.header = header
columns = objects.isEmpty ? [] : type(of: objects[0]).tableHeaders.map { TextTableColumn(header: $0) }
self.header = header ?? T.tableHeader
columns = T.columnHeaders.map { TextTableColumn(header: $0) }
objects.forEach { addRow(values: $0.tableValues) }
}

Expand Down
62 changes: 56 additions & 6 deletions Tests/SwiftyTextTableTests/SwiftyTextTableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class SwiftyTextTableTests: XCTestCase {
func testTableObjects() {
// swiftlint:disable:next nesting
struct TableObject: TextTableObject {
static var tableHeaders: [String] {
static var columnHeaders: [String] {
return [ "foo", "bar", "baz"]
}

Expand Down Expand Up @@ -146,11 +146,60 @@ class SwiftyTextTableTests: XCTestCase {
XCTAssertEqual(output, expected)

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

func testTableObjectsWithHeader() {
// swiftlint:disable:next nesting
struct TableObject: TextTableObject {
static var tableHeader: String? {
return "foo table"
}
static var columnHeaders: [String] {
return [ "foo", "bar", "baz"]
}

let foo: Int
let bar: String
let baz: Double

var tableValues: [CustomStringConvertible] {
return [foo, bar, baz]
}
}

let objects = [
TableObject(foo: 1, bar: "2", baz: 3),
TableObject(foo: 11, bar: "22", baz: 33),
TableObject(foo: 111, bar: "222", baz: 333)
]

let output = TextTable(objects: objects).render()
let expected = "+-------------------+\n" +
"| foo table |\n" +
"+-------------------+\n" +
"| foo | bar | baz |\n" +
"+-----+-----+-------+\n" +
"| 1 | 2 | 3.0 |\n" +
"| 11 | 22 | 33.0 |\n" +
"| 111 | 222 | 333.0 |\n" +
"+-----+-----+-------+"

XCTAssertEqual(output, expected)

let emptyOutput = TextTable(objects: [TableObject]()).render()
let emptyExpected = "+-----------------+\n" +
"| foo table |\n" +
"+-----------------+\n" +
"| foo | bar | baz |\n" +
"+-----+-----+-----+\n" +
"\n" +
"+-----+-----+-----+"
XCTAssertEqual(emptyOutput, emptyExpected)
}
}
Expand All @@ -165,6 +214,7 @@ class SwiftyTextTableTests: XCTestCase {
("testRenderCustomWithHeader", testRenderCustomWithHeader),
("testStripping", testStripping),
("testTableObjects", testTableObjects),
("testTableObjectsWithHeader", testTableObjectsWithHeader)
]
}
}
Expand Down

0 comments on commit a033948

Please sign in to comment.