From 14c09f7110f4f23d4acd7aa0d32553c4c63e8d43 Mon Sep 17 00:00:00 2001 From: kishikawa katsumi Date: Thu, 4 Jun 2015 15:06:42 -0700 Subject: [PATCH] Add async test cases --- .../project.pbxproj | 24 +- .../ExampleFramework/Networking.swift | 35 ++ ...eworkTests.swift => CalculatorTests.swift} | 2 +- .../NetworkingTests.swift | 42 ++ .../results/Calculator.swift.gcov | 2 +- .../results/Networking.swift.gcov | 38 ++ ...age.ExampleFramework_Calculator.swift.html | 2 +- ...age.ExampleFramework_Networking.swift.html | 509 ++++++++++++++++++ .../ExampleFramework/results/coverage.html | 22 +- .../CoverageReporterTests.swift | 38 +- 10 files changed, 686 insertions(+), 28 deletions(-) create mode 100644 Examples/ExampleFramework/ExampleFramework/Networking.swift rename Examples/ExampleFramework/ExampleFrameworkTests/{ExampleFrameworkTests.swift => CalculatorTests.swift} (94%) create mode 100644 Examples/ExampleFramework/ExampleFrameworkTests/NetworkingTests.swift create mode 100644 Examples/ExampleFramework/results/Networking.swift.gcov create mode 100644 Examples/ExampleFramework/results/coverage.ExampleFramework_Networking.swift.html diff --git a/Examples/ExampleFramework/ExampleFramework.xcodeproj/project.pbxproj b/Examples/ExampleFramework/ExampleFramework.xcodeproj/project.pbxproj index 9916674..476d825 100644 --- a/Examples/ExampleFramework/ExampleFramework.xcodeproj/project.pbxproj +++ b/Examples/ExampleFramework/ExampleFramework.xcodeproj/project.pbxproj @@ -9,10 +9,14 @@ /* Begin PBXBuildFile section */ 1401EC2A1B15141B000D8737 /* ExampleFramework.h in Headers */ = {isa = PBXBuildFile; fileRef = 1401EC291B15141B000D8737 /* ExampleFramework.h */; settings = {ATTRIBUTES = (Public, ); }; }; 1401EC301B15141B000D8737 /* ExampleFramework.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1401EC241B15141B000D8737 /* ExampleFramework.framework */; }; - 1401EC371B15141B000D8737 /* ExampleFrameworkTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1401EC361B15141B000D8737 /* ExampleFrameworkTests.swift */; }; + 1401EC371B15141B000D8737 /* CalculatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1401EC361B15141B000D8737 /* CalculatorTests.swift */; }; 1401EC411B151494000D8737 /* Calculator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1401EC401B151494000D8737 /* Calculator.swift */; }; + 1499139D1B20F7E90079325B /* Networking.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1499139C1B20F7E90079325B /* Networking.swift */; }; + 1499139E1B20F7E90079325B /* Networking.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1499139C1B20F7E90079325B /* Networking.swift */; }; + 149913A01B20F92B0079325B /* NetworkingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1499139F1B20F92B0079325B /* NetworkingTests.swift */; }; + 149913A11B20F92B0079325B /* NetworkingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1499139F1B20F92B0079325B /* NetworkingTests.swift */; }; 14DE827C1B1B586E00E1BAA4 /* ExampleFramework.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 14DE82711B1B586D00E1BAA4 /* ExampleFramework.framework */; }; - 14DE828A1B1B5A0400E1BAA4 /* ExampleFrameworkTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1401EC361B15141B000D8737 /* ExampleFrameworkTests.swift */; }; + 14DE828A1B1B5A0400E1BAA4 /* CalculatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1401EC361B15141B000D8737 /* CalculatorTests.swift */; }; 14DE828B1B1B5A1F00E1BAA4 /* Calculator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1401EC401B151494000D8737 /* Calculator.swift */; }; 14DE82B31B1B66D400E1BAA4 /* ExampleFramework.h in Headers */ = {isa = PBXBuildFile; fileRef = 1401EC291B15141B000D8737 /* ExampleFramework.h */; settings = {ATTRIBUTES = (Public, ); }; }; /* End PBXBuildFile section */ @@ -40,8 +44,10 @@ 1401EC291B15141B000D8737 /* ExampleFramework.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ExampleFramework.h; sourceTree = ""; }; 1401EC2F1B15141B000D8737 /* ExampleFrameworkTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; name = ExampleFrameworkTests.xctest; path = ExampleFrameworkiOSTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 1401EC351B15141B000D8737 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - 1401EC361B15141B000D8737 /* ExampleFrameworkTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExampleFrameworkTests.swift; sourceTree = ""; }; + 1401EC361B15141B000D8737 /* CalculatorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CalculatorTests.swift; sourceTree = ""; }; 1401EC401B151494000D8737 /* Calculator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Calculator.swift; sourceTree = ""; }; + 1499139C1B20F7E90079325B /* Networking.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Networking.swift; sourceTree = ""; }; + 1499139F1B20F92B0079325B /* NetworkingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NetworkingTests.swift; sourceTree = ""; }; 14DE82711B1B586D00E1BAA4 /* ExampleFramework.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = ExampleFramework.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 14DE827B1B1B586E00E1BAA4 /* ExampleFramework-MacTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "ExampleFramework-MacTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; /* End PBXFileReference section */ @@ -105,6 +111,7 @@ children = ( 1401EC291B15141B000D8737 /* ExampleFramework.h */, 1401EC401B151494000D8737 /* Calculator.swift */, + 1499139C1B20F7E90079325B /* Networking.swift */, 1401EC271B15141B000D8737 /* Supporting Files */, ); path = ExampleFramework; @@ -121,7 +128,8 @@ 1401EC331B15141B000D8737 /* ExampleFrameworkTests */ = { isa = PBXGroup; children = ( - 1401EC361B15141B000D8737 /* ExampleFrameworkTests.swift */, + 1401EC361B15141B000D8737 /* CalculatorTests.swift */, + 1499139F1B20F92B0079325B /* NetworkingTests.swift */, 1401EC341B15141B000D8737 /* Supporting Files */, ); path = ExampleFrameworkTests; @@ -309,6 +317,7 @@ buildActionMask = 2147483647; files = ( 1401EC411B151494000D8737 /* Calculator.swift in Sources */, + 1499139D1B20F7E90079325B /* Networking.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -316,7 +325,8 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 1401EC371B15141B000D8737 /* ExampleFrameworkTests.swift in Sources */, + 1401EC371B15141B000D8737 /* CalculatorTests.swift in Sources */, + 149913A01B20F92B0079325B /* NetworkingTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -325,6 +335,7 @@ buildActionMask = 2147483647; files = ( 14DE828B1B1B5A1F00E1BAA4 /* Calculator.swift in Sources */, + 1499139E1B20F7E90079325B /* Networking.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -332,7 +343,8 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 14DE828A1B1B5A0400E1BAA4 /* ExampleFrameworkTests.swift in Sources */, + 14DE828A1B1B5A0400E1BAA4 /* CalculatorTests.swift in Sources */, + 149913A11B20F92B0079325B /* NetworkingTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Examples/ExampleFramework/ExampleFramework/Networking.swift b/Examples/ExampleFramework/ExampleFramework/Networking.swift new file mode 100644 index 0000000..2f210f0 --- /dev/null +++ b/Examples/ExampleFramework/ExampleFramework/Networking.swift @@ -0,0 +1,35 @@ +// +// Networking.swift +// ExampleFramework +// +// Created by Kishikawa Katsumi on 2015/06/04. +// Copyright (c) 2015 Realm. All rights reserved. +// + +import Foundation + +public class Networking { + let requesetURL: NSURL + + public init() { + if let URL = NSURL(string: "http://www.google.com") { + requesetURL = URL + } else { + fatalError("Unknown error occurred") + } + } + + public init(URL: NSURL) { + requesetURL = URL + } + + public func request(completion:(data: NSData?, response: NSURLResponse, error: NSError?) -> Void) -> Void { + var session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration()) + var request = NSMutableURLRequest(URL: requesetURL) + + session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in + completion(data: data, response: response, error: error) + }).resume() + } + +} diff --git a/Examples/ExampleFramework/ExampleFrameworkTests/ExampleFrameworkTests.swift b/Examples/ExampleFramework/ExampleFrameworkTests/CalculatorTests.swift similarity index 94% rename from Examples/ExampleFramework/ExampleFrameworkTests/ExampleFrameworkTests.swift rename to Examples/ExampleFramework/ExampleFrameworkTests/CalculatorTests.swift index 1f0be69..9861fb1 100644 --- a/Examples/ExampleFramework/ExampleFrameworkTests/ExampleFrameworkTests.swift +++ b/Examples/ExampleFramework/ExampleFrameworkTests/CalculatorTests.swift @@ -9,7 +9,7 @@ import XCTest import ExampleFramework -class ExampleFrameworkTests: XCTestCase { +class CalculatorTests: XCTestCase { override func setUp() { super.setUp() diff --git a/Examples/ExampleFramework/ExampleFrameworkTests/NetworkingTests.swift b/Examples/ExampleFramework/ExampleFrameworkTests/NetworkingTests.swift new file mode 100644 index 0000000..b5931c0 --- /dev/null +++ b/Examples/ExampleFramework/ExampleFrameworkTests/NetworkingTests.swift @@ -0,0 +1,42 @@ +// +// NetworkingTests.swift +// ExampleFramework +// +// Created by kishikawakatsumi on 2015/06/04. +// Copyright (c) 2015年 kishikawakatsumi. All rights reserved. +// + +import XCTest +import ExampleFramework + +class NetworkingTests: XCTestCase { + + override func setUp() { + super.setUp() + } + + override func tearDown() { + super.tearDown() + } + + func testAsyncNetworking() { + let expectation = expectationWithDescription("networking") + + let networking = Networking(URL: NSURL(string: "http://www.google.com")!) + networking.request { (data, response, error) -> Void in + if let response = response as? NSHTTPURLResponse { + XCTAssertEqual(response.statusCode, 200) + } else { + XCTFail("Request failed") + } + expectation.fulfill() + } + + waitForExpectationsWithTimeout(5.0, handler: { (error) -> Void in + if let error = error { + XCTFail("Connection timeout") + } + }) + } + +} diff --git a/Examples/ExampleFramework/results/Calculator.swift.gcov b/Examples/ExampleFramework/results/Calculator.swift.gcov index 00a7d95..2fd25c3 100644 --- a/Examples/ExampleFramework/results/Calculator.swift.gcov +++ b/Examples/ExampleFramework/results/Calculator.swift.gcov @@ -1,4 +1,4 @@ - -: 0:Source:/Users/katsumi/Desktop/Realm/SwiftCov/Examples/ExampleFramework/ExampleFramework/Calculator.swift + -: 0:Source:/Users/katsumi/Dropbox/Xcode Projects/SwiftCov/Examples/ExampleFramework/ExampleFramework/Calculator.swift -: 0:Runs:1 -: 0:Programs:1 -: 1:// diff --git a/Examples/ExampleFramework/results/Networking.swift.gcov b/Examples/ExampleFramework/results/Networking.swift.gcov new file mode 100644 index 0000000..d600f08 --- /dev/null +++ b/Examples/ExampleFramework/results/Networking.swift.gcov @@ -0,0 +1,38 @@ + -: 0:Source:/Users/katsumi/Dropbox/Xcode Projects/SwiftCov/Examples/ExampleFramework/ExampleFramework/Networking.swift + -: 0:Runs:1 + -: 0:Programs:1 + -: 1:// + -: 2:// Networking.swift + -: 3:// ExampleFramework + -: 4:// + -: 5:// Created by Kishikawa Katsumi on 2015/06/04. + -: 6:// Copyright (c) 2015 Realm. All rights reserved. + -: 7:// + -: 8: + -: 9:import Foundation + -: 10: + -: 11:public class Networking { + -: 12: let requesetURL: NSURL + -: 13: + -: 14: public init() { +#####: 15: if let URL = NSURL(string: "http://www.google.com") { +#####: 16: requesetURL = URL +#####: 17: } else { +#####: 18: fatalError("Unknown error occurred") + -: 19: } + -: 20: } + -: 21: + -: 22: public init(URL: NSURL) { + 1: 23: requesetURL = URL + -: 24: } + -: 25: + -: 26: public func request(completion:(data: NSData?, response: NSURLResponse, error: NSError?) -> Void) -> Void { + 1: 27: var session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration()) + 1: 28: var request = NSMutableURLRequest(URL: requesetURL) + -: 29: + 2: 30: session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in + 1: 31: completion(data: data, response: response, error: error) + 2: 32: }).resume() + -: 33: } + -: 34: + -: 35:} \ No newline at end of file diff --git a/Examples/ExampleFramework/results/coverage.ExampleFramework_Calculator.swift.html b/Examples/ExampleFramework/results/coverage.ExampleFramework_Calculator.swift.html index f6ba040..9a5494d 100644 --- a/Examples/ExampleFramework/results/coverage.ExampleFramework_Calculator.swift.html +++ b/Examples/ExampleFramework/results/coverage.ExampleFramework_Calculator.swift.html @@ -261,7 +261,7 @@ Date: - 2015-06-01 + 2015-06-04 Branches: 0 diff --git a/Examples/ExampleFramework/results/coverage.ExampleFramework_Networking.swift.html b/Examples/ExampleFramework/results/coverage.ExampleFramework_Networking.swift.html new file mode 100644 index 0000000..6ff9746 --- /dev/null +++ b/Examples/ExampleFramework/results/coverage.ExampleFramework_Networking.swift.html @@ -0,0 +1,509 @@ + + + + + + Head + + + + + + + + + + + + + + +
GCC Code Coverage Report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Directory:ExampleFramework/ExecTotalCoverage
File:ExampleFramework/Networking.swiftLines:61060.0 %
Date:2015-06-04Branches:000.0 %
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
LineExecSource
1
//
2
//  Networking.swift
3
//  ExampleFramework
4
//
5
//  Created by Kishikawa Katsumi on 2015/06/04.
6
//  Copyright (c) 2015 Realm. All rights reserved.
7
//
8
9
import Foundation
10
11
public class Networking {
12
    let requesetURL: NSURL
13
14
    public init() {
15
        if let URL = NSURL(string: "http://www.google.com") {
16
            requesetURL = URL
17
        } else {
18
            fatalError("Unknown error occurred")
19
        }
20
    }
21
22
    public init(URL: NSURL) {
23
1
        requesetURL = URL
24
    }
25
26
    public func request(completion:(data: NSData?, response: NSURLResponse, error: NSError?) -> Void) -> Void {
27
1
        var session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
28
1
        var request = NSMutableURLRequest(URL: requesetURL)
29
30
2
        session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
31
1
            completion(data: data, response: response, error: error)
32
2
        }).resume()
33
    }
34
35
}
+
+ + + + +
+
+ + + + + diff --git a/Examples/ExampleFramework/results/coverage.html b/Examples/ExampleFramework/results/coverage.html index b7a9b16..90861ea 100644 --- a/Examples/ExampleFramework/results/coverage.html +++ b/Examples/ExampleFramework/results/coverage.html @@ -252,12 +252,12 @@ Date: - 2015-06-01 + 2015-06-04 Lines: - 17 - 24 - 70.8 % + 23 + 34 + 67.6 % Legend: @@ -310,6 +310,20 @@ + + Networking.swift + +
+
+
+ + 60.0 % + 6 / 10 + 100.0 % + 0 / 0 + + +
diff --git a/Source/SwiftCovFrameworkTests/CoverageReporterTests.swift b/Source/SwiftCovFrameworkTests/CoverageReporterTests.swift index b805f71..1dad214 100644 --- a/Source/SwiftCovFrameworkTests/CoverageReporterTests.swift +++ b/Source/SwiftCovFrameworkTests/CoverageReporterTests.swift @@ -10,9 +10,9 @@ import XCTest import SwiftCovFramework class CoverageReporterTests: XCTestCase { - let reportFilename = "Calculator.swift.gcov" - var fixtureFilePath: String { - return "./Examples/ExampleFramework/results/" + reportFilename + let reportFilenames = ["Calculator.swift.gcov", "Networking.swift.gcov"] + var fixtureFilePaths: [String] { + return reportFilenames.map { "./Examples/ExampleFramework/results/" + $0 } } override func setUp() { @@ -27,7 +27,7 @@ class CoverageReporterTests: XCTestCase { let temporaryDirectory = NSTemporaryDirectory().stringByAppendingPathComponent(NSProcessInfo().globallyUniqueString) NSFileManager().createDirectoryAtPath(temporaryDirectory, withIntermediateDirectories: true, attributes: nil, error: nil) - let reporter = CoverageReporter(outputDirectory: temporaryDirectory) + let reporter = CoverageReporter(outputDirectory: temporaryDirectory, threshold: 0) let xcodebuild = Xcodebuild(arguments: ["test", "-project", "./Examples/ExampleFramework/ExampleFramework.xcodeproj", @@ -43,11 +43,15 @@ class CoverageReporterTests: XCTestCase { case .Success: switch reporter.runCoverageReport(buildSettings: buildSettings) { case .Success: - let reportFilePath = temporaryDirectory.stringByAppendingPathComponent(reportFilename) - - XCTAssertEqual( - dropFirst(split(NSString(contentsOfFile: reportFilePath, encoding: NSUTF8StringEncoding, error: nil) as! String) { $0 == "\n" }), - dropFirst(split(NSString(contentsOfFile: fixtureFilePath, encoding: NSUTF8StringEncoding, error: nil) as! String) { $0 == "\n" })) + Array(zip(reportFilenames, fixtureFilePaths)) + .map { (reportFilename, fixtureFilePath) -> (String, String) in + return (temporaryDirectory.stringByAppendingPathComponent(reportFilename), fixtureFilePath) + } + .map { (reportFilePath, fixtureFilePath) in + XCTAssertEqual( + dropFirst(split(NSString(contentsOfFile: reportFilePath, encoding: NSUTF8StringEncoding, error: nil) as! String) { $0 == "\n" }), + dropFirst(split(NSString(contentsOfFile: fixtureFilePath, encoding: NSUTF8StringEncoding, error: nil) as! String) { $0 == "\n" })) + } case let .Failure(error): XCTAssertNotEqual(error.value, EXIT_SUCCESS) XCTFail("Execution failure") @@ -66,7 +70,7 @@ class CoverageReporterTests: XCTestCase { let temporaryDirectory = NSTemporaryDirectory().stringByAppendingPathComponent(NSProcessInfo().globallyUniqueString) NSFileManager().createDirectoryAtPath(temporaryDirectory, withIntermediateDirectories: true, attributes: nil, error: nil) - let reporter = CoverageReporter(outputDirectory: temporaryDirectory) + let reporter = CoverageReporter(outputDirectory: temporaryDirectory, threshold: 0) let xcodebuild = Xcodebuild(arguments: ["test", "-project", "./Examples/ExampleFramework/ExampleFramework.xcodeproj", @@ -82,11 +86,15 @@ class CoverageReporterTests: XCTestCase { case .Success: switch reporter.runCoverageReport(buildSettings: buildSettings) { case .Success: - let reportFilePath = temporaryDirectory.stringByAppendingPathComponent(reportFilename) - - XCTAssertEqual( - dropFirst(split(NSString(contentsOfFile: reportFilePath, encoding: NSUTF8StringEncoding, error: nil) as! String) { $0 == "\n" }), - dropFirst(split(NSString(contentsOfFile: fixtureFilePath, encoding: NSUTF8StringEncoding, error: nil) as! String) { $0 == "\n" })) + Array(zip(reportFilenames, fixtureFilePaths)) + .map { (reportFilename, fixtureFilePath) -> (String, String) in + return (temporaryDirectory.stringByAppendingPathComponent(reportFilename), fixtureFilePath) + } + .map { (reportFilePath, fixtureFilePath) in + XCTAssertEqual( + dropFirst(split(NSString(contentsOfFile: reportFilePath, encoding: NSUTF8StringEncoding, error: nil) as! String) { $0 == "\n" }), + dropFirst(split(NSString(contentsOfFile: fixtureFilePath, encoding: NSUTF8StringEncoding, error: nil) as! String) { $0 == "\n" })) + } case let .Failure(error): XCTAssertNotEqual(error.value, EXIT_SUCCESS) XCTFail("Execution failure")