How to use testAddSingleSubprovider_operatorSyntax method of ConcreteFakeableProtocol class

Best Mockingbird code snippet using ConcreteFakeableProtocol.testAddSingleSubprovider_operatorSyntax

Run Mockingbird automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

DefaultValueProviderTests.swift

Source: DefaultValueProviderTests.swift Github

copy
1//
2//  DefaultValueProviderTests.swift
3//  MockingbirdTests
4//
5//  Created by Andrew Chang on 4/11/20.
6//
7
8import XCTest
9import Mockingbird
10@testable import MockingbirdTestsHost
11import CoreBluetooth
12
13extension FakeableGenericClass: Providable {
14  public static func createInstance() -> Self? { FakeableGenericClass() as? Self }
15}
16
17class DefaultValueProviderTests: BaseTestCase {
18  
19  var concreteMock: FakeableTypeReferencerMock!
20  var concreteInstance: FakeableTypeReferencer { return concreteMock }
21  
22  var childMock: ChildMock!
23  var childInstance: Child { return childMock }
24  
25  var protocolMock: GrandparentProtocolMock!
26  var protocolInstance: GrandparentProtocol { return protocolMock }
27  
28  override func setUp() {
29    concreteMock = mock(FakeableTypeReferencer.self)
30    childMock = mock(Child.self)
31    protocolMock = mock(GrandparentProtocol.self)
32  }
33  
34  // MARK: - Single registration
35  
36  func testFakeableClass() {
37    let fakeClassInstance = FakeableClass(param: "hello-world")
38    var valueProvider = ValueProvider()
39    valueProvider.register(fakeClassInstance, for: FakeableClass.self)
40    concreteMock.useDefaultValues(from: valueProvider)
41    
42    XCTAssertTrue(concreteInstance.fakeableClass() === fakeClassInstance)
43    verify(concreteMock.fakeableClass()).wasCalled()
44  }
45  
46  func testFakeableSpecializedGenericClass() {
47    let fakeGenericClassInstance = FakeableGenericClass() as FakeableGenericClass<Bool>
48    var valueProvider = ValueProvider()
49    valueProvider.register(fakeGenericClassInstance, for: FakeableGenericClass<Bool>.self)
50    concreteMock.useDefaultValues(from: valueProvider)
51    
52    let genericClassReference: FakeableGenericClass<Bool> = concreteInstance.fakeableGenericClass()
53    XCTAssertTrue(genericClassReference === fakeGenericClassInstance)
54    verify(concreteMock.fakeableGenericClass())
55      .returning(FakeableGenericClass<Bool>.self)
56      .wasCalled()
57  }
58  
59  func testFakeableUnspecializedGenericClass() {
60    var valueProvider = ValueProvider()
61    valueProvider.registerType(FakeableGenericClass<Any>.self)
62    concreteMock.useDefaultValues(from: valueProvider)
63    
64    let _ = concreteInstance.fakeableGenericClass() as FakeableGenericClass<Bool>
65    let _ = concreteInstance.fakeableGenericClass() as FakeableGenericClass<String>
66    let _ = concreteInstance.fakeableGenericClass() as FakeableGenericClass<Int>
67    verify(concreteMock.fakeableGenericClass())
68      .returning(FakeableGenericClass<Bool>.self)
69      .wasCalled()
70    verify(concreteMock.fakeableGenericClass())
71      .returning(FakeableGenericClass<String>.self)
72      .wasCalled()
73    verify(concreteMock.fakeableGenericClass())
74      .returning(FakeableGenericClass<Int>.self)
75      .wasCalled()
76  }
77  
78  func testFakeableProtocol() {
79    class ConcreteFakeableProtocol: FakeableProtocol {}
80    let fakeProtocolInstance = ConcreteFakeableProtocol()
81    var valueProvider = ValueProvider()
82    valueProvider.register(fakeProtocolInstance, for: FakeableProtocol.self)
83    concreteMock.useDefaultValues(from: valueProvider)
84    
85    let concreteProtocolReference =
86      (concreteInstance.fakeableProtocol() as? ConcreteFakeableProtocol)
87    XCTAssertTrue(concreteProtocolReference === fakeProtocolInstance)
88    verify(concreteMock.fakeableProtocol()).wasCalled()
89  }
90  
91  func testFakeableStruct() {
92    let fakeStructInstance = FakeableStruct(value: 42)
93    var valueProvider = ValueProvider()
94    valueProvider.register(fakeStructInstance, for: FakeableStruct.self)
95    concreteMock.useDefaultValues(from: valueProvider)
96    
97    XCTAssertEqual(concreteInstance.fakeableStruct(), fakeStructInstance)
98    verify(concreteMock.fakeableStruct()).wasCalled()
99  }
100  
101  func testFakeableEnum() {
102    var valueProvider = ValueProvider()
103    valueProvider.register(FakeableEnum.bar, for: FakeableEnum.self)
104    concreteMock.useDefaultValues(from: valueProvider)
105    XCTAssertEqual(concreteInstance.fakeableEnum(), .bar)
106    verify(concreteMock.fakeableEnum()).wasCalled()
107  }
108  
109  func testFakeableTypealias() {
110    var valueProvider = ValueProvider()
111    valueProvider.register(FakeableTypealias(), for: FakeableTypealias.self)
112    concreteMock.useDefaultValues(from: valueProvider)
113    XCTAssertEqual(concreteInstance.fakeableTypealias(), FakeableTypealias())
114    verify(concreteMock.fakeableTypealias()).wasCalled()
115  }
116  
117  func testFakeableTypealias_handlesAliasedType() {
118    var valueProvider = ValueProvider()
119    valueProvider.register(true, for: Bool.self)
120    concreteMock.useDefaultValues(from: valueProvider)
121    XCTAssertEqual(concreteInstance.fakeableTypealias(), true)
122    verify(concreteMock.fakeableTypealias()).wasCalled()
123  }
124  
125  func testFakeableInt() {
126    var valueProvider = ValueProvider()
127    valueProvider.register(42, for: Int.self)
128    concreteMock.useDefaultValues(from: valueProvider)
129    XCTAssertEqual(concreteInstance.fakeableInt(), 42)
130    verify(concreteMock.fakeableInt()).wasCalled()
131  }
132  
133  // MARK: - Resetting
134  
135  func testClearDefaultValues() {
136    shouldFail {
137      var valueProvider = ValueProvider()
138      valueProvider.register(42, for: Int.self)
139      self.concreteMock.useDefaultValues(from: valueProvider)
140      clearStubs(on: self.concreteMock)
141      let _ = self.concreteInstance.fakeableInt()
142    }
143  }
144  
145  // MARK: - Multiple registration
146
147  func testChainedFakeableRegistration() {
148    let fakeClassInstance = FakeableClass(param: "hello-world")
149    var valueProvider = ValueProvider()
150    valueProvider.register(fakeClassInstance, for: FakeableClass.self)
151    valueProvider.register(FakeableEnum.bar, for: FakeableEnum.self)
152    valueProvider.register(42, for: Int.self)
153    concreteMock.useDefaultValues(from: valueProvider)
154    
155    XCTAssertTrue(concreteInstance.fakeableClass() === fakeClassInstance)
156    XCTAssertEqual(concreteInstance.fakeableEnum(), .bar)
157    XCTAssertEqual(concreteInstance.fakeableInt(), 42)
158    
159    verify(concreteMock.fakeableClass()).wasCalled()
160    verify(concreteMock.fakeableEnum()).wasCalled()
161    verify(concreteMock.fakeableInt()).wasCalled()
162  }
163  
164  func testMultipleFakeableRegistration_overridesPreviousRegistration() {
165    var valueProvider = ValueProvider()
166    valueProvider.register(42, for: Int.self)
167    valueProvider.register(99, for: Int.self)
168    concreteMock.useDefaultValues(from: valueProvider)
169    XCTAssertEqual(concreteInstance.fakeableInt(), 99)
170    verify(concreteMock.fakeableInt()).wasCalled()
171  }
172  
173  func testRemovePreviousFakeableRegistration() {
174    shouldFail {
175      var valueProvider = ValueProvider()
176      valueProvider.register(42, for: Int.self)
177      valueProvider.remove(Int.self)
178      self.concreteMock.useDefaultValues(from: valueProvider)
179      _ = self.concreteInstance.fakeableInt()
180    }
181  }
182  
183  func testRemovePreviousGenericFakeableRegistration() {
184    shouldFail {
185      var valueProvider = ValueProvider()
186      valueProvider.registerType(FakeableGenericClass<Any>.self)
187      valueProvider.remove(FakeableGenericClass<Any>.self)
188      self.concreteMock.useDefaultValues(from: valueProvider)
189      _ = self.concreteInstance.fakeableGenericClass() as FakeableGenericClass<Bool>
190    }
191  }
192  
193  // MARK: - Composition
194  
195  func testAddSingleSubprovider() {
196    var valueProvider = ValueProvider()
197    valueProvider.add(.standardProvider)
198    concreteMock.useDefaultValues(from: valueProvider)
199    XCTAssertEqual(concreteInstance.fakeableString(), "")
200    verify(concreteMock.fakeableString()).wasCalled()
201  }
202  
203  func testAddSingleSubprovider_operatorSyntax() {
204    let valueProvider = ValueProvider() + .standardProvider
205    concreteMock.useDefaultValues(from: valueProvider)
206    XCTAssertEqual(concreteInstance.fakeableString(), "")
207    verify(concreteMock.fakeableString()).wasCalled()
208  }
209  
210  func testAddMultipleSubproviders() {
211    var valueProvider = ValueProvider()
212    valueProvider.add(.collectionsProvider)
213    valueProvider.add(.primitivesProvider)
214    concreteMock.useDefaultValues(from: valueProvider)
215    XCTAssertEqual(concreteInstance.fakeableArray(), [])
216    XCTAssertEqual(concreteInstance.fakeableInt(), Int())
217    verify(concreteMock.fakeableArray()).wasCalled()
218    verify(concreteMock.fakeableInt()).wasCalled()
219  }
220  
221  func testAddMultipleSubproviders_nonMutatingOperation() {
222    let valueProvider = ValueProvider.collectionsProvider.adding(.primitivesProvider)
223    concreteMock.useDefaultValues(from: valueProvider)
224    XCTAssertEqual(concreteInstance.fakeableArray(), [])
225    XCTAssertEqual(concreteInstance.fakeableInt(), Int())
226    verify(concreteMock.fakeableArray()).wasCalled()
227    verify(concreteMock.fakeableInt()).wasCalled()
228  }
229  
230  func testAddMultipleSubproviders_nonMutatingOperation_operatorSyntax() {
231    let valueProvider = .collectionsProvider + .primitivesProvider
232    concreteMock.useDefaultValues(from: valueProvider)
233    XCTAssertEqual(concreteInstance.fakeableArray(), [])
234    XCTAssertEqual(concreteInstance.fakeableInt(), Int())
235    verify(concreteMock.fakeableArray()).wasCalled()
236    verify(concreteMock.fakeableInt()).wasCalled()
237  }
238  
239  // MARK: - Precedence
240  
241  func testConcreteStubOverridesDefaultValueStub_priorToRegistration() {
242    given(concreteMock.fakeableInt()) ~> 99
243    var valueProvider = ValueProvider()
244    valueProvider.register(42, for: Int.self)
245    concreteMock.useDefaultValues(from: valueProvider)
246    XCTAssertEqual(concreteInstance.fakeableInt(), 99)
247    verify(concreteMock.fakeableInt()).wasCalled()
248  }
249  
250  func testConcreteStubOverridesDefaultValueStub_afterRegistration() {
251    var valueProvider = ValueProvider()
252    valueProvider.register(42, for: Int.self)
253    concreteMock.useDefaultValues(from: valueProvider)
254    given(concreteMock.fakeableInt()) ~> 99
255    XCTAssertEqual(concreteInstance.fakeableInt(), 99)
256    verify(concreteMock.fakeableInt()).wasCalled()
257  }
258  
259  // MARK: - Presets
260  
261  func testUseStandardProvider() {
262    concreteMock.useDefaultValues(from: .standardProvider)
263    XCTAssertEqual(concreteInstance.fakeableInt(), Int())
264    XCTAssertEqual(concreteInstance.fakeableString(), String())
265    XCTAssertEqual(concreteInstance.fakeableCGFloat(), CGFloat())
266    XCTAssertLessThan(concreteInstance.fakeableDate(), Date())
267    XCTAssertEqual(concreteInstance.fakeableArray(), [])
268  }
269  
270  func testUseStandardProviderWithObjCTypeFromProperty() {
271    let objcMock = mock(CBPeripheral.self).useDefaultValues(from: .standardProvider)
272    XCTAssertEqual(objcMock.canSendWriteWithoutResponse, Bool())
273    verify(objcMock.canSendWriteWithoutResponse).wasCalled()
274  }
275  
276  func testUseStandardProviderWithObjCTypeFromMethod() {
277    let objcMock = mock(CBPeripheral.self).useDefaultValues(from: .standardProvider)
278    XCTAssertEqual(objcMock.maximumWriteValueLength(for: .withResponse), Int())
279    verify(objcMock.maximumWriteValueLength(for: .withResponse)).wasCalled()
280  }
281  
282  func testUsePrimitivesProvider() {
283    concreteMock.useDefaultValues(from: .primitivesProvider)
284    XCTAssertEqual(concreteInstance.fakeableInt(), Int())
285    XCTAssertEqual(concreteInstance.fakeableUInt(), UInt())
286    XCTAssertEqual(concreteInstance.fakeableFloat(), Float())
287    XCTAssertEqual(concreteInstance.fakeableDouble(), Double())
288    XCTAssertEqual(concreteInstance.fakeableBool(), Bool())
289  }
290  
291  func testUseCollectionsProvider() {
292    concreteMock.useDefaultValues(from: .collectionsProvider)
293    XCTAssertEqual(concreteInstance.fakeableArray(), [])
294    XCTAssertEqual(concreteInstance.fakeableSet(), [])
295    XCTAssertEqual(concreteInstance.fakeableDictionary(), [:])
296  }
297  
298  // MARK: - Tuples
299  
300  func testTuplesDefaultValueProvider_2Tuple() {
301    concreteMock.useDefaultValues(from: .standardProvider)
302    let tuple = concreteInstance.fakeable2Tuple()
303    XCTAssertEqual(tuple.0, String())
304    XCTAssertEqual(tuple.1, Int())
305  }
306  
307  func testTuplesDefaultValueProvider_3Tuple() {
308    concreteMock.useDefaultValues(from: .standardProvider)
309    let tuple = concreteInstance.fakeable3Tuple()
310    XCTAssertEqual(tuple.0, String())
311    XCTAssertEqual(tuple.1, Int())
312    XCTAssertEqual(tuple.2, Bool())
313  }
314  
315  func testTuplesDefaultValueProvider_4Tuple() {
316    concreteMock.useDefaultValues(from: .standardProvider)
317    let tuple = concreteInstance.fakeable4Tuple()
318    XCTAssertEqual(tuple.0, String())
319    XCTAssertEqual(tuple.1, Int())
320    XCTAssertEqual(tuple.2, Bool())
321    XCTAssertEqual(tuple.3, Double())
322  }
323  
324  func testTuplesDefaultValueProvider_5Tuple() {
325    concreteMock.useDefaultValues(from: .standardProvider)
326    let tuple = concreteInstance.fakeable5Tuple()
327    XCTAssertEqual(tuple.0, String())
328    XCTAssertEqual(tuple.1, Int())
329    XCTAssertEqual(tuple.2, Bool())
330    XCTAssertEqual(tuple.3, Double())
331    XCTAssertEqual(tuple.4, Float())
332  }
333  
334  func testTuplesDefaultValueProvider_6Tuple() {
335    concreteMock.useDefaultValues(from: .standardProvider)
336    let tuple = concreteInstance.fakeable6Tuple()
337    XCTAssertEqual(tuple.0, String())
338    XCTAssertEqual(tuple.1, Int())
339    XCTAssertEqual(tuple.2, Bool())
340    XCTAssertEqual(tuple.3, Double())
341    XCTAssertEqual(tuple.4, Float())
342    XCTAssertEqual(tuple.5, UInt())
343  }
344  
345  // MARK: - Only explicit inclusion
346  
347  func testPrimitivesTypeGroup_excludesStringTypes() {
348    shouldFail {
349      self.concreteMock.useDefaultValues(from: .primitivesProvider)
350      _ = self.concreteInstance.fakeableString()
351    }
352  }
353  
354  func testPrimitivesTypeGroup_excludesCollectionTypes() {
355    shouldFail {
356      self.concreteMock.useDefaultValues(from: .primitivesProvider)
357      _ = self.concreteInstance.fakeableArray()
358    }
359  }
360  
361  func testStringsTypeGroup_excludesCollectionTypes() {
362    shouldFail {
363      self.concreteMock.useDefaultValues(from: .stringsProvider)
364      _ = self.concreteInstance.fakeableArray()
365    }
366  }
367  
368  func testCollectionsTypeGroup_excludesPrimitiveTypes() {
369    shouldFail {
370      self.concreteMock.useDefaultValues(from: .collectionsProvider)
371      _ = self.concreteInstance.fakeableBool()
372    }
373  }
374  
375  func testCommonTypeGroup_excludesCustomClassType() {
376    shouldFail {
377      self.concreteMock.useDefaultValues(from: .standardProvider)
378      _ = self.concreteInstance.fakeableClass()
379    }
380  }
381}
382
383private class ProtocolImplementation: GrandparentProtocol {
384  var grandparentPrivateSetterInstanceVariable = true
385  var grandparentInstanceVariable = true
386  func grandparentTrivialInstanceMethod() {}
387  func grandparentParameterizedInstanceMethod(param1: Bool, _ param2: Int) -> Bool { return true }
388  static var grandparentPrivateSetterStaticVariable = true
389  static var grandparentStaticVariable = true
390  static func grandparentTrivialStaticMethod() {}
391  static func grandparentParameterizedStaticMethod(param1: Bool, _ param2: Int) -> Bool {
392    return true
393  }
394}
395
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger testAddSingleSubprovider_operatorSyntax code on LambdaTest Cloud Grid

Execute automation tests with testAddSingleSubprovider_operatorSyntax on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)