Skip to content

Commit

Permalink
Improve code coverage of box types (#42)
Browse files Browse the repository at this point in the history
* Add a bunch of trivial yet missing unit tests to box types

* Remove unused methods from `UnkeyedBox` & `KeyedBox`

* Fix formatting

* Made test fixture of `KeyedBoxTests` immutable
  • Loading branch information
regexident authored and hodovani committed Dec 24, 2018
1 parent 3d22e01 commit 68f9c8f
Show file tree
Hide file tree
Showing 14 changed files with 144 additions and 34 deletions.
31 changes: 14 additions & 17 deletions Sources/XMLCoder/Box/KeyedBox.swift
Expand Up @@ -7,7 +7,7 @@

import Foundation

struct KeyedStorage<Key: Hashable, Value> {
struct KeyedStorage<Key: Hashable & Comparable, Value> {
typealias Buffer = [Key: Value]

fileprivate var buffer: Buffer = [:]
Expand All @@ -33,29 +33,13 @@ struct KeyedStorage<Key: Hashable, Value> {
}
}

func filter(_ isIncluded: (Key, Value) throws -> Bool) rethrows -> [(Key, Value)] {
return try buffer.filter(isIncluded)
}

func map<T>(_ transform: (Key, Value) throws -> T) rethrows -> [T] {
return try buffer.map(transform)
}

func compactMap<T>(_ transform: ((Key, Value)) throws -> T?) rethrows -> [T] {
return try buffer.compactMap(transform)
}

func mapValues<T>(_ transform: (Value) throws -> T) rethrows -> [Key: T] {
return try buffer.mapValues(transform)
}

func mapValues<T>(_ transform: (Value) throws -> T) rethrows -> [(Key, T)] {
return Array(try mapValues(transform))
}

func mapValues(_ transform: (Value) throws -> Value) rethrows -> KeyedStorage {
return KeyedStorage(try mapValues(transform))
}
}

extension KeyedStorage: Sequence {
Expand All @@ -72,6 +56,13 @@ extension KeyedStorage: ExpressibleByDictionaryLiteral {
}
}

extension KeyedStorage: CustomStringConvertible
where Key: Comparable {
var description: String {
return "[\(buffer)]"
}
}

class KeyedBox {
typealias Key = String
typealias Attribute = SimpleBox
Expand Down Expand Up @@ -116,3 +107,9 @@ extension KeyedBox: Box {
return nil
}
}

extension KeyedBox: CustomStringConvertible {
var description: String {
return "\(elements)"
}
}
14 changes: 1 addition & 13 deletions Sources/XMLCoder/Box/UnkeyedBox.swift
Expand Up @@ -42,18 +42,6 @@ class UnkeyedBox {
func insert(_ newElement: Element, at index: Int) {
unboxed.insert(newElement, at: index)
}

func filter(_ isIncluded: (Element) throws -> Bool) rethrows -> [Element] {
return try unboxed.filter(isIncluded)
}

func map<T>(_ transform: (Element) throws -> T) rethrows -> [T] {
return try unboxed.map(transform)
}

func compactMap<T>(_ transform: (Element) throws -> T?) rethrows -> [T] {
return try unboxed.compactMap(transform)
}
}

extension UnkeyedBox: Box {
Expand All @@ -76,6 +64,6 @@ extension UnkeyedBox: Sequence {

extension UnkeyedBox: CustomStringConvertible {
var description: String {
return unboxed.description
return "\(unboxed)"
}
}
5 changes: 5 additions & 0 deletions Tests/XMLCoderTests/Box/BoolBoxTests.swift
Expand Up @@ -11,6 +11,11 @@ import XCTest
class BoolBoxTests: XCTestCase {
typealias Boxed = BoolBox

func testIsNull() {
let box = Boxed(false)
XCTAssertEqual(box.isNull, false)
}

func testUnbox() {
let values: [Boxed.Unboxed] = [
false,
Expand Down
5 changes: 4 additions & 1 deletion Tests/XMLCoderTests/Box/DataBoxTests.swift
Expand Up @@ -11,7 +11,10 @@ import XCTest
class DataBoxTests: XCTestCase {
typealias Boxed = DataBox

typealias FromXMLString = (String) -> Boxed?
func testIsNull() {
let box = Boxed(Data(), format: .base64)
XCTAssertEqual(box.isNull, false)
}

func testUnbox() {
let values: [Boxed.Unboxed] = [
Expand Down
5 changes: 5 additions & 0 deletions Tests/XMLCoderTests/Box/DateBoxTests.swift
Expand Up @@ -17,6 +17,11 @@ class DateBoxTests: XCTestCase {
return formatter
}()

func testIsNull() {
let box = Boxed(Date(), format: .iso8601)
XCTAssertEqual(box.isNull, false)
}

func testUnbox() {
let values: [Boxed.Unboxed] = [
Date(timeIntervalSince1970: 0.0),
Expand Down
5 changes: 5 additions & 0 deletions Tests/XMLCoderTests/Box/DecimalBoxTests.swift
Expand Up @@ -11,6 +11,11 @@ import XCTest
class DecimalBoxTests: XCTestCase {
typealias Boxed = DecimalBox

func testIsNull() {
let box = Boxed(42.0)
XCTAssertEqual(box.isNull, false)
}

func testUnbox() {
let values: [Boxed.Unboxed] = [
-1.23,
Expand Down
5 changes: 5 additions & 0 deletions Tests/XMLCoderTests/Box/FloatBoxTests.swift
Expand Up @@ -11,6 +11,11 @@ import XCTest
class FloatBoxTests: XCTestCase {
typealias Boxed = FloatBox

func testIsNull() {
let box = Boxed(42.0)
XCTAssertEqual(box.isNull, false)
}

func testUnbox() {
let values: [Boxed.Unboxed] = [
-3e2,
Expand Down
5 changes: 5 additions & 0 deletions Tests/XMLCoderTests/Box/IntBoxTests.swift
Expand Up @@ -11,6 +11,11 @@ import XCTest
class IntBoxTests: XCTestCase {
typealias Boxed = IntBox

func testIsNull() {
let box = Boxed(-42)
XCTAssertEqual(box.isNull, false)
}

func testUnbox() {
let values: [Boxed.Unboxed] = [
-42,
Expand Down
35 changes: 34 additions & 1 deletion Tests/XMLCoderTests/Box/KeyedBoxTests.swift
Expand Up @@ -9,11 +9,18 @@ import XCTest
@testable import XMLCoder

class KeyedBoxTests: XCTestCase {
lazy var box = KeyedBox(
typealias Boxed = KeyedBox

let box = Boxed(
elements: ["foo": StringBox("bar"), "baz": IntBox(42)],
attributes: ["baz": StringBox("blee")]
)

func testIsNull() {
let box = Boxed()
XCTAssertEqual(box.isNull, false)
}

func testUnbox() {
let (elements, attributes) = box.unbox()

Expand All @@ -28,4 +35,30 @@ class KeyedBoxTests: XCTestCase {
func testXMLString() {
XCTAssertEqual(box.xmlString(), nil)
}

func testDescription() {
// FIXME: once we have an order-preserving storage
// we can check against the full description:
let description = box.description
XCTAssertTrue(description.contains("\"foo\": bar"))
XCTAssertTrue(description.contains("\"baz\": 42"))
}

func testSequence() {
var sortedElements: [(String, Box)] = Array(box.elements)
sortedElements.sort { $0.0 < $1.0 }

XCTAssertEqual(sortedElements[0].0, "baz")
XCTAssertEqual(sortedElements[1].0, "foo")
}

func testSubscript() {
let box = Boxed(
elements: ["foo": StringBox("bar"), "baz": IntBox(42)],
attributes: ["baz": StringBox("blee")]
)
box.elements["bar"] = NullBox()
XCTAssertEqual(box.elements.count, 3)
XCTAssertEqual(box.elements["bar"] as? NullBox, NullBox())
}
}
15 changes: 14 additions & 1 deletion Tests/XMLCoderTests/Box/NullBoxTests.swift
Expand Up @@ -11,8 +11,21 @@ import XCTest
class NullBoxTests: XCTestCase {
typealias Boxed = NullBox

let box = Boxed()

func testIsNull() {
XCTAssertEqual(box.isNull, true)
}

func testXMLString() {
let box = Boxed()
XCTAssertEqual(box.xmlString(), nil)
}

func testEqual() {
XCTAssertEqual(box, Boxed())
}

func testDescription() {
XCTAssertEqual(box.description, "null")
}
}
5 changes: 5 additions & 0 deletions Tests/XMLCoderTests/Box/StringBoxTests.swift
Expand Up @@ -11,6 +11,11 @@ import XCTest
class StringBoxTests: XCTestCase {
typealias Boxed = StringBox

func testIsNull() {
let box = Boxed("lorem ipsum")
XCTAssertEqual(box.isNull, false)
}

func testUnbox() {
let values: [Boxed.Unboxed] = [
"",
Expand Down
5 changes: 5 additions & 0 deletions Tests/XMLCoderTests/Box/UIntBoxTests.swift
Expand Up @@ -11,6 +11,11 @@ import XCTest
class UIntBoxTests: XCTestCase {
typealias Boxed = UIntBox

func testIsNull() {
let box = Boxed(UInt(42))
XCTAssertEqual(box.isNull, false)
}

func testUnbox() {
let values: [Boxed.Unboxed] = [
1,
Expand Down
5 changes: 5 additions & 0 deletions Tests/XMLCoderTests/Box/URLBoxTests.swift
Expand Up @@ -11,6 +11,11 @@ import XCTest
class URLBoxTests: XCTestCase {
typealias Boxed = URLBox

func testIsNull() {
let box = Boxed(URL(string: "http://example.com")!)
XCTAssertEqual(box.isNull, false)
}

func testUnbox() {
let values: [Boxed.Unboxed] = [
URL(string: "file:///")!,
Expand Down
38 changes: 37 additions & 1 deletion Tests/XMLCoderTests/Box/UnkeyedBoxTests.swift
Expand Up @@ -9,7 +9,14 @@ import XCTest
@testable import XMLCoder

class UnkeyedBoxTests: XCTestCase {
lazy var box = UnkeyedBox([StringBox("foo"), IntBox(42)])
typealias Boxed = UnkeyedBox

let box = Boxed([StringBox("foo"), IntBox(42)])

func testIsNull() {
let box = Boxed()
XCTAssertEqual(box.isNull, false)
}

func testUnbox() {
let unboxed = box.unbox()
Expand All @@ -21,4 +28,33 @@ class UnkeyedBoxTests: XCTestCase {
func testXMLString() {
XCTAssertEqual(box.xmlString(), nil)
}

func testDescription() {
XCTAssertEqual(box.description, "[foo, 42]")
}

func testSequence() {
let sequence = IteratorSequence(box.makeIterator())
let array: [Box] = Array(sequence)
XCTAssertEqual(array[0] as? StringBox, StringBox("foo"))
XCTAssertEqual(array[1] as? IntBox, IntBox(42))
}

func testSubscript() {
let box = Boxed([StringBox("foo"), IntBox(42)])
box[0] = NullBox()
XCTAssertEqual(box.count, 2)
XCTAssertEqual(box[0] as? NullBox, NullBox())
XCTAssertEqual(box[1] as? IntBox, IntBox(42))
}

func testInsertAt() {
let box = Boxed([StringBox("foo"), IntBox(42)])
box.insert(NullBox(), at: 1)
XCTAssertEqual(box.count, 3)

XCTAssertEqual(box[0] as? StringBox, StringBox("foo"))
XCTAssertEqual(box[1] as? NullBox, NullBox())
XCTAssertEqual(box[2] as? IntBox, IntBox(42))
}
}

0 comments on commit 68f9c8f

Please sign in to comment.