Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vermont42 committed May 3, 2019
1 parent 076b329 commit 8cbea8e
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 17 deletions.
8 changes: 8 additions & 0 deletions Conjugar.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
038E03202273BF29005CFC96 /* RatingsFetcherTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 038E031F2273BF29005CFC96 /* RatingsFetcherTests.swift */; };
038E032222760A40005CFC96 /* UIViewExtensionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 038E032122760A40005CFC96 /* UIViewExtensionsTests.swift */; };
038E0324227617FC005CFC96 /* UIApplicationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 038E0323227617FC005CFC96 /* UIApplicationTests.swift */; };
038E03262279CCCB005CFC96 /* SettingsVCTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 038E03252279CCCB005CFC96 /* SettingsVCTests.swift */; };
038E03282279D034005CFC96 /* URLSessionExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 038E03272279D034005CFC96 /* URLSessionExtension.swift */; };
E10178021F3F753400F0BC97 /* UIViewExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = E10178011F3F753400F0BC97 /* UIViewExtensions.swift */; };
E107B08B1EB66CE6004C5E91 /* verbs.xml in Resources */ = {isa = PBXBuildFile; fileRef = E107B08A1EB66CE6004C5E91 /* verbs.xml */; };
E107B0921EB66F28004C5E91 /* VerbParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = E107B0911EB66F28004C5E91 /* VerbParser.swift */; };
Expand Down Expand Up @@ -161,6 +163,8 @@
038E031F2273BF29005CFC96 /* RatingsFetcherTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RatingsFetcherTests.swift; sourceTree = "<group>"; };
038E032122760A40005CFC96 /* UIViewExtensionsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIViewExtensionsTests.swift; sourceTree = "<group>"; };
038E0323227617FC005CFC96 /* UIApplicationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIApplicationTests.swift; sourceTree = "<group>"; };
038E03252279CCCB005CFC96 /* SettingsVCTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsVCTests.swift; sourceTree = "<group>"; };
038E03272279D034005CFC96 /* URLSessionExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLSessionExtension.swift; sourceTree = "<group>"; };
E10178011F3F753400F0BC97 /* UIViewExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIViewExtensions.swift; sourceTree = "<group>"; };
E107B08A1EB66CE6004C5E91 /* verbs.xml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = verbs.xml; sourceTree = "<group>"; };
E107B0911EB66F28004C5E91 /* VerbParser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VerbParser.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -365,6 +369,7 @@
E1E092C221ED3C6900828C37 /* UserDefaultsGetterSetter.swift */,
E18F7F371ECA7C9A00633200 /* Utterer.swift */,
038E031D22734800005CFC96 /* URLProtocolStub.swift */,
038E03272279D034005CFC96 /* URLSessionExtension.swift */,
);
name = Utils;
sourceTree = "<group>";
Expand Down Expand Up @@ -434,6 +439,7 @@
E1F06AEC1E8F05F400ADD2E1 /* MainTabBarVCTests.swift */,
E15D3BF01F532F4E003771BB /* BrowseVerbsVCTests.swift */,
E14589F31F5B7BB000EEF141 /* BrowseInfoVCTests.swift */,
038E03252279CCCB005CFC96 /* SettingsVCTests.swift */,
E14589F51F5B7E9D00EEF141 /* VerbVCTests.swift */,
);
path = Controllers;
Expand Down Expand Up @@ -796,6 +802,7 @@
E1EC71012198BA6400872787 /* AWSAnalyticsService.swift in Sources */,
E188B56021AF78CE009777D8 /* ReviewPromptable.swift in Sources */,
E19382041F0822B2005268FF /* InfoVC.swift in Sources */,
038E03282279D034005CFC96 /* URLSessionExtension.swift in Sources */,
E14271391F01EFF2001D19E1 /* GameCenter.swift in Sources */,
E18F7F3A1ECA7DA700633200 /* SoundPlayer.swift in Sources */,
E107B0921EB66F28004C5E91 /* VerbParser.swift in Sources */,
Expand Down Expand Up @@ -855,6 +862,7 @@
E14589F41F5B7BB000EEF141 /* BrowseInfoVCTests.swift in Sources */,
E14589F61F5B7E9D00EEF141 /* VerbVCTests.swift in Sources */,
038E031C2271E239005CFC96 /* AnalyticsServiceableTests.swift in Sources */,
038E03262279CCCB005CFC96 /* SettingsVCTests.swift in Sources */,
E15D3BF61F533997003771BB /* MockNavigationC.swift in Sources */,
E198F9021F5D79F100BAF553 /* VerbViewTests.swift in Sources */,
038E03202273BF29005CFC96 /* RatingsFetcherTests.swift in Sources */,
Expand Down
8 changes: 1 addition & 7 deletions Conjugar/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

let mainTabBarVC: MainTabBarVC
let settings: Settings
let stubSession = URLSession.stubSession(ratingsCount: 42)

if CommandLine.arguments.contains("enable-ui-testing") {
settings = Settings(getterSetter: DictionaryGetterSetter())
Expand Down Expand Up @@ -56,13 +57,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
return true
}

private var stubSession: URLSession {
URLProtocolStub.testURLs = [RatingsFetcher.iTunesURL: RatingsFetcher.stubData()]
let config = URLSessionConfiguration.ephemeral
config.protocolClasses = [URLProtocolStub.self]
return URLSession(configuration: config)
}

private func configureStatusBar() {
// I like blue and prefer not to override preferredStatusBarStyle in every UIViewController.
let statusBarKey = "statusBar"
Expand Down
6 changes: 4 additions & 2 deletions Conjugar/SettingsVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,12 @@ class SettingsVC: UIViewController {
}
}

@objc func rateReview() {
@objc func rateReview(completion: @escaping ((Bool) -> ()) = { _ in }) {
guard let url = URL(string: "https://itunes.apple.com/us/app/immigration/id\(RatingsFetcher.iTunesID)?action=write-review") else {
return
}
UIApplication.shared.open(url)
UIApplication.shared.open(url, options: [:], completionHandler: { didSucceed in
completion(didSucceed)
})
}
}
18 changes: 18 additions & 0 deletions Conjugar/URLSessionExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// URLSessionExtension.swift
// Conjugar
//
// Created by Joshua Adams on 5/1/19.
// Copyright © 2019 Josh Adams. All rights reserved.
//

import Foundation

extension URLSession {
static func stubSession(ratingsCount: Int) -> URLSession {
URLProtocolStub.testURLs = [RatingsFetcher.iTunesURL: RatingsFetcher.stubData(ratingsCount: ratingsCount)]
let config = URLSessionConfiguration.ephemeral
config.protocolClasses = [URLProtocolStub.self]
return URLSession(configuration: config)
}
}
70 changes: 70 additions & 0 deletions ConjugarTests/Controllers/SettingsVCTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//
// SettingsVCTests.swift
// ConjugarTests
//
// Created by Joshua Adams on 5/1/19.
// Copyright © 2019 Josh Adams. All rights reserved.
//

import XCTest
import UIKit
@testable import Conjugar

class SettingsVCTests: XCTestCase {
func testSettings() {
var analytic = ""
let settings = Settings(getterSetter: DictionaryGetterSetter(dictionary: [:]))
let gameCenter = TestGameCenter(isAuthenticated: false)
let ratingsCount = 42
let svc = SettingsVC(settings: settings, analyticsService: TestAnalyticsService(fire: { fired in analytic = fired }), gameCenter: gameCenter, session: URLSession.stubSession(ratingsCount: ratingsCount))
let nc = MockNavigationC(rootViewController: svc)
UIApplication.shared.keyWindow?.rootViewController = nc
XCTAssertNotNil(UIApplication.shared.keyWindow?.rootViewController)
XCTAssertNotNil(svc)
svc.viewWillAppear(true)
XCTAssertTrue(nc.pushedViewController is SettingsVC)
XCTAssertEqual(analytic, "visited viewController: \(SettingsVC.self) ")

XCTAssertEqual(settings.region, .latinAmerica)
let regionControl = svc.settingsView.regionControl
regionControl.selectedSegmentIndex = 0
svc.regionChanged(regionControl)
XCTAssertEqual(settings.region, .spain)

XCTAssertEqual(settings.difficulty, .easy)
let difficultyControl = svc.settingsView.difficultyControl
difficultyControl.selectedSegmentIndex = 2
svc.difficultyChanged(difficultyControl)
XCTAssertEqual(settings.difficulty, .difficult)
difficultyControl.selectedSegmentIndex = 1
svc.difficultyChanged(difficultyControl)
XCTAssertEqual(settings.difficulty, .moderate)

XCTAssertEqual(settings.secondSingularBrowse, .tu)
let browseVosControl = svc.settingsView.browseVosControl
browseVosControl.selectedSegmentIndex = 2
svc.browseVosChanged(browseVosControl)
XCTAssertEqual(settings.secondSingularBrowse, .both)
browseVosControl.selectedSegmentIndex = 1
svc.browseVosChanged(browseVosControl)
XCTAssertEqual(settings.secondSingularBrowse, .vos)

XCTAssertEqual(settings.secondSingularQuiz, .tu)
let quizVosControl = svc.settingsView.quizVosControl
quizVosControl.selectedSegmentIndex = 1
svc.quizVosChanged(quizVosControl)
XCTAssertEqual(settings.secondSingularQuiz, .vos)

XCTAssertFalse(gameCenter.isAuthenticated)
svc.authenticate()
XCTAssert(gameCenter.isAuthenticated)

let expectatiön = expectation(description: "testRateReview")
svc.rateReview(completion: { didSucceed in
XCTAssert(didSucceed)
expectatiön.fulfill()
})
let timeout: TimeInterval = 0.5
wait(for: [expectatiön], timeout: timeout)
}
}
9 changes: 1 addition & 8 deletions ConjugarTests/Utils/RatingsFetcherTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,9 @@ class RatingsFetcherTests: XCTestCase {
testDescription(count: 42, expectedDescription: "There are 42 ratings for this version of Conjugar. Add yours!")
}

private func stubSession(ratingsCount: Int) -> URLSession {
URLProtocolStub.testURLs = [RatingsFetcher.iTunesURL: RatingsFetcher.stubData(ratingsCount: ratingsCount)]
let config = URLSessionConfiguration.ephemeral
config.protocolClasses = [URLProtocolStub.self]
return URLSession(configuration: config)
}

private func testDescription(count: Int, expectedDescription: String) {
let expectatiön = expectation(description: "testDescription")
RatingsFetcher.fetchRatingsDescription(session: stubSession(ratingsCount: count), completion: { actualDescription in
RatingsFetcher.fetchRatingsDescription(session: URLSession.stubSession(ratingsCount: count), completion: { actualDescription in
XCTAssertEqual(actualDescription, expectedDescription)
expectatiön.fulfill()
})
Expand Down

0 comments on commit 8cbea8e

Please sign in to comment.