Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds tests for the Session class #118

Merged
merged 3 commits into from
Mar 8, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ XCTMain([
MemorySessionDriverTests(),
ResponseTests(),
RouterTests(),
RouteTests()
RouteTests(),
SessionTests()
])
118 changes: 118 additions & 0 deletions Tests/Vapor/SessionTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
//
// SessionTests.swift
// Vapor
//
// Created by James Richard on 3/7/16.
// Copyright © 2016 Tanner Nelson. All rights reserved.
//

@testable import Vapor
import XCTest

#if os(Linux)
extension SessionTests: XCTestCaseProvider {
var allTests : [(String, () throws -> Void)] {
return [
("testDestroy_asksDriverToDestroy", testDestroy_asksDriverToDestroy),
("testSubscriptGet_asksDriverForValue", testSubscriptGet_asksDriverForValue),
("testSubscriptSet_asksDriverToSetValue", testSubscriptSet_asksDriverToSetValue)
]
}
}
#endif

class SessionTests: XCTestCase {
var originalDriver: SessionDriver!
private var testDriver: TestDriver!

// TODO: When XCTestCase is converted from a protocol to a class on Linux, these should be renamed to setUp() and
// tearDown(), and not called manually.
func doSetUp() {
originalDriver = Session.driver
testDriver = TestDriver()
Session.driver = testDriver
}

func doTearDown() {
Session.driver = originalDriver
testDriver = nil
}

func testDestroy_asksDriverToDestroy() {
doSetUp()
defer { doTearDown() }
let subject = Session()
subject.destroy()
XCTAssertEqual(testDriver.actions.count, 1)
guard !testDriver.actions.isEmpty else { return }
guard case .Destroy(let session) = testDriver.actions[0] else {
XCTFail("Recorded action was not a destroy action")
return
}

XCTAssert(session === subject)
}

func testSubscriptGet_asksDriverForValue() {
doSetUp()
defer { doTearDown() }
let subject = Session()
_ = subject["test"]

XCTAssertEqual(testDriver.actions.count, 1)
guard !testDriver.actions.isEmpty else { return }
guard case .ValueFor(let key, let session) = testDriver.actions[0] else {
XCTFail("Recorded action was not a value for action")
return
}

XCTAssertEqual(key, "test")
XCTAssert(session === subject)
}

func testSubscriptSet_asksDriverToSetValue() {
doSetUp()
defer { doTearDown() }
let subject = Session()
subject["foo"] = "bar"

XCTAssertEqual(testDriver.actions.count, 1)
guard !testDriver.actions.isEmpty else { return }
guard case .SetValue(let value, let key, let session) = testDriver.actions[0] else {
XCTFail("Recorded action was not a set value action")
return
}

XCTAssertEqual(value, "bar")
XCTAssertEqual(key, "foo")
XCTAssert(session === subject)
}
}

private class TestDriver: SessionDriver {
enum Action {
case ValueFor(key: String, session: Session)
case SetValue(value: String?, key: String, session: Session)
case Destroy(session: Session)
}

var actions = [Action]()

func makeSessionIdentifier() -> String {
return "Foo"
}

func valueFor(key key: String, inSession session: Session) -> String? {
actions.append(.ValueFor(key: key, session: session))
return nil
}

func set(value: String?, forKey key: String, inSession session: Session) {
actions.append(.SetValue(value: value, key: key, session: session))
}

func destroy(session: Session) {
actions.append(.Destroy(session: session))
}

}
12 changes: 8 additions & 4 deletions XcodeProject/Vapor.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
1A33BBAC1C874555000499CE /* NSLock+Closure.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A33BBAB1C874555000499CE /* NSLock+Closure.swift */; };
1AD708021C8E6A9500F17339 /* SessionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AD708011C8E6A9500F17339 /* SessionTests.swift */; };
1AF2F51B1C89DE2A00A7B2EA /* MemorySessionDriverTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AF2F51A1C89DE2A00A7B2EA /* MemorySessionDriverTests.swift */; };
3492B2EF1C787DD600D8E588 /* RouteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3492B2EE1C787DD600D8E588 /* RouteTests.swift */; };
34CC59561C7BE3C9007CA680 /* LogTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34CC59551C7BE3C9007CA680 /* LogTests.swift */; };
Expand Down Expand Up @@ -89,6 +90,7 @@

/* Begin PBXFileReference section */
1A33BBAB1C874555000499CE /* NSLock+Closure.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "NSLock+Closure.swift"; path = "../../Sources/Vapor/Utilities/NSLock+Closure.swift"; sourceTree = "<group>"; };
1AD708011C8E6A9500F17339 /* SessionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SessionTests.swift; path = ../../Tests/Vapor/SessionTests.swift; sourceTree = "<group>"; };
1AF2F51A1C89DE2A00A7B2EA /* MemorySessionDriverTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MemorySessionDriverTests.swift; path = ../../Tests/Vapor/MemorySessionDriverTests.swift; sourceTree = "<group>"; };
288CFC2D1C7AC01A00E4617A /* Application.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Application.swift; path = ../Sources/Vapor/Core/Application.swift; sourceTree = SOURCE_ROOT; };
288CFC301C7AC02A00E4617A /* Provider.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Provider.swift; path = ../Sources/Vapor/Core/Provider.swift; sourceTree = SOURCE_ROOT; };
Expand Down Expand Up @@ -389,16 +391,17 @@
C352E6901C62BB1C00E26467 /* Tests */ = {
isa = PBXGroup;
children = (
C352E6931C62BB1C00E26467 /* Info.plist */,
C3F683351C7D1EF300033AA2 /* ControllerTests.swift */,
C3433FD21C7D119D009F0876 /* HashTests.swift */,
C352E6931C62BB1C00E26467 /* Info.plist */,
34CC59551C7BE3C9007CA680 /* LogTests.swift */,
1AF2F51A1C89DE2A00A7B2EA /* MemorySessionDriverTests.swift */,
800EFA571C8231EE00F6C066 /* ProcessTests.swift */,
800EFA591C83A00300F6C066 /* QueryParametersTests.swift */,
C38808631C62C0390067DADD /* ResponseTests.swift */,
C3943DC21C7663E80014F4EE /* RouterTests.swift */,
3492B2EE1C787DD600D8E588 /* RouteTests.swift */,
800EFA571C8231EE00F6C066 /* ProcessTests.swift */,
800EFA591C83A00300F6C066 /* QueryParametersTests.swift */,
1AF2F51A1C89DE2A00A7B2EA /* MemorySessionDriverTests.swift */,
1AD708011C8E6A9500F17339 /* SessionTests.swift */,
);
path = Tests;
sourceTree = "<group>";
Expand Down Expand Up @@ -704,6 +707,7 @@
800EFA581C8231EE00F6C066 /* ProcessTests.swift in Sources */,
C38808641C62C0390067DADD /* ResponseTests.swift in Sources */,
800EFA5A1C83A00300F6C066 /* QueryParametersTests.swift in Sources */,
1AD708021C8E6A9500F17339 /* SessionTests.swift in Sources */,
3492B2EF1C787DD600D8E588 /* RouteTests.swift in Sources */,
C3F683361C7D1EF300033AA2 /* ControllerTests.swift in Sources */,
34CC59561C7BE3C9007CA680 /* LogTests.swift in Sources */,
Expand Down