@@ -2,129 +2,125 @@ import XCTest
2
2
import Combine
3
3
@testable import SwiftBloc
4
4
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) . \n States expected: \( expected) "
28
+ XCTAssert ( areEqual, message)
29
+ }
30
+ cancellable. cancel ( )
31
+ verify ? ( bloc)
32
+ }
33
+ }
38
34
39
35
final class SwiftBlocTests : XCTestCase {
40
36
func testExample( ) {
41
37
XCTAssert ( true )
42
38
}
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
+ ]
130
126
}
0 commit comments