Two types of Swift macros

Two types of Swift macros

What is macro? 💡 Macro is a feature that generates code during compilation. Unlike macros in C, which work like “find and replace”, Swift macros are type-safe and context aware, making them powerful tools reducing boilerplate code. Two types of macros attached - use @ prefix, tied to a declaration adding extra logic to it, like: @Test, @Model, @Observable freestanding - use # prefix, standalone code that can be invoked independently as a part of the code, like #expect, #Predicate, #warning Example of attached macro ⤵️...

November 26, 2024 · 1 min · Maciej Gomolka
#3 XCTest vs. Swift Testing - Unwrapping optionals

#3 XCTest vs. Swift Testing - Unwrapping optionals

Optionals are a core of Swift - we deal with them daily, both in production and testing code. Whether you write tests with XCTest or Swift Testing, unwrapping optionals is a common case. In XCTest there is XCTUnwrap operator. Swift Testing introduces #require macro. Is there any difference between them? Not really! Both require the test function to handle exceptions and try keyword before. Gif ⤵️ Code ⤵️ XCTest func testNewYorkTimeZone() throws { let newYorkTimeZone = try XCTUnwrap( TimeZone(identifier: "America/New_York") ) XCTAssertEqual(newYorkTimeZone....

November 21, 2024 · 1 min · Maciej Gomolka
#1 Swift code refactor in action - user profile name

#1 Swift code refactor in action - user profile name

Swift code refactor in action 👨🏻‍💻 Common scenario: formatting user profile name - I bet any of you faced this kind of task. At first glance, it look straightforward, but when you take a closer look, you’ll notice two potential improvements: 1️⃣ One single return - simplification of the function flow. 2️⃣ Centralised formatting logic - reduces the chance of bugs. Check out the animated gif and the code where I refactor to address these issues....

November 20, 2024 · 2 min · Maciej Gomolka
#2 XCTest vs. Swift Testing - Has error testing been simplified?

#2 XCTest vs. Swift Testing - Has error testing been simplified?

Today we check how testing error has changed in the new framework. In XCTest, we use XCTAssertThrowsError to check if a specific error is thrown. This assertion comes with the error handler closure where we can perform additional checks like e.g. verifying the exact error type. With Swift Testing, this process is even simpler, especially when an error conforms to Equatable. We can directly specify the expected error in the expect macro and it automatically checks the type....

November 19, 2024 · 1 min · Maciej Gomolka
#1 XCTest vs. Swift Testing - fresh look on a new testing framework

#1 XCTest vs. Swift Testing - fresh look on a new testing framework

New Series! XCTest vs. Swift Testing - fresh look on a new testing framework. Swift Testing was presented at WWDC24 as a new, modern, simplified framework for writing automated tests. It’s a perfect candidate to replace XCTest unit tests, so it’s definitely worth learning. I haven’t had a chance yet to use Swift Testing in production and the series is my motivation for me to discover it. Today we cover 2 basic differences ⤵️...

November 14, 2024 · 2 min · Maciej Gomolka