Skip to content

Instantly share code, notes, and snippets.

@froggomad
Last active March 18, 2020 23:49
Show Gist options
  • Save froggomad/8cc209211fd18d1ae260a21a4f66c916 to your computer and use it in GitHub Desktop.
Save froggomad/8cc209211fd18d1ae260a21a4f66c916 to your computer and use it in GitHub Desktop.
Tests For the Protocol Challenge
//
// ProtocolChallengeUnitTestsTests.swift
// ProtocolChallengeUnitTestsTests
//
// Created by Kenny on 3/18/20.
// Copyright © 2020 Hazy Studios. All rights reserved.
//
import XCTest
@testable import ProtocolChallengeUnitTests
class ProtocolChallengeUnitTestsTests: XCTestCase {
func testAllRanks() {
//check count
XCTAssertEqual(Rank.allRanks.count, 13)
//check rank names
let allRanks = [Rank.ace, Rank.two, Rank.three, Rank.four, Rank.five, Rank.six, Rank.seven, Rank.eight, Rank.nine, Rank.ten, Rank.jack, Rank.queen, Rank.king]
XCTAssertEqual(allRanks, Rank.allRanks)
}
//=======================
// MARK: - Test Card Descriptions
func testValueCardDescriptions() {
XCTAssertEqual(Rank.two.description, "2")
XCTAssertEqual(Rank.three.description, "3")
XCTAssertEqual(Rank.four.description, "4")
XCTAssertEqual(Rank.five.description, "5")
XCTAssertEqual(Rank.six.description, "6")
XCTAssertEqual(Rank.seven.description, "7")
XCTAssertEqual(Rank.eight.description, "8")
XCTAssertEqual(Rank.nine.description, "9")
XCTAssertEqual(Rank.ten.description, "10")
}
func lowercase(_ str: String) -> String {
return str.lowercased()
}
func testFaceCardDescriptions() {
XCTAssertEqual(lowercase(Rank.ace.description), "ace")
XCTAssertEqual(lowercase(Rank.king.description), "king")
XCTAssertEqual(lowercase(Rank.queen.description), "queen")
XCTAssertEqual(lowercase(Rank.jack.description), "jack")
}
//=======================
// MARK: - TestSuits
func testAllSuits() {
XCTAssertEqual(Suit.allSuits.count, 4)
XCTAssert(Suit.allSuits.contains(.clubs))
XCTAssert(Suit.allSuits.contains(.diamonds))
XCTAssert(Suit.allSuits.contains(.hearts))
XCTAssert(Suit.allSuits.contains(.spades))
}
//: ## Step 4
//: Using the two enums above, create a `struct` called `Card` to model a single playing card. It should have constant properties for each constituent piece (one for suit and one for rank).
//: ## Step 5
//: Make the card also conform to `CustomStringConvertible`. When turned into a string, a card's value should look something like this, "ace of spades", or "3 of diamonds".
//: Step 18
//: Make the `Card` type conform to the `Comparable` protocol. Implement the `<` and `==` methods such that they compare the ranks of the `lhs` and `rhs` arguments passed in. For the `==` method, compare **both** the rank and the suit.
//=======================
// MARK: - Test Card struct
func testCard() {
let twoOfHearts = Card(rank: .two, suit: .hearts)
XCTAssertEqual(twoOfHearts.rank, .two)
XCTAssertEqual(twoOfHearts.suit, .hearts)
XCTAssertEqual(twoOfHearts.description, "\(twoOfHearts.rank) of \(twoOfHearts.suit)")
let threeOfHearts = Card(rank: .three, suit: .hearts)
XCTAssert(twoOfHearts.rank < threeOfHearts.rank)
let twoOfSpades = Card(rank: .two, suit: .spades)
XCTAssertNotEqual(twoOfSpades, twoOfHearts)
}
//: ## Step 6
//: Create a `struct` to model a deck of cards. It should be called `Deck` and have an array of `Card` objects as a constant property. A custom `init` function should be created that initializes the array with a card of each rank and suit. You'll want to iterate over all ranks, and then over all suits (this is an example of _nested `for` loops_). See the next 2 steps before you continue with the nested loops.
//: ## Step 9
//: Back to the `Deck` and the nested loops. Now that you have a way to get arrays of all rank values and all suit values, create 2 `for` loops in the `init` method, one nested inside the other, where you iterate over each value of rank, and then iterate over each value of suit. See an example below to get an idea of how this will work. Imagine an enum that contains the 4 cardinal directions, and imagine that enum has a property `allDirections` that returns an array of them.
//: ```
//: for direction in Compass.allDirections {
//:
//:}
//:```
//: ## Step 10
//: These loops will allow you to match up every rank with every suit. Make a `Card` object from all these pairings and append each card to the `cards` property of the deck. At the end of the `init` method, the `cards` array should contain a full deck of standard playing card objects.
//: ## Step 11
//: Add a method to the deck called `drawCard()`. It takes no arguments and it returns a `Card` object. Have it draw a random card from the deck of cards and return it.
//: - Callout(Hint): There should be `52` cards in the deck. So what if you created a random number within those bounds and then retrieved that card from the deck? Remember that arrays are indexed from `0` and take that into account with your random number picking.
//=======================
// MARK: - Test Deck Class
func testDeck() {
let deck = Deck()
XCTAssertEqual(deck.cards.count, 52)
var cards: [Card] = []
for suit in Suit.allSuits {
for rank in Rank.allRanks {
let aCard = Card(rank: rank, suit: suit)
cards.append(aCard)
}
}
XCTAssertEqual(cards, deck.cards)
let randCard = deck.drawCard()
XCTAssert(Rank.allRanks.contains(randCard.rank))
}
//=======================
// MARK: - Test Game Methods exist and run
func testGame() {
let highLow = HighLow()
highLow.delegate = CardGameTracker()
highLow.play()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment