spec/rley/formatter/debug_spec.rb in rley-0.6.07 vs spec/rley/formatter/debug_spec.rb in rley-0.6.08
- old
+ new
@@ -1,40 +1,50 @@
require_relative '../../spec_helper'
require 'stringio'
-require_relative '../support/grammar_abc_helper'
require_relative '../../../lib/rley/lexical/token'
-require_relative '../../../lib/rley/parser/gfg_earley_parser'
-require_relative '../../../lib/rley/ptree/parse_tree'
-require_relative '../../../lib/rley/parse_tree_visitor'
require_relative '../../../lib/rley/engine'
+require_relative '../support/grammar_abc_helper'
+require_relative '../support/grammar_sppf_helper'
+
# Load the class under test
require_relative '../../../lib/rley/formatter/debug'
module Rley # Re-open the module to get rid of qualified names
module Formatter
describe Debug do
- # Factory method. Build a production with the given sequence
+ include GrammarSPPFHelper # Mix-in module with builder for grammar sppf
+
+ # Factory method. Build a production with the given sequence
# of symbols as its rhs.
let(:grammar_abc) do
sandbox = Object.new
sandbox.extend(GrammarABCHelper)
builder = sandbox.grammar_abc_builder
builder.grammar
end
-
+
# Variables for the terminal symbols
let(:a_) { grammar_abc.name2symbol['a'] }
let(:b_) { grammar_abc.name2symbol['b'] }
let(:c_) { grammar_abc.name2symbol['c'] }
-
+
# Helper method that mimicks the output of a tokenizer
# for the language specified by grammar_abc
let(:grm_abc_tokens1) do
%w[a a b c c].map { |ch| Lexical::Token.new(ch, ch) }
end
+ let(:grammar_sppf) do
+ builder = grammar_sppf_builder
+ builder.grammar
+ end
+
+ let(:sample_tokens) do
+ %w[a b b b].map { |ch| Lexical::Token.new(ch, ch) }
+ end
+
# Factory method that builds a sample parse tree.
# Generated tree has the following structure:
# S[0,5]
# +- A[0,5]
# +- a[0,0]
@@ -45,34 +55,47 @@
# | +- c[3,4]
# +- c[4,5]
# Capital letters represent non-terminal nodes
let(:grm_abc_ptree1) do
engine = Rley::Engine.new
- engine.use_grammar(grammar_abc)
+ engine.use_grammar(grammar_abc)
parse_result = engine.parse(grm_abc_tokens1)
ptree = engine.convert(parse_result)
ptree
end
-
+
+ # Factory method that builds a sample parse forest.
+ # Generated forest has the following structure:
+ let(:grm_sppf_pforest1) do
+ engine = Rley::Engine.new
+ engine.use_grammar(grammar_sppf)
+ parse_result = engine.parse(sample_tokens)
+ engine.to_pforest(parse_result)
+ end
+
let(:destination) { StringIO.new('', 'w') }
context 'Standard creation & initialization:' do
it 'should be initialized with an IO argument' do
expect { Debug.new(StringIO.new('', 'w')) }.not_to raise_error
end
-
+
it 'should know its output destination' do
instance = Debug.new(destination)
expect(instance.output).to eq(destination)
end
+
+ it 'should have a zero indentation' do
+ instance = Debug.new(destination)
+ expect(instance.indentation).to be_zero
+ end
end # context
-
-
- context 'Formatting events:' do
+ context 'Formatting events:' do
it 'should support visit events of a parse tree' do
instance = Debug.new(destination)
+ expect(instance.output).to eq(destination)
visitor = Rley::ParseTreeVisitor.new(grm_abc_ptree1)
instance.render(visitor)
expectations = <<-SNIPPET
before_ptree
before_non_terminal
@@ -100,9 +123,99 @@
after_subnodes
after_non_terminal
after_subnodes
after_non_terminal
after_ptree
+SNIPPET
+ expect(destination.string).to eq(expectations)
+ end
+
+ it 'should support visit events of a parse forest' do
+ instance = Debug.new(destination)
+ expect(instance.output).to eq(destination)
+ visitor = Rley::ParseForestVisitor.new(grm_sppf_pforest1)
+ instance.render(visitor)
+ expectations = <<-SNIPPET
+before_pforest
+ before_non_terminal
+ before_subnodes
+ before_non_terminal
+ before_subnodes
+ before_alternative
+ before_subnodes
+ before_terminal
+ after_terminal
+ before_non_terminal
+ before_subnodes
+ before_terminal
+ after_terminal
+ before_terminal
+ after_terminal
+ before_terminal
+ after_terminal
+ after_subnodes
+ after_non_terminal
+ after_subnodes
+ after_alternative
+ before_alternative
+ before_subnodes
+ before_non_terminal
+ before_subnodes
+ before_alternative
+ before_subnodes
+ before_terminal
+ after_terminal
+ after_subnodes
+ after_alternative
+ before_alternative
+ before_subnodes
+ before_non_terminal
+ before_subnodes
+ before_epsilon
+ after_epsilon
+ after_subnodes
+ after_non_terminal
+ before_non_terminal
+ before_subnodes
+ before_alternative
+ before_subnodes
+ before_terminal
+ after_terminal
+ after_subnodes
+ after_alternative
+ before_alternative
+ before_subnodes
+ before_non_terminal
+ before_subnodes
+ before_epsilon
+ after_epsilon
+ after_subnodes
+ after_non_terminal
+ after_subnodes
+ after_alternative
+ after_subnodes
+ after_non_terminal
+ after_subnodes
+ after_alternative
+ after_subnodes
+ after_non_terminal
+ before_non_terminal
+ before_subnodes
+ before_terminal
+ after_terminal
+ before_terminal
+ after_terminal
+ before_terminal
+ after_terminal
+ after_subnodes
+ after_non_terminal
+ after_subnodes
+ after_alternative
+ after_subnodes
+ after_non_terminal
+ after_subnodes
+ after_non_terminal
+after_pforest
SNIPPET
expect(destination.string).to eq(expectations)
end
end # context
end # describe