Add spec for lexer and change names of spec files
This commit is contained in:
parent
4d6af6422a
commit
d02ecf53b0
29 changed files with 172 additions and 48 deletions
|
@ -20,7 +20,7 @@ module Mm2ep
|
|||
vars.each do |var|
|
||||
tab_vars[var.split('=')[0]] = var.split('=')[1]
|
||||
end
|
||||
|
||||
binding.pry
|
||||
# Give vars name and value from shell command to parser
|
||||
parser.names=tab_vars
|
||||
|
||||
|
@ -30,7 +30,7 @@ module Mm2ep
|
|||
puts "EVAL: #{token.to_s}"
|
||||
|
||||
parser.check_grammar line, token
|
||||
exit 1 unless !token.nil? && token.errors.empty?
|
||||
exit 1 unless !token.nil? && token.errors.empty?
|
||||
|
||||
puts "RESULT: #{token.compute}"
|
||||
|
||||
|
|
1
spec/files/error_lexer_illegal_character.txt
Normal file
1
spec/files/error_lexer_illegal_character.txt
Normal file
|
@ -0,0 +1 @@
|
|||
?
|
1
spec/files/success_lexer_and_op.txt
Normal file
1
spec/files/success_lexer_and_op.txt
Normal file
|
@ -0,0 +1 @@
|
|||
AND
|
1
spec/files/success_lexer_eq_op.txt
Normal file
1
spec/files/success_lexer_eq_op.txt
Normal file
|
@ -0,0 +1 @@
|
|||
=
|
1
spec/files/success_lexer_f_bool.txt
Normal file
1
spec/files/success_lexer_f_bool.txt
Normal file
|
@ -0,0 +1 @@
|
|||
false False
|
1
spec/files/success_lexer_l_par.txt
Normal file
1
spec/files/success_lexer_l_par.txt
Normal file
|
@ -0,0 +1 @@
|
|||
(
|
1
spec/files/success_lexer_not_op.txt
Normal file
1
spec/files/success_lexer_not_op.txt
Normal file
|
@ -0,0 +1 @@
|
|||
NOT
|
1
spec/files/success_lexer_number.txt
Normal file
1
spec/files/success_lexer_number.txt
Normal file
|
@ -0,0 +1 @@
|
|||
7
|
1
spec/files/success_lexer_or_op.txt
Normal file
1
spec/files/success_lexer_or_op.txt
Normal file
|
@ -0,0 +1 @@
|
|||
OR
|
1
spec/files/success_lexer_r_par.txt
Normal file
1
spec/files/success_lexer_r_par.txt
Normal file
|
@ -0,0 +1 @@
|
|||
)
|
1
spec/files/success_lexer_string.txt
Normal file
1
spec/files/success_lexer_string.txt
Normal file
|
@ -0,0 +1 @@
|
|||
"Arya Stark"
|
1
spec/files/success_lexer_t_bool.txt
Normal file
1
spec/files/success_lexer_t_bool.txt
Normal file
|
@ -0,0 +1 @@
|
|||
true True
|
1
spec/files/success_lexer_var.txt
Normal file
1
spec/files/success_lexer_var.txt
Normal file
|
@ -0,0 +1 @@
|
|||
character
|
94
spec/mm2ep_depend/lexer_spec.rb
Normal file
94
spec/mm2ep_depend/lexer_spec.rb
Normal file
|
@ -0,0 +1,94 @@
|
|||
require 'spec_helper'
|
||||
require 'mm2ep_depend'
|
||||
|
||||
describe Mm2ep::Depend::Lexer do
|
||||
let(:lexer) do
|
||||
Mm2ep::Depend::Lexer.new
|
||||
end
|
||||
|
||||
it 'has to recognize AND operator' do
|
||||
line = File
|
||||
.read(testfile('success_lexer_and_op.txt')).delete("\n")
|
||||
lexer.input(line.chomp)
|
||||
assert_equal('AND_OP', lexer.next.type.to_s)
|
||||
end
|
||||
|
||||
it 'has to recognize EQ operator' do
|
||||
line = File
|
||||
.read(testfile('success_lexer_eq_op.txt')).delete("\n")
|
||||
lexer.input(line.chomp)
|
||||
assert_equal('EQ_OP', lexer.next.type.to_s)
|
||||
end
|
||||
|
||||
it 'has to recognize OR operator' do
|
||||
line = File
|
||||
.read(testfile('success_lexer_or_op.txt')).delete("\n")
|
||||
lexer.input(line.chomp)
|
||||
assert_equal('OR_OP', lexer.next.type.to_s)
|
||||
end
|
||||
|
||||
it 'has to recognize NOT operator' do
|
||||
line = File
|
||||
.read(testfile('success_lexer_not_op.txt')).delete("\n")
|
||||
lexer.input(line.chomp)
|
||||
assert_equal('NOT_OP', lexer.next.type.to_s)
|
||||
end
|
||||
|
||||
it 'has to recognize false boolean' do
|
||||
line = File
|
||||
.read(testfile('success_lexer_f_bool.txt')).delete("\n")
|
||||
lexer.input(line.chomp)
|
||||
assert_equal('F_BOOL', lexer.next.type.to_s)
|
||||
assert_equal('F_BOOL', lexer.next.type.to_s)
|
||||
end
|
||||
|
||||
it 'has to recognize true boolean' do
|
||||
line = File
|
||||
.read(testfile('success_lexer_t_bool.txt')).delete("\n")
|
||||
lexer.input(line.chomp)
|
||||
assert_equal('T_BOOL', lexer.next.type.to_s)
|
||||
assert_equal('T_BOOL', lexer.next.type.to_s)
|
||||
end
|
||||
|
||||
it 'has to recognize left parenthesis' do
|
||||
line = File
|
||||
.read(testfile('success_lexer_l_par.txt')).delete("\n")
|
||||
lexer.input(line.chomp)
|
||||
assert_equal('L_PAR', lexer.next.type.to_s)
|
||||
end
|
||||
|
||||
it 'has to recognize right parenthesis' do
|
||||
line = File
|
||||
.read(testfile('success_lexer_r_par.txt')).delete("\n")
|
||||
lexer.input(line.chomp)
|
||||
assert_equal('R_PAR', lexer.next.type.to_s)
|
||||
end
|
||||
|
||||
it 'has to recognize number' do
|
||||
line = File
|
||||
.read(testfile('success_lexer_number.txt')).delete("\n")
|
||||
lexer.input(line.chomp)
|
||||
assert_equal('NUMBER', lexer.next.type.to_s)
|
||||
end
|
||||
|
||||
it 'has to recognize string' do
|
||||
line = File
|
||||
.read(testfile('success_lexer_string.txt')).delete("\n")
|
||||
lexer.input(line.chomp)
|
||||
assert_equal('STRING', lexer.next.type.to_s)
|
||||
end
|
||||
|
||||
it 'has to recognize var' do
|
||||
line = File
|
||||
.read(testfile('success_lexer_var.txt')).delete("\n")
|
||||
lexer.input(line.chomp)
|
||||
assert_equal('VAR', lexer.next.type.to_s)
|
||||
end
|
||||
|
||||
it 'has to recognize illegal character and replace them with erase them' do
|
||||
line = File
|
||||
.read(testfile('error_lexer_illegal_character.txt')).delete("\n")
|
||||
lexer.input(line.chomp)
|
||||
assert_equal('', lexer.next.to_s)
|
||||
end
|
||||
end
|
|
@ -2,9 +2,15 @@ require 'spec_helper'
|
|||
require 'mm2ep_depend'
|
||||
|
||||
describe Mm2ep::Depend::Parser do
|
||||
let(:parser) do
|
||||
Mm2ep::Depend::Parser.new(
|
||||
Mm2ep::Depend::Lexer.new
|
||||
)
|
||||
end
|
||||
|
||||
it 'has to report var which is not defined' do
|
||||
line = File.read(testfile('simple_eq_expr_boolexpr.txt')).delete("\n")
|
||||
parser = Mm2ep::Depend::Parser.new(Mm2ep::Depend::Lexer.new)
|
||||
line = File
|
||||
.read(testfile('success_simple_eq_expr_boolexpr.txt')).delete("\n")
|
||||
parser.names = {}
|
||||
token = parser.parse(line.chomp)
|
||||
token.errors
|
||||
|
@ -14,8 +20,8 @@ describe Mm2ep::Depend::Parser do
|
|||
end
|
||||
|
||||
it 'has to report vars which are not defined' do
|
||||
line = File.read(testfile('simple_expr_or_expr.txt')).delete("\n")
|
||||
parser = Mm2ep::Depend::Parser.new(Mm2ep::Depend::Lexer.new)
|
||||
line = File
|
||||
.read(testfile('success_simple_expr_or_expr.txt')).delete("\n")
|
||||
parser.names = {}
|
||||
token = parser.parse(line.chomp)
|
||||
token.errors
|
||||
|
@ -25,8 +31,8 @@ describe Mm2ep::Depend::Parser do
|
|||
end
|
||||
|
||||
it 'has to report invalid_grammar' do
|
||||
line = File.read(testfile('grammar_partially_invalid.txt')).delete("\n")
|
||||
parser = Mm2ep::Depend::Parser.new(Mm2ep::Depend::Lexer.new)
|
||||
line = File
|
||||
.read(testfile('error_grammar_partially_invalid.txt')).delete("\n")
|
||||
parser.names = { 'a_girl_has_no_name' => true, 'character' => 'Arya Stark' }
|
||||
token = parser.parse(line.chomp)
|
||||
parser.check_grammar line, token
|
||||
|
@ -36,8 +42,8 @@ describe Mm2ep::Depend::Parser do
|
|||
end
|
||||
|
||||
it 'has to be nil when grammar is completely invalid' do
|
||||
line = File.read(testfile('grammar_completely_invalid.txt')).delete("\n")
|
||||
parser = Mm2ep::Depend::Parser.new(Mm2ep::Depend::Lexer.new)
|
||||
line = File
|
||||
.read(testfile('error_grammar_completely_invalid.txt')).delete("\n")
|
||||
parser.names = {}
|
||||
token = parser.parse(line.chomp)
|
||||
parser.check_grammar line, token
|
||||
|
|
|
@ -2,30 +2,36 @@ require 'spec_helper'
|
|||
require 'mm2ep_depend'
|
||||
|
||||
describe Mm2ep::Depend::Parser do
|
||||
let(:parser) do
|
||||
Mm2ep::Depend::Parser.new(
|
||||
Mm2ep::Depend::Lexer.new
|
||||
)
|
||||
end
|
||||
|
||||
it 'has to do not before or' do
|
||||
line = File.read(testfile('priority_not_or.txt')).delete("\n")
|
||||
parser = Mm2ep::Depend::Parser.new(Mm2ep::Depend::Lexer.new)
|
||||
line = File
|
||||
.read(testfile('success_priority_not_or.txt')).delete("\n")
|
||||
token = parser.parse(line.chomp)
|
||||
assert_equal(false, token.compute)
|
||||
end
|
||||
|
||||
it 'has to do not before and' do
|
||||
line = File.read(testfile('priority_not_and.txt')).delete("\n")
|
||||
parser = Mm2ep::Depend::Parser.new(Mm2ep::Depend::Lexer.new)
|
||||
line = File
|
||||
.read(testfile('success_priority_not_and.txt')).delete("\n")
|
||||
token = parser.parse(line.chomp)
|
||||
assert_equal(true, token.compute)
|
||||
end
|
||||
|
||||
it 'has to do and before or' do
|
||||
line = File.read(testfile('priority_or_and.txt')).delete("\n")
|
||||
parser = Mm2ep::Depend::Parser.new(Mm2ep::Depend::Lexer.new)
|
||||
line = File
|
||||
.read(testfile('success_priority_or_and.txt')).delete("\n")
|
||||
token = parser.parse(line.chomp)
|
||||
assert_equal(false, token.compute)
|
||||
end
|
||||
|
||||
it 'has to do and before or operators' do
|
||||
line = File.read(testfile('priority_or_and_or.txt')).delete("\n")
|
||||
parser = Mm2ep::Depend::Parser.new(Mm2ep::Depend::Lexer.new)
|
||||
line = File
|
||||
.read(testfile('success_priority_or_and_or.txt')).delete("\n")
|
||||
token = parser.parse(line.chomp)
|
||||
assert_equal(true, token.compute)
|
||||
end
|
||||
|
|
|
@ -2,64 +2,70 @@ require 'spec_helper'
|
|||
require 'mm2ep_depend'
|
||||
|
||||
describe Mm2ep::Depend::Parser do
|
||||
let(:parser) do
|
||||
Mm2ep::Depend::Parser.new(
|
||||
Mm2ep::Depend::Lexer.new
|
||||
)
|
||||
end
|
||||
|
||||
it 'has to apply not on expr' do
|
||||
line = File.read(testfile('simple_not_expr.txt')).delete("\n")
|
||||
parser = Mm2ep::Depend::Parser.new(Mm2ep::Depend::Lexer.new)
|
||||
line = File
|
||||
.read(testfile('success_simple_not_expr.txt')).delete("\n")
|
||||
token = parser.parse(line.chomp)
|
||||
assert_equal(false, token.compute)
|
||||
end
|
||||
|
||||
it 'has to evaluate eq with bool expr and return true' do
|
||||
line = File.read(testfile('simple_eq_expr_boolexpr.txt')).delete("\n")
|
||||
parser = Mm2ep::Depend::Parser.new(Mm2ep::Depend::Lexer.new)
|
||||
line = File
|
||||
.read(testfile('success_simple_eq_expr_boolexpr.txt')).delete("\n")
|
||||
parser.names = { 'truc_bidule' => 'true' }
|
||||
token = parser.parse(line.chomp)
|
||||
assert_equal(true, token.compute)
|
||||
end
|
||||
|
||||
it 'has to evaluate eq with bool expr and return false' do
|
||||
line = File.read(testfile('simple_eq_expr_boolexpr.txt')).delete("\n")
|
||||
parser = Mm2ep::Depend::Parser.new(Mm2ep::Depend::Lexer.new)
|
||||
line = File
|
||||
.read(testfile('success_simple_eq_expr_boolexpr.txt')).delete("\n")
|
||||
parser.names = { 'truc_bidule' => 'false' }
|
||||
token = parser.parse(line.chomp)
|
||||
assert_equal(false, token.compute)
|
||||
end
|
||||
|
||||
it 'has to evaluate eq with number and return true' do
|
||||
line = File.read(testfile('simple_eq_expr_number.txt')).delete("\n")
|
||||
parser = Mm2ep::Depend::Parser.new(Mm2ep::Depend::Lexer.new)
|
||||
line = File
|
||||
.read(testfile('success_simple_eq_expr_number.txt')).delete("\n")
|
||||
parser.names = { 'truc_bidule' => '10' }
|
||||
token = parser.parse(line.chomp)
|
||||
assert_equal(true, token.compute)
|
||||
end
|
||||
|
||||
it 'has to evaluate eq with number and return false' do
|
||||
line = File.read(testfile('simple_eq_expr_number.txt')).delete("\n")
|
||||
parser = Mm2ep::Depend::Parser.new(Mm2ep::Depend::Lexer.new)
|
||||
line = File
|
||||
.read(testfile('success_simple_eq_expr_number.txt')).delete("\n")
|
||||
parser.names = { 'truc_bidule' => '11' }
|
||||
token = parser.parse(line.chomp)
|
||||
assert_equal(false, token.compute)
|
||||
end
|
||||
|
||||
it 'has to evaluate eq with string and return true' do
|
||||
line = File.read(testfile('simple_eq_expr_string.txt')).delete("\n")
|
||||
parser = Mm2ep::Depend::Parser.new(Mm2ep::Depend::Lexer.new)
|
||||
line = File
|
||||
.read(testfile('success_simple_eq_expr_string.txt')).delete("\n")
|
||||
parser.names = { 'truc_bidule' => 'test' }
|
||||
token = parser.parse(line.chomp)
|
||||
assert_equal(true, token.compute)
|
||||
end
|
||||
|
||||
it 'has to evaluate eq with string and return false' do
|
||||
line = File.read(testfile('simple_eq_expr_string.txt')).delete("\n")
|
||||
parser = Mm2ep::Depend::Parser.new(Mm2ep::Depend::Lexer.new)
|
||||
line = File
|
||||
.read(testfile('success_simple_eq_expr_string.txt')).delete("\n")
|
||||
parser.names = { 'truc_bidule' => 'tes' }
|
||||
token = parser.parse(line.chomp)
|
||||
assert_equal(false, token.compute)
|
||||
end
|
||||
|
||||
it 'has to evaluate true OR true and return true' do
|
||||
line = File.read(testfile('simple_expr_or_expr.txt')).delete("\n")
|
||||
parser = Mm2ep::Depend::Parser.new(Mm2ep::Depend::Lexer.new)
|
||||
line = File
|
||||
.read(testfile('success_simple_expr_or_expr.txt')).delete("\n")
|
||||
parser.names = { 'truc_bidule' => 'true',
|
||||
'machin' => 'true' }
|
||||
token = parser.parse(line.chomp)
|
||||
|
@ -67,8 +73,8 @@ describe Mm2ep::Depend::Parser do
|
|||
end
|
||||
|
||||
it 'has to evaluate true OR false and return true' do
|
||||
line = File.read(testfile('simple_expr_or_expr.txt')).delete("\n")
|
||||
parser = Mm2ep::Depend::Parser.new(Mm2ep::Depend::Lexer.new)
|
||||
line = File
|
||||
.read(testfile('success_simple_expr_or_expr.txt')).delete("\n")
|
||||
parser.names = { 'truc_bidule' => 'true',
|
||||
'machin' => 'false' }
|
||||
token = parser.parse(line.chomp)
|
||||
|
@ -76,8 +82,8 @@ describe Mm2ep::Depend::Parser do
|
|||
end
|
||||
|
||||
it 'has to evaluate false OR true and return true' do
|
||||
line = File.read(testfile('simple_expr_or_expr.txt')).delete("\n")
|
||||
parser = Mm2ep::Depend::Parser.new(Mm2ep::Depend::Lexer.new)
|
||||
line = File
|
||||
.read(testfile('success_simple_expr_or_expr.txt')).delete("\n")
|
||||
parser.names = { 'truc_bidule' => 'false',
|
||||
'machin' => 'true' }
|
||||
token = parser.parse(line.chomp)
|
||||
|
@ -85,8 +91,8 @@ describe Mm2ep::Depend::Parser do
|
|||
end
|
||||
|
||||
it 'has to evaluate false OR false and return false' do
|
||||
line = File.read(testfile('simple_expr_or_expr.txt')).delete("\n")
|
||||
parser = Mm2ep::Depend::Parser.new(Mm2ep::Depend::Lexer.new)
|
||||
line = File
|
||||
.read(testfile('success_simple_expr_or_expr.txt')).delete("\n")
|
||||
parser.names = { 'truc_bidule' => 'false',
|
||||
'machin' => 'false' }
|
||||
token = parser.parse(line.chomp)
|
||||
|
@ -94,8 +100,8 @@ describe Mm2ep::Depend::Parser do
|
|||
end
|
||||
|
||||
it 'has to evaluate true AND true and return true' do
|
||||
line = File.read(testfile('simple_expr_and_expr.txt')).delete("\n")
|
||||
parser = Mm2ep::Depend::Parser.new(Mm2ep::Depend::Lexer.new)
|
||||
line = File
|
||||
.read(testfile('success_simple_expr_and_expr.txt')).delete("\n")
|
||||
parser.names = { 'truc_bidule' => 'true',
|
||||
'machin' => 'true' }
|
||||
token = parser.parse(line.chomp)
|
||||
|
@ -103,8 +109,8 @@ describe Mm2ep::Depend::Parser do
|
|||
end
|
||||
|
||||
it 'has to evaluate true AND false and return false' do
|
||||
line = File.read(testfile('simple_expr_and_expr.txt')).delete("\n")
|
||||
parser = Mm2ep::Depend::Parser.new(Mm2ep::Depend::Lexer.new)
|
||||
line = File
|
||||
.read(testfile('success_simple_expr_and_expr.txt')).delete("\n")
|
||||
parser.names = { 'truc_bidule' => 'true',
|
||||
'machin' => 'false' }
|
||||
token = parser.parse(line.chomp)
|
||||
|
@ -112,8 +118,8 @@ describe Mm2ep::Depend::Parser do
|
|||
end
|
||||
|
||||
it 'has to evaluate false AND true and return false' do
|
||||
line = File.read(testfile('simple_expr_and_expr.txt')).delete("\n")
|
||||
parser = Mm2ep::Depend::Parser.new(Mm2ep::Depend::Lexer.new)
|
||||
line = File
|
||||
.read(testfile('success_simple_expr_and_expr.txt')).delete("\n")
|
||||
parser.names = { 'truc_bidule' => 'false',
|
||||
'machin' => 'true' }
|
||||
token = parser.parse(line.chomp)
|
||||
|
@ -121,8 +127,8 @@ describe Mm2ep::Depend::Parser do
|
|||
end
|
||||
|
||||
it 'has to evaluate false AND false and return false' do
|
||||
line = File.read(testfile('simple_expr_and_expr.txt')).delete("\n")
|
||||
parser = Mm2ep::Depend::Parser.new(Mm2ep::Depend::Lexer.new)
|
||||
line = File
|
||||
.read(testfile('success_simple_expr_and_expr.txt')).delete("\n")
|
||||
parser.names = { 'truc_bidule' => 'false',
|
||||
'machin' => 'false' }
|
||||
token = parser.parse(line.chomp)
|
||||
|
|
Loading…
Reference in a new issue