rpl.rb/spec/parser_spec.rb

331 lines
11 KiB
Ruby
Raw Normal View History

2021-11-09 16:50:01 +01:00
# frozen_string_literal: true
2022-02-17 15:09:29 +01:00
require 'minitest/autorun'
2021-10-31 09:18:34 +01:00
2022-02-25 20:04:33 +01:00
require 'rpl/parser'
require 'rpl/types'
2022-02-17 15:09:29 +01:00
class TestParser < MiniTest::Test
2022-02-25 20:04:33 +01:00
include Types
2022-02-25 20:04:33 +01:00
def test_number
result = Parser.parse( '1' )
assert_equal 1, result.size
assert_equal RplNumeric, result.first.class
assert_equal BigDecimal( 1 ), result.first.value
result = Parser.parse( '0b101' )
assert_equal 1, result.size
assert_equal RplNumeric, result.first.class
assert_equal 2, result.first.base
2022-02-25 20:04:33 +01:00
assert_equal BigDecimal( 5 ), result.first.value
result = Parser.parse( '0o57' )
assert_equal 1, result.size
assert_equal RplNumeric, result.first.class
assert_equal 8, result.first.base
2022-02-25 20:04:33 +01:00
assert_equal BigDecimal( 47 ), result.first.value
result = Parser.parse( '0xfa' )
2022-02-25 20:04:33 +01:00
assert_equal 1, result.size
assert_equal RplNumeric, result.first.class
assert_equal 16, result.first.base
assert_equal BigDecimal( 250 ), result.first.value
result = Parser.parse( '3b10' )
assert_equal 1, result.size
assert_equal RplNumeric, result.first.class
assert_equal 3, result.first.base
2022-02-25 20:04:33 +01:00
assert_equal BigDecimal( 3 ), result.first.value
2021-11-09 16:50:01 +01:00
end
def test_word
2022-02-25 20:04:33 +01:00
result = Parser.parse( 'dup' )
assert_equal 1, result.size
assert_equal RplName, result.first.class
assert_equal 'dup', result.first.value
assert_equal false, result.first.not_to_evaluate
2021-11-09 16:50:01 +01:00
end
def test_string
2022-02-25 20:04:33 +01:00
result = Parser.parse( '"test"' )
assert_equal 1, result.size
assert_equal RplString, result.first.class
assert_equal 'test', result.first.value
result = Parser.parse( '" test"' )
assert_equal 1, result.size
assert_equal RplString, result.first.class
assert_equal ' test', result.first.value
result = Parser.parse( '"test "' )
assert_equal 1, result.size
assert_equal RplString, result.first.class
assert_equal 'test ', result.first.value
result = Parser.parse( '" test "' )
assert_equal 1, result.size
assert_equal RplString, result.first.class
assert_equal ' test ', result.first.value
result = Parser.parse( '" « test » "' )
assert_equal 1, result.size
assert_equal RplString, result.first.class
assert_equal ' « test » ', result.first.value
2021-11-09 16:50:01 +01:00
end
def test_name
2022-02-25 20:04:33 +01:00
result = Parser.parse( "'test'" )
assert_equal 1, result.size
assert_equal RplName, result.first.class
assert_equal 'test', result.first.value
assert_equal true, result.first.not_to_evaluate
result = Parser.parse( 'test' )
assert_equal 1, result.size
assert_equal RplName, result.first.class
assert_equal 'test', result.first.value
assert_equal false, result.first.not_to_evaluate
result = Parser.parse( '+' )
assert_equal 1, result.size
assert_equal RplName, result.first.class
assert_equal '+', result.first.value
assert_equal false, result.first.not_to_evaluate
result = Parser.parse( '√' )
assert_equal 1, result.size
assert_equal RplName, result.first.class
assert_equal '√', result.first.value
assert_equal false, result.first.not_to_evaluate
2021-11-09 16:50:01 +01:00
end
def test_program
2022-02-25 20:04:33 +01:00
result = Parser.parse( '« test »' )
assert_equal 1, result.size
assert_equal RplProgram, result.first.class
assert_equal 'test', result.first.value
result = Parser.parse( '«test »' )
assert_equal 1, result.size
assert_equal RplProgram, result.first.class
assert_equal 'test', result.first.value
result = Parser.parse( '« test»' )
assert_equal 1, result.size
assert_equal RplProgram, result.first.class
assert_equal 'test', result.first.value
result = Parser.parse( '«test»' )
assert_equal 1, result.size
assert_equal RplProgram, result.first.class
assert_equal 'test', result.first.value
result = Parser.parse( '« test test »' )
assert_equal 1, result.size
assert_equal RplProgram, result.first.class
assert_equal 'test test', result.first.value
result = Parser.parse( '« test « test » »' )
assert_equal 1, result.size
assert_equal RplProgram, result.first.class
assert_equal 'test « test »', result.first.value
result = Parser.parse( '« test "test" test »' )
assert_equal 1, result.size
assert_equal RplProgram, result.first.class
assert_equal 'test "test" test', result.first.value
2021-11-09 16:50:01 +01:00
end
2022-02-17 15:14:20 +01:00
def test_list
2022-02-25 20:04:33 +01:00
result = Parser.parse( '{ test }' )
assert_equal 1, result.size
assert_equal RplList, result.first.class
assert_equal 1, result.first.value.size
assert_equal RplName, result.first.value.first.class
assert_equal 'test', result.first.value.first.value
result = Parser.parse( '{test }' )
assert_equal 1, result.size
assert_equal RplList, result.first.class
assert_equal 1, result.first.value.size
assert_equal RplName, result.first.value.first.class
assert_equal 'test', result.first.value.first.value
result = Parser.parse( '{ test}' )
assert_equal 1, result.size
assert_equal RplList, result.first.class
assert_equal 1, result.first.value.size
assert_equal RplName, result.first.value.first.class
assert_equal 'test', result.first.value.first.value
result = Parser.parse( '{test}' )
assert_equal 1, result.size
assert_equal RplList, result.first.class
assert_equal 1, result.first.value.size
assert_equal RplName, result.first.value.first.class
assert_equal 'test', result.first.value.first.value
result = Parser.parse( '{ test test }' )
assert_equal 1, result.size
assert_equal RplList, result.first.class
assert_equal 2, result.first.value.size
assert_equal RplName, result.first.value.first.class
assert_equal 'test', result.first.value.first.value
assert_equal RplName, result.last.value.first.class
assert_equal 'test', result.last.value.first.value
result = Parser.parse( '{ test { test } }' )
# we have one list
assert_equal 1, result.size
assert_equal RplList, result.first.class
# that list has 2 elements
assert_equal 2, result.first.value.size
# first element is a name
assert_equal RplName, result.first.value.first.class
assert_equal 'test', result.first.value.first.value
# second is a list
assert_equal RplList, result.first.value.last.class
# that list has one element
assert_equal 1, result.first.value.last.value.size
# and it's a name
assert_equal RplName, result.first.value.last.value.first.class
assert_equal 'test', result.first.value.last.value.first.value
result = Parser.parse( '{ test "test" test }' )
# we have one list
assert_equal 1, result.size
assert_equal RplList, result.first.class
# that list has 3 elements
assert_equal 3, result.first.value.size
# first and last element is a name
assert_equal RplName, result.first.value[0].class
assert_equal 'test', result.first.value[0].value
assert_equal RplName, result.first.value[2].class
assert_equal 'test', result.first.value[2].value
# middle element is a string
assert_equal RplString, result.first.value[1].class
assert_equal 'test', result.first.value[1].value
2022-02-17 15:14:20 +01:00
end
2021-11-09 16:50:01 +01:00
def test_number_number
2022-02-25 20:04:33 +01:00
result = Parser.parse( '2 3' )
assert_equal 2, result.size
assert_equal RplNumeric, result.first.class
assert_equal BigDecimal( 2 ), result.first.value
assert_equal RplNumeric, result.last.class
assert_equal BigDecimal( 3 ), result.last.value
2021-11-09 16:50:01 +01:00
end
2021-11-10 11:01:26 +01:00
def test_number_number_word
2022-02-25 20:04:33 +01:00
result = Parser.parse( '2 3 +' )
assert_equal 3, result.size
assert_equal RplNumeric, result.first.class
assert_equal BigDecimal( 2 ), result.first.value
assert_equal RplNumeric, result[1].class
assert_equal BigDecimal( 3 ), result[1].value
assert_equal RplName, result[2].class
assert_equal '+', result[2].value
2021-11-10 11:01:26 +01:00
end
2021-11-09 16:50:01 +01:00
def test_number_string
2022-02-25 20:04:33 +01:00
result = Parser.parse( '4 "test"' )
assert_equal 2, result.size
assert_equal RplNumeric, result.first.class
assert_equal BigDecimal( 4 ), result.first.value
assert_equal RplString, result.last.class
assert_equal 'test', result.last.value
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-25 20:04:33 +01:00
result = Parser.parse( '""' )
assert_equal 1, result.size
assert_equal RplString, result.first.class
assert_equal '', result.first.value
2021-12-15 13:30:57 +01:00
end
def test_spacestring
2022-02-25 20:04:33 +01:00
result = Parser.parse( '" "' )
assert_equal 1, result.size
assert_equal RplString, result.first.class
assert_equal ' ', result.first.value
2021-12-15 13:30:57 +01:00
end
def test_string_spacestring
2022-02-25 20:04:33 +01:00
result = Parser.parse( '"test string" " "' )
assert_equal 2, result.size
assert_equal RplString, result.first.class
assert_equal 'test string', result.first.value
assert_equal RplString, result.last.class
assert_equal ' ', result.last.value
2021-12-15 13:30:57 +01:00
end
def test_string_word
2022-02-25 20:04:33 +01:00
result = Parser.parse( '"test string" split' )
assert_equal 2, result.size
assert_equal RplString, result.first.class
assert_equal 'test string', result.first.value
assert_equal RplName, result.last.class
assert_equal 'split', result.last.value
assert_equal false, result.last.not_to_evaluate
2021-12-15 13:30:57 +01:00
end
def test_spacestring_word
2022-02-25 20:04:33 +01:00
result = Parser.parse( '" " split' )
assert_equal 2, result.size
assert_equal RplString, result.first.class
assert_equal ' ', result.first.value
assert_equal RplName, result.last.class
assert_equal 'split', result.last.value
assert_equal false, result.last.not_to_evaluate
2021-12-15 13:30:57 +01:00
end
2021-11-24 15:04:27 +01:00
def test_program_name
2022-02-25 20:04:33 +01:00
result = Parser.parse( "« 2 dup * » 'carré' sto" )
assert_equal 3, result.size
assert_equal RplProgram, result.first.class
assert_equal '2 dup *', result.first.value
assert_equal RplName, result[1].class
assert_equal 'carré', result[1].value
assert_equal true, result[1].not_to_evaluate
assert_equal RplName, result.last.class
assert_equal 'sto', result.last.value
assert_equal false, result.last.not_to_evaluate
2021-11-24 15:04:27 +01:00
end
2022-02-22 15:53:22 +01:00
def test_with_multiline
2022-02-25 20:04:33 +01:00
result = Parser.parse( "« 2
2022-02-22 15:53:22 +01:00
dup * »
'carré' sto" )
2022-02-25 20:04:33 +01:00
assert_equal 3, result.size
assert_equal RplProgram, result.first.class
assert_equal '2 dup *', result.first.value
assert_equal RplName, result[1].class
assert_equal 'carré', result[1].value
assert_equal true, result[1].not_to_evaluate
assert_equal RplName, result.last.class
assert_equal 'sto', result.last.value
assert_equal false, result.last.not_to_evaluate
2022-02-22 15:53:22 +01:00
end
def test_with_comments
2022-02-25 20:04:33 +01:00
result = Parser.parse( "« 2 #deux
2022-02-22 15:53:22 +01:00
# on duplique le deux
dup * »
# on va STOcker ce programme dans la variable 'carré'
'carré' sto" )
2022-02-25 20:04:33 +01:00
assert_equal 3, result.size
assert_equal RplProgram, result.first.class
assert_equal '2 dup *', result.first.value
assert_equal RplName, result[1].class
assert_equal 'carré', result[1].value
assert_equal true, result[1].not_to_evaluate
assert_equal RplName, result.last.class
assert_equal 'sto', result.last.value
assert_equal false, result.last.not_to_evaluate
2022-02-22 15:53:22 +01:00
end
end