Skip to content
This repository has been archived by the owner on May 5, 2020. It is now read-only.

Sink to swim unit tests. #115

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Powerup.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
D9D10A401EE130BD003584F4 /* ResultsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9D10A3F1EE130BD003584F4 /* ResultsViewController.swift */; };
D9DD913E1F0363DF009DE3B5 /* ModelClassTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9DD913D1F0363DF009DE3B5 /* ModelClassTests.swift */; };
D9E190A61EE952CD00109F39 /* Accessory.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9E190A51EE952CD00109F39 /* Accessory.swift */; };
D9E922E71F1C9FFA0091D8BB /* SinkToSwimTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9E922E61F1C9FFA0091D8BB /* SinkToSwimTests.swift */; };
D9FFC0E31EE91EE4000F3CA6 /* CustomizeAvatarViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9FFC0E21EE91EE4000F3CA6 /* CustomizeAvatarViewController.swift */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -126,6 +127,7 @@
D9D10A3F1EE130BD003584F4 /* ResultsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ResultsViewController.swift; sourceTree = "<group>"; };
D9DD913D1F0363DF009DE3B5 /* ModelClassTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ModelClassTests.swift; sourceTree = "<group>"; };
D9E190A51EE952CD00109F39 /* Accessory.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Accessory.swift; sourceTree = "<group>"; };
D9E922E61F1C9FFA0091D8BB /* SinkToSwimTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SinkToSwimTests.swift; sourceTree = "<group>"; };
D9FFC0E21EE91EE4000F3CA6 /* CustomizeAvatarViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomizeAvatarViewController.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -203,6 +205,7 @@
2A0CA5671B294C6B00ED8781 /* PowerupTests */ = {
isa = PBXGroup;
children = (
D9E922E61F1C9FFA0091D8BB /* SinkToSwimTests.swift */,
D9A1572D1F0A205200B3A51B /* DatabaseEntryTests.swift */,
D910FF241F06304F00C444DA /* DatabaseAccessorTests.swift */,
D910FF221F05FF5E00C444DA /* CustomizeAvatarViewControllerTests.swift */,
Expand Down Expand Up @@ -368,7 +371,7 @@
};
2A0CA5631B294C6B00ED8781 = {
CreatedOnToolsVersion = 6.3.2;
LastSwiftMigration = 0800;
LastSwiftMigration = 0830;
TestTargetID = 2A0CA54E1B294C6B00ED8781;
};
};
Expand Down Expand Up @@ -455,6 +458,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
D9E922E71F1C9FFA0091D8BB /* SinkToSwimTests.swift in Sources */,
D9A1572E1F0A205200B3A51B /* DatabaseEntryTests.swift in Sources */,
D910FF231F05FF5E00C444DA /* CustomizeAvatarViewControllerTests.swift in Sources */,
D910FF251F06304F00C444DA /* DatabaseAccessorTests.swift in Sources */,
Expand Down Expand Up @@ -642,6 +646,7 @@
OTHER_SWIFT_FLAGS = "";
PRODUCT_BUNDLE_IDENTIFIER = "com.example.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = PowerupTests;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Powerup.app/Powerup";
};
Expand All @@ -651,6 +656,7 @@
isa = XCBuildConfiguration;
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
CLANG_ENABLE_MODULES = YES;
INFOPLIST_FILE = PowerupTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
OTHER_SWIFT_FLAGS = "";
Expand Down
172 changes: 172 additions & 0 deletions PowerupTests/SinkToSwimTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import XCTest
@testable import Powerup

class SinkToSwimTests: XCTestCase {

// The game instance.
var sinkToSwimGame: SinkToSwimGameScene!

override func setUp() {
super.setUp()

// The size of the game is irrelevant for the tests.
sinkToSwimGame = SinkToSwimGameScene(size: CGSize(width: 500.0, height: 500.0))
}

override func tearDown() {
sinkToSwimGame = nil

super.tearDown()
}

/** Test that questions are presented correctly. */
func testQuestionConfiguration() {
// Given (The mock questions, correctAnswer is irrelevant in this test.)
let mockQuestions = [
SinkToSwimQuestion(description: "Mock question one", correctAnswer: true),
SinkToSwimQuestion(description: "Mock question two", correctAnswer: true),
SinkToSwimQuestion(description: "Mock question three", correctAnswer: true)
]

// Inject the mock questions to the game scene.
sinkToSwimGame.questions = mockQuestions

for mockQuestion in mockQuestions {

// When
sinkToSwimGame.showNextQuestion()

// Then
XCTAssertEqual(mockQuestion.description, sinkToSwimGame.questionLabel.text)
}
}

/** Test that when correct/wrong answers are chosen, the score is correctly responded. */
func testScoreChangeWhenAnswerIsChosen() {
// Given (Question description is irrelevant in this test.)
let mockQuestion = [
SinkToSwimQuestion(description: "", correctAnswer: true),
SinkToSwimQuestion(description: "", correctAnswer: true),
SinkToSwimQuestion(description: "", correctAnswer: false),
SinkToSwimQuestion(description: "", correctAnswer: false)
]

// Inject the mock question to the game scene.
sinkToSwimGame.questions = mockQuestion

// Go to the first question.
sinkToSwimGame.showNextQuestion()

// Given
sinkToSwimGame.score = 0
sinkToSwimGame.currQuestion = 0

// When (Choose correct answer when answer is true).
sinkToSwimGame.answerChosen(choice: true)

// Then
XCTAssertEqual(sinkToSwimGame.score, 1)


// Given
sinkToSwimGame.score = 10
sinkToSwimGame.currQuestion = 1

// When (Choose the wrong answer when answer is true).
sinkToSwimGame.answerChosen(choice: false)

// Then (Score remains the same).
XCTAssertEqual(sinkToSwimGame.score, 10)


// Given
sinkToSwimGame.score = 5
sinkToSwimGame.currQuestion = 2

// When (Choose the correct answer when answer is false).
sinkToSwimGame.answerChosen(choice: false)

// Then
XCTAssertEqual(sinkToSwimGame.score, 6)


// Given
sinkToSwimGame.score = 8
sinkToSwimGame.currQuestion = 3

// When (Choose the wrong answer when answer is false).
sinkToSwimGame.answerChosen(choice: true)

// Then
XCTAssertEqual(sinkToSwimGame.score, 8)
}

/** Test that tick timer correctly modifies the timer */
func testTickTimer() {
// Given
sinkToSwimGame.timer = 25

// When
sinkToSwimGame.tickTimer()

// Then
XCTAssertEqual(sinkToSwimGame.timerLabel.text, "24")
}

/** Test that the boat sinks when raisingBoat == false, Test that the boat raises when raisingBoat == true */
func testBoatSinkRaise() {
// Given (Sinking)
sinkToSwimGame.raisingBoat = false

var boatOriginalPositionY = sinkToSwimGame.avatarBoatSprite.position.y

// Ensure that timer isn't zero.
sinkToSwimGame.timer = 30

// When (Update an arbitrary amount of frames and interval.)
sinkToSwimGame.update(5.0)
sinkToSwimGame.update(10.0)
sinkToSwimGame.update(15.0)

// Then
XCTAssert(sinkToSwimGame.avatarBoatSprite.position.y < boatOriginalPositionY)


// Given (Raising)
sinkToSwimGame.raisingBoat = true

boatOriginalPositionY = sinkToSwimGame.avatarBoatSprite.position.y

// When (Update an arbitrary amount of frames and interval.)
sinkToSwimGame.update(23.0)
sinkToSwimGame.update(30.0)

// Then
XCTAssert(sinkToSwimGame.avatarBoatSprite.position.y > boatOriginalPositionY)
}

/** Test that the game ends when timer reaches 0 */
func testTimerReachesZeroGameOver() {
// Given
sinkToSwimGame.timer = 0

// When (Update an arbitrary interval.)
sinkToSwimGame.update(1.0)

// Then
XCTAssert(sinkToSwimGame.isGameOver)
}

/** Test that the game ends when the pointer drops to the bottom of the gauge */
func testPointerDropGameOver() {
// Given (arbitrary x pos, y pos at the bottom of the gauge.)
sinkToSwimGame.waterGaugePointerSprite.position = CGPoint(x: 0.0, y: sinkToSwimGame.waterGaugeMinUnit * Double(sinkToSwimGame.waterGaugeSprite.size.height))

// When (Update arbitrary intervals.)
sinkToSwimGame.update(1.0)
sinkToSwimGame.update(2.0)

// Then
XCTAssert(sinkToSwimGame.isGameOver)
}
}