Repository navigation
Releases
Releases · joeoct-oss/Understudy
0.7.0
Added
TestClock, a clock that moves only when the test moves it, so code that waits can be tested without real time passing.advance(by:)andadvance(to:)wake each sleep the window reaches, in deadline order, and let the woken code run on to its next sleep, even across actors and the main actor; a woken task readsnowas the deadline it woke at, so a sleep it starts late still counts from then;advance()lets ready work run, andrun()moves on until nothing waits. A cancelled sleep throwsCancellationError. It needs macOS 13, iOS 16, tvOS 16 or watchOS 9.@GenerateSpieson a protocol with associated types writes a generic spy, one generic parameter per associated type with its constraints (Sendableadded in aSendableprotocol and in an actor protocol):RepositorySpy<Product>(). Awhereclause on an associated type or on the protocol, and a static requirement next to an associated type, are refused with a message pointing to the class form.@GenerateSpieson a protocol that inherits another protocol with@GenerateSpieswrites a spy that stores the parent's spy, taken first by its initializer,ChildSpy(base: BaseSpy(ping: 3)), and answers the parent's requirements through it, with the parent's spies read on the child's (child.pingSpy). Several parents and a parent's parents work too. For this, the macro also writes<Name>SpyForwardingnext to every protocol. A parent from the standard library, a parent with associated types, an actor protocol, and a protocol declared inside another type can't be forwarded to, and say so..verifiesMocks, a Swift Testing trait for a test or a suite (Swift 6.1 or later): every spy given an expectation during the test is verified when it ends, so a forgottenverify()can't pass silently. An unmet expectation is reported at itsexpectline, and a call nobody expected at the test; a spy with no expectations is never checked, and a problemverify()already reported isn't reported again.expectgains a defaultedsourceLocation, so existing calls compile unchanged..spi.yml, so the Swift Package Index builds the package and hosts the API docs ofUnderstudyandUnderstudySamples.- A coverage report on Codecov for main and each pull request (the Coverage report workflow).
scripts/check-coverage.shwrites the measured lines as one LCOV file whenCOVERAGE_LCOVnames a path. The coverage gate itself is unchanged.
Changed
- Breaking, only for a test that declares its own type named
<Name>SpyForwardingnext to a protocol with@GenerateSpies: the macro now writes a protocol of that name there, so the two clash. Rename the test's type. - Breaking, only for code that keeps a strict spy's
expectas a function value:expect(_:times:)andexpect(times:where:)gained asourceLocationthat defaults to the caller's, so.verifiesMockscan point at theexpectline. Calls don't change.expectandexpectNoCalls()now live, likeverify, only where Swift Testing can be imported, which every supported toolchain can; without it a strict spy could never report. - Pull requests run the tests on Swift 6.0 and 6.4 only. Swift 6.1, 6.2 and 6.3 run on every push to main, in the Linux checks a release waits for, so every version still passes before a version is published.
- CodeRabbit's review checks the shared code style and clean code (short functions, clear names, no repeated logic), and says exactly which macro test checks are wanted. SonarQube Cloud keeps tests apart from sources and leaves coverage to Codecov, Codecov reports through statuses only, and
CODE_STYLE.mdno longer contradicts itself on macro tests. The review uses the chill profile and reports broken style and testing rules inline as should-fix findings, so an open one blocks the merge. - Every test has a display name that states what it checks, such as "A spy keeps every input in order", so a test report reads as a list of what the package promises. Test function names stay as they were, for
swift test --filter.
Fixed
- The code
@GenerateSpieswrites names every type it uses with its module (Understudy.Spy,Understudy.PropertySpy,Swift.Void,_Concurrency.TaskLocaland the rest), so a double still builds next to a type of the test's own calledSpy,VoidorTaskLocal.@Sample's defaults keep the type as the property writes it, so they follow a type of the app's own calledDateorURL.
0.6.0
Added
- Typed throws:
@GenerateSpieswrites a requirement such asfunc signIn(...) async throws(AuthError) -> Account. It gets aThrowingSpy, as a plainthrowsdoes, and its body rethrows the scripted error asAuthError. An error of another type stops the test with a message naming the spy and both types.throws(any Error)is a plainthrows, andthrows(Never)throws nothing. - Generics inside a collection: a generic parameter inside an array, an optional, a dictionary's value or a set, such as
func tags<T: Tag>(_ items: [T]) -> [T], is recorded as the same container ofanyof its constraints ([any Tag]; a set asSet<AnyHashable>), and a returned one is cast back to the type the caller asks for. A generic parameter inside a closure, a tuple or another generic type is still refused. @SpiedPropertywith a first value:@SpiedProperty var user: String = "ana"starts its spy with"ana", and the double's initializer doesn't ask for it. It works in aSendableclass and in an actor, where@Recordedcan't. Without a value it works as before.- Subscripts: in a
@GenerateSpiesclass,subscript(index: Int) -> Item { @Spied get {} @Spied set {} }(Swift needs the empty bodies; Swift 6.4 or later), and a protocol's subscript requirements (every Swift version). Gets go tosubscriptSpy, aSpyof the index, and sets tosubscriptSetSpy, aSpyof the index and thenewValue; a get-only subscript has only the first. Several indices are a labelled tuple, and overloads add the index names. The double's initializer asks for the first answer unless it is optional. - Static members:
@Spied static funcand@SpiedProperty static var, and a protocol's static requirements. The static spies live in a box held in a task local, and.freshSpies(SpyClock.self), a Swift Testing trait for a test or a suite (Swift 6.1 or later), gives each test its own, so parallel tests never share them. Without the trait, every test shares one box. A static spy starts from the obvious first answer (nil,"",0,false, empty), and a static requirement with none is refused. A double with static spies conforms to the newStaticSpies, whosewithFreshSpiesruns a block with a fresh box. - Fix-its: each refusal with one clear fix offers it in the editor:
lettovarfor a@SpiedProperty, removing a@Spiedfunction's body, naming a parameter written_: Int, adding: <#Type#>to a property with no type, and@Recordedto@SpiedPropertyin an actor. @GenerateSpiesrefuses@Recordedin aSendableclass, which can't hold a wrapped property, with a fix-it to@SpiedProperty.
Changed
- The double's initializer gives an obvious first answer when the test passes none:
""forString,0for whole and decimal numbers andDecimal,falseforBool, and empty arrays, dictionaries and sets.SpyCatalog()now compiles when every answer has one. A type with no obvious answer is still a required argument, and every argument a test passes today is still accepted. The macro reads the type as written, soSwift.Stringor a typealias stays required. - The README is rewritten for a first-time user: what Understudy is (spies and sample values), install, a quick start, then each tool with one short example. Every example is compiled and run against the current API.
0.5.0
Added
@Sample, in the new productUnderstudySamples: on a struct, it writesstatic func sample(...), with one parameter per stored property, each with a default, so a test gets a complete value in one call, such asProduct.sample(price: 0).@SampleDefault(value)sets a property's default. The sample is written for debug builds only, and is as visible as the struct.CallOrder { ... }, which watches every spy in a block ofWatched(spy, as: name)lines, forSpyandThrowingSpyalike.watch(_:as:)stays, for a spy the test starts watching later.@Recorded, a property wrapper that spies on a property whose first value is at its declaration:@Recorded var user = "ana", with$useritsPropertySpy. It works in doubles written by hand and in@GenerateSpiesclasses, next to@SpiedProperty, and a@GenerateSpiesclass may have@Recordedproperties only.@GenerateSpiesrefuses it on an actor, where@SpiedPropertyis the way.
Fixed
- Understudy now asks for swift-syntax 602 or later. It said 600, but the macros don't build with 600, and 601 gives a wrong extra error for a
@SpiedPropertywith accessors, so a project that resolved either got a broken Understudy.
0.4.0
Changed
@Understudyis now@GenerateSpies, which says what it does: it writes a spy for every@Spiedfunction and@SpiedPropertyproperty.@Spiedand@SpiedPropertykeep their names. Replace@Understudywith@GenerateSpies; nothing else changes.- The README is a short guide: requirements, install, a quick start, one small example per feature, and what the macros refuse and why. Every example is checked against the current API.
Added
reset()onSpy,ThrowingSpyandPropertySpy, which forgets the calls (or gets and sets), wherenextCall(within:)was, and the expectations, and keeps the scripted answers.- Actor doubles:
@GenerateSpieson anactorwrites the same members as on a class, withnonisolatedspies a test reads withoutawait. On a protocol that inheritsActor, it writes an actor spy. - Generic functions:
@Spiedwrites a generic function whose generic parameters are whole parameter types or the whole return type (TorT?). The spy records each asanyof its constraints, orAny, and the answer is cast back to the type the caller asks for.
0.3.0
Added
@SpiedProperty, which marks avarof an@Understudyclass for the macro to write, andPropertySpy, which keeps every value set, counts the gets and answers with the last value set.@Understudyon a protocol now writes itsvarrequirements too.- Held calls:
holdNextCall()makes the next async call wait until the test resumes it with an answer, an error or the scripted answer. Async requirements written by@Understudygo through the newrespond(to:), so they can be held. nextCall(within:), which waits for a call made from another task, giving each call once, in order.CallOrder, which keeps the order of calls across several spies.- Strict spies:
expect(_:times:),expect(times:where:)andexpectNoCalls(), checked byverify(), which records each expected call that didn't happen and each call nobody expected as a Swift Testing issue.
0.2.0
Added
@Understudy, a macro that writes a test double. On a class in a test target, it writes a spy and a body for every requirement marked@Spiedand declared with no body, and an initializer that asks for every answer with no safe default. On a protocol, it writes a spy class next to it, named after the protocol withSpyadded.@Spied, which marks the requirements@Understudywrites.
See the README for an example.
0.1.0
First release.
Spykeeps every input a test double's requirement is called with and answers as the test scripts: a standing answer, one-off answers queued in front of it (willReturnNext(_:then:)), or an answer worked out from the input.ThrowingSpydoes the same for requirements that throw (willThrow(_:),willGiveNext(_:then:)).- Pure Swift, Foundation only, no dependencies. Safe under Swift 6 strict concurrency, from main-actor and
Sendabletest doubles alike.
Supports iOS 13, macOS 10.15, tvOS 13, watchOS 6, visionOS 1 and Linux.