Skip to content

Commit 76cb4d9

Browse files
committed
Merge pull request #1 from masters3d/SR1187AddTests
Added tests and lowercased enums
2 parents 6e7a771 + 83cb9c2 commit 76cb4d9

File tree

8 files changed

+178
-17
lines changed

8 files changed

+178
-17
lines changed

Sources/PlayingCard.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public func ==(lhs: PlayingCard, rhs: PlayingCard) -> Bool {
3131
extension PlayingCard: Comparable {}
3232

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

3737
// MARK: - CustomStringConvertible

Sources/Rank.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
*/
1010

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

1717
// MARK: - Comparable
@@ -22,7 +22,7 @@ public func <(lhs: Rank, rhs: Rank) -> Bool {
2222
switch (lhs, rhs) {
2323
case (_, _) where lhs == rhs:
2424
return false
25-
case (.Ace, _):
25+
case (.ace, _):
2626
return false
2727
default:
2828
return lhs.rawValue < rhs.rawValue
@@ -34,10 +34,10 @@ public func <(lhs: Rank, rhs: Rank) -> Bool {
3434
extension Rank : CustomStringConvertible {
3535
public var description: String {
3636
switch self {
37-
case .Ace: return "A"
38-
case .Jack: return "J"
39-
case .Queen: return "Q"
40-
case .King: return "K"
37+
case .ace: return "A"
38+
case .jack: return "J"
39+
case .queen: return "Q"
40+
case .king: return "K"
4141
default:
4242
return "\(rawValue)"
4343
}

Sources/Suit.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010

1111
public enum Suit: String {
12-
case Spades, Hearts, Diamonds, Clubs
12+
case spades, hearts, diamonds, clubs
1313
}
1414

1515
// MARK: - Comparable
@@ -20,9 +20,9 @@ public func <(lhs: Suit, rhs: Suit) -> Bool {
2020
switch (lhs, rhs) {
2121
case (_, _) where lhs == rhs:
2222
return false
23-
case (.Spades, _),
24-
(.Hearts, .Diamonds), (.Hearts, .Clubs),
25-
(.Diamonds, .Clubs):
23+
case (.spades, _),
24+
(.hearts, .diamonds), (.hearts, .clubs),
25+
(.diamonds, .clubs):
2626
return false
2727
default:
2828
return true
@@ -34,10 +34,10 @@ public func <(lhs: Suit, rhs: Suit) -> Bool {
3434
extension Suit : CustomStringConvertible {
3535
public var description: String {
3636
switch self {
37-
case .Spades: return "♠︎"
38-
case .Hearts: return ""
39-
case .Diamonds: return ""
40-
case .Clubs: return "♣︎"
37+
case .spades: return "♠︎"
38+
case .hearts: return ""
39+
case .diamonds: return ""
40+
case .clubs: return "♣︎"
4141
}
4242
}
4343
}

Tests/LinuxMain.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See http://swift.org/LICENSE.txt for license information
8+
See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
9+
*/
10+
11+
@testable import PlayingCardTestSuite
12+
13+
import XCTest
14+
15+
XCTMain([testCase(CardTest.allTest),
16+
testCase(RankTest.allTest),
17+
testCase(SuitTest.allTest)
18+
])

Tests/PlayingCard/CardTests.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See http://swift.org/LICENSE.txt for license information
8+
See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
9+
*/
10+
11+
@testable import PlayingCard
12+
import XCTest
13+
14+
class CardTests: XCTestCase {
15+
16+
func testCardSingle() {
17+
let card1 = PlayingCard(rank: .queen, suit: .diamonds)
18+
let card2 = PlayingCard(rank: .king, suit: .diamonds)
19+
let card3 = PlayingCard(rank: .ace, suit: .clubs)
20+
let card4 = PlayingCard(rank: .queen, suit: .diamonds)
21+
let card5 = PlayingCard(rank: .queen, suit: .clubs)
22+
23+
XCTAssertGreaterThan(card2, card1)
24+
XCTAssertGreaterThan(card3, card2)
25+
XCTAssertEqual(card1, card4)
26+
XCTAssertGreaterThan(card4, card5)
27+
}
28+
29+
func testCardStringEquality() {
30+
let card1 = PlayingCard(rank: .jack, suit: .clubs)
31+
let card2 = PlayingCard(rank: .two, suit: .hearts)
32+
let card3 = PlayingCard(rank: .queen, suit: .diamonds)
33+
34+
XCTAssertEqual(String(card1), "♣︎J")
35+
XCTAssertEqual(String(card2), "♡2")
36+
XCTAssertEqual(String(card3), "♢Q")
37+
}
38+
39+
}

Tests/PlayingCard/RankTests.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See http://swift.org/LICENSE.txt for license information
8+
See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
9+
*/
10+
11+
@testable import PlayingCard
12+
import XCTest
13+
14+
class RankTests: XCTestCase {
15+
16+
func testRankStringEquality() {
17+
let numbers = [2,3,4,5,6,7,8,9,10]
18+
let suits = numbers.map{Rank(rawValue:$0)}.flatMap{$0}.map{Int(String($0)) ?? 0}
19+
20+
XCTAssertEqual(String(Rank.ace), "A")
21+
XCTAssertEqual(String(Rank.jack), "J")
22+
XCTAssertEqual(String(Rank.queen), "Q")
23+
XCTAssertEqual(String(Rank.king), "K")
24+
XCTAssertEqual(numbers, suits)
25+
}
26+
27+
func testRankComparable() {
28+
XCTAssertGreaterThan(Rank.ace, Rank.two)
29+
XCTAssertGreaterThan(Rank.ace, Rank.king)
30+
XCTAssertGreaterThan(Rank.king, Rank.queen)
31+
XCTAssertGreaterThan(Rank.queen, Rank.jack)
32+
XCTAssertGreaterThan(Rank.jack, Rank.ten)
33+
}
34+
35+
}

Tests/PlayingCard/SuitTests.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See http://swift.org/LICENSE.txt for license information
8+
See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
9+
*/
10+
11+
@testable import PlayingCard
12+
import XCTest
13+
14+
class SuitTests: XCTestCase {
15+
16+
func testSuitStringEquality() {
17+
XCTAssertEqual(String(Suit.spades), "♠︎")
18+
XCTAssertEqual(String(Suit.hearts), "")
19+
XCTAssertEqual(String(Suit.diamonds), "")
20+
XCTAssertEqual(String(Suit.clubs), "♣︎")
21+
}
22+
23+
func testSuitComparable() {
24+
XCTAssertGreaterThan(Suit.spades, Suit.diamonds)
25+
XCTAssertGreaterThan(Suit.hearts, Suit.diamonds )
26+
XCTAssertGreaterThan(Suit.hearts, Suit.clubs)
27+
XCTAssertGreaterThan(Suit.diamonds, Suit.clubs)
28+
}
29+
30+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright 2015 - 2016 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See http://swift.org/LICENSE.txt for license information
8+
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
extension CardTests {
12+
13+
static var allTests : [(String, CardTests -> () throws -> Void)] {
14+
return [
15+
("testCardSingle", testCardSingle),
16+
("testCardStringEquality",testCardStringEquality)
17+
]
18+
}
19+
}
20+
21+
extension RankTests {
22+
23+
static var allTests : [(String, RankTests -> () throws -> Void)] {
24+
return [
25+
("testRankStringEquality",testRankStringEquality),
26+
("testRankComparable", testRankComparable)
27+
]
28+
}
29+
}
30+
31+
extension SuitTests {
32+
33+
static var allTests : [(String, SuitTests -> () throws -> Void)] {
34+
return [
35+
("testSuitStringEquality",testSuitStringEquality),
36+
("testSuitComparable",testSuitComparable )
37+
]
38+
}
39+
}

0 commit comments

Comments
 (0)