1
+ //===----------------------------------------------------------------------===//
2
+ //
3
+ // This source file is part of the Swift.org open source project
4
+ //
5
+ // Copyright (c) 2024 Apple Inc. and the Swift project authors
6
+ // Licensed under Apache License v2.0 with Runtime Library Exception
7
+ //
8
+ // See https://swift.org/LICENSE.txt for license information
9
+ // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10
+ //
11
+ //===----------------------------------------------------------------------===//
12
+
13
+ import SwiftSyntax
14
+ import XCTest
15
+
16
+ class UtilsTests : XCTestCase {
17
+
18
+ public func testIsVoid( ) {
19
+ XCTAssertTrue ( TypeSyntax ( " Void " ) . isVoid)
20
+ XCTAssertTrue ( TypeSyntax ( " Swift.Void " ) . isVoid)
21
+ XCTAssertTrue ( TypeSyntax ( " () " ) . isVoid)
22
+
23
+ XCTAssertFalse ( TypeSyntax ( " (Int, Int) " ) . isVoid)
24
+ XCTAssertFalse ( TypeSyntax ( " Swift " ) . isVoid)
25
+ XCTAssertFalse ( TypeSyntax ( " Swift.() " ) . isVoid)
26
+ XCTAssertFalse ( TypeSyntax ( " Int " ) . isVoid)
27
+ XCTAssertFalse ( TypeSyntax ( " (()) " ) . isVoid)
28
+ XCTAssertFalse ( TypeSyntax ( " (Void) " ) . isVoid)
29
+ }
30
+
31
+ public func testIsInt( ) {
32
+ XCTAssertTrue ( TypeSyntax ( " Int " ) . canRepresentBasicType ( type: Int . self) )
33
+ XCTAssertTrue ( TypeSyntax ( " Swift.Int " ) . canRepresentBasicType ( type: Int . self) )
34
+ XCTAssertTrue ( TypeSyntax ( " Int " ) . canRepresentBasicType ( type: Swift . Int. self) )
35
+ XCTAssertTrue ( TypeSyntax ( " Swift.Int " ) . canRepresentBasicType ( type: Swift . Int. self) )
36
+
37
+ // Only the canonical type syntax matches
38
+ XCTAssertFalse ( TypeSyntax ( " CInt " ) . canRepresentBasicType ( type: Int . self) )
39
+ XCTAssertFalse ( TypeSyntax ( " Swift.CInt " ) . canRepresentBasicType ( type: Int . self) )
40
+ XCTAssertFalse ( TypeSyntax ( " CInt " ) . canRepresentBasicType ( type: Swift . Int. self) )
41
+ XCTAssertFalse ( TypeSyntax ( " Swift.CInt " ) . canRepresentBasicType ( type: Swift . Int. self) )
42
+ }
43
+
44
+ public func testIsCInt( ) {
45
+ // Match against the canonical type (platform dependent)
46
+ XCTAssertEqual ( TypeSyntax ( " Swift.Int " ) . canRepresentBasicType ( type: Swift . CInt. self) , CInt . self == Int . self)
47
+ XCTAssertEqual ( TypeSyntax ( " Int " ) . canRepresentBasicType ( type: Swift . CInt. self) , CInt . self == Int . self)
48
+ XCTAssertEqual ( TypeSyntax ( " Int " ) . canRepresentBasicType ( type: CInt . self) , CInt . self == Int . self)
49
+ XCTAssertEqual ( TypeSyntax ( " Swift.Int " ) . canRepresentBasicType ( type: CInt . self) , CInt . self == Int . self)
50
+
51
+ XCTAssertFalse ( TypeSyntax ( " Swift.CInt " ) . canRepresentBasicType ( type: Swift . CInt. self) )
52
+ XCTAssertFalse ( TypeSyntax ( " CInt " ) . canRepresentBasicType ( type: Swift . CInt. self) )
53
+ XCTAssertFalse ( TypeSyntax ( " CInt " ) . canRepresentBasicType ( type: CInt . self) )
54
+ XCTAssertFalse ( TypeSyntax ( " Swift.CInt " ) . canRepresentBasicType ( type: CInt . self) )
55
+ }
56
+
57
+ public func testIsArrayType( ) {
58
+ // Only plain name types are supported
59
+ XCTAssertFalse ( TypeSyntax ( " [Int] " ) . canRepresentBasicType ( type: [ Int ] . self) )
60
+ XCTAssertFalse ( TypeSyntax ( " Int " ) . canRepresentBasicType ( type: [ Int ] . self) )
61
+ }
62
+
63
+ public func testIsOptionalType( ) {
64
+ // Only plain name types are supported
65
+ XCTAssertFalse ( TypeSyntax ( " Int? " ) . canRepresentBasicType ( type: Int ? . self) )
66
+ XCTAssertFalse ( TypeSyntax ( " Optional<Int> " ) . canRepresentBasicType ( type: Int ? . self) )
67
+ XCTAssertFalse ( TypeSyntax ( " Int " ) . canRepresentBasicType ( type: [ Int ] . self) )
68
+ }
69
+
70
+ public func testIsTupleTypes( ) {
71
+ // Only plain name types are supported
72
+ XCTAssertFalse ( TypeSyntax ( " () " ) . canRepresentBasicType ( type: Void . self) )
73
+ XCTAssertFalse ( TypeSyntax ( " Void " ) . canRepresentBasicType ( type: Void . self) )
74
+ XCTAssertFalse ( TypeSyntax ( " (Int, Int) " ) . canRepresentBasicType ( type: ( Int, Int) . self) )
75
+ }
76
+ }
0 commit comments