Skip to content

Commit

Permalink
Merge pull request #1 from masters3d/SR1187AddTests
Browse files Browse the repository at this point in the history
Added tests and lowercased enums
  • Loading branch information
abertelrud committed May 22, 2016
2 parents 6e7a771 + 83cb9c2 commit 76cb4d9
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Sources/PlayingCard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public func ==(lhs: PlayingCard, rhs: PlayingCard) -> Bool {
extension PlayingCard: Comparable {}

public func <(lhs: PlayingCard, rhs: PlayingCard) -> Bool {
return lhs.suit < rhs.suit || (lhs.suit == rhs.suit && lhs.rank < rhs.rank)
return lhs.rank == rhs.rank ? lhs.suit < rhs.suit : lhs.rank < rhs.rank
}

// MARK: - CustomStringConvertible
Expand Down
16 changes: 8 additions & 8 deletions Sources/Rank.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
*/

public enum Rank : Int {
case Ace = 1
case Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten
case Jack, Queen, King
case two = 2
case three, four, five, six, seven, eight, nine, ten
case jack, queen, king, ace
}

// MARK: - Comparable
Expand All @@ -22,7 +22,7 @@ public func <(lhs: Rank, rhs: Rank) -> Bool {
switch (lhs, rhs) {
case (_, _) where lhs == rhs:
return false
case (.Ace, _):
case (.ace, _):
return false
default:
return lhs.rawValue < rhs.rawValue
Expand All @@ -34,10 +34,10 @@ public func <(lhs: Rank, rhs: Rank) -> Bool {
extension Rank : CustomStringConvertible {
public var description: String {
switch self {
case .Ace: return "A"
case .Jack: return "J"
case .Queen: return "Q"
case .King: return "K"
case .ace: return "A"
case .jack: return "J"
case .queen: return "Q"
case .king: return "K"
default:
return "\(rawValue)"
}
Expand Down
16 changes: 8 additions & 8 deletions Sources/Suit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/

public enum Suit: String {
case Spades, Hearts, Diamonds, Clubs
case spades, hearts, diamonds, clubs
}

// MARK: - Comparable
Expand All @@ -20,9 +20,9 @@ public func <(lhs: Suit, rhs: Suit) -> Bool {
switch (lhs, rhs) {
case (_, _) where lhs == rhs:
return false
case (.Spades, _),
(.Hearts, .Diamonds), (.Hearts, .Clubs),
(.Diamonds, .Clubs):
case (.spades, _),
(.hearts, .diamonds), (.hearts, .clubs),
(.diamonds, .clubs):
return false
default:
return true
Expand All @@ -34,10 +34,10 @@ public func <(lhs: Suit, rhs: Suit) -> Bool {
extension Suit : CustomStringConvertible {
public var description: String {
switch self {
case .Spades: return "♠︎"
case .Hearts: return ""
case .Diamonds: return ""
case .Clubs: return "♣︎"
case .spades: return "♠︎"
case .hearts: return ""
case .diamonds: return ""
case .clubs: return "♣︎"
}
}
}
18 changes: 18 additions & 0 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
This source file is part of the Swift.org open source project

Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
*/

@testable import PlayingCardTestSuite

import XCTest

XCTMain([testCase(CardTest.allTest),
testCase(RankTest.allTest),
testCase(SuitTest.allTest)
])
39 changes: 39 additions & 0 deletions Tests/PlayingCard/CardTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
This source file is part of the Swift.org open source project

Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
*/

@testable import PlayingCard
import XCTest

class CardTests: XCTestCase {

func testCardSingle() {
let card1 = PlayingCard(rank: .queen, suit: .diamonds)
let card2 = PlayingCard(rank: .king, suit: .diamonds)
let card3 = PlayingCard(rank: .ace, suit: .clubs)
let card4 = PlayingCard(rank: .queen, suit: .diamonds)
let card5 = PlayingCard(rank: .queen, suit: .clubs)

XCTAssertGreaterThan(card2, card1)
XCTAssertGreaterThan(card3, card2)
XCTAssertEqual(card1, card4)
XCTAssertGreaterThan(card4, card5)
}

func testCardStringEquality() {
let card1 = PlayingCard(rank: .jack, suit: .clubs)
let card2 = PlayingCard(rank: .two, suit: .hearts)
let card3 = PlayingCard(rank: .queen, suit: .diamonds)

XCTAssertEqual(String(card1), "♣︎J")
XCTAssertEqual(String(card2), "♡2")
XCTAssertEqual(String(card3), "♢Q")
}

}
35 changes: 35 additions & 0 deletions Tests/PlayingCard/RankTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
This source file is part of the Swift.org open source project

Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
*/

@testable import PlayingCard
import XCTest

class RankTests: XCTestCase {

func testRankStringEquality() {
let numbers = [2,3,4,5,6,7,8,9,10]
let suits = numbers.map{Rank(rawValue:$0)}.flatMap{$0}.map{Int(String($0)) ?? 0}

XCTAssertEqual(String(Rank.ace), "A")
XCTAssertEqual(String(Rank.jack), "J")
XCTAssertEqual(String(Rank.queen), "Q")
XCTAssertEqual(String(Rank.king), "K")
XCTAssertEqual(numbers, suits)
}

func testRankComparable() {
XCTAssertGreaterThan(Rank.ace, Rank.two)
XCTAssertGreaterThan(Rank.ace, Rank.king)
XCTAssertGreaterThan(Rank.king, Rank.queen)
XCTAssertGreaterThan(Rank.queen, Rank.jack)
XCTAssertGreaterThan(Rank.jack, Rank.ten)
}

}
30 changes: 30 additions & 0 deletions Tests/PlayingCard/SuitTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
This source file is part of the Swift.org open source project

Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
*/

@testable import PlayingCard
import XCTest

class SuitTests: XCTestCase {

func testSuitStringEquality() {
XCTAssertEqual(String(Suit.spades), "♠︎")
XCTAssertEqual(String(Suit.hearts), "")
XCTAssertEqual(String(Suit.diamonds), "")
XCTAssertEqual(String(Suit.clubs), "♣︎")
}

func testSuitComparable() {
XCTAssertGreaterThan(Suit.spades, Suit.diamonds)
XCTAssertGreaterThan(Suit.hearts, Suit.diamonds )
XCTAssertGreaterThan(Suit.hearts, Suit.clubs)
XCTAssertGreaterThan(Suit.diamonds, Suit.clubs)
}

}
39 changes: 39 additions & 0 deletions Tests/PlayingCard/XCTestManifests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
This source file is part of the Swift.org open source project

Copyright 2015 - 2016 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

extension CardTests {

static var allTests : [(String, CardTests -> () throws -> Void)] {
return [
("testCardSingle", testCardSingle),
("testCardStringEquality",testCardStringEquality)
]
}
}

extension RankTests {

static var allTests : [(String, RankTests -> () throws -> Void)] {
return [
("testRankStringEquality",testRankStringEquality),
("testRankComparable", testRankComparable)
]
}
}

extension SuitTests {

static var allTests : [(String, SuitTests -> () throws -> Void)] {
return [
("testSuitStringEquality",testSuitStringEquality),
("testSuitComparable",testSuitComparable )
]
}
}

0 comments on commit 76cb4d9

Please sign in to comment.