How to use testDeclaredType_parsesFunctionTypeAttributes method of DeclaredTypeTests class

Best Mockingbird code snippet using DeclaredTypeTests.testDeclaredType_parsesFunctionTypeAttributes

Run Mockingbird automation tests on LambdaTest cloud grid

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

DeclaredTypeTests.swift

Source: DeclaredTypeTests.swift Github

copy
1//
2//  DeclaredTypeTests.swift
3//  MockingbirdTests
4//
5//  Created by Andrew Chang on 9/2/19.
6//
7
8import XCTest
9@testable import MockingbirdGenerator
10
11class DeclaredTypeTests: XCTestCase {
12  
13  // MARK: - Tuples
14  
15  func testDeclaredType_parsesUnlabeledTuple() {
16    let actual = DeclaredType(from: "(Int, Bool)")
17    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Tuple((DeclaredType(Single(Int)), DeclaredType(Single(Bool)))))")
18  }
19  
20  func testDeclaredType_parsesLabeledTuple() {
21    let actual = DeclaredType(from: "(a: Int, b: Bool)")
22    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Tuple((a: DeclaredType(Single(Int)), b: DeclaredType(Single(Bool)))))")
23  }
24  
25  func testDeclaredType_parsesPartiallyLabeledTuple() {
26    let actual = DeclaredType(from: "(a: Int, Bool, String)")
27    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Tuple((a: DeclaredType(Single(Int)), DeclaredType(Single(Bool)), DeclaredType(Single(String)))))")
28  }
29  
30  func testDeclaredType_parsesNestedUnlabeledTuples() {
31    let actual = DeclaredType(from: "((Int, Int), (Bool, Bool))")
32    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Tuple((DeclaredType(Tuple((DeclaredType(Single(Int)), DeclaredType(Single(Int))))), DeclaredType(Tuple((DeclaredType(Single(Bool)), DeclaredType(Single(Bool))))))))")
33  }
34  
35  func testDeclaredType_parsesNestedLabeledTuples() {
36    let actual = DeclaredType(from: "(a: (a: Int, b: Int), b: (a: Bool, b: Bool))")
37    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Tuple((a: DeclaredType(Tuple((a: DeclaredType(Single(Int)), b: DeclaredType(Single(Int))))), b: DeclaredType(Tuple((a: DeclaredType(Single(Bool)), b: DeclaredType(Single(Bool))))))))")
38  }
39  
40  func testDeclaredType_parsesNestedPartiallyLabeledTuples() {
41    let actual = DeclaredType(from: "(a: (a: Int, Bool, String), b: (b: Bool, Bool, String))")
42    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Tuple((a: DeclaredType(Tuple((a: DeclaredType(Single(Int)), DeclaredType(Single(Bool)), DeclaredType(Single(String))))), b: DeclaredType(Tuple((b: DeclaredType(Single(Bool)), DeclaredType(Single(Bool)), DeclaredType(Single(String))))))))")
43  }
44  
45  func testDeclaredType_parsesEmptyTuples() {
46    let actual = DeclaredType(from: "()")
47    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Tuple(()))")
48  }
49  
50  func testDeclaredType_parsesNestedEmptyUnlabledTuples() {
51    let actual = DeclaredType(from: "((), ())")
52    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Tuple((DeclaredType(Tuple(())), DeclaredType(Tuple(())))))")
53  }
54  
55  func testDeclaredType_parsesNestedEmptyLabledTuples() {
56    let actual = DeclaredType(from: "(a: (), b: ())")
57    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Tuple((a: DeclaredType(Tuple(())), b: DeclaredType(Tuple(())))))")
58  }
59  
60  // MARK: - Parenthesized Expressions
61  
62  func testDeclaredType_parsesParenthesizedPrimitive() {
63    let actual = DeclaredType(from: "(Bool)")
64    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Bool))")
65  }
66  
67  func testDeclaredType_parsesNestedParenthesizedPrimitive() {
68    let actual = DeclaredType(from: "(((Bool)))")
69    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Bool))")
70  }
71  
72  func testDeclaredType_parsesOptionalParenthesizedPrimitive() {
73    let actual = DeclaredType(from: "(Bool)?")
74    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Bool)?)")
75  }
76  
77  func testDeclaredType_parsesSingleNestedOptionalParenthesizedPrimitive() {
78    let actual = DeclaredType(from: "(((Bool)?))")
79    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Bool)?)")
80  }
81  
82  func testDeclaredType_parsesMultipleNestedOptionalParenthesizedPrimitive() {
83    let actual = DeclaredType(from: "(((Bool)?))??!")
84    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Bool)???!)")
85  }
86  
87  // MARK: - Functions
88  
89  func testDeclaredType_parsesTrivialFunctionTypes() {
90    let actual = DeclaredType(from: "() -> Void")
91    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function(() -> DeclaredType(Single(Void)))))")
92    XCTAssert(actual.isFunction)
93  }
94  
95  func testDeclaredType_parsesSingleParameterFunctionTypes() {
96    let actual = DeclaredType(from: "(Int) -> String")
97    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function((Parameter(DeclaredType(Single(Int)))) -> DeclaredType(Single(String)))))")
98    XCTAssert(actual.isFunction)
99  }
100  
101  func testDeclaredType_parsesMultipleParameterFunctionTypes() {
102    let actual = DeclaredType(from: "(Int, Bool) -> String")
103    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function((Parameter(DeclaredType(Single(Int))), Parameter(DeclaredType(Single(Bool)))) -> DeclaredType(Single(String)))))")
104    XCTAssert(actual.isFunction)
105  }
106  
107  func testDeclaredType_parsesLabeledFunctionTypes() {
108    let actual = DeclaredType(from: "(_ a: Int, _ b: Bool) -> String")
109    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function((Parameter(_ a: DeclaredType(Single(Int))), Parameter(_ b: DeclaredType(Single(Bool)))) -> DeclaredType(Single(String)))))")
110    XCTAssert(actual.isFunction)
111  }
112  
113  func testDeclaredType_parsesChainedFunctionTypes() {
114    let actual = DeclaredType(from: "(Int) -> (Bool) -> Void")
115    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function((Parameter(DeclaredType(Single(Int)))) -> DeclaredType(Single(Function((Parameter(DeclaredType(Single(Bool)))) -> DeclaredType(Single(Void))))))))")
116    XCTAssert(actual.isFunction)
117  }
118  
119  func testDeclaredType_parsesChainedTupleFunctionTypes() {
120    let actual = DeclaredType(from: "(Int) -> ((Bool) -> Void)")
121    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function((Parameter(DeclaredType(Single(Int)))) -> DeclaredType(Single(Function((Parameter(DeclaredType(Single(Bool)))) -> DeclaredType(Single(Void))))))))")
122    XCTAssert(actual.isFunction)
123  }
124  
125  func testDeclaredType_parsesFunctionTypeInFunctionTypeParameters() {
126    let actual = DeclaredType(from: "((Int, Bool) -> String, Int) -> Void")
127    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function((Parameter(DeclaredType(Single(Function((Parameter(DeclaredType(Single(Int))), Parameter(DeclaredType(Single(Bool)))) -> DeclaredType(Single(String)))))), Parameter(DeclaredType(Single(Int)))) -> DeclaredType(Single(Void)))))")
128    XCTAssert(actual.isFunction)
129  }
130  
131  func testDeclaredType_parsesFunctionTypeAttributes() {
132    let actual = DeclaredType(from: "(@autoclosure @escaping (Int, Bool) -> String, Int) -> Void")
133    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function((Parameter(@escaping @autoclosure DeclaredType(Single(Function((Parameter(DeclaredType(Single(Int))), Parameter(DeclaredType(Single(Bool)))) -> DeclaredType(Single(String)))))), Parameter(DeclaredType(Single(Int)))) -> DeclaredType(Single(Void)))))")
134    XCTAssert(actual.isFunction)
135  }
136  
137  func testDeclaredType_parsesFunctionTypeAttributesWithoutWhitespace() {
138    let actual = DeclaredType(from: "(@escaping(String)) -> Void")
139    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function((Parameter(@escaping DeclaredType(Single(String)))) -> DeclaredType(Single(Void)))))")
140    XCTAssert(actual.isFunction)
141  }
142  
143  func testDeclaredType_parsesFunctionTypeAttributesChainedWithoutWhitespace() {
144    let actual = DeclaredType(from: "(@[email protected]()) -> String")
145    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function((Parameter(@escaping @autoclosure DeclaredType(Tuple(())))) -> DeclaredType(Single(String)))))")
146    XCTAssert(actual.isFunction)
147  }
148  
149  func testDeclaredType_parsesFunctionTypeInoutFunctionTypeParameters() {
150    let actual = DeclaredType(from: "((inout Int, inout Bool) -> String, inout Int) -> Void")
151    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function((Parameter(DeclaredType(Single(Function((Parameter(inout DeclaredType(Single(Int))), Parameter(inout DeclaredType(Single(Bool)))) -> DeclaredType(Single(String)))))), Parameter(inout DeclaredType(Single(Int)))) -> DeclaredType(Single(Void)))))")
152    XCTAssert(actual.isFunction)
153  }
154  
155  func testDeclaredType_parsesFunctionTypeVariadicFunctionTypeParameters() {
156    let actual = DeclaredType(from: "((Int..., Bool ...) -> String, Int ...) -> Void")
157    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function((Parameter(DeclaredType(Single(Function((Parameter(DeclaredType(Single(Int))...), Parameter(DeclaredType(Single(Bool))...)) -> DeclaredType(Single(String)))))), Parameter(DeclaredType(Single(Int))...)) -> DeclaredType(Single(Void)))))")
158    XCTAssert(actual.isFunction)
159  }
160  
161  func testDeclaredType_parsesFunctionTypeUnlabeledTupleTypeParameters() {
162    let actual = DeclaredType(from: "((Int, String), (Int, String)) -> Void")
163    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function((Parameter(DeclaredType(Tuple((DeclaredType(Single(Int)), DeclaredType(Single(String)))))), Parameter(DeclaredType(Tuple((DeclaredType(Single(Int)), DeclaredType(Single(String))))))) -> DeclaredType(Single(Void)))))")
164    XCTAssert(actual.isFunction)
165  }
166  
167  func testDeclaredType_parsesFunctionTypeLabeledTupleTypeParameters() {
168    let actual = DeclaredType(from: "((a: Int, b: String), (a: Int, b: String)) -> Void")
169    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function((Parameter(DeclaredType(Tuple((a: DeclaredType(Single(Int)), b: DeclaredType(Single(String)))))), Parameter(DeclaredType(Tuple((a: DeclaredType(Single(Int)), b: DeclaredType(Single(String))))))) -> DeclaredType(Single(Void)))))")
170    XCTAssert(actual.isFunction)
171  }
172  
173  func testDeclaredType_parsesFunctionTypePartiallyLabeledTupleTypeParameters() {
174    let actual = DeclaredType(from: "((a: Int, Bool, String), (a: Int, Bool, String)) -> Void")
175    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function((Parameter(DeclaredType(Tuple((a: DeclaredType(Single(Int)), DeclaredType(Single(Bool)), DeclaredType(Single(String)))))), Parameter(DeclaredType(Tuple((a: DeclaredType(Single(Int)), DeclaredType(Single(Bool)), DeclaredType(Single(String))))))) -> DeclaredType(Single(Void)))))")
176    XCTAssert(actual.isFunction)
177  }
178  
179  func testDeclaredType_parsesFunctionTypeEmptyTupleTypeParameters() {
180    let actual = DeclaredType(from: "((), ()) -> Void")
181    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function((Parameter(DeclaredType(Tuple(()))), Parameter(DeclaredType(Tuple(())))) -> DeclaredType(Single(Void)))))")
182    XCTAssert(actual.isFunction)
183  }
184  
185  func testDeclaredType_parsesThrowingFunctionType() {
186    let actual = DeclaredType(from: "() throws -> Bool")
187    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function(() throws -> DeclaredType(Single(Bool)))))")
188    XCTAssert(actual.isFunction)
189  }
190  
191  func testDeclaredType_parsesChainedThrowingFunctionTypes() {
192    let actual = DeclaredType(from: "() throws -> () throws -> Bool")
193    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function(() throws -> DeclaredType(Single(Function(() throws -> DeclaredType(Single(Bool))))))))")
194    XCTAssert(actual.isFunction)
195  }
196  
197  func testDeclaredType_parsesParenthesizedFunction() {
198    let actual = DeclaredType(from: "((Bool) -> Bool)")
199    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function((Parameter(DeclaredType(Single(Bool)))) -> DeclaredType(Single(Bool)))))")
200    XCTAssert(actual.isFunction)
201  }
202  
203  func testDeclaredType_parsesNestedParenthesizedFunction() {
204    let actual = DeclaredType(from: "((((Bool) -> Bool)))")
205    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function((Parameter(DeclaredType(Single(Bool)))) -> DeclaredType(Single(Bool)))))")
206    XCTAssert(actual.isFunction)
207  }
208  
209  func testDeclaredType_parsesOptionalParenthesizedFunction() {
210    let actual = DeclaredType(from: "((Bool) -> Bool)?")
211    XCTAssertEqual(String(reflecting: actual), "DeclaredType((Single(Function((Parameter(DeclaredType(Single(Bool)))) -> DeclaredType(Single(Bool)))))?)")
212    XCTAssert(actual.isFunction)
213  }
214  
215  // MARK: - Generics
216  
217  func testDeclaredType_parsesGenericType() {
218    let actual = DeclaredType(from: "Array<String>")
219    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Array<DeclaredType(Single(String))>))")
220  }
221  
222  func testDeclaredType_parsesGenericFunctionTypeParameters() {
223    let actual = DeclaredType(from: "(SignalProducer<String, Bool>) -> Void")
224    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function((Parameter(DeclaredType(Single(SignalProducer<DeclaredType(Single(String)), DeclaredType(Single(Bool))>)))) -> DeclaredType(Single(Void)))))")
225    XCTAssert(actual.isFunction)
226  }
227  
228  func testDeclaredType_parsesGenericFunctionParameter() {
229    let actual = DeclaredType(from: "(GenericType<Bool>) -> Void")
230    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function((Parameter(DeclaredType(Single(GenericType<DeclaredType(Single(Bool))>)))) -> DeclaredType(Single(Void)))))")
231    XCTAssert(actual.isFunction)
232  }
233  
234  func testDeclaredType_parsesMultipleGenericFunctionParameters() {
235    let actual = DeclaredType(from: "(GenericType<Bool>, GenericType<Int>) -> Void")
236    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function((Parameter(DeclaredType(Single(GenericType<DeclaredType(Single(Bool))>))), Parameter(DeclaredType(Single(GenericType<DeclaredType(Single(Int))>)))) -> DeclaredType(Single(Void)))))")
237    XCTAssert(actual.isFunction)
238  }
239  
240  func testDeclaredType_parsesGenericFunctionReturnType() {
241    let actual = DeclaredType(from: "() -> GenericType<Bool>")
242    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function(() -> DeclaredType(Single(GenericType<DeclaredType(Single(Bool))>)))))")
243    XCTAssert(actual.isFunction)
244  }
245  
246  func testDeclaredType_parsesGenericFunctionWrappedReturnType() {
247    let actual = DeclaredType(from: "() -> (SignalProducer<String, Bool>)")
248    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function(() -> DeclaredType(Single(SignalProducer<DeclaredType(Single(String)), DeclaredType(Single(Bool))>)))))")
249    XCTAssert(actual.isFunction)
250  }
251  
252  func testDeclaredType_parsesGenericFunctionOptionalReturnType() {
253    let actual = DeclaredType(from: "() -> GenericType<Bool>?")
254    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function(() -> DeclaredType(Single(GenericType<DeclaredType(Single(Bool))>)?))))")
255    XCTAssert(actual.isFunction)
256  }
257  
258  func testDeclaredType_parsesGenericFunctionWrappedOptionalReturnType() {
259    let actual = DeclaredType(from: "() -> (GenericType<Bool>)?")
260    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function(() -> DeclaredType(Single(GenericType<DeclaredType(Single(Bool))>)?))))")
261    XCTAssert(actual.isFunction)
262  }
263  
264  func testDeclaredType_parsesGenericFunctionParametersAndReturnType() {
265    let actual = DeclaredType(from: "(GenericType<Bool>, GenericType<Int>) -> SignalProducer<String, Bool>")
266    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function((Parameter(DeclaredType(Single(GenericType<DeclaredType(Single(Bool))>))), Parameter(DeclaredType(Single(GenericType<DeclaredType(Single(Int))>)))) -> DeclaredType(Single(SignalProducer<DeclaredType(Single(String)), DeclaredType(Single(Bool))>)))))")
267    XCTAssert(actual.isFunction)
268  }
269  
270  func testDeclaredType_parsesGenericFunctionParametersAndWrappedReturnType() {
271    let actual = DeclaredType(from: "(GenericType<Bool>, GenericType<Int>) -> (SignalProducer<String, Bool>)")
272    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function((Parameter(DeclaredType(Single(GenericType<DeclaredType(Single(Bool))>))), Parameter(DeclaredType(Single(GenericType<DeclaredType(Single(Int))>)))) -> DeclaredType(Single(SignalProducer<DeclaredType(Single(String)), DeclaredType(Single(Bool))>)))))")
273    XCTAssert(actual.isFunction)
274  }
275  
276  // MARK: Optionals
277  
278  func testDeclaredType_parsesOptionalType() {
279    let actual = DeclaredType(from: "String?")
280    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(String)?)")
281    XCTAssertTrue(actual.isOptional)
282  }
283  
284  func testDeclaredType_parsesGenericOptionalType() {
285    let actual = DeclaredType(from: "Array<String>?")
286    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Array<DeclaredType(Single(String))>)?)")
287    XCTAssertTrue(actual.isOptional)
288  }
289  
290  func testDeclaredType_parsesOptionalGenericTypeParameter() {
291    let actual = DeclaredType(from: "Array<String?>")
292    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Array<DeclaredType(Single(String)?)>))")
293    XCTAssertFalse(actual.isOptional)
294  }
295  
296  func testDeclaredType_parsesMultipleOptionalGenericTypeParameters() {
297    let actual = DeclaredType(from: "Dictionary<String?, Int?>")
298    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Dictionary<DeclaredType(Single(String)?), DeclaredType(Single(Int)?)>))")
299    XCTAssertFalse(actual.isOptional)
300  }
301  
302  func testDeclaredType_parsesGenericImplicitlyUnwrappedOptionalType() {
303    let actual = DeclaredType(from: "Array<String>!")
304    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Array<DeclaredType(Single(String))>)!)")
305    XCTAssertTrue(actual.isOptional)
306  }
307  
308  func testDeclaredType_parsesGenericMultiWrappedOptionalType() {
309    let actual = DeclaredType(from: "Array<String>???")
310    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Array<DeclaredType(Single(String))>)???)")
311    XCTAssertTrue(actual.isOptional)
312  }
313  
314  func testDeclaredType_parsesGenericMultiWrappedOptionalTypeWithImplicitlyUnwrappedEnding() {
315    let actual = DeclaredType(from: "Array<String>???!")
316    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Array<DeclaredType(Single(String))>)???!)")
317    XCTAssertTrue(actual.isOptional)
318  }
319  
320  func testDeclaredType_parsesOptionalTuple() {
321    let actual = DeclaredType(from: "(String?, Int?)?")
322    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Tuple((DeclaredType(Single(String)?), DeclaredType(Single(Int)?)))?)")
323    XCTAssertTrue(actual.isTuple)
324    XCTAssertTrue(actual.isOptional)
325  }
326  
327  func testDeclaredType_parsesOptionalArray() {
328    let actual = DeclaredType(from: "[String?]?")
329    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single([DeclaredType(Single(String)?)])?)")
330    XCTAssertTrue(actual.isCollection)
331    XCTAssertTrue(actual.isOptional)
332  }
333  
334  func testDeclaredType_parsesOptionalDictionary() {
335    let actual = DeclaredType(from: "[String: Int?]?")
336    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single([DeclaredType(Single(String)): DeclaredType(Single(Int)?)])?)")
337    XCTAssertTrue(actual.isCollection)
338    XCTAssertTrue(actual.isOptional)
339  }
340  
341  func testDeclaredType_parsesOptionalFunctionTuple() {
342    let actual = DeclaredType(from: "((Int) -> Void)?")
343    XCTAssertEqual(String(reflecting: actual), "DeclaredType((Single(Function((Parameter(DeclaredType(Single(Int)))) -> DeclaredType(Single(Void)))))?)")
344    XCTAssertTrue(actual.isFunction)
345    XCTAssertTrue(actual.isOptional)
346  }
347  
348  func testDeclaredType_parsesFunctionWithOptionalReturnType() {
349    let actual = DeclaredType(from: "(Int) -> Bool?")
350    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function((Parameter(DeclaredType(Single(Int)))) -> DeclaredType(Single(Bool)?))))")
351    XCTAssertTrue(actual.isFunction)
352    XCTAssertFalse(actual.isOptional)
353  }
354  
355  func testDeclaredType_parsesOptionalFunctionWithOptionalReturnType() {
356    let actual = DeclaredType(from: "((Int) -> Bool?)?")
357    XCTAssertEqual(String(reflecting: actual), "DeclaredType((Single(Function((Parameter(DeclaredType(Single(Int)))) -> DeclaredType(Single(Bool)?))))?)")
358    XCTAssertTrue(actual.isFunction)
359    XCTAssertTrue(actual.isOptional)
360  }
361  
362  func testDeclaredType_parsesOptionalFunctionWithOptionalTupleReturnType() {
363    let actual = DeclaredType(from: "((Int) -> (Bool, Int)?)?")
364    XCTAssertEqual(String(reflecting: actual), "DeclaredType((Single(Function((Parameter(DeclaredType(Single(Int)))) -> DeclaredType(Tuple((DeclaredType(Single(Bool)), DeclaredType(Single(Int))))?))))?)")
365    XCTAssertTrue(actual.isFunction)
366    XCTAssertTrue(actual.isOptional)
367  }
368  
369  // MARK: - Qualified types
370  
371  func testDeclaredType_parsesFullyQualifiedType() {
372    let actual = DeclaredType(from: "Foundation.Array.Element")
373    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Foundation.Array.Element))")
374  }
375  
376  func testDeclaredType_parsesGenericQualifiedType() {
377    let actual = DeclaredType(from: "Foundation.Array<String>.Element")
378    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Foundation.Array<DeclaredType(Single(String))>.Element))")
379  }
380  
381  func testDeclaredType_parsesMultipleGenericParametersQualifiedType() {
382    let actual = DeclaredType(from: "Foundation.Dictionary<String, Int>.Element")
383    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Foundation.Dictionary<DeclaredType(Single(String)), DeclaredType(Single(Int))>.Element))")
384  }
385  
386  func testDeclaredType_parsesMultipleGenericComponentsQualifiedType() {
387    let actual = DeclaredType(from: "Foundation.Dictionary<String, Int>.Array<String>")
388    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Foundation.Dictionary<DeclaredType(Single(String)), DeclaredType(Single(Int))>.Array<DeclaredType(Single(String))>))")
389  }
390  
391  func testDeclaredType_parsesFullyQualifiedTupleType() {
392    let actual = DeclaredType(from: "(Foundation.Array.Element, Foundation.NSObject)")
393    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Tuple((DeclaredType(Single(Foundation.Array.Element)), DeclaredType(Single(Foundation.NSObject)))))")
394  }
395  
396  func testDeclaredType_parsesFullyQualifiedFunctionParameterType() {
397    let actual = DeclaredType(from: "(Foundation.Array.Element, Foundation.NSObject) -> Void")
398    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function((Parameter(DeclaredType(Single(Foundation.Array.Element))), Parameter(DeclaredType(Single(Foundation.NSObject)))) -> DeclaredType(Single(Void)))))")
399    XCTAssertTrue(actual.isFunction)
400  }
401  
402  func testDeclaredType_parsesFullyQualifiedFunctionReturnType() {
403    let actual = DeclaredType(from: "() -> Foundation.NSObject")
404    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function(() -> DeclaredType(Single(Foundation.NSObject)))))")
405    XCTAssertTrue(actual.isFunction)
406  }
407  
408  func testDeclaredType_parsesFullyQualifiedGenericFunctionParameter() {
409    let actual = DeclaredType(from: "(Foundation.Array<T>) -> Void")
410    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function((Parameter(DeclaredType(Single(Foundation.Array<DeclaredType(Single(T))>)))) -> DeclaredType(Single(Void)))))")
411    XCTAssert(actual.isFunction)
412  }
413  
414  func testDeclaredType_parsesFullyQualifiedGenericFunctionParameterAndReturnType() {
415    let actual = DeclaredType(from: "(Foundation.Array<T>) -> Foundation.Array<T>")
416    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function((Parameter(DeclaredType(Single(Foundation.Array<DeclaredType(Single(T))>)))) -> DeclaredType(Single(Foundation.Array<DeclaredType(Single(T))>)))))")
417    XCTAssert(actual.isFunction)
418  }
419  
420  func testDeclaredType_parsesFullyQualifiedCompoundGenericFunctionReturnType() {
421    let actual = DeclaredType(from: "() -> Foundation.Array<Foundation.Set<String>>")
422    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function(() -> DeclaredType(Single(Foundation.Array<DeclaredType(Single(Foundation.Set<DeclaredType(Single(String))>))>)))))")
423    XCTAssert(actual.isFunction)
424  }
425  
426  func testDeclaredType_parsesFullyQualifiedCompoundGenericFunctionParameter() {
427    let actual = DeclaredType(from: "(Foundation.Array<Foundation.Set<String>>) -> Void")
428    XCTAssertEqual(String(reflecting: actual), "DeclaredType(Single(Function((Parameter(DeclaredType(Single(Foundation.Array<DeclaredType(Single(Foundation.Set<DeclaredType(Single(String))>))>)))) -> DeclaredType(Single(Void)))))")
429    XCTAssert(actual.isFunction)
430  }
431  
432  // MARK: - Parameters
433  
434  func testParameterType_parsesDefaultParameters() {
435    let actual = Function.Parameter(from: "label parameter: String = \"Hello\"")
436    XCTAssertEqual(String(reflecting: actual), "Parameter(label parameter: DeclaredType(Single(String)))")
437    XCTAssertEqual(actual.defaultValue, "\"Hello\"")
438  }
439}
440
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

Most used method in DeclaredTypeTests

Trigger testDeclaredType_parsesFunctionTypeAttributes code on LambdaTest Cloud Grid

Execute automation tests with testDeclaredType_parsesFunctionTypeAttributes 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)