File tree Expand file tree Collapse file tree
StructBuilderMacro/Enum/Helper
Tests/StructBuilderMacroTests Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -36,5 +36,6 @@ struct MyObject {
3636
3737@Buildable
3838enum MyEnum {
39+ case `none`
3940 case myCase
4041}
Original file line number Diff line number Diff line change 88import SwiftSyntax
99
1010func getFirstEnumCaseName( from enumDecl: EnumDeclSyntax ) throws -> TokenSyntax {
11- guard let firstCaseName = enumDecl. memberBlock. members. first? . decl. as ( EnumCaseDeclSyntax . self) ? . elements. first? . name else {
11+ let enumCaseDecls = enumDecl. memberBlock. members. compactMap { $0. decl. as ( EnumCaseDeclSyntax . self) }
12+ guard let firstCaseName = enumCaseDecls. first? . elements. first? . name else {
1213 throw " Missing enum case "
1314 }
1415 return firstCaseName
Original file line number Diff line number Diff line change @@ -35,5 +35,117 @@ class BuildableEnumTests: XCTestCase {
3535 macros: testMacros
3636 )
3737 }
38+
39+ func test_should_set_first_enum_case_as_default_value( ) {
40+ assertMacroExpansion (
41+ """
42+ @Buildable
43+ enum MyEnum {
44+ case myCase, mySecondCase
45+ }
46+ """ ,
47+ expandedSource: """
48+
49+ enum MyEnum {
50+ case myCase, mySecondCase
51+ }
52+
53+ struct MyEnumBuilder {
54+ var value: MyEnum = .myCase
55+
56+ func build() -> MyEnum {
57+ return value
58+ }
59+ }
60+
61+ """ ,
62+ macros: testMacros
63+ )
64+ }
65+
66+ func test_should_set_first_enum_case_as_default_value_when_enum_has_rawvalue( ) {
67+ assertMacroExpansion (
68+ """
69+ @Buildable
70+ enum MyEnum: String {
71+ case myCase = " a "
72+ case mySecondCase = " b "
73+ }
74+ """ ,
75+ expandedSource: """
76+
77+ enum MyEnum: String {
78+ case myCase = " a "
79+ case mySecondCase = " b "
80+ }
81+
82+ struct MyEnumBuilder {
83+ var value: MyEnum = .myCase
84+
85+ func build() -> MyEnum {
86+ return value
87+ }
88+ }
89+
90+ """ ,
91+ macros: testMacros
92+ )
93+ }
94+
95+ func test_should_set_first_enum_case_as_default_value_when_identifier_uses_special_keyword( ) {
96+ assertMacroExpansion (
97+ """
98+ @Buildable
99+ enum MyEnum {
100+ case `none`
101+ }
102+ """ ,
103+ expandedSource: """
104+
105+ enum MyEnum {
106+ case `none`
107+ }
108+
109+ struct MyEnumBuilder {
110+ var value: MyEnum = .`none`
111+
112+ func build() -> MyEnum {
113+ return value
114+ }
115+ }
116+
117+ """ ,
118+ macros: testMacros
119+ )
120+ }
121+
122+ func test_should_set_first_enum_case_as_default_value_when_first_enum_member_is_no_case_declaration( ) {
123+ assertMacroExpansion (
124+ """
125+ @Buildable
126+ enum MyEnum {
127+ var someVariable: String? { nil }
128+ case myCase
129+ }
130+ """ ,
131+ expandedSource: """
132+
133+ enum MyEnum {
134+ var someVariable: String? { nil }
135+ case myCase
136+ }
137+
138+ struct MyEnumBuilder {
139+ var value: MyEnum = .myCase
140+
141+ func build() -> MyEnum {
142+ return value
143+ }
144+ }
145+
146+ """ ,
147+ macros: testMacros
148+ )
149+ }
38150}
39151
You can’t perform that action at this time.
0 commit comments