class GroupingTest

Public Instance Methods

setup() click to toggle source
# File activesupport/test/core_ext/array/grouping_test.rb, line 7
def setup
  # In Ruby < 2.4, test we avoid Integer#/ (redefined by mathn)
  Fixnum.send :private, :/ unless 0.class == Integer
end
teardown() click to toggle source
# File activesupport/test/core_ext/array/grouping_test.rb, line 12
def teardown
  Fixnum.send :public, :/ unless 0.class == Integer
end
test_in_groups_invalid_argument() click to toggle source
# File activesupport/test/core_ext/array/grouping_test.rb, line 97
def test_in_groups_invalid_argument
  assert_raises(ArgumentError) { [].in_groups_of(0) }
  assert_raises(ArgumentError) { [].in_groups_of(-1) }
  assert_raises(ArgumentError) { [].in_groups_of(nil) }
end
test_in_groups_of_pads_with_specified_values() click to toggle source
# File activesupport/test/core_ext/array/grouping_test.rb, line 35
def test_in_groups_of_pads_with_specified_values
  groups = []

  ("a".."g").to_a.in_groups_of(3, "foo") do |group|
    groups << group
  end

  assert_equal [%w(a b c), %w(d e f), %w(g foo foo)], groups
end
test_in_groups_of_with_padding() click to toggle source
# File activesupport/test/core_ext/array/grouping_test.rb, line 26
def test_in_groups_of_with_padding
  groups = []
  ("a".."g").to_a.in_groups_of(3) do |group|
    groups << group
  end

  assert_equal [%w(a b c), %w(d e f), ["g", nil, nil]], groups
end
test_in_groups_of_with_perfect_fit() click to toggle source
# File activesupport/test/core_ext/array/grouping_test.rb, line 16
def test_in_groups_of_with_perfect_fit
  groups = []
  ("a".."i").to_a.in_groups_of(3) do |group|
    groups << group
  end

  assert_equal [%w(a b c), %w(d e f), %w(g h i)], groups
  assert_equal [%w(a b c), %w(d e f), %w(g h i)], ("a".."i").to_a.in_groups_of(3)
end
test_in_groups_of_without_padding() click to toggle source
# File activesupport/test/core_ext/array/grouping_test.rb, line 45
def test_in_groups_of_without_padding
  groups = []

  ("a".."g").to_a.in_groups_of(3, false) do |group|
    groups << group
  end

  assert_equal [%w(a b c), %w(d e f), %w(g)], groups
end
test_in_groups_returned_array_size() click to toggle source
# File activesupport/test/core_ext/array/grouping_test.rb, line 55
def test_in_groups_returned_array_size
  array = (1..7).to_a

  1.upto(array.size + 1) do |number|
    assert_equal number, array.in_groups(number).size
  end
end
test_in_groups_with_block() click to toggle source
# File activesupport/test/core_ext/array/grouping_test.rb, line 67
def test_in_groups_with_block
  array = (1..9).to_a
  groups = []

  array.in_groups(3) do |group|
    groups << group
  end

  assert_equal array.in_groups(3), groups
end
test_in_groups_with_empty_array() click to toggle source
# File activesupport/test/core_ext/array/grouping_test.rb, line 63
def test_in_groups_with_empty_array
  assert_equal [[], [], []], [].in_groups(3)
end
test_in_groups_with_padding() click to toggle source
# File activesupport/test/core_ext/array/grouping_test.rb, line 83
def test_in_groups_with_padding
  array = (1..7).to_a

  assert_equal [[1, 2, 3], [4, 5, nil], [6, 7, nil]],
    array.in_groups(3)
  assert_equal [[1, 2, 3], [4, 5, "foo"], [6, 7, "foo"]],
    array.in_groups(3, "foo")
end
test_in_groups_with_perfect_fit() click to toggle source
# File activesupport/test/core_ext/array/grouping_test.rb, line 78
def test_in_groups_with_perfect_fit
  assert_equal [[1, 2, 3], [4, 5, 6], [7, 8, 9]],
    (1..9).to_a.in_groups(3)
end
test_in_groups_without_padding() click to toggle source
# File activesupport/test/core_ext/array/grouping_test.rb, line 92
def test_in_groups_without_padding
  assert_equal [[1, 2, 3], [4, 5], [6, 7]],
    (1..7).to_a.in_groups(3, false)
end