2021-11-24 13:33:44 +01:00
|
|
|
# coding: utf-8
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-02-17 15:09:29 +01:00
|
|
|
require 'minitest/autorun'
|
2021-11-24 13:33:44 +01:00
|
|
|
|
2022-02-15 17:06:19 +01:00
|
|
|
require 'rpl'
|
2021-11-24 13:33:44 +01:00
|
|
|
|
2022-02-17 15:09:29 +01:00
|
|
|
class TestLanguageString < MiniTest::Test
|
2021-11-24 15:04:56 +01:00
|
|
|
def test_to_string
|
2022-02-10 14:50:59 +01:00
|
|
|
interpreter = Rpl.new
|
2022-02-09 13:38:32 +01:00
|
|
|
interpreter.run '2 →str'
|
2021-11-24 15:04:56 +01:00
|
|
|
|
2022-01-26 18:30:51 +01:00
|
|
|
assert_equal [{ value: '2', type: :string }],
|
2022-02-09 13:38:32 +01:00
|
|
|
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
|
2022-02-09 13:38:32 +01:00
|
|
|
interpreter.run '"2" str→'
|
2021-11-24 15:04:56 +01:00
|
|
|
|
|
|
|
assert_equal [{ value: 2, type: :numeric, base: 10 }],
|
2022-02-09 13:38:32 +01:00
|
|
|
interpreter.stack
|
2021-11-24 15:04:56 +01:00
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
interpreter = Rpl.new
|
2022-02-09 13:38:32 +01:00
|
|
|
interpreter.run '"« dup * » \'carré\' sto" str→'
|
2021-11-24 15:04:56 +01:00
|
|
|
|
2022-01-26 18:30:51 +01:00
|
|
|
assert_equal [{ value: 'dup *', type: :program },
|
|
|
|
{ value: 'carré', type: :name },
|
2021-11-24 15:04:56 +01:00
|
|
|
{ value: 'sto', type: :word }],
|
2022-02-09 13:38:32 +01:00
|
|
|
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
|
2022-02-09 13:38:32 +01:00
|
|
|
interpreter.run '71 chr'
|
2021-11-24 15:04:56 +01:00
|
|
|
|
2022-01-26 18:30:51 +01:00
|
|
|
assert_equal [{ value: 'G', type: :string }],
|
2022-02-09 13:38:32 +01:00
|
|
|
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
|
2022-02-09 13:38:32 +01:00
|
|
|
interpreter.run '"G" num'
|
2021-11-24 15:04:56 +01:00
|
|
|
|
|
|
|
assert_equal [{ value: 71, type: :numeric, base: 10 }],
|
2022-02-09 13:38:32 +01:00
|
|
|
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
|
2022-02-09 13:38:32 +01:00
|
|
|
interpreter.run '"test" size'
|
2021-11-24 15:04:56 +01:00
|
|
|
|
|
|
|
assert_equal [{ value: 4, type: :numeric, base: 10 }],
|
2022-02-09 13:38:32 +01:00
|
|
|
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
|
2022-02-09 13:38:32 +01:00
|
|
|
interpreter.run '"test of POS" "of" pos'
|
2021-11-24 15:04:56 +01:00
|
|
|
|
|
|
|
assert_equal [{ value: 5, type: :numeric, base: 10 }],
|
2022-02-09 13:38:32 +01:00
|
|
|
interpreter.stack
|
2021-11-24 15:04:56 +01:00
|
|
|
end
|
|
|
|
|
2021-11-24 13:33:44 +01:00
|
|
|
def test_sub
|
2022-02-10 14:50:59 +01:00
|
|
|
interpreter = Rpl.new
|
2022-02-09 13:38:32 +01:00
|
|
|
interpreter.run '"my string to sub" 4 6 sub'
|
2021-11-24 13:33:44 +01:00
|
|
|
|
2022-01-26 18:30:51 +01:00
|
|
|
assert_equal [{ value: 'str', type: :string }],
|
2022-02-09 13:38:32 +01:00
|
|
|
interpreter.stack
|
2021-11-24 13:33:44 +01:00
|
|
|
end
|
2021-12-15 13:32:48 +01:00
|
|
|
|
|
|
|
def test_rev
|
2022-02-10 14:50:59 +01:00
|
|
|
interpreter = Rpl.new
|
2022-02-09 13:38:32 +01:00
|
|
|
interpreter.run '"my string to sub" rev'
|
2021-12-15 13:32:48 +01:00
|
|
|
|
2022-01-26 18:30:51 +01:00
|
|
|
assert_equal [{ value: 'my string to sub'.reverse, type: :string }],
|
2022-02-09 13:38:32 +01:00
|
|
|
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
|
2022-02-09 13:38:32 +01:00
|
|
|
interpreter.run '"my string to sub" " " split'
|
2021-12-15 13:32:48 +01:00
|
|
|
|
2022-01-26 18:30:51 +01:00
|
|
|
assert_equal [{ value: 'my', type: :string },
|
|
|
|
{ value: 'string', type: :string },
|
|
|
|
{ value: 'to', type: :string },
|
|
|
|
{ value: 'sub', type: :string }],
|
2022-02-09 13:38:32 +01:00
|
|
|
interpreter.stack
|
2021-12-15 13:32:48 +01:00
|
|
|
|
2022-02-10 14:50:59 +01:00
|
|
|
interpreter = Rpl.new
|
2022-02-09 13:38:32 +01:00
|
|
|
interpreter.run '"my,string,to sub" "," split'
|
2021-12-15 13:32:48 +01:00
|
|
|
|
2022-01-26 18:30:51 +01:00
|
|
|
assert_equal [{ value: 'my', type: :string },
|
|
|
|
{ value: 'string', type: :string },
|
|
|
|
{ value: 'to sub', type: :string }],
|
2022-02-09 13:38:32 +01:00
|
|
|
interpreter.stack
|
2021-12-15 13:32:48 +01:00
|
|
|
end
|
2021-11-24 13:33:44 +01:00
|
|
|
end
|