1+ // RUN: %target-run-simple-swift
2+ // REQUIRES: executable_test
3+ // REQUIRES: OS=macosx
4+
5+ import Foundation
6+ import StdlibUnittest
7+ import SwiftSyntax
8+
9+ func cannedStructDecl( ) -> StructDeclSyntax {
10+ let fooID = SyntaxFactory . makeIdentifier ( " Foo " , trailingTrivia: . spaces( 1 ) )
11+ let structKW = SyntaxFactory . makeStructKeyword ( trailingTrivia: . spaces( 1 ) )
12+ let rBrace = SyntaxFactory . makeRightBraceToken ( leadingTrivia: . newlines( 1 ) )
13+ return StructDeclSyntax {
14+ $0. useStructKeyword ( structKW)
15+ $0. useIdentifier ( fooID)
16+ $0. useLeftBrace ( SyntaxFactory . makeLeftBraceToken ( ) )
17+ $0. useRightBrace ( rBrace)
18+ }
19+ }
20+
21+ var SyntaxFactoryAPI = TestSuite ( " SyntaxFactoryAPI " )
22+
23+ SyntaxFactoryAPI . test ( " Generated " ) {
24+
25+ let structDecl = cannedStructDecl ( )
26+
27+ expectEqual ( " \( structDecl) " ,
28+ """
29+ struct Foo {
30+ }
31+ """ )
32+
33+ let forType = SyntaxFactory . makeIdentifier ( " for " ,
34+ leadingTrivia: . backticks( 1 ) ,
35+ trailingTrivia: [
36+ . backticks( 1 ) , . spaces( 1 )
37+ ] )
38+ let newBrace = SyntaxFactory . makeRightBraceToken ( leadingTrivia: . newlines( 2 ) )
39+
40+ let renamed = structDecl. withIdentifier ( forType)
41+ . withRightBrace ( newBrace)
42+
43+ expectEqual ( " \( renamed) " ,
44+ """
45+ struct `for` {
46+
47+ }
48+ """ )
49+
50+ expectNotEqual ( structDecl. leftBrace, renamed. leftBrace)
51+ expectEqual ( structDecl, structDecl. root)
52+ expectNil ( structDecl. parent)
53+ expectNotNil ( structDecl. leftBrace. parent)
54+ expectEqual ( structDecl. leftBrace. parent, structDecl)
55+
56+ // Ensure that accessing children via named identifiers is exactly the
57+ // same as accessing them as their underlying data.
58+ expectEqual ( structDecl. leftBrace, structDecl. child ( at: 7 ) )
59+
60+ expectEqual ( " \( structDecl. rightBrace) " ,
61+ """
62+
63+ }
64+ """ )
65+ }
66+
67+ SyntaxFactoryAPI . test ( " TokenSyntax " ) {
68+ let tok = SyntaxFactory . makeStructKeyword ( )
69+ expectEqual ( " \( tok) " , " struct " )
70+ expectTrue ( tok. isPresent)
71+
72+ let preSpacedTok = tok. withLeadingTrivia ( . spaces( 3 ) )
73+ expectEqual ( " \( preSpacedTok) " , " struct " )
74+
75+ let postSpacedTok = tok. withTrailingTrivia ( . spaces( 6 ) )
76+ expectEqual ( " \( postSpacedTok) " , " struct " )
77+
78+ let prePostSpacedTok = preSpacedTok. withTrailingTrivia ( . spaces( 4 ) )
79+ expectEqual ( " \( prePostSpacedTok) " , " struct " )
80+ }
81+
82+ SyntaxFactoryAPI . test ( " FunctionCallSyntaxBuilder " ) {
83+ let string = SyntaxFactory . makeStringLiteralExpr ( " Hello, world! " )
84+ let printID = SyntaxFactory . makeVariableExpr ( " print " )
85+ let arg = FunctionCallArgumentSyntax {
86+ $0. useExpression ( string)
87+ }
88+ let call = FunctionCallExprSyntax {
89+ $0. useCalledExpression ( printID)
90+ $0. useLeftParen ( SyntaxFactory . makeLeftParenToken ( ) )
91+ $0. addFunctionCallArgument ( arg)
92+ $0. useRightParen ( SyntaxFactory . makeRightParenToken ( ) )
93+ }
94+ expectEqual ( " \( call) " , " print( \" Hello, world! \" ) " )
95+
96+ let terminatorArg = FunctionCallArgumentSyntax {
97+ $0. useLabel ( SyntaxFactory . makeIdentifier ( " terminator " ) )
98+ $0. useColon ( SyntaxFactory . makeColonToken ( trailingTrivia: . spaces( 1 ) ) )
99+ $0. useExpression ( SyntaxFactory . makeStringLiteralExpr ( " " ) )
100+ }
101+ let callWithTerminator = call. withArgumentList (
102+ SyntaxFactory . makeFunctionCallArgumentList ( [
103+ arg. withTrailingComma (
104+ SyntaxFactory . makeCommaToken ( trailingTrivia: . spaces( 1 ) ) ) ,
105+ terminatorArg
106+ ] )
107+ )
108+
109+ expectEqual ( " \( callWithTerminator) " ,
110+ " print( \" Hello, world! \" , terminator: \" \" ) " )
111+ }
112+
113+ runAllTests ( )
0 commit comments