Skip to content

Commit

Permalink
feature(coswift): add unit test && fix bugs
Browse files Browse the repository at this point in the history
1. add promise test
2. fix coroutine cancel problem
3. add cocore spec
4. fix promise reject can't await problem
  • Loading branch information
NianJi committed Mar 8, 2019
1 parent 1ad4f47 commit 63cb903
Show file tree
Hide file tree
Showing 23 changed files with 355 additions and 134 deletions.
6 changes: 4 additions & 2 deletions Examples/coSwiftDemo/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ target 'coSwiftDemo' do
use_frameworks!

pod 'coswift', :path => '../../'
pod 'coobjc', :path => '../../'
pod 'cocore', :path => '../../'
# Pods for coSwiftDemo

end
Expand All @@ -15,6 +15,8 @@ target 'coSwiftDemoTests' do
use_frameworks!

pod 'coswift', :path => '../../'
pod 'coobjc', :path => '../../'
pod 'cocore', :path => '../../'
pod 'Quick', '~> 1.3.0'
pod 'Nimble', '~> 7.3.0'

end
20 changes: 12 additions & 8 deletions Examples/coSwiftDemo/coSwiftDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
A35C048A21E73C1200AC7D6E /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A35C048821E73C1200AC7D6E /* LaunchScreen.storyboard */; };
A35C049221E73DB500AC7D6E /* DataService.swift in Sources */ = {isa = PBXBuildFile; fileRef = A35C049121E73DB500AC7D6E /* DataService.swift */; };
A362488992902709BC2684EF /* Pods_coSwiftDemoTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7777908A066FA35360524D20 /* Pods_coSwiftDemoTests.framework */; };
A3D67CFD21F086F5009328CC /* coSwiftDemoTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3D67CFC21F086F5009328CC /* coSwiftDemoTests.swift */; };
A3D67CFD21F086F5009328CC /* coSwiftPromiseTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3D67CFC21F086F5009328CC /* coSwiftPromiseTests.swift */; };
BE3A5FB7A33301FC4AF57A86 /* Pods_coSwiftDemo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C730B8DC19A4897343C94B94 /* Pods_coSwiftDemo.framework */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -42,7 +42,7 @@
A35C048B21E73C1200AC7D6E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
A35C049121E73DB500AC7D6E /* DataService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataService.swift; sourceTree = "<group>"; };
A3D67CFA21F086F5009328CC /* coSwiftDemoTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = coSwiftDemoTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
A3D67CFC21F086F5009328CC /* coSwiftDemoTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = coSwiftDemoTests.swift; sourceTree = "<group>"; };
A3D67CFC21F086F5009328CC /* coSwiftPromiseTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = coSwiftPromiseTests.swift; sourceTree = "<group>"; };
A3D67CFE21F086F5009328CC /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
C730B8DC19A4897343C94B94 /* Pods_coSwiftDemo.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_coSwiftDemo.framework; sourceTree = BUILT_PRODUCTS_DIR; };
C8132E3B863C5D961133D7D6 /* Pods-coSwiftDemoTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-coSwiftDemoTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-coSwiftDemoTests/Pods-coSwiftDemoTests.debug.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -125,7 +125,7 @@
A3D67CFB21F086F5009328CC /* coSwiftDemoTests */ = {
isa = PBXGroup;
children = (
A3D67CFC21F086F5009328CC /* coSwiftDemoTests.swift */,
A3D67CFC21F086F5009328CC /* coSwiftPromiseTests.swift */,
A3D67CFE21F086F5009328CC /* Info.plist */,
);
path = coSwiftDemoTests;
Expand Down Expand Up @@ -260,13 +260,13 @@
);
inputPaths = (
"${SRCROOT}/Pods/Target Support Files/Pods-coSwiftDemo/Pods-coSwiftDemo-frameworks.sh",
"${BUILT_PRODUCTS_DIR}/coobjc-iOS11.0/coobjc.framework",
"${BUILT_PRODUCTS_DIR}/cocore-iOS11.0/cocore.framework",
"${BUILT_PRODUCTS_DIR}/coswift-iOS11.0/coswift.framework",
"${BUILT_PRODUCTS_DIR}/fishhook-iOS11.0/fishhook.framework",
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/coobjc.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/cocore.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/coswift.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/fishhook.framework",
);
Expand Down Expand Up @@ -300,15 +300,19 @@
);
inputPaths = (
"${SRCROOT}/Pods/Target Support Files/Pods-coSwiftDemoTests/Pods-coSwiftDemoTests-frameworks.sh",
"${BUILT_PRODUCTS_DIR}/coobjc-iOS12.1/coobjc.framework",
"${BUILT_PRODUCTS_DIR}/cocore-iOS12.1/cocore.framework",
"${BUILT_PRODUCTS_DIR}/coswift-iOS12.1/coswift.framework",
"${BUILT_PRODUCTS_DIR}/fishhook-iOS12.1/fishhook.framework",
"${BUILT_PRODUCTS_DIR}/Nimble/Nimble.framework",
"${BUILT_PRODUCTS_DIR}/Quick/Quick.framework",
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/coobjc.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/cocore.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/coswift.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/fishhook.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Nimble.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Quick.framework",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
Expand All @@ -332,7 +336,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
A3D67CFD21F086F5009328CC /* coSwiftDemoTests.swift in Sources */,
A3D67CFD21F086F5009328CC /* coSwiftPromiseTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
44 changes: 0 additions & 44 deletions Examples/coSwiftDemo/coSwiftDemoTests/coSwiftDemoTests.swift

This file was deleted.

208 changes: 208 additions & 0 deletions Examples/coSwiftDemo/coSwiftDemoTests/coSwiftPromiseTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
//
// coSwiftDemoTests.swift
// coSwiftDemoTests
//
// Copyright © 2018 Alibaba Group Holding Limited All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import XCTest
import coswift
import Quick
import Nimble

func testPromise1() -> Promise<String> {
let promise = Promise<String>();

DispatchQueue.main.async {
promise.fulfill(value: "123")
}
return promise
}

func testPromise2() -> Promise<String> {
let promise = Promise<String>();

DispatchQueue.main.async {
promise.reject(error: NSError(domain: "aa", code: -1, userInfo: nil))
}
return promise
}

func testPromise3() -> Promise<String> {
let promise = Promise<String>();


let timer = Timer.scheduledTimer(withTimeInterval: 3, repeats: false) { (timer) in
promise.fulfill(value: "1234")
}

promise.onCancel { (promise) in
timer.invalidate()
}
return promise
}

class PromiseSpec: QuickSpec {


override func spec() {
describe("Promise tests") {

it("fulfill return the value") {

let co = co_launch {

let result = try await { testPromise1() }

switch result {
case .fulfilled(let str):
expect(str).to(equal("123"))
break
case .rejected( _):
expect(false).to(beTrue())
break
}
}

waitUntil { done in

co_launch {

co.join()
done()
}

}
}



it("reject should return nil, and error.") {

let co = co_launch {

let result = try await { testPromise2() }

switch result {
case .fulfilled(_ ):
expect(false).to(beTrue())
break
case .rejected(let error):
expect(error as NSError).to(equal(NSError(domain: "aa", code: -1, userInfo: nil)))
break
}
}

waitUntil(timeout: 2) { done in

co_launch {

co.join()
done()
}

}
}

it("test simple chained promise") {

let promise = testPromise1()

promise
.then(work: { (retStr) -> Data in
expect(retStr).to(equal("123"))
return retStr.data(using: String.Encoding.utf8)!
})
.then(work: { (data) -> String in

let retStr = String(data: data, encoding: String.Encoding.utf8)!
expect(retStr).to(equal("123"))
return retStr
})

var step = 0;
promise
.catch(reject: { (error) in
expect(false).to(beTrue())
})
.then(work: { (retStr) -> Data in
step = 1;
expect(retStr).to(equal("123"))
return retStr.data(using: String.Encoding.utf8)!
})
.then(work: { (data) -> String in
step = 2;
let retStr = String(data: data, encoding: String.Encoding.utf8)!
expect(retStr).to(equal("123"))
return retStr
})

waitUntil(timeout: 3, action: { (done) in

expect(step).to(equal(2))
done()
})
}

it("cancel a coroutine when await a promise") {

let co = co_launch {

_ = try await{ testPromise3() }

// should be cancelled
expect(false).to(beTrue())
}

waitUntil(timeout: 5, action: { (done) in
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(1000), execute: {

co.cancel()
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(1000), execute: {
done()
})
})
})
}

it("cancel a finished coroutine will do nothing") {

let co = co_launch {

let result = try await{ testPromise3() }

switch result {
case .fulfilled(let str):
expect(str).to(equal("1234"))
break
case .rejected( _):
expect(false).to(beTrue())
break
}
}

waitUntil(timeout: 5, action: { (done) in
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(3100), execute: {

co.cancel()
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(1000), execute: {
done()
})
})
})
}
}
}
}

2 changes: 2 additions & 0 deletions Examples/coobjcBaseExample/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ use_frameworks!
target 'coobjcBaseExample' do

pod 'coobjc', :path => '../../'
pod 'cocore', :path => '../../'

end

target 'coobjcBaseExampleTests' do

pod 'coobjc', :path => '../../'
pod 'cocore', :path => '../../'

pod 'Specta', '~> 1.0'
pod 'Expecta', '~> 1.0' # expecta matchers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,7 @@
);
inputPaths = (
"${SRCROOT}/Pods/Target Support Files/Pods-coobjcBaseExampleTests/Pods-coobjcBaseExampleTests-frameworks.sh",
"${BUILT_PRODUCTS_DIR}/cocore/cocore.framework",
"${BUILT_PRODUCTS_DIR}/coobjc/coobjc.framework",
"${BUILT_PRODUCTS_DIR}/fishhook/fishhook.framework",
"${BUILT_PRODUCTS_DIR}/Expecta/Expecta.framework",
Expand All @@ -707,6 +708,7 @@
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/cocore.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/coobjc.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/fishhook.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Expecta.framework",
Expand Down Expand Up @@ -743,11 +745,13 @@
);
inputPaths = (
"${SRCROOT}/Pods/Target Support Files/Pods-coobjcBaseExample/Pods-coobjcBaseExample-frameworks.sh",
"${BUILT_PRODUCTS_DIR}/cocore/cocore.framework",
"${BUILT_PRODUCTS_DIR}/coobjc/coobjc.framework",
"${BUILT_PRODUCTS_DIR}/fishhook/fishhook.framework",
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/cocore.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/coobjc.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/fishhook.framework",
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#import <Expecta/Expecta.h>
#import <OCMock/OCMock.h>
#import <coobjc/coobjc.h>
#import <coobjc/co_autorelease.h>

static COActor *countActor = nil;

Expand Down
Loading

0 comments on commit 63cb903

Please sign in to comment.