From 1ddccc3eee563df1e59abbcb3b3ab67eaaff310e Mon Sep 17 00:00:00 2001 From: Gwenhael Le Moine Date: Wed, 8 Dec 2021 16:08:49 +0100 Subject: [PATCH] all tests use lang.run() instead of handcrafted stacks --- language.rb | 2 +- spec/language_branch_spec.rb | 30 ++-- spec/language_operations_spec.rb | 275 +++++++++++++------------------ spec/language_program_spec.rb | 18 +- spec/language_stack_spec.rb | 140 +++++++--------- spec/language_store_spec.rb | 184 ++++++--------------- spec/language_string_spec.rb | 55 +++---- spec/language_time-date_spec.rb | 18 +- 8 files changed, 282 insertions(+), 440 deletions(-) diff --git a/language.rb b/language.rb index 6072b85..9ef8c03 100644 --- a/language.rb +++ b/language.rb @@ -7,7 +7,7 @@ require './lib/runner' module Rpl class Language - attr_reader :stack + attr_reader :stack, :dictionary def initialize( stack = [] ) @stack = stack diff --git a/spec/language_branch_spec.rb b/spec/language_branch_spec.rb index 6b31db2..8995000 100644 --- a/spec/language_branch_spec.rb +++ b/spec/language_branch_spec.rb @@ -7,36 +7,30 @@ require_relative '../language' class TestLanguageBranch < Test::Unit::TestCase def test_ifte - stack, _dictionary = Rpl::Lang::Core.ifte( [{ type: :boolean, value: true }, - { type: :program, value: '« 2 3 + »' }, - { type: :program, value: '« 2 3 - »' }], - Rpl::Lang::Dictionary.new ) + lang = Rpl::Language.new + lang.run 'true « 2 3 + » « 2 3 - » ifte' assert_equal [{ value: 5, type: :numeric, base: 10 }], - stack + lang.stack - stack, _dictionary = Rpl::Lang::Core.ifte( [{ type: :boolean, value: false }, - { type: :program, value: '« 2 3 + »' }, - { type: :program, value: '« 2 3 - »' }], - Rpl::Lang::Dictionary.new ) + lang = Rpl::Language.new + lang.run 'false « 2 3 + » « 2 3 - » ifte' assert_equal [{ value: -1, type: :numeric, base: 10 }], - stack + lang.stack end def test_ift - stack, _dictionary = Rpl::Lang::Core.ift( [{ type: :boolean, value: true }, - { type: :program, value: '« 2 3 + »' }], - Rpl::Lang::Dictionary.new ) + lang = Rpl::Language.new + lang.run 'true « 2 3 + » ift' assert_equal [{ value: 5, type: :numeric, base: 10 }], - stack + lang.stack - stack, _dictionary = Rpl::Lang::Core.ift( [{ type: :boolean, value: false }, - { type: :program, value: '« 2 3 + »' }], - Rpl::Lang::Dictionary.new ) + lang = Rpl::Language.new + lang.run 'false « 2 3 + » ift' assert_equal [], - stack + lang.stack end end diff --git a/spec/language_operations_spec.rb b/spec/language_operations_spec.rb index 447e7dc..f6cf2c9 100644 --- a/spec/language_operations_spec.rb +++ b/spec/language_operations_spec.rb @@ -7,285 +7,238 @@ require_relative '../language' class TesttLanguageOperations < Test::Unit::TestCase def test_add - stack, _dictionary = Rpl::Lang::Core.add [{ value: 1, type: :numeric, base: 10 }, - { value: 2, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new + lang = Rpl::Language.new + lang.run '1 2 +' assert_equal [{ value: 3, type: :numeric, base: 10 }], - stack + lang.stack - stack, _dictionary = Rpl::Lang::Core.add [{ value: 1, type: :numeric, base: 10 }, - { value: '"a"', type: :string }], - Rpl::Lang::Dictionary.new + lang = Rpl::Language.new + lang.run '1 "a" +' assert_equal [{ value: '"1a"', type: :string }], - stack + lang.stack - stack, _dictionary = Rpl::Lang::Core.add [{ value: 1, type: :numeric, base: 10 }, - { value: "'a'", type: :name }], - Rpl::Lang::Dictionary.new + lang = Rpl::Language.new + lang.run '1 \'a\' +' assert_equal [{ value: '"1a"', type: :string }], - stack + lang.stack - stack, _dictionary = Rpl::Lang::Core.add [{ value: "'a'", type: :name }, - { value: 1, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new + lang = Rpl::Language.new + lang.run '\'a\' 1 +' assert_equal [{ value: "'a1'", type: :name }], - stack + lang.stack - stack, _dictionary = Rpl::Lang::Core.add [{ value: "'a'", type: :name }, - { value: '"b"', type: :string }], - Rpl::Lang::Dictionary.new + lang = Rpl::Language.new + lang.run '\'a\' "b" +' assert_equal [{ value: "'ab'", type: :name }], - stack + lang.stack - stack, _dictionary = Rpl::Lang::Core.add [{ value: "'a'", type: :name }, - { value: "'b'", type: :name }], - Rpl::Lang::Dictionary.new + lang = Rpl::Language.new + lang.run '\'a\' \'b\' +' assert_equal [{ value: "'ab'", type: :name }], - stack + lang.stack - stack, _dictionary = Rpl::Lang::Core.add [{ value: '"a"', type: :string }, - { value: '"b"', type: :string }], - Rpl::Lang::Dictionary.new + lang = Rpl::Language.new + lang.run '"a" "b" +' assert_equal [{ value: '"ab"', type: :string }], - stack + lang.stack - stack, _dictionary = Rpl::Lang::Core.add [{ value: '"a"', type: :string }, - { value: "'b'", type: :name }], - Rpl::Lang::Dictionary.new + lang = Rpl::Language.new + lang.run '"a" \'b\' +' assert_equal [{ value: '"ab"', type: :string }], - stack + lang.stack - stack, _dictionary = Rpl::Lang::Core.add [{ value: '"a"', type: :string }, - { value: 1, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new + lang = Rpl::Language.new + lang.run '"a" 1 +' assert_equal [{ value: '"a1"', type: :string }], - stack + lang.stack end def test_subtract - stack, _dictionary = Rpl::Lang::Core.subtract [{ value: 1, type: :numeric, base: 10 }, - { value: 2, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new + lang = Rpl::Language.new + lang.run '1 2 -' assert_equal [{ value: -1, type: :numeric, base: 10 }], - stack + lang.stack - stack, _dictionary = Rpl::Lang::Core.subtract [{ value: 2, type: :numeric, base: 10 }, - { value: 1, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new + lang = Rpl::Language.new + lang.run '2 1 -' assert_equal [{ value: 1, type: :numeric, base: 10 }], - stack + lang.stack end def test_negate - stack, _dictionary = Rpl::Lang::Core.negate [{ value: -1, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new - + lang = Rpl::Language.new + lang.run '-1 chs' assert_equal [{ value: 1, type: :numeric, base: 10 }], - stack - - stack, _dictionary = Rpl::Lang::Core.negate [{ value: 1, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new + lang.stack + lang = Rpl::Language.new + lang.run '1 chs' assert_equal [{ value: -1, type: :numeric, base: 10 }], - stack + lang.stack end def test_multiply - stack, _dictionary = Rpl::Lang::Core.multiply [{ value: 3, type: :numeric, base: 10 }, - { value: 4, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new + lang = Rpl::Language.new + lang.run '3 4 *' assert_equal [{ value: 12, type: :numeric, base: 10 }], - stack + lang.stack end def test_divide - stack, _dictionary = Rpl::Lang::Core.divide [{ value: 3.0, type: :numeric, base: 10 }, - { value: 4, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new + lang = Rpl::Language.new + lang.run '3.0 4 /' assert_equal [{ value: 0.75, type: :numeric, base: 10 }], - stack - - # stack, _dictionary = Rpl::Lang::Core.divide [{ value: 3, type: :numeric, base: 10 }, - # { value: 4, type: :numeric, base: 10 }] - # assert_equal [{ value: 0.75, type: :numeric, base: 10 }], - # stack + lang.stack end def test_inverse - stack, _dictionary = Rpl::Lang::Core.inverse [{ value: 4, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new + lang = Rpl::Language.new + lang.run '4 inv' assert_equal [{ value: 0.25, type: :numeric, base: 10 }], - stack + lang.stack end def test_power - stack, _dictionary = Rpl::Lang::Core.power [{ value: 3, type: :numeric, base: 10 }, - { value: 4, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new + lang = Rpl::Language.new + lang.run '3 4 ^' assert_equal [{ value: 81, type: :numeric, base: 10 }], - stack + lang.stack end def test_sqrt - stack, _dictionary = Rpl::Lang::Core.sqrt [{ value: 16, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new + lang = Rpl::Language.new + lang.run '16 √' assert_equal [{ value: 4, type: :numeric, base: 10 }], - stack + lang.stack end def test_sq - stack, _dictionary = Rpl::Lang::Core.sq [{ value: 4, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new + lang = Rpl::Language.new + lang.run '4 sq' assert_equal [{ value: 16, type: :numeric, base: 10 }], - stack + lang.stack end def test_abs - stack, _dictionary = Rpl::Lang::Core.abs [{ value: -1, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new - + lang = Rpl::Language.new + lang.run '-1 abs' assert_equal [{ value: 1, type: :numeric, base: 10 }], - stack - - stack, _dictionary = Rpl::Lang::Core.abs [{ value: 1, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new + lang.stack + lang = Rpl::Language.new + lang.run '1 abs' assert_equal [{ value: 1, type: :numeric, base: 10 }], - stack + lang.stack end def test_dec - stack, _dictionary = Rpl::Lang::Core.dec [{ value: 1, type: :numeric, base: 16 }], - Rpl::Lang::Dictionary.new - + lang = Rpl::Language.new + lang.run '0x1 dec' assert_equal [{ value: 1, type: :numeric, base: 10 }], - stack + lang.stack end def test_hex - stack, _dictionary = Rpl::Lang::Core.hex [{ value: 1, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new - + lang = Rpl::Language.new + lang.run '1 hex' assert_equal [{ value: 1, type: :numeric, base: 16 }], - stack + lang.stack end def test_bin - stack, _dictionary = Rpl::Lang::Core.bin [{ value: 1, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new - + lang = Rpl::Language.new + lang.run '1 bin' assert_equal [{ value: 1, type: :numeric, base: 2 }], - stack + lang.stack end def test_base - stack, _dictionary = Rpl::Lang::Core.base [{ value: 1, type: :numeric, base: 10 }, - { value: 31, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new - + lang = Rpl::Language.new + lang.run '1 31 base' assert_equal [{ value: 1, type: :numeric, base: 31 }], - stack + lang.stack end def test_sign - stack, _dictionary = Rpl::Lang::Core.sign [{ value: -10, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new - + lang = Rpl::Language.new + lang.run '-10 sign' assert_equal [{ value: -1, type: :numeric, base: 10 }], - stack - - stack, _dictionary = Rpl::Lang::Core.sign [{ value: 10, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new + lang.stack + lang = Rpl::Language.new + lang.run '10 sign' assert_equal [{ value: 1, type: :numeric, base: 10 }], - stack - stack, _dictionary = Rpl::Lang::Core.sign [{ value: 0, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new + lang.stack + lang = Rpl::Language.new + lang.run '0 sign' assert_equal [{ value: 0, type: :numeric, base: 10 }], - stack + lang.stack end def test_percent - stack, _dictionary = Rpl::Lang::Core.percent [{ value: 2, type: :numeric, base: 10 }, - { value: 33, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new - + lang = Rpl::Language.new + lang.run '2 33 %' assert_equal [{ value: 0.66, type: :numeric, base: 10 }], - stack + lang.stack end def test_inverse_percent - stack, _dictionary = Rpl::Lang::Core.inverse_percent [{ value: 2, type: :numeric, base: 10 }, - { value: 0.66, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new - + lang = Rpl::Language.new + lang.run '2 0.66 %CH' assert_equal [{ value: 33, type: :numeric, base: 10 }], - stack + lang.stack end def test_mod - stack, _dictionary = Rpl::Lang::Core.mod [{ value: 9, type: :numeric, base: 10 }, - { value: 4, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new - + lang = Rpl::Language.new + lang.run '9 4 mod' assert_equal [{ value: 1, type: :numeric, base: 10 }], - stack + lang.stack end def test_fact - stack, _dictionary = Rpl::Lang::Core.fact [{ value: 5, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new - + lang = Rpl::Language.new + lang.run '5 !' assert_equal [{ value: 24, type: :numeric, base: 10 }], - stack + lang.stack end def test_floor - stack, _dictionary = Rpl::Lang::Core.floor [{ value: 5.23, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new - + lang = Rpl::Language.new + lang.run '5.23 floor' assert_equal [{ value: 5, type: :numeric, base: 10 }], - stack + lang.stack end def test_ceil - stack, _dictionary = Rpl::Lang::Core.ceil [{ value: 5.23, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new - + lang = Rpl::Language.new + lang.run '5.23 ceil' assert_equal [{ value: 6, type: :numeric, base: 10 }], - stack + lang.stack end def test_min - stack, _dictionary = Rpl::Lang::Core.min [{ value: 1, type: :numeric, base: 10 }, - { value: 2, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new - + lang = Rpl::Language.new + lang.run '1 2 min' assert_equal [{ value: 1, type: :numeric, base: 10 }], - stack - - stack, _dictionary = Rpl::Lang::Core.min [{ value: 2, type: :numeric, base: 10 }, - { value: 1, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new + lang.stack + lang = Rpl::Language.new + lang.run '2 1 min' assert_equal [{ value: 1, type: :numeric, base: 10 }], - stack + lang.stack end def test_max - stack, _dictionary = Rpl::Lang::Core.max [{ value: 1, type: :numeric, base: 10 }, - { value: 2, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new - + lang = Rpl::Language.new + lang.run '1 2 max' assert_equal [{ value: 2, type: :numeric, base: 10 }], - stack - - stack, _dictionary = Rpl::Lang::Core.max [{ value: 2, type: :numeric, base: 10 }, - { value: 1, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new + lang.stack + lang = Rpl::Language.new + lang.run '2 1 max' assert_equal [{ value: 2, type: :numeric, base: 10 }], - stack + lang.stack end end diff --git a/spec/language_program_spec.rb b/spec/language_program_spec.rb index a1b0dda..dd38c59 100644 --- a/spec/language_program_spec.rb +++ b/spec/language_program_spec.rb @@ -6,24 +6,18 @@ require_relative '../language' class TestLanguageProgram < Test::Unit::TestCase def test_eval - stack, _dictionary = Rpl::Lang::Core.eval( [{ value: '« 2 dup * dup »', type: :program }], Rpl::Lang::Dictionary.new ) + lang = Rpl::Language.new + lang.run '« 2 dup * dup » eval' assert_equal [{ value: 4, type: :numeric, base: 10 }, { value: 4, type: :numeric, base: 10 }], - stack + lang.stack - stack, _dictionary = Rpl::Lang::Core.eval( [{ value: 4, type: :numeric, base: 10 }, - { value: "'dup'", type: :name }], Rpl::Lang::Dictionary.new ) + lang = Rpl::Language.new + lang.run '4 \'dup\' eval' assert_equal [{ value: 4, type: :numeric, base: 10 }, { value: 4, type: :numeric, base: 10 }], - stack - - stack, _dictionary = Rpl::Lang::Core.eval( [{ value: 4, type: :numeric, base: 10 }, - { value: 'dup', type: :word }], Rpl::Lang::Dictionary.new ) - - assert_equal [{ value: 4, type: :numeric, base: 10 }, - { value: 4, type: :numeric, base: 10 }], - stack + lang.stack end end diff --git a/spec/language_stack_spec.rb b/spec/language_stack_spec.rb index 6d8c2c1..1b5f894 100644 --- a/spec/language_stack_spec.rb +++ b/spec/language_stack_spec.rb @@ -7,88 +7,80 @@ require_relative '../language' class TestLanguageStack < Test::Unit::TestCase def test_swap - stack, _dictionary = Rpl::Lang::Core.swap [{ value: 1, type: :numeric, base: 10 }, - { value: 2, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new + lang = Rpl::Language.new + lang.run '1 2 swap' + assert_equal [{ value: 2, type: :numeric, base: 10 }, { value: 1, type: :numeric, base: 10 }], - stack + lang.stack end def test_drop - stack, _dictionary = Rpl::Lang::Core.drop [{ value: 1, type: :numeric, base: 10 }, - { value: 2, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new + lang = Rpl::Language.new + lang.run '1 2 drop' + assert_equal [{ value: 1, type: :numeric, base: 10 }], - stack + lang.stack end def test_drop2 - stack, _dictionary = Rpl::Lang::Core.drop2 [{ value: 1, type: :numeric, base: 10 }, - { value: 2, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new + lang = Rpl::Language.new + lang.run '1 2 drop2' + assert_equal [], - stack + lang.stack end def test_dropn - stack, _dictionary = Rpl::Lang::Core.dropn [{ value: 1, type: :numeric, base: 10 }, - { value: 2, type: :numeric, base: 10 }, - { value: 3, type: :numeric, base: 10 }, - { value: 4, type: :numeric, base: 10 }, - { value: 3, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new + lang = Rpl::Language.new + lang.run '1 2 3 4 3 dropn' + assert_equal [{ value: 1, type: :numeric, base: 10 }], - stack + lang.stack end def test_del - stack, _dictionary = Rpl::Lang::Core.del [{ value: 1, type: :numeric, base: 10 }, - { value: 2, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new - assert_equal [], - stack + lang = Rpl::Language.new + lang.run '1 2 del' + + assert_empty lang.stack end def test_rot - stack, _dictionary = Rpl::Lang::Core.rot [{ value: 1, type: :numeric, base: 10 }, - { value: 2, type: :numeric, base: 10 }, - { value: 3, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new + lang = Rpl::Language.new + lang.run '1 2 3 rot' + assert_equal [{ value: 2, type: :numeric, base: 10 }, { value: 3, type: :numeric, base: 10 }, { value: 1, type: :numeric, base: 10 }], - stack + lang.stack end def test_dup - stack, _dictionary = Rpl::Lang::Core.dup [{ value: 1, type: :numeric, base: 10 }, - { value: 2, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new + lang = Rpl::Language.new + lang.run '1 2 dup' + assert_equal [{ value: 1, type: :numeric, base: 10 }, { value: 2, type: :numeric, base: 10 }, { value: 2, type: :numeric, base: 10 }], - stack + lang.stack end def test_dup2 - stack, _dictionary = Rpl::Lang::Core.dup2 [{ value: 1, type: :numeric, base: 10 }, - { value: 2, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new + lang = Rpl::Language.new + lang.run '1 2 dup2' + assert_equal [{ value: 1, type: :numeric, base: 10 }, { value: 2, type: :numeric, base: 10 }, { value: 1, type: :numeric, base: 10 }, { value: 2, type: :numeric, base: 10 }], - stack + lang.stack end def test_dupn - stack, _dictionary = Rpl::Lang::Core.dupn [{ value: 1, type: :numeric, base: 10 }, - { value: 2, type: :numeric, base: 10 }, - { value: 3, type: :numeric, base: 10 }, - { value: 4, type: :numeric, base: 10 }, - { value: 3, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new + lang = Rpl::Language.new + lang.run '1 2 3 4 3 dupn' + assert_equal [{ value: 1, type: :numeric, base: 10 }, { value: 2, type: :numeric, base: 10 }, { value: 3, type: :numeric, base: 10 }, @@ -96,78 +88,68 @@ class TestLanguageStack < Test::Unit::TestCase { value: 2, type: :numeric, base: 10 }, { value: 3, type: :numeric, base: 10 }, { value: 4, type: :numeric, base: 10 }], - stack + lang.stack end def test_pick - stack, _dictionary = Rpl::Lang::Core.pick [{ value: 1, type: :numeric, base: 10 }, - { value: 2, type: :numeric, base: 10 }, - { value: 3, type: :numeric, base: 10 }, - { value: 4, type: :numeric, base: 10 }, - { value: 3, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new + lang = Rpl::Language.new + lang.run '1 2 3 4 3 pick' + assert_equal [{ value: 1, type: :numeric, base: 10 }, { value: 2, type: :numeric, base: 10 }, { value: 3, type: :numeric, base: 10 }, { value: 4, type: :numeric, base: 10 }, { value: 2, type: :numeric, base: 10 }], - stack + lang.stack end def test_depth - stack, _dictionary = Rpl::Lang::Core.depth [], - Rpl::Lang::Dictionary.new - assert_equal [{ value: 0, type: :numeric, base: 10 }], - stack + lang = Rpl::Language.new + lang.run 'depth' + + assert_equal [{ value: 0, type: :numeric, base: 10 }], + lang.stack + + lang = Rpl::Language.new + lang.run '1 2 depth' - stack, _dictionary = Rpl::Lang::Core.depth [{ value: 1, type: :numeric, base: 10 }, - { value: 2, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new assert_equal [{ value: 1, type: :numeric, base: 10 }, { value: 2, type: :numeric, base: 10 }, { value: 2, type: :numeric, base: 10 }], - stack + lang.stack end def test_roll - stack, _dictionary = Rpl::Lang::Core.roll [{ value: 1, type: :numeric, base: 10 }, - { value: 2, type: :numeric, base: 10 }, - { value: 3, type: :numeric, base: 10 }, - { value: 4, type: :numeric, base: 10 }, - { value: 3, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new + lang = Rpl::Language.new + lang.run '1 2 3 4 3 roll' + assert_equal [{ value: 1, type: :numeric, base: 10 }, { value: 3, type: :numeric, base: 10 }, { value: 4, type: :numeric, base: 10 }, { value: 2, type: :numeric, base: 10 }], - stack + lang.stack end def test_rolld - stack, _dictionary = Rpl::Lang::Core.rolld [{ value: 1, type: :numeric, base: 10 }, - { value: 2, type: :numeric, base: 10 }, - { value: 4, type: :numeric, base: 10 }, - { value: 3, type: :numeric, base: 10 }, - { value: 2, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new + lang = Rpl::Language.new + lang.run '1 2 4 3 2 rolld' + assert_equal [{ value: 1, type: :numeric, base: 10 }, { value: 2, type: :numeric, base: 10 }, { value: 3, type: :numeric, base: 10 }, { value: 4, type: :numeric, base: 10 }], - stack + lang.stack end def test_over - stack, _dictionary = Rpl::Lang::Core.over [{ value: 1, type: :numeric, base: 10 }, - { value: 2, type: :numeric, base: 10 }, - { value: 3, type: :numeric, base: 10 }, - { value: 4, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new + lang = Rpl::Language.new + lang.run '1 2 3 4 over' + assert_equal [{ value: 1, type: :numeric, base: 10 }, { value: 2, type: :numeric, base: 10 }, { value: 3, type: :numeric, base: 10 }, { value: 4, type: :numeric, base: 10 }, { value: 3, type: :numeric, base: 10 }], - stack + lang.stack end end diff --git a/spec/language_store_spec.rb b/spec/language_store_spec.rb index 446cf13..f672e55 100644 --- a/spec/language_store_spec.rb +++ b/spec/language_store_spec.rb @@ -6,178 +6,100 @@ require_relative '../language' class TestLanguageProgram < Test::Unit::TestCase def test_sto - stack, dictionary = Rpl::Lang::Core.sto( [{ value: '« 2 dup * »', type: :program }, - { value: "'quatre'", type: :name }], - Rpl::Lang::Dictionary.new ) - assert_equal [], stack + lang = Rpl::Language.new + lang.run '« 2 dup * » \'quatre\' sto' + assert_empty lang.stack - stack, _dictionary = Rpl::Lang::Core.eval( [{ value: 'quatre', type: :word }], - dictionary ) + lang.run 'quatre' assert_equal [{ value: 4, type: :numeric, base: 10 }], - stack + lang.stack end def test_rcl - _stack, dictionary = Rpl::Lang::Core.sto( [{ value: '« 2 dup * »', type: :program }, - { value: "'quatre'", type: :name }], - Rpl::Lang::Dictionary.new ) - - stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'quatre'", type: :name }], - dictionary ) + lang = Rpl::Language.new + lang.run '« 2 dup * » \'quatre\' sto \'quatre\' rcl' assert_equal [{ value: '« 2 dup * »', type: :program }], - stack + lang.stack end def test_purge - _stack, dictionary = Rpl::Lang::Core.sto( [{ value: '« 2 dup * »', type: :program }, - { value: "'quatre'", type: :name }], - Rpl::Lang::Dictionary.new ) - - _stack, dictionary = Rpl::Lang::Core.purge( [{ value: "'quatre'", type: :name }], - dictionary ) - assert_equal nil, - dictionary['quatre'] + lang = Rpl::Language.new + lang.run '« 2 dup * » \'quatre\' sto \'quatre\' purge' + assert_nil lang.dictionary['quatre'] end def test_vars - _stack, dictionary = Rpl::Lang::Core.sto( [{ value: '« 2 dup * »', type: :program }, - { value: "'quatre'", type: :name }], - Rpl::Lang::Dictionary.new ) - _stack, dictionary = Rpl::Lang::Core.sto( [{ value: 1, type: :numeric, base: 10 }, - { value: "'un'", type: :name }], - dictionary ) - - stack, _dictionary = Rpl::Lang::Core.vars( [], - dictionary ) + lang = Rpl::Language.new + lang.run '« 2 dup * » \'quatre\' sto 1 \'un\' sto vars' assert_equal [{ value: ["'quatre'", "'un'"], type: :list }], - stack + lang.stack end def test_clusr - _stack, dictionary = Rpl::Lang::Core.sto( [{ value: '« 2 dup * »', type: :program }, - { value: "'quatre'", type: :name }], - Rpl::Lang::Dictionary.new ) - _stack, dictionary = Rpl::Lang::Core.sto( [{ value: 1, type: :numeric, base: 10 }, - { value: "'un'", type: :name }], - dictionary ) - - _stack, dictionary = Rpl::Lang::Core.clusr( [], - dictionary ) - assert_equal( {}, - dictionary.vars ) + lang = Rpl::Language.new + lang.run '« 2 dup * » \'quatre\' sto 1 \'un\' sto clusr' + assert_empty lang.dictionary.vars end def test_sto_add - _stack, dictionary = Rpl::Lang::Core.sto( [{ value: 1, type: :numeric, base: 10 }, - { value: "'un'", type: :name }], - Rpl::Lang::Dictionary.new ) - - _stack, dictionary = Rpl::Lang::Core.sto_add( [{ value: "'un'", type: :name }, - { value: 3, type: :numeric, base: 10 }], - dictionary ) - stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'un'", type: :name }], - dictionary ) + lang = Rpl::Language.new + lang.run '1 \'test\' sto \'test\' 3 sto+ \'test\' rcl' assert_equal [{ value: 4, type: :numeric, base: 10 }], - stack + lang.stack - _stack, dictionary = Rpl::Lang::Core.sto_add( [{ value: 3, type: :numeric, base: 10 }, - { value: "'un'", type: :name }], - dictionary ) - stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'un'", type: :name }], - dictionary ) - assert_equal [{ value: 7, type: :numeric, base: 10 }], - stack + lang = Rpl::Language.new + lang.run '1 \'test\' sto 3 \'test\' sto+ \'test\' rcl' + assert_equal [{ value: 4, type: :numeric, base: 10 }], + lang.stack end def test_sto_subtract - _stack, dictionary = Rpl::Lang::Core.sto( [{ value: 1, type: :numeric, base: 10 }, - { value: "'un'", type: :name }], - Rpl::Lang::Dictionary.new ) - - _stack, dictionary = Rpl::Lang::Core.sto_subtract( [{ value: "'un'", type: :name }, - { value: 3, type: :numeric, base: 10 }], - dictionary ) - stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'un'", type: :name }], - dictionary ) + lang = Rpl::Language.new + lang.run '1 \'test\' sto \'test\' 3 sto- \'test\' rcl' assert_equal [{ value: -2, type: :numeric, base: 10 }], - stack + lang.stack - _stack, dictionary = Rpl::Lang::Core.sto_subtract( [{ value: 3, type: :numeric, base: 10 }, - { value: "'un'", type: :name }], - dictionary ) - stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'un'", type: :name }], - dictionary ) - assert_equal [{ value: -5, type: :numeric, base: 10 }], - stack + lang = Rpl::Language.new + lang.run '1 \'test\' sto 3 \'test\' sto- \'test\' rcl' + assert_equal [{ value: -2, type: :numeric, base: 10 }], + lang.stack end def test_sto_multiply - _stack, dictionary = Rpl::Lang::Core.sto( [{ value: 2, type: :numeric, base: 10 }, - { value: "'un'", type: :name }], - Rpl::Lang::Dictionary.new ) - - _stack, dictionary = Rpl::Lang::Core.sto_multiply( [{ value: "'un'", type: :name }, - { value: 3, type: :numeric, base: 10 }], - dictionary ) - stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'un'", type: :name }], - dictionary ) + lang = Rpl::Language.new + lang.run '2 \'test\' sto \'test\' 3 sto* \'test\' rcl' assert_equal [{ value: 6, type: :numeric, base: 10 }], - stack + lang.stack - _stack, dictionary = Rpl::Lang::Core.sto_multiply( [{ value: 3, type: :numeric, base: 10 }, - { value: "'un'", type: :name }], - dictionary ) - stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'un'", type: :name }], - dictionary ) - assert_equal [{ value: 18, type: :numeric, base: 10 }], - stack + lang = Rpl::Language.new + lang.run '2 \'test\' sto 3 \'test\' sto* \'test\' rcl' + assert_equal [{ value: 6, type: :numeric, base: 10 }], + lang.stack end def test_sto_divide - _stack, dictionary = Rpl::Lang::Core.sto( [{ value: 2, type: :numeric, base: 10 }, - { value: "'un'", type: :name }], - Rpl::Lang::Dictionary.new ) + lang = Rpl::Language.new + lang.run '3 \'test\' sto \'test\' 2.0 sto÷ \'test\' rcl' + assert_equal [{ value: 1.5, type: :numeric, base: 10 }], + lang.stack - _stack, dictionary = Rpl::Lang::Core.sto_divide( [{ value: "'un'", type: :name }, - { value: 4.0, type: :numeric, base: 10 }], - dictionary ) - stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'un'", type: :name }], - dictionary ) - assert_equal [{ value: 0.5, type: :numeric, base: 10 }], - stack - - _stack, dictionary = Rpl::Lang::Core.sto_divide( [{ value: 5, type: :numeric, base: 10 }, - { value: "'un'", type: :name }], - dictionary ) - stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'un'", type: :name }], - dictionary ) - assert_equal [{ value: 0.1, type: :numeric, base: 10 }], - stack + lang = Rpl::Language.new + lang.run '3 \'test\' sto 2.0 \'test\' sto÷ \'test\' rcl' + assert_equal [{ value: 1.5, type: :numeric, base: 10 }], + lang.stack end def test_sto_negate - _stack, dictionary = Rpl::Lang::Core.sto( [{ value: 2, type: :numeric, base: 10 }, - { value: "'un'", type: :name }], - Rpl::Lang::Dictionary.new ) - - _stack, dictionary = Rpl::Lang::Core.sto_negate( [{ value: "'un'", type: :name }], - dictionary ) - stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'un'", type: :name }], - dictionary ) - assert_equal [{ value: -2, type: :numeric, base: 10 }], - stack + lang = Rpl::Language.new + lang.run '3 \'test\' sto \'test\' sneg \'test\' rcl' + assert_equal [{ value: -3, type: :numeric, base: 10 }], + lang.stack end def test_sto_inverse - _stack, dictionary = Rpl::Lang::Core.sto( [{ value: 2, type: :numeric, base: 10 }, - { value: "'un'", type: :name }], - Rpl::Lang::Dictionary.new ) - - _stack, dictionary = Rpl::Lang::Core.sto_inverse( [{ value: "'un'", type: :name }], - dictionary ) - stack, _dictionary = Rpl::Lang::Core.rcl( [{ value: "'un'", type: :name }], - dictionary ) + lang = Rpl::Language.new + lang.run '2 \'test\' sto \'test\' sinv \'test\' rcl' assert_equal [{ value: 0.5, type: :numeric, base: 10 }], - stack + lang.stack end end diff --git a/spec/language_string_spec.rb b/spec/language_string_spec.rb index 7991b1a..8d38621 100644 --- a/spec/language_string_spec.rb +++ b/spec/language_string_spec.rb @@ -7,69 +7,66 @@ require_relative '../language' class TestLanguageString < Test::Unit::TestCase def test_to_string - stack, _dictionary = Rpl::Lang::Core.to_string( [{ value: 2, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new ) + lang = Rpl::Language.new + lang.run '2 →str' assert_equal [{ value: '2', type: :string }], - stack + lang.stack end def test_from_string - stack, _dictionary = Rpl::Lang::Core.from_string( [{ value: '2', type: :string }], - Rpl::Lang::Dictionary.new ) + lang = Rpl::Language.new + lang.run '"2" str→' assert_equal [{ value: 2, type: :numeric, base: 10 }], - stack + lang.stack - stack, _dictionary = Rpl::Lang::Core.from_string( [{ value: "« 2 dup * » 'carré' sto", type: :string }], - Rpl::Lang::Dictionary.new ) + lang = Rpl::Language.new + lang.run '"« dup * » \'carré\' sto" str→' - assert_equal [{ value: '« 2 dup * »', type: :program }, + assert_equal [{ value: '« dup * »', type: :program }, { value: "'carré'", type: :name }, { value: 'sto', type: :word }], - stack + lang.stack end def test_chr - stack, _dictionary = Rpl::Lang::Core.chr( [{ value: 71, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new ) + lang = Rpl::Language.new + lang.run '71 chr' assert_equal [{ value: 'G', type: :string }], - stack + lang.stack end def test_num - stack, _dictionary = Rpl::Lang::Core.num( [{ value: 'G', type: :string }], - Rpl::Lang::Dictionary.new ) + lang = Rpl::Language.new + lang.run '"G" num' assert_equal [{ value: 71, type: :numeric, base: 10 }], - stack + lang.stack end def test_size - stack, _dictionary = Rpl::Lang::Core.size( [{ value: 'test', type: :string }], - Rpl::Lang::Dictionary.new ) + lang = Rpl::Language.new + lang.run '"test" size' assert_equal [{ value: 4, type: :numeric, base: 10 }], - stack + lang.stack end def test_pos - stack, _dictionary = Rpl::Lang::Core.pos( [{ value: 'test of POS', type: :string }, - { value: 'of', type: :string }], - Rpl::Lang::Dictionary.new ) + lang = Rpl::Language.new + lang.run '"test of POS" "of" pos' assert_equal [{ value: 5, type: :numeric, base: 10 }], - stack + lang.stack end def test_sub - stack, _dictionary = Rpl::Lang::Core.sub( [{ value: 'test', type: :string }, - { value: 1, type: :numeric, base: 10 }, - { value: 2, type: :numeric, base: 10 }], - Rpl::Lang::Dictionary.new ) + lang = Rpl::Language.new + lang.run '"my string to sub" 4 6 sub' - assert_equal [{ value: 'es', type: :string }], - stack + assert_equal [{ value: 'str', type: :string }], + lang.stack end end diff --git a/spec/language_time-date_spec.rb b/spec/language_time-date_spec.rb index 97cad93..98fb5d8 100644 --- a/spec/language_time-date_spec.rb +++ b/spec/language_time-date_spec.rb @@ -8,28 +8,28 @@ require_relative '../language' class TestLanguageTimeDate < Test::Unit::TestCase def test_time now = Time.now.to_s - stack, _dictionary = Rpl::Lang::Core.time( [], - Rpl::Lang::Dictionary.new ) + lang = Rpl::Language.new + lang.run 'time' assert_equal [{ value: now, type: :string }], - stack + lang.stack end def test_date now = Date.today.to_s - stack, _dictionary = Rpl::Lang::Core.date( [], - Rpl::Lang::Dictionary.new ) + lang = Rpl::Language.new + lang.run 'date' assert_equal [{ value: now, type: :string }], - stack + lang.stack end def test_ticks - stack, _dictionary = Rpl::Lang::Core.ticks( [], - Rpl::Lang::Dictionary.new ) + lang = Rpl::Language.new + lang.run 'ticks' # TODO: better test, but how? assert_equal :numeric, - stack[0][:type] + lang.stack[0][:type] end end