Unlock This Episode
Our Free plan includes 1 subscriber-only episode of your choice, plus weekly updates from our newsletter.
Introduction
OK! So everything compiles and tests run and pass just as before! Unfortunately it was pretty complicated. We really butted heads with some seriously complicated protocol features, like Self
, non-final classes, and required initializers. But at least now the Snapshottable
protocol is completely decoupled from taking image snapshots. In fact, it’s more generic than any snapshot testing library out there. Can we do anything interesting with that?
Subscribe to Point-Free
Access this episode, plus all past and future episodes when you become a subscriber.
Already a subscriber? Log in
Exercises
Translate the
Snapshottable
protocol into aSnapshotting
struct. How do you capture the associated type constraint?Translate each conformance of
Diffable
into a witness value onDiffing
.String
UIImage
Translate the
Snapshottable
protocol into aSnapshotting
struct. How do you capture the associated type constraint?Translate each conformance of
Snapshottable
into a witness value onSnapshotting
.String
UIImage
CALayer
UIView
UIViewController
Translate the
assertSnapshot
generic algorithm to take an explicitSnapshotting
witness.
References
Protocol-Oriented Programming in Swift
Apple • Tuesday Jun 16, 2015Apple’s eponymous WWDC talk on protocol-oriented programming:
At the heart of Swift’s design are two incredibly powerful ideas: protocol-oriented programming and first class value semantics. Each of these concepts benefit predictability, performance, and productivity, but together they can change the way we think about programming. Find out how you can apply these ideas to improve the code you write.
uber/ios-snapshot-test-case
Uber, previously FacebookFacebook released a snapshot testing framework known as FBSnapshotTestCase
back in 2013, and many in the
iOS community adopted it. The library gives you an API to assert snapshots of UIView
’s that will take
a screenshot of your UI and compare it against a reference image in your repo. If a single pixel is off
it will fail the test. Since then Facebook has stopped maintaining it and transfered ownership to Uber.
Snapshot Testing in Swift
Stephen Celis • Friday Sep 1, 2017Stephen gave an overview of snapshot testing, its benefits, and how one may snapshot Swift data types, walking through a minimal implementation.
Protocol Witnesses: App Builders 2019
Brandon Williams • Friday May 3, 2019Brandon gave a talk about “protocol witnesses” at the 2019 App Builders conference. The basics of scraping protocols is covered as well as some interesting examples of where this technique really shines when applied to snapshot testing and animations.
Protocol-oriented programming is strongly recommended in the Swift community, and Apple has given a lot of guidance on how to use it in your everyday code. However, there has not been a lot of attention on when it is not appropriate, and what to do in that case. We will explore this idea, and show that there is a completely straightforward and mechanical way to translate any protocol into a concrete datatype. Once you do this you can still write your code much like you would with protocols, but all of the complexity inherit in protocols go away. Even more amazing, a new type of composition appears that is difficult to see when dealing with only protocols. We will also demo a real life, open source library that was originally written in the protocol-oriented way, but after running into many problems with the protocols, it was rewritten entirely in this witness-oriented way. The outcome was really surprising, and really powerful.