Skip to content

Commit 4f1fd55

Browse files
committed
tests
1 parent d2060ba commit 4f1fd55

8 files changed

+126
-177
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import PackageDescription
55

66
let package = Package(
77
name: "SwiftBloc",
8-
platforms: [ .iOS(.v13) ],
8+
platforms: [ .iOS(.v13), .macOS(.v10_15) ],
99
products: [
1010
// Products define the executables and libraries produced by a package, and make them visible to other packages.
1111
.library(

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ ruby bloc_template /MY_PROJECT/MY_CUBIT_FOLDER Counter cubit
3636

3737
```ruby
3838
# creates a bloc
39-
ruby bloc_template /MY_PROJECT/MY_CUBIT_FOLDER Counter bloc
39+
ruby bloc_template /MY_PROJECT/MY_BLOC_FOLDER Counter bloc
4040
```
4141

4242
### Cubit

Sources/Bloc.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//
22
// Bloc.swift
3-
// Pods-SwiftBloc_Example
3+
// SwiftBloc
44
//
5-
// Created by Kachalov, Victor on 27.02.21.
5+
// Created by Kachalov, Victor on 01.04.21.
66
//
77

88
//TODO:

Sources/BlocObserver.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//
22
// BlocObserver.swift
3-
// Pods-SwiftBloc_Example
3+
// SwiftBloc
44
//
5-
// Created by Kachalov, Victor on 27.02.21.
5+
// Created by Kachalov, Victor on 01.04.21.
66
//
77

88
import Foundation

Sources/BlocTest.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
2-
// File.swift
3-
//
2+
// BlocTest.swift
3+
// SwiftBloc
44
//
55
// Created by Kachalov, Victor on 01.04.21.
66
//
@@ -10,14 +10,12 @@ import Combine
1010

1111
final public class BlocTest<S: Equatable, B: Base<S>> {
1212
public static func execute(
13-
description: String,
1413
build: () -> B,
1514
act: ((B) -> Void)?,
1615
wait: TimeInterval? = 0,
1716
expect: (() -> Any)?,
1817
verify: ((Bool, String) -> Void)
1918
) {
20-
print("Running test: \(description)")
2119
var areEqual = false
2220
var states = [S]()
2321
let bloc = build()
@@ -26,9 +24,8 @@ final public class BlocTest<S: Equatable, B: Base<S>> {
2624
.subscribe(on: scheduler)
2725
.delay(for: .seconds(wait ?? 0), scheduler: scheduler)
2826
.sink(receiveValue: { value in
29-
print("New state added: \(value)")
3027
states.append(value)
31-
} )
28+
})
3229
act?(bloc)
3330
if expect != nil {
3431
let expected = expect!()

Sources/Cubit.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// Cubit.swift
3-
// Pods-SwiftBloc_Example
3+
// SwiftBloc
44
//
55
// Created by Kachalov, Victor on 27.02.21.
66
//

Tests/BlocTest.swift

Lines changed: 0 additions & 44 deletions
This file was deleted.

Tests/SwiftBlocTests.swift

Lines changed: 116 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -2,129 +2,125 @@ import XCTest
22
import Combine
33
@testable import SwiftBloc
44

5-
//final public class BlocTest<S: Equatable, B: Base<S>> {
6-
// public static func execute(
7-
// description: String,
8-
// build: () -> B,
9-
// act: ((B) -> Void)?,
10-
// wait: TimeInterval? = 0,
11-
// expect: (() -> Any)?,
12-
// verify: ((B) -> Void)? = nil
13-
// ) {
14-
// print("Running test: \(description)")
15-
// var areEqual = false
16-
// var states = [S]()
17-
// let bloc = build()
18-
// let scheduler = ImmediateScheduler.shared
19-
// let cancellable = bloc.$state
20-
// .subscribe(on: scheduler)
21-
// .delay(for: .seconds(wait ?? 0), scheduler: scheduler)
22-
// .sink(receiveValue: { value in
23-
// print("New state added: \(value)")
24-
// states.append(value)
25-
// } )
26-
// act?(bloc)
27-
// if expect != nil {
28-
// let expected = expect!()
29-
// areEqual = "\(states)" == "\(expected)"
30-
// let message = "State received: \(states). \nStates expected: \(expected)"
31-
// XCTAssert(areEqual, message)
32-
// }
33-
// cancellable.cancel()
34-
// verify?(bloc)
35-
// }
36-
//}
37-
5+
final public class BlocTest<S: Equatable, B: Base<S>> {
6+
public static func execute(
7+
build: () -> B,
8+
act: ((B) -> Void)?,
9+
wait: TimeInterval? = 0,
10+
expect: (() -> Any)?,
11+
verify: ((B) -> Void)? = nil
12+
) {
13+
var areEqual = false
14+
var states = [S]()
15+
let bloc = build()
16+
let scheduler = ImmediateScheduler.shared
17+
let cancellable = bloc.$state
18+
.subscribe(on: scheduler)
19+
.delay(for: .seconds(wait ?? 0), scheduler: scheduler)
20+
.sink(receiveValue: { value in
21+
states.append(value)
22+
})
23+
act?(bloc)
24+
if expect != nil {
25+
let expected = expect!()
26+
areEqual = "\(states)" == "\(expected)"
27+
let message = "State received: \(states). \nStates expected: \(expected)"
28+
XCTAssert(areEqual, message)
29+
}
30+
cancellable.cancel()
31+
verify?(bloc)
32+
}
33+
}
3834

3935
final class SwiftBlocTests: XCTestCase {
4036
func testExample() {
4137
XCTAssert(true)
4238
}
43-
// func testCounterBlocInitial() {
44-
// BlocTest.execute(description: "CounterBloc", build: {
45-
// MockCounterBloc()
46-
// }, act: { (_) in
47-
// // DO NOTHING
48-
// }, expect: {
49-
// [
50-
// MockCounterState(count: 0)
51-
// ]
52-
// })
53-
// }
54-
// func testCounterBlocIncrement() {
55-
// BlocTest.execute(description: "CounterBloc", build: {
56-
// MockCounterBloc()
57-
// }, act: { (bloc) in
58-
// bloc.add(event: .increment)
59-
// bloc.add(event: .increment)
60-
// }, expect: {
61-
// [
62-
// MockCounterState(count: 0),
63-
// MockCounterState(count: 1),
64-
// MockCounterState(count: 2)
65-
// ]
66-
// })
67-
// }
68-
// func testCounterBlocDecrement() {
69-
// BlocTest.execute(description: "CounterBloc", build: {
70-
// MockCounterBloc()
71-
// }, act: { (bloc) in
72-
// bloc.add(event: .decrement)
73-
// bloc.add(event: .decrement)
74-
// }, wait: 3.0, expect: {
75-
// [
76-
// MockCounterState(count: 0),
77-
// MockCounterState(count: -1),
78-
// MockCounterState(count: -2)
79-
// ]
80-
// })
81-
// }
82-
// func testCounterCubitInitial() {
83-
// BlocTest.execute(description: "CounterCubit", build: {
84-
// MockCounterCubit()
85-
// }, act: { (_) in
86-
// // DO NOTHING
87-
// }, expect: {
88-
// [
89-
// 0
90-
// ]
91-
// })
92-
// }
93-
// func testCounterCubitIncrement() {
94-
// BlocTest.execute(description: "CounterBloc", build: {
95-
// MockCounterCubit()
96-
// }, act: { (cubit) in
97-
// cubit.increment()
98-
// cubit.increment()
99-
// }, expect: {
100-
// [
101-
// 0,
102-
// 1,
103-
// 2
104-
// ]
105-
// })
106-
// }
107-
// func testCounterCubitDecrement() {
108-
// BlocTest.execute(description: "CounterBloc", build: {
109-
// MockCounterCubit()
110-
// }, act: { (cubit) in
111-
// cubit.decrement()
112-
// cubit.decrement()
113-
// }, expect: {
114-
// [
115-
// 0,
116-
// -1,
117-
// -2
118-
// ]
119-
// })
120-
// }
121-
//
122-
// static var allTests = [
123-
// ("testCounterBlocInitial", testCounterBlocInitial),
124-
// ("testCounterBlocIncrement", testCounterBlocIncrement),
125-
// ("testCounterBlocDecrement", testCounterBlocDecrement),
126-
// ("testCounterCubitInitial", testCounterCubitInitial),
127-
// ("testCounterCubitIncrement", testCounterCubitIncrement),
128-
// ("testCounterCubitDecrement", testCounterCubitDecrement)
129-
// ]
39+
func testCounterBlocInitial() {
40+
BlocTest.execute(build: {
41+
MockCounterBloc()
42+
}, act: { (_) in
43+
// DO NOTHING
44+
}, expect: {
45+
[
46+
MockCounterState(count: 0)
47+
]
48+
})
49+
}
50+
func testCounterBlocIncrement() {
51+
BlocTest.execute(build: {
52+
MockCounterBloc()
53+
}, act: { (bloc) in
54+
bloc.add(event: .increment)
55+
bloc.add(event: .increment)
56+
}, expect: {
57+
[
58+
MockCounterState(count: 0),
59+
MockCounterState(count: 1),
60+
MockCounterState(count: 2)
61+
]
62+
})
63+
}
64+
func testCounterBlocDecrement() {
65+
BlocTest.execute(build: {
66+
MockCounterBloc()
67+
}, act: { (bloc) in
68+
bloc.add(event: .decrement)
69+
bloc.add(event: .decrement)
70+
}, wait: 3.0, expect: {
71+
[
72+
MockCounterState(count: 0),
73+
MockCounterState(count: -1),
74+
MockCounterState(count: -2)
75+
]
76+
})
77+
}
78+
func testCounterCubitInitial() {
79+
BlocTest.execute(description: "CounterCubit", build: {
80+
MockCounterCubit()
81+
}, act: { (_) in
82+
// DO NOTHING
83+
}, expect: {
84+
[
85+
0
86+
]
87+
})
88+
}
89+
func testCounterCubitIncrement() {
90+
BlocTest.execute(build: {
91+
MockCounterCubit()
92+
}, act: { (cubit) in
93+
cubit.increment()
94+
cubit.increment()
95+
}, expect: {
96+
[
97+
0,
98+
1,
99+
2
100+
]
101+
})
102+
}
103+
func testCounterCubitDecrement() {
104+
BlocTest.execute(build: {
105+
MockCounterCubit()
106+
}, act: { (cubit) in
107+
cubit.decrement()
108+
cubit.decrement()
109+
}, expect: {
110+
[
111+
0,
112+
-1,
113+
-2
114+
]
115+
})
116+
}
117+
118+
static var allTests = [
119+
("testCounterBlocInitial", testCounterBlocInitial),
120+
("testCounterBlocIncrement", testCounterBlocIncrement),
121+
("testCounterBlocDecrement", testCounterBlocDecrement),
122+
("testCounterCubitInitial", testCounterCubitInitial),
123+
("testCounterCubitIncrement", testCounterCubitIncrement),
124+
("testCounterCubitDecrement", testCounterCubitDecrement)
125+
]
130126
}

0 commit comments

Comments
 (0)