From 62d8d92b5049878c776a873ecadd804d36299372 Mon Sep 17 00:00:00 2001 From: Torsten Date: Mon, 9 Mar 2020 13:47:57 +0200 Subject: [PATCH] restrict list index to integer --- lib/parfait/list.rb | 1 + test/parfait/test_list1.rb | 3 +++ 2 files changed, 4 insertions(+) diff --git a/lib/parfait/list.rb b/lib/parfait/list.rb index ac4ce759..ef4f286d 100644 --- a/lib/parfait/list.rb +++ b/lib/parfait/list.rb @@ -53,6 +53,7 @@ module Parfait # set the value at index. # Lists start from index 0 def get( index ) + raise "Only integers, #{index.class}" unless index.is_a?(::Integer) raise "Only positive indexes, #{index}" if index < 0 if index >= data_length return nil unless @next_list diff --git a/test/parfait/test_list1.rb b/test/parfait/test_list1.rb index 3b4d0e7c..413fa6fa 100644 --- a/test/parfait/test_list1.rb +++ b/test/parfait/test_list1.rb @@ -32,6 +32,9 @@ module Parfait assert_equal 2 , @list.index_of( :three ) assert_nil @list.index_of( :four ) end + def test_get_fail + assert_raises{ @list.get(:me) } + end def test_inspect assert @list.inspect.include?("one") , @list.inspect assert @list.inspect.include?("three") , @list.inspect