rpl.rb/spec/language_string_spec.rb

100 lines
2.4 KiB
Ruby
Raw Normal View History

# coding: utf-8
# frozen_string_literal: true
require 'test/unit'
2022-02-10 14:50:59 +01:00
require_relative '../rpl'
class TestLanguageString < Test::Unit::TestCase
2021-11-24 15:04:56 +01:00
def test_to_string
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '2 →str'
2021-11-24 15:04:56 +01:00
assert_equal [{ value: '2', type: :string }],
interpreter.stack
2021-11-24 15:04:56 +01:00
end
def test_from_string
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '"2" str→'
2021-11-24 15:04:56 +01:00
assert_equal [{ value: 2, type: :numeric, base: 10 }],
interpreter.stack
2021-11-24 15:04:56 +01:00
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '"« dup * » \'carré\' sto" str→'
2021-11-24 15:04:56 +01:00
assert_equal [{ value: 'dup *', type: :program },
{ value: 'carré', type: :name },
2021-11-24 15:04:56 +01:00
{ value: 'sto', type: :word }],
interpreter.stack
2021-11-24 15:04:56 +01:00
end
def test_chr
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '71 chr'
2021-11-24 15:04:56 +01:00
assert_equal [{ value: 'G', type: :string }],
interpreter.stack
2021-11-24 15:04:56 +01:00
end
def test_num
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '"G" num'
2021-11-24 15:04:56 +01:00
assert_equal [{ value: 71, type: :numeric, base: 10 }],
interpreter.stack
2021-11-24 15:04:56 +01:00
end
def test_size
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '"test" size'
2021-11-24 15:04:56 +01:00
assert_equal [{ value: 4, type: :numeric, base: 10 }],
interpreter.stack
2021-11-24 15:04:56 +01:00
end
def test_pos
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '"test of POS" "of" pos'
2021-11-24 15:04:56 +01:00
assert_equal [{ value: 5, type: :numeric, base: 10 }],
interpreter.stack
2021-11-24 15:04:56 +01:00
end
def test_sub
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '"my string to sub" 4 6 sub'
assert_equal [{ value: 'str', type: :string }],
interpreter.stack
end
2021-12-15 13:32:48 +01:00
def test_rev
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '"my string to sub" rev'
2021-12-15 13:32:48 +01:00
assert_equal [{ value: 'my string to sub'.reverse, type: :string }],
interpreter.stack
2021-12-15 13:32:48 +01:00
end
def test_split
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '"my string to sub" " " split'
2021-12-15 13:32:48 +01:00
assert_equal [{ value: 'my', type: :string },
{ value: 'string', type: :string },
{ value: 'to', type: :string },
{ value: 'sub', type: :string }],
interpreter.stack
2021-12-15 13:32:48 +01:00
2022-02-10 14:50:59 +01:00
interpreter = Rpl.new
interpreter.run '"my,string,to sub" "," split'
2021-12-15 13:32:48 +01:00
assert_equal [{ value: 'my', type: :string },
{ value: 'string', type: :string },
{ value: 'to sub', type: :string }],
interpreter.stack
2021-12-15 13:32:48 +01:00
end
end