aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/core_ext/array/grouping_test.rb
blob: c182b91826c67b80edf286160fec7d3f2e8e6fd6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# frozen_string_literal: true

require "abstract_unit"
require "active_support/core_ext/array"

class GroupingTest < ActiveSupport::TestCase
  def setup
    # In Ruby < 2.4, test we avoid Integer#/ (redefined by mathn)
    Fixnum.send :private, :/ unless 0.class == Integer
  end

  def teardown
    Fixnum.send :public, :/ unless 0.class == Integer
  end

  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

  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

  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

  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

  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

  def test_in_groups_with_empty_array
    assert_equal [[], [], []], [].in_groups(3)
  end

  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

  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

  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

  def test_in_groups_without_padding
    assert_equal [[1, 2, 3], [4, 5], [6, 7]],
      (1..7).to_a.in_groups(3, false)
  end

  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
end

class SplitTest < ActiveSupport::TestCase
  def test_split_with_empty_array
    assert_equal [[]], [].split(0)
  end

  def test_split_with_argument
    a = [1, 2, 3, 4, 5]
    assert_equal [[1, 2], [4, 5]],  a.split(3)
    assert_equal [[1, 2, 3, 4, 5]], a.split(0)
    assert_equal [1, 2, 3, 4, 5], a
  end

  def test_split_with_block
    a = (1..10).to_a
    assert_equal [[1, 2], [4, 5], [7, 8], [10]], a.split { |i| i % 3 == 0 }
    assert_equal [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], a
  end

  def test_split_with_edge_values
    a = [1, 2, 3, 4, 5]
    assert_equal [[], [2, 3, 4, 5]],  a.split(1)
    assert_equal [[1, 2, 3, 4], []],  a.split(5)
    assert_equal [[], [2, 3, 4], []], a.split { |i| i == 1 || i == 5 }
    assert_equal [1, 2, 3, 4, 5], a
  end

  def test_split_with_repeated_values
    a = [1, 2, 3, 5, 5, 3, 4, 6, 2, 1, 3]
    assert_equal [[1, 2], [5, 5], [4, 6, 2, 1], []], a.split(3)
    assert_equal [[1, 2, 3], [], [3, 4, 6, 2, 1, 3]], a.split(5)
    assert_equal [[1, 2], [], [], [], [4, 6, 2, 1], []], a.split { |i| i == 3 || i == 5 }
    assert_equal [1, 2, 3, 5, 5, 3, 4, 6, 2, 1, 3], a
  end
end