Skip to content

Commit 643b245

Browse files
committed
Some minor changes for the 'if' implementation
* Made the conditiion label optional (looks more like an if statement) * Add basic tests to check that the condition is handled correctly
1 parent 8e72f4e commit 643b245

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed

Sources/QRCode/QRCode+Builder.swift

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -180,17 +180,27 @@ public extension QRCode.Builder {
180180
// MARK: - If condition
181181

182182
public 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

Tests/QRCodeTests/QRCodeBuilderTests.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)