run lox tests from the book's repo, inspired by @hparker
FossilOrigin-Name: f477507f650934b9117cce181e4cdc50dda31c1126651319132feb7c768a1a93
diff --git a/ruby/test/test_helper.rb b/ruby/test/test_helper.rb
index 2b59721..4ed7277 100644
--- a/ruby/test/test_helper.rb
+++ b/ruby/test/test_helper.rb
@@ -2,5 +2,37 @@ require "minitest"
module Lox
class Test < Minitest::Test
+ LOX_BIN = File.expand_path("../bin/lox", __dir__)
+
+ def assert_lox(path)
+ src = File.read(path)
+
+ expected_out = src.scan(/(?<=\/\/ expect: )(?~\n)/).join("\n")
+
+ out, _err, _status = Open3.capture3(LOX_BIN, path)
+
+ assert_equal expected_out, out
+ end
+ end
+
+ book_src = File.expand_path(ENV.fetch("CRAFTING_INTERPRETERS_SRC"))
+ Dir.chdir(book_src) do
+ lox_tests = Dir["./test/**/*.lox"]
+ .group_by {|path| path.rpartition(?/).first.sub(/\.\/test\/?/, "") }
+
+ lox_tests.each do |dir, paths|
+ klass = Class.new(Test) do
+ paths.each do |path|
+ name = File.basename(path, ".lox")
+ define_method("test_#{name}") do
+ assert_lox File.expand_path(path, book_src)
+ end
+ end
+ end
+
+ suite = dir.split(?/).map(&:capitalize).join
+ suite = "Root" if suite.empty?
+ Object.const_set("Test#{suite}", klass)
+ end
end
end
diff --git a/ruby/test/test_lox.rb b/ruby/test/test_lox.rb
index 21b62d6..afdff93 100644
--- a/ruby/test/test_lox.rb
+++ b/ruby/test/test_lox.rb
@@ -34,4 +34,3 @@ class TestRunner < Lox::Test
runner.run("src")
end
end
-