2021-10-30 23:37:15 +02:00
|
|
|
# coding: utf-8
|
2021-11-09 16:50:01 +01:00
|
|
|
# frozen_string_literal: true
|
2021-10-30 23:37:15 +02:00
|
|
|
|
2021-11-09 16:50:01 +01:00
|
|
|
require 'test/unit'
|
2021-10-31 09:18:34 +01:00
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
require_relative '../rpl'
|
2021-10-30 23:37:15 +02:00
|
|
|
|
|
|
|
class TestParser < Test::Unit::TestCase
|
2021-11-09 16:50:01 +01:00
|
|
|
def test_number
|
2022-02-10 14:50:59 +01:00
|
|
|
result = Rpl.new.parse( '1' )
|
2021-11-23 16:16:21 +01:00
|
|
|
assert_equal [{ value: 1, type: :numeric, base: 10 }], result
|
2021-11-09 16:50:01 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_word
|
2022-02-10 14:50:59 +01:00
|
|
|
result = Rpl.new.parse( '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
|
2022-02-10 14:50:59 +01:00
|
|
|
result = Rpl.new.parse( '"test"' )
|
2022-01-26 18:30:51 +01:00
|
|
|
assert_equal [{ value: 'test', type: :string }], result
|
2021-11-09 16:50:01 +01:00
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
result = Rpl.new.parse( '" test"' )
|
2022-01-26 18:30:51 +01:00
|
|
|
assert_equal [{ value: ' test', type: :string }], result
|
2021-11-09 16:50:01 +01:00
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
result = Rpl.new.parse( '"test "' )
|
2022-01-26 18:30:51 +01:00
|
|
|
assert_equal [{ value: 'test ', type: :string }], result
|
2021-11-09 16:50:01 +01:00
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
result = Rpl.new.parse( '" test "' )
|
2022-01-26 18:30:51 +01:00
|
|
|
assert_equal [{ value: ' test ', type: :string }], result
|
2022-01-26 16:15:12 +01:00
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
result = Rpl.new.parse( '" « test » "' )
|
2022-01-26 18:30:51 +01:00
|
|
|
assert_equal [{ value: ' « test » ', type: :string }], result
|
2021-11-09 16:50:01 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_name
|
2022-02-10 14:50:59 +01:00
|
|
|
result = Rpl.new.parse( "'test'" )
|
2022-01-26 18:30:51 +01:00
|
|
|
assert_equal [{ value: 'test', type: :name }], result
|
2021-11-09 16:50:01 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_program
|
2022-02-10 14:50:59 +01:00
|
|
|
result = Rpl.new.parse( '« test »' )
|
2022-01-26 18:30:51 +01:00
|
|
|
assert_equal [{ value: 'test', type: :program }], result
|
2021-11-09 16:50:01 +01:00
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
result = Rpl.new.parse( '«test »' )
|
2022-01-26 18:30:51 +01:00
|
|
|
assert_equal [{ value: 'test', type: :program }], result
|
2021-11-09 16:50:01 +01:00
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
result = Rpl.new.parse( '« test»' )
|
2022-01-26 18:30:51 +01:00
|
|
|
assert_equal [{ value: 'test', type: :program }], result
|
2021-11-09 22:03:39 +01:00
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
result = Rpl.new.parse( '«test»' )
|
2022-01-26 18:30:51 +01:00
|
|
|
assert_equal [{ value: 'test', type: :program }], result
|
2021-11-09 16:50:01 +01:00
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
result = Rpl.new.parse( '« test test »' )
|
2022-01-26 18:30:51 +01:00
|
|
|
assert_equal [{ value: 'test test', type: :program }], result
|
2021-11-09 16:50:01 +01:00
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
result = Rpl.new.parse( '« test « test » »' )
|
2022-01-26 18:30:51 +01:00
|
|
|
assert_equal [{ value: 'test « test »', type: :program }], result
|
2021-11-09 16:50:01 +01:00
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
result = Rpl.new.parse( '« test "test" test »' )
|
2022-01-26 18:30:51 +01:00
|
|
|
assert_equal [{ value: 'test "test" test', type: :program }], result
|
2021-11-09 16:50:01 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_number_number
|
2022-02-10 14:50:59 +01:00
|
|
|
result = Rpl.new.parse( '2 3' )
|
2021-11-23 16:16:21 +01:00
|
|
|
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
|
2022-02-10 14:50:59 +01:00
|
|
|
result = Rpl.new.parse( '2 3 +' )
|
2021-11-23 16:16:21 +01:00
|
|
|
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
|
2022-02-10 14:50:59 +01:00
|
|
|
result = Rpl.new.parse( '4 "test"' )
|
2022-01-26 18:30:51 +01:00
|
|
|
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
|
|
|
|
2021-12-15 13:30:57 +01:00
|
|
|
def test_emptystring
|
2022-02-10 14:50:59 +01:00
|
|
|
result = Rpl.new.parse( '""' )
|
2021-12-15 13:30:57 +01:00
|
|
|
|
2022-01-26 18:30:51 +01:00
|
|
|
assert_equal [{ value: '', type: :string }], result
|
2021-12-15 13:30:57 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_spacestring
|
2022-02-10 14:50:59 +01:00
|
|
|
result = Rpl.new.parse( '" "' )
|
2021-12-15 13:30:57 +01:00
|
|
|
|
2022-01-26 18:30:51 +01:00
|
|
|
assert_equal [{ value: ' ', type: :string }], result
|
2021-12-15 13:30:57 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_string_spacestring
|
2022-02-10 14:50:59 +01:00
|
|
|
result = Rpl.new.parse( '"test string" " "' )
|
2021-12-15 13:30:57 +01:00
|
|
|
|
2022-01-26 18:30:51 +01:00
|
|
|
assert_equal [{ value: 'test string', type: :string },
|
|
|
|
{ value: ' ', type: :string }], result
|
2021-12-15 13:30:57 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_string_word
|
2022-02-10 14:50:59 +01:00
|
|
|
result = Rpl.new.parse( '"test string" split' )
|
2021-12-15 13:30:57 +01:00
|
|
|
|
2022-01-26 18:30:51 +01:00
|
|
|
assert_equal [{ value: 'test string', type: :string },
|
2021-12-15 13:30:57 +01:00
|
|
|
{ value: 'split', type: :word }], result
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_spacestring_word
|
2022-02-10 14:50:59 +01:00
|
|
|
result = Rpl.new.parse( '" " split' )
|
2021-12-15 13:30:57 +01:00
|
|
|
|
2022-01-26 18:30:51 +01:00
|
|
|
assert_equal [{ value: ' ', type: :string },
|
2021-12-15 13:30:57 +01:00
|
|
|
{ value: 'split', type: :word }], result
|
|
|
|
end
|
|
|
|
|
2021-11-24 15:04:27 +01:00
|
|
|
def test_program_name
|
2022-02-10 14:50:59 +01:00
|
|
|
result = Rpl.new.parse( "« 2 dup * » 'carré' sto" )
|
2021-11-24 15:04:27 +01:00
|
|
|
|
2022-01-26 18:30:51 +01:00
|
|
|
assert_equal [{ value: '2 dup *', type: :program },
|
|
|
|
{ value: 'carré', type: :name },
|
2021-11-24 15:04:27 +01:00
|
|
|
{ value: 'sto', type: :word }],
|
|
|
|
result
|
|
|
|
end
|
2021-10-30 23:37:15 +02:00
|
|
|
end
|