rpl.rb/spec/parser_spec.rb

87 lines
2.9 KiB
Ruby
Raw Normal View History

# coding: utf-8
2021-11-09 16:50:01 +01:00
# frozen_string_literal: true
2021-11-09 16:50:01 +01:00
require 'test/unit'
2021-10-31 09:18:34 +01:00
2021-11-09 16:50:01 +01:00
require_relative '../lib/parser'
class TestParser < Test::Unit::TestCase
2021-11-09 16:50:01 +01:00
def test_number
result = Rpl::Parser.new.parse_input( '1' )
assert_equal [{ value: 1, type: :numeric, base: 10 }], result
2021-11-09 16:50:01 +01:00
end
def test_word
result = Rpl::Parser.new.parse_input( 'dup' )
2021-11-10 11:01:26 +01:00
assert_equal [{ value: 'dup', type: :word }], result
2021-11-09 16:50:01 +01:00
end
def test_string
result = Rpl::Parser.new.parse_input( '"test"' )
2021-11-09 16:50:01 +01:00
assert_equal [{ value: '"test"', type: :string }], result
result = Rpl::Parser.new.parse_input( '" test"' )
2021-11-09 16:50:01 +01:00
assert_equal [{ value: '" test"', type: :string }], result
result = Rpl::Parser.new.parse_input( '"test "' )
2021-11-09 16:50:01 +01:00
assert_equal [{ value: '"test "', type: :string }], result
result = Rpl::Parser.new.parse_input( '" test "' )
2021-11-09 16:50:01 +01:00
assert_equal [{ value: '" test "', type: :string }], result
end
def test_name
result = Rpl::Parser.new.parse_input( "'test'" )
2021-11-09 16:50:01 +01:00
assert_equal [{ value: "'test'", type: :name }], result
end
def test_program
result = Rpl::Parser.new.parse_input( '« test »' )
2021-11-09 16:50:01 +01:00
assert_equal [{ value: '« test »', type: :program }], result
result = Rpl::Parser.new.parse_input( '«test »' )
2021-11-09 22:03:39 +01:00
assert_equal [{ value: '« test »', type: :program }], result
2021-11-09 16:50:01 +01:00
result = Rpl::Parser.new.parse_input( '« test»' )
2021-11-09 22:03:39 +01:00
assert_equal [{ value: '« test »', type: :program }], result
result = Rpl::Parser.new.parse_input( '«test»' )
2021-11-09 22:03:39 +01:00
assert_equal [{ value: '« test »', type: :program }], result
2021-11-09 16:50:01 +01:00
result = Rpl::Parser.new.parse_input( '« test test »' )
2021-11-09 16:50:01 +01:00
assert_equal [{ value: '« test test »', type: :program }], result
result = Rpl::Parser.new.parse_input( '« test « test » »' )
2021-11-09 16:50:01 +01:00
assert_equal [{ value: '« test « test » »', type: :program }], result
result = Rpl::Parser.new.parse_input( '« test "test" test »' )
2021-11-09 16:50:01 +01:00
assert_equal [{ value: '« test "test" test »', type: :program }], result
end
def test_number_number
result = Rpl::Parser.new.parse_input( '2 3' )
assert_equal [{ value: 2, type: :numeric, base: 10 }, { value: 3, type: :numeric, base: 10 }], result
2021-11-09 16:50:01 +01:00
end
2021-11-10 11:01:26 +01:00
def test_number_number_word
result = Rpl::Parser.new.parse_input( '2 3 +' )
assert_equal [{ value: 2, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 },
2021-11-10 11:01:26 +01:00
{ value: '+', type: :word }], result
end
2021-11-09 16:50:01 +01:00
def test_number_string
result = Rpl::Parser.new.parse_input( '4 "test"' )
assert_equal [{ value: 4, type: :numeric, base: 10 }, { value: '"test"', type: :string }], result
2021-11-09 16:50:01 +01:00
end
2021-11-24 15:04:27 +01:00
def test_program_name
result = Rpl::Parser.new.parse_input( "« 2 dup * » 'carré' sto" )
assert_equal [{ value: '« 2 dup * »', type: :program },
{ value: "'carré'", type: :name },
{ value: 'sto', type: :word }],
result
end
end