Files
moonlight-ios/MoonlightUnitTests/Input/ControllerUnitTests.swift
David 983c65d399 Rewrote the Controller class in Swift (#216)
* Enabled Defines Modules for swift code

* Created bridge header, created swift Controller

-  Created Controller.swift
-  Created bridge header for use of objc in swift

* Finished porting Controller files to swift

* Added comments, created MoonlightUnitTest

-  Added comments for Controller.swift
-  Created MoonlightUnitTest for testing new class

* Started writing tests for ControllerUnitTests

-  General formatting
-  Wrote helper functions
-  Wrote tests for first four properties

* Finished writing Controller tests

* Removed commented out lines
2016-04-11 20:44:02 -07:00

193 lines
8.1 KiB
Swift

//
// ControllerUnitTests.swift
// Moonlight
//
// Created by David Aghassi on 4/11/16.
// Copyright © 2016 Moonlight Stream. All rights reserved.
//
import XCTest
@testable import Moonlight
/**
Tests the `Controller.swift` class
*/
class ControllerUnitTests: XCTestCase {
var controllerUnderTest: Controller!
override func setUp() {
controllerUnderTest = Controller()
XCTAssertNotNil(controllerUnderTest, "controllerUnderTest is nil, and should not be for this test. See line \(#line) \n")
}
override func tearDown() {
resetAllValues()
}
// MARK: Helper Methods
/**
Sets all the values of `controllerUnderTest` to `0`
*/
func resetAllValues() {
controllerUnderTest.emulatingButtonFlags = CInt(0)
controllerUnderTest.lastButtonFlags = CInt(0)
controllerUnderTest.lastLeftStickX = CShort(0)
controllerUnderTest.lastLeftStickY = CShort(0)
controllerUnderTest.lastRightStickX = CShort(0)
controllerUnderTest.lastRightStickY = CShort(0)
controllerUnderTest.lastLeftTrigger = CChar(0)
controllerUnderTest.lastRightTrigger = CChar(0)
}
/**
Displays "*name* failed to set CInt correctly." Shows expected and actual, as well as failure line.
- parameter name: The property being tested
- parameter expected: The expected value for the property
- parameter actual: The actual value for the property
- returns: A string with the failure in it. Formatted to state actual, expected, and the failure line.
*/
func displayCIntFailure(name: String, expected: CInt, actual: CInt) -> String {
return "\(name) failed to set CInt correctly \n. Expected: \(expected)\n. Actual: \(actual) \n. See line \(#line) \n"
}
/**
Displays "*name* failed to set CShort correctly." Shows expected and actual, as well as failure line.
- parameter name: The property being tested
- parameter expected: The expected value for the property
- parameter actual: The actual value for the property
- returns: A string with the failure in it. Formatted to state actual, expected, and the failure line.
*/
func displayCShortFailure(name: String, expected: CShort, actual: CShort) -> String {
return "\(name) failed to set CShort correctly \n. Expected: \(expected)\n. Actual: \(actual) \n. See line \(#line) \n"
}
/**
Displays "*name* failed to set CCHar correctly." Shows expected and actual, as well as the failure line.
- parameter name: The property being tested
- parameter expected: The expected value for the property
- parameter actual: The actual value for the property
- returns: A string with the failure in it. Formatted to state actual, expected, and the failure line.
*/
func displayCCharFailure(name: String, expected: CChar, actual: CChar) -> String {
return "\(name) failed to set CChar correctly \n. Expected: \(expected)\n. Actual: \(actual) \n. See line \(#line) \n"
}
// MARK: Tests
/**
Asserts that the `emulatingButtonFlags` is of type `CInt` and can be set and gotten from properly
*/
func test_Assert_emulatingButtonFlags_Sets_To_CInt() {
// Assert type hasn't changed
XCTAssertTrue(controllerUnderTest.emulatingButtonFlags.dynamicType == CInt.self, "Expected emulatingButtonFlags to be of type CInt. See line \(#line) \n")
// Assert value is assigned properly.
let expected = CInt(1)
controllerUnderTest.emulatingButtonFlags = expected
XCTAssertTrue(controllerUnderTest.emulatingButtonFlags == expected, displayCIntFailure("emulatingButtonFlags", expected: expected, actual: controllerUnderTest.emulatingButtonFlags))
}
/**
Asserts that the `lastButtonFlags` is of type `CInt` and can be set and gotten from properly
*/
func test_Assert_lastButtonFlags_Sets_To_CInt() {
// Assert type hasn't changed
XCTAssertTrue(controllerUnderTest.lastButtonFlags.dynamicType == CInt.self, "Expected lastButtonFlags to be of type CInt. See line \(#line) \n")
// Assert value is assigned properly.
let expected = CInt(1)
controllerUnderTest.lastButtonFlags = expected
XCTAssertTrue(controllerUnderTest.lastButtonFlags == expected, displayCIntFailure("lastButtonFlags", expected: expected, actual: controllerUnderTest.lastButtonFlags))
}
/**
Asserts that the `lastLeftStickX` is of type `CShort` and can be set and gotten from properly
*/
func test_Assert_lastLeftStickX_Sets_To_CShort() {
// Assert type hasn't changed
XCTAssertTrue(controllerUnderTest.lastLeftStickX.dynamicType == CShort.self, "Expected lastLeftStickX to be of type CShort. See line \(#line) \n")
// Assert value is assigned properly.
let expected = CShort(1)
controllerUnderTest.lastLeftStickX = expected
XCTAssertTrue(controllerUnderTest.lastLeftStickX == expected, displayCShortFailure("lastLeftStickX", expected: expected, actual: controllerUnderTest.lastLeftStickX))
}
/**
Asserts that lastLeftStickY` is of type `CShort` and can be set and gotten from properly
*/
func test_Assert_lastLeftStickY_Sets_To_CShort() {
// Assert type hasn't changed
XCTAssertTrue(controllerUnderTest.lastLeftStickY.dynamicType == CShort.self, "Expected lastLeftStickY to be of type CShort. See line \(#line) \n")
// Assert value is assigned properly.
let expected = CShort(1)
controllerUnderTest.lastLeftStickY = expected
XCTAssertTrue(controllerUnderTest.lastLeftStickY == expected, displayCShortFailure("lastLeftStickY", expected: expected, actual: controllerUnderTest.lastLeftStickY))
}
/**
Asserts that the `lastRightStickX` is of type `CShort` and can be set and gotten from properly
*/
func test_Assert_lastRightStickX_SetsTo_CShort() {
// Assert type hasn't changed
XCTAssertTrue(controllerUnderTest.lastRightStickX.dynamicType == CShort.self, "Expected lastRightStickX to be of type CShort. See line \(#line) \n")
// Assert value is assigned properly.
let expected = CShort(1)
controllerUnderTest.lastRightStickX = expected
XCTAssertTrue(controllerUnderTest.lastRightStickX == expected, displayCShortFailure("lastRightStickX", expected: expected, actual: controllerUnderTest.lastRightStickX))
}
/**
Asserts that the `lastRightStickY` is of type `CShort` and can be set and gotten from properly
*/
func test_Assert_lastRightStickY_Sets_To_CShort() {
// Assert type hasn't changed
XCTAssertTrue(controllerUnderTest.lastRightStickY.dynamicType == CShort.self, "Expected lastRightStickY to be of type CShort. See line \(#line) \n")
// Assert value is assigned properly.
let expected = CShort(1)
controllerUnderTest.lastRightStickY = expected
XCTAssertTrue(controllerUnderTest.lastRightStickY == expected, displayCShortFailure("lastRightStickY", expected: expected, actual: controllerUnderTest.lastRightStickY))
}
/**
Asserts that the `lastLeftTrigger` is of type `CChar` and can be set and gotten from properly
*/
func test_Assert_lastLeftTrigger_Sets_To_CChar() {
// Assert type hasn't changed
XCTAssertTrue(controllerUnderTest.lastLeftTrigger.dynamicType == CChar.self, "Expected lastLeftTrigger to be of type CChar. See line \(#line) \n")
// Assert value is assigned properly.
let expected = CChar(1)
controllerUnderTest.lastLeftTrigger = expected
XCTAssertTrue(controllerUnderTest.lastLeftTrigger == expected, displayCCharFailure("lastLeftTrigger", expected: expected, actual: controllerUnderTest.lastLeftTrigger))
}
/**
Asserts that the `lastRightTrigger` is of type `CChar` and can be set and gotten from properly
*/
func test_Assert_lastRightTrigger_Sets_To_CChar() {
// Assert type hasn't changed
XCTAssertTrue(controllerUnderTest.lastRightTrigger.dynamicType == CChar.self, "Expected lastRightTrigger to be of type CChar. See line \(#line) \n")
// Assert value is assigned properly.
let expected = CChar(1)
controllerUnderTest.lastRightTrigger = expected
XCTAssertTrue(controllerUnderTest.lastRightTrigger == expected, displayCCharFailure("lastRightTrigger", expected: expected, actual: controllerUnderTest.lastRightTrigger))
}
}