spec/front_end/parser_spec.rb in loxxy-0.0.9 vs spec/front_end/parser_spec.rb in loxxy-0.0.10
- old
+ new
@@ -253,12 +253,29 @@
expect(expr.operands[1].literal.value).to eq('ve')
end
end # context
context 'Parsing comparison expressions' do
- it 'should parse the comparison of two number literals' do
- %w[> >= < <=].each do |predicate|
+ it 'should parse the comparison of two number literals' do
+ %w[> >= < <=].each do |predicate|
+ input = "3 #{predicate} 2;"
+ ptree = subject.parse(input)
+ parent = ptree.root.subnodes[0]
+ expect(parent).to be_kind_of(Rley::PTree::NonTerminalNode)
+ expect(parent.symbol.name).to eq('exprStmt')
+ expr = parent.subnodes[0]
+ expect(expr).to be_kind_of(Ast::LoxBinaryExpr)
+ expect(expr.operator).to eq(predicate.to_sym)
+ expect(expr.operands[0].literal.value).to eq(3)
+ expect(expr.operands[1].literal.value).to eq(2)
+ end
+ end
+ end # context
+
+ context 'Parsing equality expressions' do
+ it 'should parse the equality of two number literals' do
+ %w[!= ==].each do |predicate|
input = "3 #{predicate} 2;"
ptree = subject.parse(input)
parent = ptree.root.subnodes[0]
expect(parent).to be_kind_of(Rley::PTree::NonTerminalNode)
expect(parent.symbol.name).to eq('exprStmt')
@@ -266,9 +283,25 @@
expect(expr).to be_kind_of(Ast::LoxBinaryExpr)
expect(expr.operator).to eq(predicate.to_sym)
expect(expr.operands[0].literal.value).to eq(3)
expect(expr.operands[1].literal.value).to eq(2)
end
+ end
+
+ it 'should parse combination of equality expressions' do
+ input = '5 != 2 == false; // A bit contrived example'
+ ptree = subject.parse(input)
+ parent = ptree.root.subnodes[0]
+ expect(parent).to be_kind_of(Rley::PTree::NonTerminalNode)
+ expect(parent.symbol.name).to eq('exprStmt')
+ expr = parent.subnodes[0]
+ expect(expr).to be_kind_of(Ast::LoxBinaryExpr)
+ expect(expr.operator).to eq(:==)
+ expect(expr.operands[0]).to be_kind_of(Ast::LoxBinaryExpr)
+ expect(expr.operands[0].operator).to eq(:!=)
+ expect(expr.operands[0].operands[0].literal.value).to eq(5)
+ expect(expr.operands[0].operands[1].literal.value).to eq(2)
+ expect(expr.operands[1].literal.value).to be_falsey
end
end # context
end # describe
end # module
end # module