Built with Fixrb
RSpec clone
The language of RSpec, in about 250 lines, on the libraries of Fixrb.
RSpec clone is a small test framework that speaks the language of RSpec: describe, context, it, its, let, subject, before, expect, is_expected and pending, with a few matchers, and nothing else. It is not a gem of Fixrb: it is another project of the author of Fixrb, Cyril Kato, and its home is its repository. It is here for the way it is made: spectus judges every expectation, matchi gives every matcher, and defi sends the method of each its: three of the libraries Fix 1.0 is built on. What was left to write, the language of RSpec, took about 250 lines.
- Version
- 2.0.0
- Ruby
- 3.3 or later, with
fork - Dependencies
- defi, matchi, spectus
- Code
- About 250 lines
- License
- MIT
Install
Add gem "r_spec-clone" to your Gemfile, or run gem install r_spec-clone, then require "r_spec".
In one file
A spec file requires r_spec, and its examples run as the file loads, in its order: there is no command to learn, since ruby runs the file. Here is a duck again, as on the home page, this time in the words of RSpec:
require "r_spec"
# A duck, in the words of RSpec.
class Duck
def walk = "Klop klop!"
def swim = "Swoosh..."
end
RSpec.describe Duck do
subject { described_class.new }
its(:walk) { is_expected.to eq "Klop klop!" }
its(:swim) { is_expected.to eq "Swoosh..." }
context "when asked to speak" do
its(:speak) { is_expected.to raise_exception NoMethodError }
end
it "walks before it swims" do
expect(subject.walk).to match(/Klop/)
expect { subject.swim }.not_to raise_exception StandardError
end
pending "flies"
end
ruby duck_spec.rb duck_spec.rb:12: Success: expected "Klop klop!" to eq "Klop klop!" duck_spec.rb:13: Success: expected "Swoosh..." to eq "Swoosh..." duck_spec.rb:16: Success: expected #<NoMethodError: undefined method 'speak' for an instance of Duck> to raise exception NoMethodError duck_spec.rb:20: Success: expected "Klop klop!" to match /Klop/ duck_spec.rb:21: Success: expected "Swoosh..." not to raise exception StandardError duck_spec.rb:24: Pending: flies
One line per expectation: where it is written, its outcome, and the summary of spectus. It is the report of Fix, since the judge is the same. The context ran in a child process; its sent speak inside the expectation, so that what it raised could be expected; pending wrote its line and ran nothing. Every expectation was met, so the file exits with 0. The first one that is not met ends the run, with the status 1: the rest of the file would run on a false premise.
What it leaves out
RSpec is a large framework, and a pleasure to work with every day. The clone borrows its language and leaves the rest out, on purpose: each thing left out is one thing less to know before reading a spec file.
- Nothing is monkey-patched, and nothing runs from an
at_exithook: norspeccommand, no filter, no random order, no formatter. letandsubjectare plain methods, called each time: nothing is memoized.beforeis the only hook: noafter, noaround.- A
contextruns in a child process: what its examples change in the Ruby process stays there. - The expected value is the one that receives the comparison: an actual value that lies about
==cannot pass. - The run stops at the first expectation that is not met.
The README lists every difference, and Coming from RSpec, in its wiki, what to write instead of what RSpec has. RSpec clone is an independent project, neither part of RSpec nor endorsed by its team, to whom it owes its language: thanks to them.
How it is built
An expectation of the clone is a few calls into the libraries. expect { … }.to(matcher) is Spectus.must(matcher), called with the block, and not_to is Spectus.must_not: every expectation is a MUST, the first of the three levels of Fix. eq, be_nil, match and the others each build a matcher of matchi. its hands its method to defi, which sends it to the subject inside the block. Without the clone, three expectations read:
require "defi"
require "matchi"
require "spectus"
# expect { "foo".upcase }.to eq "FOO": a matcher of Matchi, in a MUST of
# Spectus, called with the block.
upcase = Spectus.must(Matchi::Eq.new("FOO")).call { "foo".upcase }
upcase.to_s # => "Success: expected \"FOO\" to eq \"FOO\""
# expect("foo").not_to be_nil: a MUST NOT.
present = Spectus.must_not(Matchi::Be.new(nil)).call { "foo" }
present.to_s # => "Success: expected \"foo\" not to be nil"
# its(:size) { is_expected.to eq 3 }, on "foo": Defi sends the method inside
# the block, so that what it raises can be judged too.
size = Defi::Challenge.new(:size)
sized = Spectus.must(Matchi::Eq.new(3)).call { size.to("foo").call }
sized.to_s # => "Success: expected 3 to eq 3"
What the clone adds is the language. A group is a class, and a nested group a subclass of it; let defines a private method, before a module, and an example is an instance, where expect and the matchers are words; a context forks. The judging, the matching and the words of the report come from the libraries, where Fix uses them too: what is fixed in one is fixed for both. The clone was first built, in 2021, on test_tube and expresenter, two other libraries of Fixrb; its version 2.0 was built again, on the engine of Fix 1.0.
RSpec clone or Fix?
Both judge the same way; they differ in what a test file is. In the clone, as in RSpec, a file describes one subject: its examples name a class, build it and expect things of it, each one required. In Fix, a specification names no class: it is written once, tested against any subject, and says what is required, what is recommended and what is optional. Take the clone to keep the habits of RSpec in a small runner without surprises; take Fix when a behaviour deserves a document of its own.
They also work together. A specification of Fix is a matcher, through matchi-fix, and to takes any matcher; a let with a parameter gives it a word:
require "r_spec"
require "matchi/fix"
# What a duck does, as a specification of Fix.
Fix :Duck do
on :walk do
it MUST eq "Klop klop!"
end
on :speak do
it MAY eq "Quaaaaaack!"
end
end
Duck = Struct.new(:walk)
RSpec.describe "A specification of Fix, as a matcher" do
let(:fix) { |name| Matchi::Fix.new(name) }
it { expect(Duck.new("Klop klop!")).to fix(:Duck) }
it { expect(Duck.new("Clank!")).not_to fix(:Duck) }
end
ruby fix_spec.rb fix_spec.rb:20: Success: expected #<struct Duck walk="Klop klop!"> to fix Duck fix_spec.rb:21: Success: expected #<struct Duck walk="Clank!"> not to fix Duck
The specification says what a duck does; the spec file says which objects are ducks.
Documentation
- README: the language, the matchers, the report, and the changes in 2.0.
- Wiki: examples to run, each with what it writes, and a cheat sheet.
- API reference
- RubyGems
- Source code