Hegel for Ruby
[!IMPORTANT]
This is an unofficial, third-party implementation. The Hegel project and
the libhegel engine belong to Antithesis, LLC. This repository has no
affiliation with Antithesis or with the hegeldev organization, and nobody
there reviews or endorses it.
The official implementations are
Rust,
Go,
C++,
TypeScript,
Java, and
OCaml. Report problems with this
gem here, not to them.
[!NOTE]
Hegel itself is in beta, and its maintainers expect to make breaking changes.
See https://hegel.dev/compatibility.
Hegel is a property-based testing framework based on
Hypothesis. Instead of writing
tests with hand-picked inputs, you state a property that must hold for every
input. Hegel generates inputs, tries to falsify the property, and shrinks any
failure to a minimal counterexample.
This gem drives libhegel, the native
engine that the official implementations also drive. The engine runs in the
same process, through the ffi gem. There is no
server and no Python dependency.
Installation
The gem is published as hegeltest:
# Gemfile
gem "hegeltest", group: :test
The require path is hegel, and the namespace is Hegel.
require "hegeltest" also works, so Bundler's automatic require finds it.
The gem needs Ruby 3.4 or later. Each platform gem carries the matching
libhegel build, so an install compiles nothing and needs no engine on the
side:
| Platform | Gem platform |
|---|
| Linux x64 | x86_64-linux |
| Linux arm64 | aarch64-linux |
| macOS arm64 | arm64-darwin |
| Windows x64 | x64-mingw-ucrt |
| Windows arm64 | aarch64-mingw-ucrt |
The Hegel maintainers permit a third-party project to redistribute their
release binaries (hegeldev/hegel-rust#411).
A project that already has a Gemfile.lock needs one more step. Bundler adds
only the platform of the machine that runs bundle install, so a lock made on
a Mac does not name Linux. A Linux CI job then cannot find the gem. Add each
platform your CI uses:
bundle lock --add-platform x86_64-linux
On a platform with no published libhegel build, such as macOS on Intel, build
the engine yourself and set HEGEL_LIBHEGEL_PATH to the library file or to the
directory that holds it. The same variable makes any platform use a local
engine build in place of the bundled one.
Which libhegel a release carries
Each gem release pins one libhegel release. To see the pinned version in your
installed gem:
ruby -rhegel -e 'puts Hegel::LIBHEGEL_VERSION'
The CHANGELOG section of each gem release starts with the
engine move, when there is one, and links the release notes of that engine
version. The GitHub releases
show the same text.
When libhegel publishes a new release, a pull request moves this gem to it.
The gem version and the engine version are independent: a gem release can
change the engine, the Ruby code, or both.
Quickstart
# spec/spec_helper.rb
require "hegel"
RSpec.configure do |config|
config.include Hegel::Syntax::Methods
end
# spec/my_sort_spec.rb
def my_sort(ls) = ls.sort.uniq # oops: uniq drops duplicates
RSpec.describe "my_sort" do
it "matches the builtin sort" do
Hegel.test do |tc|
xs = tc.draw(arrays(integers))
expect(my_sort(xs)).to eq(xs.sort)
end
end
end
That test fails, and Hegel shrinks the failure to the smallest input that
still shows the bug. The report looks like this:
Falsified after 11 test cases (0 discarded):
xs = [0, 0]
To reproduce this failure, pass the blob below to Hegel.test:
reproduce_failure: "AXicY2VgYGBkZOBiZEBhMAAAAd8AIQ=="
The count of test cases and the blob change from run to run, and between
engine releases. The shrunk value is the part to read: two duplicates are all
it takes. Hegel names the value xs by reading the line the draw was written
on. Then it re-raises RSpec's own expectation failure, so RSpec reports it as
its own.
Including Hegel::Syntax::Methods makes the generators available without a
prefix, the way FactoryBot makes create available. The same generators are
also available as Hegel::Generators.arrays(...) without the include.
To keep the generator names out of example groups that have their own
helpers, include the methods only in tagged groups:
config.include Hegel::Syntax::Methods, :hegel