Swift Testing Challange - Can you refactor this?

Swift Testing Challange - Can you refactor this?

Intro Have you already started using Swift Testing instead of XCTest? I’m curious to see how you can refactor the test function (add_returnsCorrectSum) from the code snippet to use all powers of the Swift Testing framework. Could you explain what benefits does your refactored version has compared to my original code snippet? My approach In both approaches the test gives the same result. The point is the refactored version uses a Swift Testing parametrised test, and this makes a real difference. ...

April 17, 2025 · 2 min · Maciej Gomolka
#3 Swift code refactor in action - price $$$

#3 Swift code refactor in action - a sneaky problem hidden in code snippet

Swift code refactor in action 👨🏻‍💻 Take a close look at the validate function. There’s a sneaky problem hidden in this code snippet. What will the function call return when passed nil? What problem is hidden here? First, the guard statement is redundant here. We can simplify the function to ⤵️ func validate(password: String?) -> Bool { password?.count ?? 0 > 8 } There’s no need to wrap password?.count ?? 0 in parentheses since the ?? operator already has higher precedence than >. ...

April 9, 2025 · 2 min · Maciej Gomolka
Do You Know the Difference Between Imperative, Functional, and Reactive Programming?

Imperative, Functional, Functional Reactive: Do you know the difference?

Paradigms Imperative In the imperative approach, we have a sequence of instructions that describe step by step how the program’s state is modified. Let’s look on the example ⤵️ var value = 0 func increment() { value += 1 // Mutating the state } print(value) // 0 increment() print(value) // 1 In the code we have a mutable variable value and a function increment that mutates the state (value). We first print the initial value, then increment it, and finally print the updated value. ...

February 25, 2025 · 5 min · Maciej Gomolka
#7 XCTest vs Swift Testing: A modern way of linking bugs

#7 XCTest vs Swift Testing: A modern way of linking bugs

What’s the difference? In XCTest we relied on the old fashioned simple comments to add more context to our test case e.g link to the bug description. With Swift Testing, we now have a special bug trait that can be passed to the @Test macro. The bug trait takes a URL String as argument and optionally a title for the bug allowing us to add short description of it. The key advantage over regular comment is that the bug title is visible in the test results. Better yet, tapping on it takes you directly to the related webpage with the bug report. ...

December 13, 2024 · 2 min · Maciej Gomolka
#6 XCTest vs Swift Testing - Parameterized tests in the fight for more reusable code

#6 XCTest vs Swift Testing - Parameterized tests in the fight for more reusable code

What’s the difference? XCTest doesn’t provide a built-in solution for parameterized tests. To achieve this, we create test cases as structs or tuples, defining test inputs and expected results. Then, we write a loop to iterate through these test cases and execute the necessary assertions. Swift Testing simplifies this process by allowing tests to be parameterized directly. Using the @Test macro, you can pass test cases as an argument. What’s the benefit? While you still need to define your test cases, the iteration code is no longer needed — Swift Testing handles it for you. ...

December 5, 2024 · 2 min · Maciej Gomolka