Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preview: Introducing patterns #40

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open

Preview: Introducing patterns #40

wants to merge 12 commits into from

Conversation

felixvisee
Copy link
Contributor

@felixvisee felixvisee commented Jan 6, 2017

This pull request introduces several new types for recording interactions and corresponding values as well was verifying interactions with multiple recorders.

First, recording of interactions and corresponding values has been split up into two protocols, InteractionRecording and ValueRecording. Using protocols allows developers to define their own recorders if necessary. Dobby ships with one default, thread-safe recorder. None of the aforementioned protocols needs to deal with verification, they just need to take care of recording.

Second, verification has been re-implemented using Pattern, which is thread-safe too. Unlike mocks, patterns can verify set up expectations with multiple interaction recorders. For example, if your test double has multiple methods, you can easily verify that they are invoked in order:

class Thing {
    let fstRecorder = Recorder<Int>()
    let sndRecorder = Recorder<String>()

    func fst(x: Int) {
        fstRecorder.record(x)
    }

    func snd(y: String) {
        sndRecorder.record(y)
    }
}

let thing = Thing()
thing.fst(1)
thing.snd("foobar")

let pattern = Pattern()
pattern.expect(1, in: thing.fstRecorder)
pattern.expect("foobar", in: thing.sndRecorder)
pattern.verify()

Besides verification across multiple recorders, this has several other benefits. Whether expectations are strict or nice and must be fulfilled in order or not is no longer a property of the entity that records interactions. Stubs can easily act as recorders, greatly simplifying the writing of test doubles.

Under the hood, this is implemented using a thread-safe global logical clock that enables ordering interactions across multiple recorders. On verification, interactions are replayed in chronological order using a binary heap-based k-way merge iterator. Matching is based on recorder object identity and the existing matchers.

Mocks are still part of the framework, but I'm considering to remove them before we release this. Stubs haven't been extended yet.

I've had this idea in mind for quite a while, but with type-safety being one of my highest priorities, it took a while to come up with a nice solution. I think this is the future of Dobby and would appreciate collaborators and interested developers to have a look and provide feedback, thanks.

Felix Jendrusch added 7 commits January 4, 2017 11:24
- `TimestampRecording` has been renamed to `InteractionRecording`,
  which now provides access to more than just the timestamp. It also
  includes a textual representation of each interaction as well as the
  file and line of occurrence.
- `IntractionRecording` has been renamed to `ValueRecording`, providing
  chronological access to recorded interactions and their corresponding
  values.
- The global logical clock is now thread-safe.
- The recorder is now thread-safe.
If the expectation is negative and the mock is ordered (and nice),
attempt to fulfill the next expectation.

Fixes #38.
@felixvisee felixvisee changed the title Preview: Introducing Behavior Preview: Introducing patterns Mar 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant