Building an Interpreter for Propositional Logic: An interlude with minitest/autorun

When we left our interpreter for propositional logic, we had a hand-rolled run_tests method which was calling our own assert_interpret_equals method. However, after looking at the Rails’ Guides bug template scripts, I was inspired to make our code an executable test script with minitest/autorun.

Here, we won’t change any of the logic itself, just update our single-file script to be executable using the ruby command which will output minitest results.

I will confess that the change was pretty simple, so I won’t walk through it. There are only two key changes. The first is to ensure our dependencies are setup at the start of the file using bundler/inline:

require "bundler/inline"
 
gemfile(true) do
source "https://rubygems.org"
 
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
 
gem "minitest"
end
 
require "minitest/autorun"

Next, we simply migrate our run_tests method and assert_interpret_equals into a Minitest::Test class with assert_equals calls, e.g.:

class LogicInterpreterTest < Minitest::Test
def test_tokens
assert_equal true, interpret('T')
assert_equal false, interpret('F')
end
 
# ...
end

That’s it! With our script updated, we can use it like so:

$ ruby logical.rb
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies...
Using bundler 2.4.8
Using minitest 5.19.0
Run options: --seed 16731
 
# Running:
 
.........
 
Finished in 0.001000s, 9000.0006 runs/s, 29000.0020 assertions/s.
 
9 runs, 29 assertions, 0 failures, 0 errors, 0 skips

I really like being able to use minitest and move beyond our hand-rolled test harness.

You can find the script we have built to this point in this revision of this Gist


All posts in this series