File tree Expand file tree Collapse file tree
Examples/Completed/conditionals Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -31,4 +31,19 @@ let possibleName: String? = "John"
3131let possibleAge : Int ? = 30
3232if let name = possibleName, let age = possibleAge {
3333 print ( " \( name) is \( age) years old " )
34+ }
35+
36+ // MARK: - Guard Statements
37+ func greet( person: [ String : String ] ) {
38+ guard let name = person [ " name " ] else {
39+ print ( " No name provided " )
40+ return
41+ }
42+
43+ guard let age = person [ " age " ] , let ageInt = Int ( age) else {
44+ print ( " Invalid age provided " )
45+ return
46+ }
47+
48+ print ( " Hello \( name) , you are \( ageInt) years old " )
3449}
Original file line number Diff line number Diff line change @@ -56,4 +56,23 @@ Group {
5656 } then: {
5757 Call ( " print " , " \\ (name) is \\ (age) years old " )
5858 }
59+
60+ Function ( " greet " , parameters: [ Parameter ( " person " , type: " [String: String] " ) ] ) {
61+ Guard {
62+ Let ( " name " , " person[ \" name \" ] " )
63+ } else: {
64+ Call ( " print " , " No name provided " )
65+ }
66+ Guard {
67+ Let ( " age " , " person[ \" age \" ] " )
68+ Let ( " ageInt " , Init ( " Int " ) {
69+ Parameter ( unlabeled: " age " )
70+ } )
71+ } else: {
72+ Call ( " print " , " Invalid age provided " )
73+ }
74+ Call ( " print " , " Hello \\ (name), you are \\ (ageInt) years old " )
75+ }
76+ } . comment {
77+ Line ( " MARK: - Guard Statements " )
5978}
Load Diff Large diffs are not rendered by default.
Original file line number Diff line number Diff line change @@ -114,15 +114,46 @@ import Testing
114114 ParameterExp ( name: " " , value: " \" \\ (name) is \\ (age) years old \" " )
115115 }
116116 }
117+
118+ // MARK: - Guard Statements
119+ Function (
120+ " greet " ,
121+ {
122+ Parameter ( name: " person " , type: " [String: String] " )
123+ }
124+ ) {
125+ Guard {
126+ Let ( " name " , " person[ \" name \" ] " )
127+ } else: {
128+ Call ( " print " ) {
129+ ParameterExp ( name: " " , value: " \" No name provided \" " )
130+ }
131+ }
132+
133+ Guard {
134+ Let ( " age " , " person[ \" age \" ] " )
135+ Let ( " ageInt " , " Int(age) " )
136+ } else: {
137+ Call ( " print " ) {
138+ ParameterExp ( name: " " , value: " \" Invalid age provided \" " )
139+ }
140+ }
141+
142+ Call ( " print " ) {
143+ ParameterExp ( name: " " , value: " \" Hello \\ (name), you are \\ (ageInt) years old \" " )
144+ }
145+ }
146+ . comment {
147+ Line ( " MARK: - Guard Statements " )
148+ }
117149 }
118150
119151 // Generate Swift from DSL
120152 var generated = program. generateCode ( )
121153 // Remove type annotations like ": Int =" for comparison to example code
122154 generated = generated. replacingOccurrences (
123155 of: " : \\ s* \\ w+ \\ s*= " , with: " = " , options: . regularExpression
124- )
125- . normalize ( )
156+ ) . normalize ( )
126157
127158 // Load expected Swift from example file
128159 let projectRoot = URL ( fileURLWithPath: FileManager . default. currentDirectoryPath)
You can’t perform that action at this time.
0 commit comments