Skip to content

Commit

Permalink
Merge pull request #125 from ydah/fix-test-1
Browse files Browse the repository at this point in the history
Improved testing for `Lrama::Command`
  • Loading branch information
yui-knk authored Oct 16, 2023
2 parents 32cea08 + b73d26d commit e9a224a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
38 changes: 17 additions & 21 deletions spec/lrama/command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
let(:outfile) { File.join(Dir.tmpdir, "parse.c") }
let(:o_option) { ["-o", "#{outfile}"] }

describe "a grammar file is specified" do
context "when grammar file is specified" do
it "ends successfully" do
command = Lrama::Command.new
expect(command.run(o_option + [fixture_path("command/basic.y")])).to be_nil
end
end

describe "STDIN mode and a grammar file is specified" do
context "when STDIN mode and a grammar file is specified" do
it "ends successfully" do
File.open(fixture_path("command/basic.y")) do |f|
allow(STDIN).to receive(:read).and_return(f.read)
Expand All @@ -22,27 +22,23 @@
end
end

describe "invalid argv" do
describe "a grammar file isn't specified" do
it "returns stderr" do
command = Lrama::Command.new
message = "File should be specified\n"
allow(STDERR).to receive(:write).with(message)
expect{ command.run([]) }.to raise_error(SystemExit) do |e|
expect(e.message).to eq(message)
end
end
context "when `--trace=time` option specified" do
it "called Report::Duration.enable" do
allow(Lrama::Report::Duration).to receive(:enable)
command = Lrama::Command.new
expect(command.run(o_option + [fixture_path("command/basic.y"), "--trace=time"])).to be_nil
expect(Lrama::Report::Duration).to have_received(:enable).once
end
end

describe "STDIN mode, but a grammar file isn't specified" do
it "returns stderr" do
command = Lrama::Command.new
message = "File name for STDIN should be specified\n"
allow(STDERR).to receive(:write).with(message)
expect{ command.run(["-"]) }.to raise_error(SystemExit) do |e|
expect(e.message).to eq(message)
end
end
context "when `--report-file` option specified" do
it "create report file" do
allow(File).to receive(:open).and_call_original
command = Lrama::Command.new
expect(command.run(o_option + [fixture_path("command/basic.y"), "--report-file=report.output"])).to be_nil
expect(File).to have_received(:open).with("report.output", "w+").once
expect(File.exist?("report.output")).to be_truthy
File.delete("report.output")
end
end
end
Expand Down
20 changes: 20 additions & 0 deletions spec/lrama/option_parser_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
require "open3"

RSpec.describe Lrama::OptionParser do
describe "invalid argv" do
context "when grammar file isn't specified" do
it "returns stderr" do
result = Open3.popen3("ruby", exe_path("lrama")) do |stdin, stdout, stderr, wait_thr|
stderr.read
end
expect(result).to eq("File should be specified\n")
end
end

context "when STDIN mode, but a grammar file isn't specified" do
it "returns stderr" do
result = Open3.popen3("ruby", exe_path("lrama"), "-") do |stdin, stdout, stderr, wait_thr|
stderr.read
end
expect(result).to eq("File name for STDIN should be specified\n")
end
end
end

describe "version option" do
it "print Lrama version and exit" do
result = Open3.popen3("ruby", exe_path("lrama"), "--version") do |stdin, stdout, stderr, wait_thr|
Expand Down

0 comments on commit e9a224a

Please sign in to comment.