|
1 | 1 | import XCTest
|
2 | 2 | import SwiftSyntax
|
3 |
| - |
4 | 3 | import SwiftSyntaxBuilder
|
5 | 4 |
|
6 | 5 | final class ExpressibleBuildablesTests: XCTestCase {
|
7 |
| - func testExpressibleAsMemberDeclListItem() { |
8 |
| - let myStruct = StructDecl(identifier: "MyStruct", members: MemberDeclBlock(membersBuilder: { |
9 |
| - VariableDecl(.var, name: "myFirstVar", type: "Int") |
| 6 | + func testExpressibleAsMemberDeclListItem() { |
| 7 | + let myStruct = StructDecl(identifier: "MyStruct", members: MemberDeclBlock(membersBuilder: { |
| 8 | + VariableDecl(.var, name: "myFirstVar", type: "Int") |
10 | 9 |
|
11 |
| - // We use `MemberDeclListItem` to ensure and show we can combine it with `ExpressibleAsMemberDeclListItem` |
12 |
| - MemberDeclListItem(decl: VariableDecl(letOrVarKeyword: .let, bindingsBuilder: { |
13 |
| - PatternBinding(pattern: IdentifierPattern(identifier: .identifier("myOtherLet")), |
14 |
| - typeAnnotation: TypeAnnotation(type: SimpleTypeIdentifier("String"))) |
15 |
| - }) |
16 |
| - ) |
| 10 | + // We use `MemberDeclListItem` to ensure and show we can combine it with `ExpressibleAsMemberDeclListItem` |
| 11 | + MemberDeclListItem(decl: VariableDecl(letOrVarKeyword: .let, bindingsBuilder: { |
| 12 | + PatternBinding(pattern: IdentifierPattern(identifier: .identifier("myOtherLet")), |
| 13 | + typeAnnotation: TypeAnnotation(type: SimpleTypeIdentifier("String"))) |
| 14 | + }) |
| 15 | + ) |
17 | 16 |
|
18 |
| - StructDecl(identifier: "InnerStruct", members: MemberDeclBlock()) |
19 |
| - }) |
20 |
| - ) |
| 17 | + StructDecl(identifier: "InnerStruct", members: MemberDeclBlock()) |
| 18 | + }) |
| 19 | + ) |
21 | 20 |
|
22 |
| - let syntax = myStruct.buildSyntax(format: Format()) |
23 |
| - XCTAssertEqual(syntax.description, """ |
24 |
| - struct MyStruct{ |
25 |
| - var myFirstVar: Int |
26 |
| - let myOtherLet: String |
27 |
| - struct InnerStruct{ |
28 |
| - } |
| 21 | + let syntax = myStruct.buildSyntax(format: Format()) |
| 22 | + XCTAssertEqual(syntax.description, """ |
| 23 | + struct MyStruct{ |
| 24 | + var myFirstVar: Int |
| 25 | + let myOtherLet: String |
| 26 | + struct InnerStruct{ |
29 | 27 | }
|
30 |
| - """) |
31 | 28 | }
|
| 29 | + """) |
| 30 | + } |
32 | 31 |
|
33 |
| - func testExpressibleAsCodeBlockItem() { |
34 |
| - let myCodeBlock = SourceFile(eofToken: .eof) { |
35 |
| - StructDecl(identifier: "MyStruct1", members: MemberDeclBlock()) |
| 32 | + func testExpressibleAsCodeBlockItem() { |
| 33 | + let myCodeBlock = SourceFile(eofToken: .eof) { |
| 34 | + StructDecl(identifier: "MyStruct1", members: MemberDeclBlock()) |
36 | 35 |
|
37 |
| - StructDecl(identifier: "MyStruct2", members: MemberDeclBlock()) |
38 |
| - } |
| 36 | + StructDecl(identifier: "MyStruct2", members: MemberDeclBlock()) |
| 37 | + } |
39 | 38 |
|
40 |
| - let syntax = myCodeBlock.buildSyntax(format: Format()) |
41 |
| - XCTAssertEqual(syntax.description, """ |
| 39 | + let syntax = myCodeBlock.buildSyntax(format: Format()) |
| 40 | + XCTAssertEqual(syntax.description, """ |
42 | 41 |
|
43 |
| - struct MyStruct1{ |
44 |
| - } |
45 |
| - struct MyStruct2{ |
46 |
| - } |
47 |
| - """) |
| 42 | + struct MyStruct1{ |
48 | 43 | }
|
| 44 | + struct MyStruct2{ |
| 45 | + } |
| 46 | + """) |
| 47 | + } |
49 | 48 |
|
50 |
| - func testExpressibleAsSwitchStmt() { |
51 |
| - let versions = [("version_1", "1.0.0"), ("version_2", "2.0.0"), ("version_3", "3.0.0"), ("version_3_1", "3.1.0")] |
52 |
| - let expression = IdentifierExpr(identifier: SyntaxFactory.makeIdentifier("version")) |
| 49 | + func testExpressibleAsSwitchStmt() { |
| 50 | + let versions = [("version_1", "1.0.0"), ("version_2", "2.0.0"), ("version_3", "3.0.0"), ("version_3_1", "3.1.0")] |
| 51 | + let expression = IdentifierExpr(identifier: SyntaxFactory.makeIdentifier("version")) |
53 | 52 |
|
54 |
| - let switchStmt = SwitchStmt(labelName: nil, |
55 |
| - expression: expression, |
56 |
| - leftBrace: .leftBrace.withTrailingTrivia(.newlines(1)), |
57 |
| - rightBrace: .rightBrace.withLeadingTrivia(.newlines(1)), |
58 |
| - casesBuilder: { |
59 |
| - for (version, semVer) in versions { |
60 |
| - SwitchCase(label: SwitchCaseLabel(caseItemsBuilder: { |
61 |
| - CaseItem(pattern: EnumCasePattern(caseName: version)) |
62 |
| - }), statementsBuilder: { |
63 |
| - ReturnStmt(expression: StringLiteralExpr(semVer, closeQuote: .stringQuote.withTrailingTrivia(.newlines(1)))) |
64 |
| - }) |
65 |
| - } |
| 53 | + let switchStmt = SwitchStmt(labelName: nil, |
| 54 | + expression: expression, |
| 55 | + leftBrace: .leftBrace.withTrailingTrivia(.newlines(1)), |
| 56 | + rightBrace: .rightBrace.withLeadingTrivia(.newlines(1)), |
| 57 | + casesBuilder: { |
| 58 | + for (version, semVer) in versions { |
| 59 | + SwitchCase(label: SwitchCaseLabel(caseItemsBuilder: { |
| 60 | + CaseItem(pattern: EnumCasePattern(caseName: version)) |
| 61 | + }), statementsBuilder: { |
| 62 | + ReturnStmt(expression: StringLiteralExpr(semVer, closeQuote: .stringQuote.withTrailingTrivia(.newlines(1)))) |
66 | 63 | })
|
| 64 | + } |
| 65 | + }) |
67 | 66 |
|
68 |
| - let syntax = switchStmt.buildSyntax(format: Format()) |
| 67 | + let syntax = switchStmt.buildSyntax(format: Format()) |
69 | 68 |
|
70 |
| - // The generated code contains whitespace after `:`. |
71 |
| - // So replacing whitespace with `␣`. |
72 |
| - XCTAssertEqual(syntax.description.replacingOccurrences(of: " ", with: "␣"), """ |
73 |
| - switch␣version{ |
74 |
| - case␣.version_1:␣ |
75 |
| - return␣"1.0.0" |
76 |
| - case␣.version_2:␣ |
77 |
| - return␣"2.0.0" |
78 |
| - case␣.version_3:␣ |
79 |
| - return␣"3.0.0" |
80 |
| - case␣.version_3_1:␣ |
81 |
| - return␣"3.1.0" |
| 69 | + // The generated code contains whitespace after `:`. |
| 70 | + // So replacing whitespace with `␣`. |
| 71 | + XCTAssertEqual(syntax.description.replacingOccurrences(of: " ", with: "␣"), """ |
| 72 | + switch␣version{ |
| 73 | + case␣.version_1:␣ |
| 74 | + return␣"1.0.0" |
| 75 | + case␣.version_2:␣ |
| 76 | + return␣"2.0.0" |
| 77 | + case␣.version_3:␣ |
| 78 | + return␣"3.0.0" |
| 79 | + case␣.version_3_1:␣ |
| 80 | + return␣"3.1.0" |
82 | 81 |
|
83 |
| - } |
84 |
| - """) |
85 | 82 | }
|
| 83 | + """) |
| 84 | + } |
86 | 85 | }
|
0 commit comments