spec/rley/parser/dotted_item_spec.rb in rley-0.0.18 vs spec/rley/parser/dotted_item_spec.rb in rley-0.1.00

- old
+ new

@@ -14,11 +14,11 @@ # Factory method. Builds a production with given left-hand side (LHS) # and given RHS (right-hand side) def build_prod(theLHS, *theRHSSymbols) return Syntax::Production.new(theLHS, theRHSSymbols) end - + let(:t_a) { Syntax::Terminal.new('A') } let(:t_b) { Syntax::Terminal.new('B') } let(:t_c) { Syntax::Terminal.new('C') } let(:nt_sentence) { Syntax::NonTerminal.new('sentence') } let(:sample_prod) { build_prod(nt_sentence, t_a, t_b, t_c) } @@ -41,11 +41,11 @@ end it 'should know its production' do expect(subject.production).to eq(sample_prod) end - + it 'should know the lhs of the production' do expect(subject.lhs).to eq(sample_prod.lhs) end it 'should know its position' do @@ -69,15 +69,15 @@ end # context context 'Provided service:' do it 'should whether its dot is at start position' do expect(subject).not_to be_at_start - + # At start position instance1 = DottedItem.new(sample_prod, 0) expect(instance1).to be_at_start - + # At start/end at the same time (production is empty) instance2 = DottedItem.new(build_prod(nt_sentence), 0) expect(instance2).to be_at_start end @@ -89,13 +89,41 @@ second_instance = DottedItem.new(empty_prod, 0) expect(second_instance).to be_reduce_item end + it 'should know the symbol before the dot' do + expect(subject.prev_symbol).to eq(t_a) + + # Case of an empty production + instance = DottedItem.new(empty_prod, 0) + expect(instance.prev_symbol).to be_nil + + # Case of a dot at start position + instance = DottedItem.new(sample_prod, 0) + expect(instance.prev_symbol).to be_nil + end + it 'should know the symbol after the dot' do expect(subject.next_symbol).to eq(t_b) end + it 'should calculate the previous position of the dot' do + expect(subject.prev_position).to eq(0) + + # Case of an empty production + instance = DottedItem.new(empty_prod, 0) + expect(instance.prev_position).to be_nil + + # Case of a dot at start position + instance = DottedItem.new(sample_prod, 0) + expect(instance.prev_position).to be_nil + + # Case of single symbol production + instance = DottedItem.new(other_prod, 1) + expect(instance.prev_position).to eq(0) + end + it 'should give its text representation' do expectation = 'sentence => A . B C' expect(subject.to_s).to eq(expectation) end end