test/base/test_item_array.rb in nanoc-4.0.0b1 vs test/base/test_item_array.rb in nanoc-4.0.0b2
- old
+ new
@@ -1,15 +1,15 @@
# encoding: utf-8
-class Nanoc::Int::ItemArrayTest < Nanoc::TestCase
+class Nanoc::Int::IdentifiableCollectionTest < Nanoc::TestCase
def setup
super
@one = Nanoc::Int::Item.new('Item One', {}, '/one/')
@two = Nanoc::Int::Item.new('Item Two', {}, '/two/')
- @items = Nanoc::Int::ItemArray.new({})
+ @items = Nanoc::Int::IdentifiableCollection.new({})
@items << @one
@items << @two
end
def test_change_item_identifier
@@ -24,326 +24,58 @@
def test_enumerable
assert_equal @one, @items.find { |i| i.identifier == '/one/' }
end
- def test_brackets_and_slice_and_at_with_index
- assert_equal @one, @items[0]
- assert_equal @one, @items.slice(0)
- assert_equal @one, @items.at(0)
-
- assert_equal @two, @items[1]
- assert_equal @two, @items.slice(1)
- assert_equal @two, @items.at(1)
-
- assert_nil @items[2]
- assert_nil @items.slice(2)
- assert_nil @items.at(2)
-
- assert_equal @two, @items[-1]
- assert_equal @two, @items.slice(-1)
- assert_equal @two, @items.at(-1)
- end
-
def test_brackets_with_glob
- @items = Nanoc::Int::ItemArray.new({ pattern_syntax: 'glob' })
+ @items = Nanoc::Int::IdentifiableCollection.new({ string_pattern_type: 'glob' })
@items << @one
@items << @two
assert_equal @one, @items['/on*/']
assert_equal @two, @items['/*wo/']
end
- def test_brackets_and_slice_with_range
- assert_equal [@one, @two], @items[0..1]
- assert_equal [@one, @two], @items[0, 2]
-
- assert_equal [@one, @two], @items.slice(0..1)
- assert_equal [@one, @two], @items.slice(0, 2)
- end
-
- def test_brackets_and_slice_and_at_with_identifier
+ def test_brackets_with_identifier
assert_equal @one, @items['/one/']
- assert_equal @one, @items.slice('/one/')
- assert_equal @one, @items.at('/one/')
-
assert_equal @two, @items['/two/']
- assert_equal @two, @items.slice('/two/')
- assert_equal @two, @items.at('/two/')
-
assert_nil @items['/max-payne/']
- assert_nil @items.slice('/max-payne/')
- assert_nil @items.at('/max-payne/')
end
- def test_brackets_and_slice_and_at_with_malformed_identifier
+ def test_brackets_with_malformed_identifier
assert_nil @items['one/']
- assert_nil @items.slice('one/')
- assert_nil @items.at('one/')
-
assert_nil @items['/one']
- assert_nil @items.slice('/one')
- assert_nil @items.at('/one')
-
assert_nil @items['one']
- assert_nil @items.slice('one')
- assert_nil @items.at('one')
-
assert_nil @items['//one/']
- assert_nil @items.slice('//one/')
- assert_nil @items.at('//one/')
end
- def test_brackets_and_slice_and_at_frozen
+ def test_brackets_frozen
@items.freeze
assert_equal @one, @items['/one/']
- assert_equal @one, @items.slice('/one/')
- assert_equal @one, @items.at('/one/')
-
assert_nil @items['/tenthousand/']
- assert_nil @items.slice('/tenthousand/')
- assert_nil @items.at('/tenthousand/')
end
def test_regex
foo = Nanoc::Int::Item.new('Item Foo', {}, '/foo/')
@items << foo
- assert_equal [@one], @items[/n/]
- assert_equal [@two, foo], @items[%r{o/}]
+ assert_equal @one, @items[/n/]
+ assert_equal @two, @items[%r{o/}] # not foo
end
def test_less_than_less_than
- assert_nil @items[2]
assert_nil @items['/foo/']
foo = Nanoc::Int::Item.new('Item Foo', {}, '/foo/')
@items << foo
- assert_equal foo, @items[2]
assert_equal foo, @items['/foo/']
end
- def test_assign
- assert_raises(TypeError) do
- @items['/blah/'] = Nanoc::Int::Item.new('Item blah', {}, '/blah/')
- end
-
- new_item = Nanoc::Int::Item.new('New Item One', {}, '/one-new/')
- @items[0] = new_item
-
- assert_equal new_item, @items[0]
- assert_equal new_item, @items['/one-new/']
- assert_nil @items['/one/']
- end
-
- def test_assign_frozen
- @items.freeze
-
- new_item = Nanoc::Int::Item.new('New Item One', {}, '/one-new/')
-
- assert_raises_frozen_error do
- @items[0] = new_item
- end
- end
-
- def test_clear
- @items.clear
-
- assert_nil @items[0]
- assert_nil @items[1]
- assert_nil @items[2]
-
- assert_nil @items['/one/']
- assert_nil @items['/two/']
- end
-
- def test_collect_bang
- @items.collect! do |i|
- Nanoc::Int::Item.new("New #{i.raw_content}", {}, "/new#{i.identifier}")
- end
-
- assert_nil @items['/one/']
- assert_nil @items['/two/']
-
- assert_equal 'New Item One', @items[0].raw_content
- assert_equal 'New Item One', @items['/new/one/'].raw_content
-
- assert_equal 'New Item Two', @items[1].raw_content
- assert_equal 'New Item Two', @items['/new/two/'].raw_content
- end
-
- def test_collect_bang_frozen
- @items.freeze
-
- assert_raises_frozen_error do
- @items.collect! do |i|
- Nanoc::Int::Item.new("New #{i.raw_content}", {}, "/new#{i.identifier}")
- end
- end
- end
-
def test_concat
new_item = Nanoc::Int::Item.new('New item', {}, '/new/')
@items.concat([new_item])
- assert_equal new_item, @items[2]
assert_equal new_item, @items['/new/']
- end
-
- def test_delete
- assert_equal @two, @items[1]
- assert_equal @two, @items['/two/']
-
- @items.delete(@two)
-
- assert_nil @items[1]
- assert_nil @items['/two/']
- end
-
- def test_delete_at
- assert_equal @two, @items[1]
- assert_equal @two, @items['/two/']
-
- @items.delete_at(1)
-
- assert_nil @items[1]
- assert_nil @items['/two/']
- end
-
- def test_delete_if
- assert_equal @two, @items[1]
- assert_equal @two, @items['/two/']
-
- @items.delete_if { |i| i.identifier == '/two/' }
-
- assert_nil @items[1]
- assert_nil @items['/two/']
- end
-
- def test_fill_all
- @items.fill { |i| Nanoc::Int::Item.new("Item #{i}", {}, "/new/#{i}/") }
-
- assert_nil @items['/one/']
- assert_nil @items['/two/']
-
- assert_equal 'Item 0', @items[0].raw_content
- assert_equal 'Item 0', @items['/new/0/'].raw_content
- assert_equal 'Item 1', @items[1].raw_content
- assert_equal 'Item 1', @items['/new/1/'].raw_content
- end
-
- def test_fill_range
- @items.fill(1..-1) { |i| Nanoc::Int::Item.new("Item #{i}", {}, "/new/#{i}/") }
-
- assert_equal @one, @items['/one/']
- assert_nil @items['/two/']
-
- assert_equal @one, @items[0]
- assert_equal @one, @items['/one/']
- assert_equal 'Item 1', @items[1].raw_content
- assert_equal 'Item 1', @items['/new/1/'].raw_content
- end
-
- if [].respond_to?(:keep_if)
- def test_keep_if
- assert_equal @two, @items[1]
- assert_equal @two, @items['/two/']
-
- @items.keep_if { |i| i.identifier == '/one/' }
-
- assert_equal @one, @items[0]
- assert_equal @one, @items['/one/']
- assert_nil @items[1]
- assert_nil @items['/two/']
- end
- end
-
- def test_pop
- @items.pop
-
- assert_equal @one, @items[0]
- assert_equal @one, @items['/one/']
- assert_nil @items[1]
- assert_nil @items['/two/']
- end
-
- def test_push
- pushy = Nanoc::Int::Item.new('Pushy', {}, '/pushy/')
- @items.push(pushy)
-
- assert_equal @one, @items[0]
- assert_equal @one, @items['/one/']
- assert_equal @two, @items[1]
- assert_equal @two, @items['/two/']
- assert_equal pushy, @items[2]
- assert_equal pushy, @items['/pushy/']
- end
-
- def test_reject_bang
- assert_equal @two, @items[1]
- assert_equal @two, @items['/two/']
-
- @items.reject! { |i| i.identifier == '/two/' }
-
- assert_nil @items[1]
- assert_nil @items['/two/']
- end
-
- def test_replace
- max = Nanoc::Int::Item.new('Max', {}, '/max/')
- mona = Nanoc::Int::Item.new('Mona', {}, '/mona/')
-
- @items.replace([max, mona])
-
- assert_nil @items['/one/']
- assert_nil @items['/two/']
-
- assert_equal max, @items[0]
- assert_equal max, @items['/max/']
- assert_equal mona, @items[1]
- assert_equal mona, @items['/mona/']
- end
-
- if [].respond_to?(:select!)
- def test_select_bang
- assert_equal @two, @items[1]
- assert_equal @two, @items['/two/']
-
- @items.select! { |i| i.identifier == '/two/' }
-
- assert_nil @items[1]
- assert_nil @items['/one/']
- end
- end
-
- def test_shift
- @items.shift
-
- assert_equal @two, @items[0]
- assert_equal @two, @items['/two/']
- assert_nil @items['/one/']
- assert_nil @items[1]
- end
-
- def test_slice_bang
- @items.slice!(1)
-
- assert_equal @one, @items[0]
- assert_equal @one, @items['/one/']
- assert_nil @items[1]
- assert_nil @items['/two/']
- end
-
- def test_unshift
- unshifty = Nanoc::Int::Item.new('Unshifty', {}, '/unshifty/')
- @items.unshift(unshifty)
-
- assert_equal unshifty, @items[0]
- assert_equal unshifty, @items['/unshifty/']
- assert_equal @one, @items[1]
- assert_equal @one, @items['/one/']
- assert_equal @two, @items[2]
- assert_equal @two, @items['/two/']
end
end