Skip to content

Commit 2e17dff

Browse files
committed
Add unit tests
1 parent 56ed829 commit 2e17dff

File tree

2 files changed

+153
-34
lines changed

2 files changed

+153
-34
lines changed

Example/Tests/Tests.swift

+151-34
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,166 @@
22

33
import Quick
44
import Nimble
5+
import RxSwift
56
import RxEventHub
67

7-
class TableOfContentsSpec: QuickSpec {
8+
class ExampleClass {
9+
var intVar: Int = 0
10+
var doubleVar: Double = 0.0
11+
var stringVar: String = ""
12+
13+
init(intVar: Int = 0, doubleVar: Double = 0.0, stringVar: String = "") {
14+
self.intVar = intVar
15+
self.doubleVar = doubleVar
16+
self.stringVar = stringVar
17+
}
18+
}
19+
20+
class ExampleOptionalEventProvider: RxEventProvider<Bool?>{}
21+
class ExampleVoidEventProvider: RxEventProvider<Void>{}
22+
class ExampleIntEventProvider: RxEventProvider<Int>{}
23+
class ExampleDoubleEventProvider: RxEventProvider<Double>{}
24+
class ExampleStringEventProvider: RxEventProvider<String>{}
25+
class ExampleTupleEventProvider: RxEventProvider<(int: Int, double: Double, string: String)>{}
26+
class ExampleClassEventProvider: RxEventProvider<ExampleClass>{
27+
override func typeKey() -> String {
28+
return String(self.dynamicType) + ".ExampleClass"
29+
}
30+
}
31+
32+
class RxEventProviderSpec: QuickSpec {
33+
834
override func spec() {
9-
describe("these will fail") {
35+
it("should have correct name") {
36+
expect(ExampleOptionalEventProvider().typeKey()) == "ExampleOptionalEventProvider"
37+
expect(ExampleVoidEventProvider().typeKey()) == "ExampleVoidEventProvider"
38+
expect(ExampleIntEventProvider().typeKey()) == "ExampleIntEventProvider"
39+
expect(ExampleDoubleEventProvider().typeKey()) == "ExampleDoubleEventProvider"
40+
expect(ExampleStringEventProvider().typeKey()) == "ExampleStringEventProvider"
41+
expect(ExampleTupleEventProvider().typeKey()) == "ExampleTupleEventProvider"
42+
expect(ExampleClassEventProvider().typeKey()) == "ExampleClassEventProvider.ExampleClass"
43+
}
44+
45+
it("should create correct publish subject of given type") {
46+
expect(String(ExampleOptionalEventProvider().publishSubject().dynamicType)) == "PublishSubject<Optional<Bool>>"
47+
expect(String(ExampleVoidEventProvider().publishSubject().dynamicType)) == "PublishSubject<()>"
48+
expect(String(ExampleIntEventProvider().publishSubject().dynamicType)) == "PublishSubject<Int>"
49+
expect(String(ExampleDoubleEventProvider().publishSubject().dynamicType)) == "PublishSubject<Double>"
50+
expect(String(ExampleStringEventProvider().publishSubject().dynamicType)) == "PublishSubject<String>"
51+
expect(String(ExampleTupleEventProvider().publishSubject().dynamicType)) == "PublishSubject<(Int, Double, String)>"
52+
expect(String(ExampleClassEventProvider().publishSubject().dynamicType)) == "PublishSubject<ExampleClass>"
53+
}
54+
}
55+
}
1056

11-
it("can do maths") {
12-
expect(1) == 2
57+
class RxEventHubSpec: QuickSpec {
58+
59+
private var disposeBag = DisposeBag()
60+
61+
override func spec() {
62+
let disposeBag = self.disposeBag
63+
let hub = RxEventHub.sharedHub
64+
65+
it("can notify and subscribe events with Void Param") {
66+
var receivedNotification = false
67+
hub.eventObservable(ExampleVoidEventProvider()).subscribeNext {
68+
receivedNotification = true
69+
}.addDisposableTo(disposeBag)
70+
hub.notify(ExampleVoidEventProvider(), data: ())
71+
72+
waitUntil { done in
73+
done()
1374
}
14-
15-
it("can read") {
16-
expect("number") == "string"
75+
76+
expect(receivedNotification) == true
77+
}
78+
79+
it("can notify and subscribe events with Int Param") {
80+
var intVar = 0
81+
hub.eventObservable(ExampleIntEventProvider()).subscribeNext { (obj) in
82+
intVar = obj
83+
}.addDisposableTo(disposeBag)
84+
hub.notify(ExampleIntEventProvider(), data: 2)
85+
86+
waitUntil { done in
87+
done()
1788
}
18-
19-
it("will eventually fail") {
20-
expect("time").toEventually( equal("done") )
89+
90+
expect(intVar) == 2
91+
}
92+
93+
it("can notify and subscribe events with Double Param") {
94+
var doubleVar = 0.0
95+
hub.eventObservable(ExampleDoubleEventProvider()).subscribeNext { (obj) in
96+
doubleVar = obj
97+
}.addDisposableTo(disposeBag)
98+
hub.notify(ExampleDoubleEventProvider(), data: 2.2)
99+
100+
waitUntil { done in
101+
done()
21102
}
22103

23-
context("these will pass") {
24-
25-
it("can do maths") {
26-
expect(23) == 23
27-
}
28-
29-
it("can read") {
30-
expect("🐮") == "🐮"
31-
}
32-
33-
it("will eventually pass") {
34-
var time = "passing"
35-
36-
dispatch_async(dispatch_get_main_queue()) {
37-
time = "done"
38-
}
39-
40-
waitUntil { done in
41-
NSThread.sleepForTimeInterval(0.5)
42-
expect(time) == "done"
43-
44-
done()
45-
}
46-
}
104+
expect(doubleVar) == 2.2
105+
}
106+
107+
it("can notify and subscribe events with String Param") {
108+
var stringVar = ""
109+
hub.eventObservable(ExampleStringEventProvider()).subscribeNext { (obj) in
110+
stringVar = obj
111+
}.addDisposableTo(disposeBag)
112+
hub.notify(ExampleStringEventProvider(), data: "abc")
113+
114+
waitUntil { done in
115+
done()
116+
}
117+
118+
expect(stringVar) == "abc"
119+
}
120+
121+
it("can notify and subscribe events with Tuple Param") {
122+
var tupleVar: (int: Int, double: Double, string: String) = (int: 0, double: 0.0, string: "")
123+
hub.eventObservable(ExampleTupleEventProvider()).subscribeNext { (obj) in
124+
tupleVar = obj
125+
}.addDisposableTo(disposeBag)
126+
hub.notify(ExampleTupleEventProvider(), data: (int: 2, double: 2.2, string: "abc"))
127+
128+
waitUntil { done in
129+
done()
130+
}
131+
132+
expect(tupleVar.int) == 2
133+
expect(tupleVar.double) == 2.2
134+
expect(tupleVar.string) == "abc"
135+
}
136+
137+
it("can notify and subscribe events with Custom Class Param") {
138+
var classVar: ExampleClass = ExampleClass()
139+
hub.eventObservable(ExampleClassEventProvider()).subscribeNext { (obj) in
140+
classVar = obj
141+
}.addDisposableTo(disposeBag)
142+
hub.notify(ExampleClassEventProvider(), data: ExampleClass(intVar: 2, doubleVar: 2.2, stringVar: "abc"))
143+
144+
waitUntil { done in
145+
done()
47146
}
147+
148+
expect(classVar.intVar) == 2
149+
expect(classVar.doubleVar) == 2.2
150+
expect(classVar.stringVar) == "abc"
151+
}
152+
153+
it("can notify and subscribe events with Optional Param") {
154+
var optionalVar: Bool? = true
155+
hub.eventObservable(ExampleOptionalEventProvider()).subscribeNext { (obj) in
156+
optionalVar = obj
157+
}.addDisposableTo(disposeBag)
158+
hub.notify(ExampleOptionalEventProvider(), data: nil)
159+
160+
waitUntil { done in
161+
done()
162+
}
163+
164+
expect(optionalVar).to(beNil())
48165
}
49166
}
50167
}

RxEventHub/Classes/RxEventHub.swift

+2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ public class RxEventHub {
8585
/// Event provider, provide name and type info for 1 kind of event.
8686
public class RxEventProvider<T> {
8787

88+
public init() {}
89+
8890
/**
8991
Create `PublishSubject` for event.
9092

0 commit comments

Comments
 (0)