File tree Expand file tree Collapse file tree 2 files changed +47
-11
lines changed
Expand file tree Collapse file tree 2 files changed +47
-11
lines changed Original file line number Diff line number Diff line change @@ -180,17 +180,27 @@ public extension QRCode.Builder {
180180// MARK: - If condition
181181
182182public extension QRCode . Builder {
183- /// Conditionally apply qr code style if a boolean is `true`, just like a regular `if` statement
184- /// - Parameter condition: A condition that has to be `true`
185- /// - Parameter configure: The `QRCode.Builder` modifier(s) that you want to apply if `condition == true`
186- /// - Returns: `QRCode.Builder`
187- func `if`( condition: Bool , configure: ( QRCode . Builder ) -> Void ) -> QRCode . Builder {
188- let builder = self
189- if condition {
190- configure ( builder)
191- }
192- return builder
193- }
183+ /// Conditionally apply qr code style if a boolean is `true`, just like a regular `if` statement
184+ /// - Parameter condition: A condition that has to be `true` for the configure block to execute
185+ /// - Parameter configureBlock: The `QRCode.Builder` modifier(s) that you want to apply if `condition == true`
186+ /// - Returns: `QRCode.Builder`
187+ ///
188+ /// Usage:
189+ ///
190+ /// ```swift
191+ /// let qrcodebuilder = try QRCode.build
192+ /// .text("Sample QR-code")
193+ /// .if(<some_condition>) { builder in
194+ /// builder.background.cornerRadius(0.75)
195+ /// }
196+ /// ```
197+ @discardableResult func `if`( _ condition: Bool , _ configureBlock: ( QRCode . Builder ) -> Void ) -> QRCode . Builder {
198+ let builder = self
199+ if condition {
200+ configureBlock ( builder)
201+ }
202+ return builder
203+ }
194204}
195205
196206// MARK: - Foreground
Original file line number Diff line number Diff line change @@ -181,4 +181,30 @@ final class QRCodeBuilderTests: XCTestCase {
181181 try outputFolder. write ( image, to: " builder-generated-rotate2.svg " )
182182 }
183183 }
184+
185+ func testIfConditionBuilding( ) throws {
186+ do {
187+ // Check that the block is NOT executed if the condition is false
188+ let qr1 = try QRCode . build
189+ . text ( " Fish and chips " )
190+ . errorCorrection ( . low)
191+ . if ( false ) { builder in
192+ builder. errorCorrection ( . high)
193+ }
194+ . document
195+ XCTAssertEqual ( qr1. errorCorrection, . low)
196+ }
197+ do {
198+ // Check that the block is executed if the condition is true
199+ let qr1 = try QRCode . build
200+ . text ( " Fish and chips " )
201+ . errorCorrection ( . low)
202+ . if ( true ) { builder in
203+ builder. errorCorrection ( . high)
204+ }
205+ . document
206+ XCTAssertEqual ( qr1. errorCorrection, . high)
207+ }
208+ }
209+
184210}
You can’t perform that action at this time.
0 commit comments