Capture and verify stdout results from tests

This commit is contained in:
Josh Holtrop 2022-10-02 10:31:07 -04:00
parent 01ef4fc27c
commit 43fb74fe4b
3 changed files with 57 additions and 7 deletions

View File

@ -1,4 +1,5 @@
require "fileutils" require "fileutils"
require "open3"
describe Propane do describe Propane do
def write_grammar(grammar) def write_grammar(grammar)
@ -15,9 +16,35 @@ describe Propane do
expect(result).to be_truthy expect(result).to be_truthy
end end
Results = Struct.new(:stdout, :stderr, :status)
def run def run
result = system("spec/run/testparser") stdout, stderr, status = Open3.capture3("spec/run/testparser")
expect(result).to be_truthy Results.new(stdout, stderr, status)
end
def lines(str)
str.lines.map(&:chomp)
end
def verify_lines(lines, patterns)
if lines.is_a?(String)
lines = lines.lines.map(&:chomp)
end
patterns.each_with_index do |pattern, i|
found_index =
if pattern.is_a?(Regexp)
lines.find_index {|line| line =~ pattern}
else
lines.find_index do |line|
line.chomp == pattern.chomp
end
end
unless found_index
$stderr.puts "Lines:"
$stderr.puts lines
raise "A line matching #{pattern.inspect} (index #{i}) was not found."
end
end
end end
before(:each) do before(:each) do
@ -39,7 +66,8 @@ Foo -> plus <<
EOF EOF
build_parser build_parser
compile("spec/test_d_lexer.d") compile("spec/test_d_lexer.d")
run results = run
expect(results.status).to eq 0
end end
it "generates a parser" do it "generates a parser" do
@ -79,7 +107,8 @@ R2 -> a b;
EOF EOF
build_parser build_parser
compile("spec/test_d_parser_identical_rules_lookahead.d") compile("spec/test_d_parser_identical_rules_lookahead.d")
run results = run
expect(results.status).to eq 0
end end
it "handles reducing a rule that could be arrived at from multiple states" do it "handles reducing a rule that could be arrived at from multiple states" do
@ -93,7 +122,8 @@ R1 -> b;
EOF EOF
build_parser build_parser
compile("spec/test_d_parser_rule_from_multiple_states.d") compile("spec/test_d_parser_rule_from_multiple_states.d")
run results = run
expect(results.status).to eq 0
end end
it "executes user code when matching lexer token" do it "executes user code when matching lexer token" do
@ -108,7 +138,15 @@ Abcs -> abc Abcs;
EOF EOF
build_parser build_parser
compile("spec/test_user_code.d") compile("spec/test_user_code.d")
run results = run
expect(results.status).to eq 0
verify_lines(results.stdout, [
"abc!",
"pass1",
"abc!",
"abc!",
"pass2",
])
end end
it "supports a pattern statement" do it "supports a pattern statement" do
@ -121,6 +159,14 @@ Start -> abc;
EOF EOF
build_parser build_parser
compile("spec/test_pattern.d") compile("spec/test_pattern.d")
run results = run
expect(results.status).to eq 0
verify_lines(results.stdout, [
"def!",
"pass1",
"def!",
"def!",
"pass2",
])
end end
end end

View File

@ -11,8 +11,10 @@ unittest
string input = "abcdef"; string input = "abcdef";
auto parser = new Testparser.Parser(cast(const(ubyte) *)input.ptr, input.length); auto parser = new Testparser.Parser(cast(const(ubyte) *)input.ptr, input.length);
assert(parser.parse() == true); assert(parser.parse() == true);
writeln("pass1");
input = "defabcdef"; input = "defabcdef";
parser = new Testparser.Parser(cast(const(ubyte) *)input.ptr, input.length); parser = new Testparser.Parser(cast(const(ubyte) *)input.ptr, input.length);
assert(parser.parse() == true); assert(parser.parse() == true);
writeln("pass2");
} }

View File

@ -11,8 +11,10 @@ unittest
string input = "abcdef"; string input = "abcdef";
auto parser = new Testparser.Parser(cast(const(ubyte) *)input.ptr, input.length); auto parser = new Testparser.Parser(cast(const(ubyte) *)input.ptr, input.length);
assert(parser.parse() == true); assert(parser.parse() == true);
writeln("pass1");
input = "abcabcdef"; input = "abcabcdef";
parser = new Testparser.Parser(cast(const(ubyte) *)input.ptr, input.length); parser = new Testparser.Parser(cast(const(ubyte) *)input.ptr, input.length);
assert(parser.parse() == true); assert(parser.parse() == true);
writeln("pass2");
} }