summaryrefslogtreecommitdiffhomepage
path: root/test/util
diff options
context:
space:
mode:
authorPaul Kmiec <[email protected]>2023-05-10 21:34:19 -0700
committerPaul Kmiec <[email protected]>2023-05-10 21:34:19 -0700
commit1d94514b6562131371130449eaccf711aa60dc95 (patch)
tree7b8a034297d3d3a3a552e08372520651c3fa040a /test/util
parent67d8a1a781e53761a483575cc5abc7e9a992d2ca (diff)
downloadcaxlsx-1d94514b6562131371130449eaccf711aa60dc95.tar.gz
caxlsx-1d94514b6562131371130449eaccf711aa60dc95.zip
Derive SimpleTypedList from Array
Currently the SimpleTypedList is wrapping `@list` Array and re-implementing certain methods to add type / value checking and delegating all the non-destrutive methods to the internal @list. Most everything that ends up being serialized to the excel file is derived from SimpleTypedList (for example, a Row is a SimpleTypedList of Cells). This means that many of the delegated methods are hit over and over again especially `[]`. This extra delegation really adds up. We can avoid the delegation by having SimpleTypedList extend Array and still remove any un-safe methods. It is possible to re-introduce an unsafe method in a subclass of SimpleTypedList if needed. Here is one way, ```ruby class MyList < SimpleTypedList def clear @clear_method ||= Array.instance_method(:clear).bind(self) @clear_method.call end end ```
Diffstat (limited to 'test/util')
-rw-r--r--test/util/tc_simple_typed_list.rb23
1 files changed, 23 insertions, 0 deletions
diff --git a/test/util/tc_simple_typed_list.rb b/test/util/tc_simple_typed_list.rb
index ac65529d..b62b5353 100644
--- a/test/util/tc_simple_typed_list.rb
+++ b/test/util/tc_simple_typed_list.rb
@@ -58,6 +58,9 @@ class TestSimpleTypedList < Test::Unit::TestCase
assert_raise(ArgumentError) { @list.delete 1 }
assert_raise(ArgumentError) { @list.delete_at 1 }
assert_raise(ArgumentError) { @list.delete_at 2 }
+ assert_raise(ArgumentError) { @list.insert(1, 3) }
+ assert_raise(ArgumentError) { @list[1] = 3 }
+
@list.push 4
assert_nothing_raised { @list.delete_at 3 }
@list.unlock
@@ -81,4 +84,24 @@ class TestSimpleTypedList < Test::Unit::TestCase
assert_equal([1, 2], @list.to_ary)
end
+
+ def test_insert
+ assert_raise(ArgumentError) { @list << nil }
+
+ assert_equal(1, @list.insert(0, 1))
+ assert_equal(2, @list.insert(1, 2))
+ assert_equal(3, @list.insert(0, 3))
+
+ assert_equal([3, 1, 2], @list)
+ end
+
+ def test_setter
+ assert_raise(ArgumentError) { @list[0] = nil }
+
+ assert_equal(1, @list[0] = 1)
+ assert_equal(2, @list[1] = 2)
+ assert_equal(3, @list[0] = 3)
+
+ assert_equal([3, 2], @list)
+ end
end