organize files into bin, lib, and test dirs

FossilOrigin-Name: a207bd9de54f2c13c832a51c766d663b04e2904e72c178756ebba72a0c15a289
private
alpha 2 years ago
parent 2e4377b600
commit 3cd5ba6587

@ -1,6 +1,4 @@
require "minitest/test_task"
Minitest::TestTask.create do |t|
t.test_globs = "./test_*.rb"
end
Minitest::TestTask.create
task default: :test

@ -0,0 +1,40 @@
#!/usr/bin/env ruby -w
$LOAD_PATH.unshift(File.expand_path("../lib", __dir__))
require "lox"
def run_prompt
loop do
print "> "
line = gets
break if line.nil? || line.empty?
begin
run(line)
rescue Lox::Error => e
puts e.message
end
end
end
def run_file(io)
run(io.read)
rescue Lox::Error => e
puts e.message
exit 65
end
def run(src)
Lox::Runner.new.run(src)
end
if __FILE__ == $0
puts "Usage: #$0 [script]" or exit 64 if ARGV.length > 1
if ARGV.empty?
run_prompt
else
run_file(ARGF)
end
end

@ -13,30 +13,6 @@ module Lox
end
end
def self.run_prompt
loop do
print "> "
line = gets
break if line.empty?
begin
run(line)
rescue Error => e
puts e.message
end
end
end
def self.run_file(io)
run(io.read)
rescue Error
puts e.message
exit 65
end
def self.run(src)
Runner.new.run(src)
end
def self.error(line, msg)
raise Error(line:, message:)
end
@ -229,13 +205,3 @@ module Lox
end
end
end
if __FILE__ == $0
puts "Usage: #$0 [script]" or exit 64 if ARGV.length > 1
if ARGV.empty?
Lox.run_prompt
else
Lox.run_file(ARGF)
end
end

@ -9,9 +9,10 @@ require "pry"
class TestLox < Minitest::Test
def test_error_on_more_than_one_arg
o, s = Open3.capture2("./lox.rb foo bar")
lox_path = File.expand_path("../bin/lox", __dir__)
o, s = Open3.capture2(lox_path, "foo", "bar")
assert_equal 64, s.exitstatus
assert_equal "Usage: ./lox.rb [script]\n", o
assert_equal "Usage: #{lox_path} [script]\n", o
end
end
Loading…
Cancel
Save