more_core_extensions 1.2.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +2 -1
- data/README.md +5 -5
- data/lib/more_core_extensions/core_ext/array/deletes.rb +2 -1
- data/lib/more_core_extensions/core_ext/array/duplicates.rb +1 -1
- data/lib/more_core_extensions/core_ext/array/element_counts.rb +8 -2
- data/lib/more_core_extensions/core_ext/array/inclusions.rb +3 -0
- data/lib/more_core_extensions/core_ext/hash/deletes.rb +1 -0
- data/lib/more_core_extensions/core_ext/object/namespace.rb +1 -9
- data/lib/more_core_extensions/version.rb +1 -1
- data/spec/core_ext/array/deletes_spec.rb +9 -11
- data/spec/core_ext/array/duplicates_spec.rb +4 -6
- data/spec/core_ext/array/element_counts_spec.rb +13 -7
- data/spec/core_ext/array/inclusions_spec.rb +35 -25
- data/spec/core_ext/array/nested_spec.rb +61 -63
- data/spec/core_ext/array/random_spec.rb +8 -10
- data/spec/core_ext/array/stretch_spec.rb +9 -11
- data/spec/core_ext/array/tableize_spec.rb +23 -25
- data/spec/core_ext/hash/deletes_spec.rb +8 -10
- data/spec/core_ext/hash/nested_spec.rb +63 -64
- data/spec/core_ext/object/namespace_spec.rb +4 -20
- data/spec/core_ext/string/formats_spec.rb +46 -48
- data/spec/core_ext/string/hex_dump_spec.rb +12 -14
- data/spec/spec_helper.rb +70 -6
- metadata +22 -44
- data/.gitignore +0 -17
- data/.travis.yml +0 -4
- data/Gemfile +0 -4
- data/Rakefile +0 -6
- data/more_core_extensions.gemspec +0 -27
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4e072368f5dec0db13f1df4e2ff8319494087d15
|
4
|
+
data.tar.gz: cf9f2bbb0c0a9ae9857a073479e44816ea622149
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f2a860d28460607a3d28f51e140754171ee53e49c5546a25f5202f35f620e3d2bfb8609a3ffe6afbe826a0ccac48abe033d0e0f2a9a058d35450d67ce213d4c7
|
7
|
+
data.tar.gz: 4515f726acb409bff78d078e92f3ce56daf3d60d57f45858149782be684f5c33b213f132d71267171e03d577cc7c8fd21e66718d747a03d69077e558491e7ee5
|
data/.rspec
CHANGED
data/README.md
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
MoreCoreExtensions are a set of core extensions beyond those provided by ActiveSupport.
|
4
4
|
|
5
|
-
[![Gem Version](https://badge.fury.io/rb/more_core_extensions.
|
6
|
-
[![Build Status](https://travis-ci.org/ManageIQ/more_core_extensions.
|
7
|
-
[![Code Climate](
|
8
|
-
[![Coverage Status](
|
9
|
-
[![Dependency Status](https://gemnasium.com/ManageIQ/more_core_extensions.
|
5
|
+
[![Gem Version](https://badge.fury.io/rb/more_core_extensions.svg)](http://badge.fury.io/rb/more_core_extensions)
|
6
|
+
[![Build Status](https://travis-ci.org/ManageIQ/more_core_extensions.svg?branch=master)](https://travis-ci.org/ManageIQ/more_core_extensions)
|
7
|
+
[![Code Climate](http://img.shields.io/codeclimate/github/ManageIQ/more_core_extensions.svg)](https://codeclimate.com/github/ManageIQ/more_core_extensions)
|
8
|
+
[![Coverage Status](http://img.shields.io/coveralls/ManageIQ/more_core_extensions.svg)](https://coveralls.io/r/ManageIQ/more_core_extensions)
|
9
|
+
[![Dependency Status](https://gemnasium.com/ManageIQ/more_core_extensions.svg)](https://gemnasium.com/ManageIQ/more_core_extensions)
|
10
10
|
|
11
11
|
## Extensions Provided
|
12
12
|
|
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'active_support'
|
1
2
|
require 'active_support/core_ext/object/blank'
|
2
3
|
|
3
4
|
module MoreCoreExtensions
|
@@ -18,4 +19,4 @@ module MoreCoreExtensions
|
|
18
19
|
end
|
19
20
|
end
|
20
21
|
|
21
|
-
Array.send(:include, MoreCoreExtensions::ArrayDeletes)
|
22
|
+
Array.send(:include, MoreCoreExtensions::ArrayDeletes)
|
@@ -1,10 +1,16 @@
|
|
1
1
|
module MoreCoreExtensions
|
2
2
|
module ArrayElementCounts
|
3
|
-
# Returns a Hash of each element to the count of those elements.
|
3
|
+
# Returns a Hash of each element to the count of those elements. Optionally
|
4
|
+
# pass a block to count by a different criteria.
|
4
5
|
#
|
5
6
|
# [1, 2, 3, 1, 3, 1].counts # => {1 => 3, 2 => 1, 3 => 2}
|
7
|
+
# %w(a aa aaa a aaa a).counts { |i| i.length } # => {1 => 3, 2 => 1, 3 => 2}
|
8
|
+
#
|
6
9
|
def element_counts
|
7
|
-
each_with_object(Hash.new(0))
|
10
|
+
each_with_object(Hash.new(0)) do |i, h|
|
11
|
+
key = block_given? ? yield(i) : i
|
12
|
+
h[key] += 1
|
13
|
+
end
|
8
14
|
end
|
9
15
|
end
|
10
16
|
end
|
@@ -7,6 +7,7 @@ module MoreCoreExtensions
|
|
7
7
|
# [1, 2, 3].include_any?(1, 4) #=> true
|
8
8
|
# [1, 2, 3].include_any?(4, 5) #=> false
|
9
9
|
def include_any?(*items)
|
10
|
+
items = items.first if items.length == 1 && items.first.kind_of?(Array)
|
10
11
|
!(self & items).empty?
|
11
12
|
end
|
12
13
|
|
@@ -17,6 +18,7 @@ module MoreCoreExtensions
|
|
17
18
|
# [1, 2, 3].include_none?(1, 4) #=> false
|
18
19
|
# [1, 2, 3].include_none?(4, 5) #=> true
|
19
20
|
def include_none?(*items)
|
21
|
+
items = items.first if items.length == 1 && items.first.kind_of?(Array)
|
20
22
|
(self & items).empty?
|
21
23
|
end
|
22
24
|
|
@@ -27,6 +29,7 @@ module MoreCoreExtensions
|
|
27
29
|
# [1, 2, 3].include_all?(1, 4) #=> false
|
28
30
|
# [1, 2, 3].include_all?(4, 5) #=> false
|
29
31
|
def include_all?(*items)
|
32
|
+
items = items.first if items.length == 1 && items.first.kind_of?(Array)
|
30
33
|
(items - self).empty?
|
31
34
|
end
|
32
35
|
|
@@ -11,15 +11,7 @@ module MoreCoreExtensions
|
|
11
11
|
# Aaa::Bbb::Ccc::Eee.in_namespace?(Aaa::Bbb::Ccc::Ddd) #=> false
|
12
12
|
def in_namespace?(val)
|
13
13
|
val_ns = val.to_s.split("::")
|
14
|
-
val_ns == namespace[0, val_ns.length]
|
15
|
-
end
|
16
|
-
|
17
|
-
#
|
18
|
-
# Returns an Array with the namespace to an Instance.
|
19
|
-
#
|
20
|
-
# Aaa::Bbb::Ccc::Ddd.new.namespace #=> ["Aaa", "Bbb", "Ccc", "Ddd"]
|
21
|
-
def namespace
|
22
|
-
self.class.namespace
|
14
|
+
val_ns == (kind_of?(Module) ? namespace : self.class.namespace)[0, val_ns.length]
|
23
15
|
end
|
24
16
|
end
|
25
17
|
end
|
@@ -1,17 +1,15 @@
|
|
1
|
-
require_relative "../../spec_helper"
|
2
|
-
|
3
1
|
describe Array do
|
4
2
|
it "#delete_nils" do
|
5
|
-
[].delete_nils.
|
6
|
-
[1].delete_nils.
|
7
|
-
[nil].delete_nils.
|
8
|
-
[1, [], nil].delete_nils.
|
3
|
+
expect([].delete_nils).to eq([])
|
4
|
+
expect([1].delete_nils).to eq([1])
|
5
|
+
expect([nil].delete_nils).to eq([])
|
6
|
+
expect([1, [], nil].delete_nils).to eq([1, []])
|
9
7
|
end
|
10
8
|
|
11
9
|
it "#delete_blanks" do
|
12
|
-
[].delete_blanks.
|
13
|
-
[1].delete_blanks.
|
14
|
-
[nil].delete_blanks.
|
15
|
-
[1, [], nil].delete_blanks.
|
10
|
+
expect([].delete_blanks).to eq([])
|
11
|
+
expect([1].delete_blanks).to eq([1])
|
12
|
+
expect([nil].delete_blanks).to eq([])
|
13
|
+
expect([1, [], nil].delete_blanks).to eq([1])
|
16
14
|
end
|
17
|
-
end
|
15
|
+
end
|
@@ -1,11 +1,9 @@
|
|
1
|
-
require_relative "../../spec_helper"
|
2
|
-
|
3
1
|
describe Array do
|
4
2
|
it '#duplicates' do
|
5
|
-
[1, 2, 3, 4].duplicates.
|
6
|
-
[1, 2, 3, 4, 2, 4].duplicates.
|
3
|
+
expect([1, 2, 3, 4].duplicates).to be_empty
|
4
|
+
expect([1, 2, 3, 4, 2, 4].duplicates).to match_array [2, 4]
|
7
5
|
|
8
|
-
['1', '2', '3', '4'].duplicates.
|
9
|
-
['1', '2', '3', '4', '2', '4'].duplicates.
|
6
|
+
expect(['1', '2', '3', '4'].duplicates).to be_empty
|
7
|
+
expect(['1', '2', '3', '4', '2', '4'].duplicates).to match_array ['2', '4']
|
10
8
|
end
|
11
9
|
end
|
@@ -1,10 +1,16 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
1
|
describe Array do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
2
|
+
describe "#element_counts" do
|
3
|
+
it "without a block" do
|
4
|
+
expect([].element_counts).to eq({})
|
5
|
+
expect([1].element_counts).to eq({1 => 1})
|
6
|
+
expect([nil].element_counts).to eq({nil => 1})
|
7
|
+
expect([1, 2, 3, 1, 3, 1].element_counts).to eq({1 => 3, 2 => 1, 3 => 2})
|
8
|
+
end
|
9
|
+
|
10
|
+
it "with a block" do
|
11
|
+
expect([].element_counts(&:size)).to eq({})
|
12
|
+
expect(%w(a).element_counts(&:size)).to eq({1 => 1})
|
13
|
+
expect(%w(a aa aaa a aaa a).element_counts(&:size)).to eq({1 => 3, 2 => 1, 3 => 2})
|
14
|
+
end
|
9
15
|
end
|
10
16
|
end
|
@@ -1,41 +1,51 @@
|
|
1
|
-
require_relative "../../spec_helper"
|
2
|
-
|
3
1
|
describe Array do
|
4
2
|
it '#include_any?' do
|
5
|
-
[1, 2, 3].include_any?(1, 2).
|
6
|
-
[1, 2, 3].include_any?(1, 4).
|
7
|
-
[1, 2, 3].include_any?(4, 5).
|
3
|
+
expect([1, 2, 3].include_any?(1, 2)).to be_truthy
|
4
|
+
expect([1, 2, 3].include_any?(1, 4)).to be_truthy
|
5
|
+
expect([1, 2, 3].include_any?(4, 5)).to be_falsey
|
6
|
+
expect([1, 2, 3].include_any?([1, 4])).to be_truthy
|
7
|
+
expect([1, 2, 3].include_any?([4, 5])).to be_falsey
|
8
8
|
|
9
|
-
['1', '2', '3'].include_any?('1', '2').
|
10
|
-
['1', '2', '3'].include_any?('1', '4').
|
11
|
-
['1', '2', '3'].include_any?('4', '5').
|
9
|
+
expect(['1', '2', '3'].include_any?('1', '2')).to be_truthy
|
10
|
+
expect(['1', '2', '3'].include_any?('1', '4')).to be_truthy
|
11
|
+
expect(['1', '2', '3'].include_any?('4', '5')).to be_falsey
|
12
|
+
expect(['1', '2', '3'].include_any?(['1', '4'])).to be_truthy
|
13
|
+
expect(['1', '2', '3'].include_any?(['4', '5'])).to be_falsey
|
12
14
|
end
|
13
15
|
|
14
16
|
it '#include_none?' do
|
15
|
-
[1, 2, 3].include_none?(1, 2).
|
16
|
-
[1, 2, 3].include_none?(1, 4).
|
17
|
-
[1, 2, 3].include_none?(4, 5).
|
17
|
+
expect([1, 2, 3].include_none?(1, 2)).to be_falsey
|
18
|
+
expect([1, 2, 3].include_none?(1, 4)).to be_falsey
|
19
|
+
expect([1, 2, 3].include_none?(4, 5)).to be_truthy
|
20
|
+
expect([1, 2, 3].include_none?([1, 4])).to be_falsey
|
21
|
+
expect([1, 2, 3].include_none?([4, 5])).to be_truthy
|
18
22
|
|
19
|
-
['1', '2', '3'].include_none?('1', '2').
|
20
|
-
['1', '2', '3'].include_none?('1', '4').
|
21
|
-
['1', '2', '3'].include_none?('4', '5').
|
23
|
+
expect(['1', '2', '3'].include_none?('1', '2')).to be_falsey
|
24
|
+
expect(['1', '2', '3'].include_none?('1', '4')).to be_falsey
|
25
|
+
expect(['1', '2', '3'].include_none?('4', '5')).to be_truthy
|
26
|
+
expect(['1', '2', '3'].include_none?(['1', '4'])).to be_falsey
|
27
|
+
expect(['1', '2', '3'].include_none?(['4', '5'])).to be_truthy
|
22
28
|
end
|
23
29
|
|
24
30
|
it '#include_all?' do
|
25
|
-
[1, 2, 3].include_all?(1, 2).
|
26
|
-
[1, 2, 3].include_all?(1, 4).
|
27
|
-
[1, 2, 3].include_all?(4, 5).
|
31
|
+
expect([1, 2, 3].include_all?(1, 2)).to be_truthy
|
32
|
+
expect([1, 2, 3].include_all?(1, 4)).to be_falsey
|
33
|
+
expect([1, 2, 3].include_all?(4, 5)).to be_falsey
|
34
|
+
expect([1, 2, 3].include_all?([1, 2])).to be_truthy
|
35
|
+
expect([1, 2, 3].include_all?([1, 4])).to be_falsey
|
28
36
|
|
29
|
-
['1', '2', '3'].include_all?('1', '2').
|
30
|
-
['1', '2', '3'].include_all?('1', '4').
|
31
|
-
['1', '2', '3'].include_all?('4', '5').
|
37
|
+
expect(['1', '2', '3'].include_all?('1', '2')).to be_truthy
|
38
|
+
expect(['1', '2', '3'].include_all?('1', '4')).to be_falsey
|
39
|
+
expect(['1', '2', '3'].include_all?('4', '5')).to be_falsey
|
40
|
+
expect(['1', '2', '3'].include_all?(['1', '2'])).to be_truthy
|
41
|
+
expect(['1', '2', '3'].include_all?(['1', '4'])).to be_falsey
|
32
42
|
end
|
33
43
|
|
34
44
|
it "#includes_index?" do
|
35
|
-
[1, 2, 3].includes_index?(-4).
|
36
|
-
[1, 2, 3].includes_index?(-3).
|
37
|
-
[1, 2, 3].includes_index?(1).
|
38
|
-
[1, 2, 3].includes_index?(2).
|
39
|
-
[1, 2, 3].includes_index?(3).
|
45
|
+
expect([1, 2, 3].includes_index?(-4)).to be_falsey
|
46
|
+
expect([1, 2, 3].includes_index?(-3)).to be_truthy
|
47
|
+
expect([1, 2, 3].includes_index?(1)).to be_truthy
|
48
|
+
expect([1, 2, 3].includes_index?(2)).to be_truthy
|
49
|
+
expect([1, 2, 3].includes_index?(3)).to be_falsey
|
40
50
|
end
|
41
51
|
end
|
@@ -1,5 +1,3 @@
|
|
1
|
-
require_relative "../../spec_helper"
|
2
|
-
|
3
1
|
shared_examples_for "core_ext/array/nested will not modify arguments" do |meth|
|
4
2
|
it "will not modify arguments" do
|
5
3
|
args = (meth == :store_path ? [1] : [])
|
@@ -7,44 +5,44 @@ shared_examples_for "core_ext/array/nested will not modify arguments" do |meth|
|
|
7
5
|
key = [3, 0, 1, 2]
|
8
6
|
key2 = key.dup
|
9
7
|
array.send(meth, key2, *args)
|
10
|
-
key2.
|
8
|
+
expect(key2).to eq(key)
|
11
9
|
|
12
10
|
key = [4, 0, 1]
|
13
11
|
key2 = key.dup
|
14
12
|
array.send(meth, key2, *args)
|
15
|
-
key2.
|
13
|
+
expect(key2).to eq(key)
|
16
14
|
end
|
17
15
|
end
|
18
16
|
|
19
17
|
shared_examples_for "core_ext/array/nested" do
|
20
18
|
context '#fetch_path' do
|
21
19
|
it "with various values" do
|
22
|
-
array.fetch_path(0).
|
23
|
-
array.fetch_path(1).
|
24
|
-
array.fetch_path(1, 0).
|
25
|
-
array.fetch_path(1, 0, 1).
|
26
|
-
array.fetch_path(2).
|
27
|
-
array.fetch_path(2, 0).
|
28
|
-
array.fetch_path(-4, 0).
|
29
|
-
array.fetch_path(2, 0, 1).
|
30
|
-
array.fetch_path(3, 0, 0, 0).
|
31
|
-
array.fetch_path(3, 0, 1, 999).
|
32
|
-
array.fetch_path(3, 0, 1, 2, 3).
|
33
|
-
array.fetch_path(4).
|
34
|
-
array.fetch_path(4, 0).
|
35
|
-
array.fetch_path(4, 0, 1).
|
36
|
-
array.fetch_path(5).
|
37
|
-
array.fetch_path(5, 0).
|
38
|
-
array.fetch_path(5, 0, 1).
|
20
|
+
expect(array.fetch_path(0)).to eq(1)
|
21
|
+
expect(array.fetch_path(1)).to eq([])
|
22
|
+
expect(array.fetch_path(1, 0)).to be_nil
|
23
|
+
expect(array.fetch_path(1, 0, 1)).to be_nil
|
24
|
+
expect(array.fetch_path(2)).to eq([2])
|
25
|
+
expect(array.fetch_path(2, 0)).to eq(2)
|
26
|
+
expect(array.fetch_path(-4, 0)).to eq(2)
|
27
|
+
expect(array.fetch_path(2, 0, 1)).to be_nil
|
28
|
+
expect(array.fetch_path(3, 0, 0, 0)).to eq(3)
|
29
|
+
expect(array.fetch_path(3, 0, 1, 999)).to be_nil
|
30
|
+
expect(array.fetch_path(3, 0, 1, 2, 3)).to be_nil
|
31
|
+
expect(array.fetch_path(4)).to eq([nil, nil, nil, nil])
|
32
|
+
expect(array.fetch_path(4, 0)).to be_nil
|
33
|
+
expect(array.fetch_path(4, 0, 1)).to be_nil
|
34
|
+
expect(array.fetch_path(5)).to eq([])
|
35
|
+
expect(array.fetch_path(5, 0)).to be_nil
|
36
|
+
expect(array.fetch_path(5, 0, 1)).to be_nil
|
39
37
|
end
|
40
38
|
|
41
39
|
it "with a nil value should raise ArgumentError" do
|
42
|
-
|
43
|
-
|
40
|
+
expect { array.fetch_path(nil) }.to raise_error(ArgumentError)
|
41
|
+
expect { array.fetch_path(3, nil, 0) }.to raise_error(ArgumentError)
|
44
42
|
end
|
45
43
|
|
46
44
|
it "with invalid values" do
|
47
|
-
|
45
|
+
expect { array.fetch_path }.to raise_error(ArgumentError)
|
48
46
|
end
|
49
47
|
|
50
48
|
include_examples "core_ext/array/nested will not modify arguments", :fetch_path
|
@@ -54,56 +52,56 @@ shared_examples_for "core_ext/array/nested" do
|
|
54
52
|
it "on an empty array" do
|
55
53
|
a = described_class.new
|
56
54
|
a.store_path(0, 1)
|
57
|
-
a.
|
55
|
+
expect(a).to eq([1])
|
58
56
|
|
59
57
|
a = described_class.new
|
60
58
|
a.store_path(1, 0, 2)
|
61
|
-
a.
|
59
|
+
expect(a).to eq([nil, [2]])
|
62
60
|
end
|
63
61
|
|
64
62
|
it "on an existing array" do
|
65
63
|
array.store_path(1, 0, 2)
|
66
|
-
array[1].
|
64
|
+
expect(array[1]).to eq([2])
|
67
65
|
array.store_path(2, 0, 3)
|
68
|
-
array[2].
|
66
|
+
expect(array[2]).to eq([3])
|
69
67
|
array.store_path(-4, 0, 3)
|
70
|
-
array[2].
|
68
|
+
expect(array[2]).to eq([3])
|
71
69
|
end
|
72
70
|
|
73
71
|
it "on an existing item that is not a array" do
|
74
72
|
array.store_path(0, 2)
|
75
|
-
array[0].
|
73
|
+
expect(array[0]).to eq(2)
|
76
74
|
array.store_path(0, 0, 3)
|
77
|
-
array[0].
|
75
|
+
expect(array[0]).to eq([3])
|
78
76
|
end
|
79
77
|
|
80
78
|
it "with an array of args" do
|
81
79
|
a = described_class.new
|
82
80
|
a.store_path([3, 0, 1, 2], 3)
|
83
|
-
a.
|
81
|
+
expect(a).to eq([nil, nil, nil, [[nil, [nil, nil, 3]]]])
|
84
82
|
end
|
85
83
|
|
86
84
|
it "with a nil value" do
|
87
85
|
a = described_class.new
|
88
86
|
a.store_path(0, 1, nil)
|
89
|
-
a.
|
87
|
+
expect(a).to eq([[nil, nil]])
|
90
88
|
end
|
91
89
|
|
92
90
|
it "with an Array value" do
|
93
91
|
a = described_class.new
|
94
92
|
a.store_path(0, 1, [2, 3])
|
95
|
-
a.
|
93
|
+
expect(a).to eq([[nil, [2, 3]]])
|
96
94
|
end
|
97
95
|
|
98
96
|
it "with a Hash value" do
|
99
97
|
a = described_class.new
|
100
98
|
a.store_path(0, 1, {2 => 3})
|
101
|
-
a.
|
99
|
+
expect(a).to eq([[nil, {2 => 3}]])
|
102
100
|
end
|
103
101
|
|
104
102
|
it "with invalid values" do
|
105
|
-
|
106
|
-
|
103
|
+
expect { described_class.new.store_path }.to raise_error(ArgumentError)
|
104
|
+
expect { described_class.new.store_path(1) }.to raise_error(ArgumentError)
|
107
105
|
end
|
108
106
|
|
109
107
|
include_examples "core_ext/array/nested will not modify arguments", :store_path
|
@@ -111,31 +109,31 @@ shared_examples_for "core_ext/array/nested" do
|
|
111
109
|
|
112
110
|
context '#has_key_path?' do
|
113
111
|
it "with various values" do
|
114
|
-
array.has_key_path?(0).
|
115
|
-
array.has_key_path?(1).
|
116
|
-
array.has_key_path?(1, 0).
|
117
|
-
array.has_key_path?(1, 0, 1).
|
118
|
-
array.has_key_path?(2).
|
119
|
-
array.has_key_path?(2, 0).
|
120
|
-
array.has_key_path?(2, 0, 1).
|
121
|
-
array.has_key_path?(3, 0, 1, 2).
|
122
|
-
array.has_key_path?(3, 0, 1, 999).
|
123
|
-
array.has_key_path?(3, 0, 1, 2, 3).
|
124
|
-
array.has_key_path?(4).
|
125
|
-
array.has_key_path?(4, 0).
|
126
|
-
array.has_key_path?(4, 0, 1).
|
127
|
-
array.has_key_path?(5).
|
128
|
-
array.has_key_path?(5, 0).
|
129
|
-
array.has_key_path?(5, 0, 1).
|
112
|
+
expect(array.has_key_path?(0)).to be_truthy
|
113
|
+
expect(array.has_key_path?(1)).to be_truthy
|
114
|
+
expect(array.has_key_path?(1, 0)).to be_falsey
|
115
|
+
expect(array.has_key_path?(1, 0, 1)).to be_falsey
|
116
|
+
expect(array.has_key_path?(2)).to be_truthy
|
117
|
+
expect(array.has_key_path?(2, 0)).to be_truthy
|
118
|
+
expect(array.has_key_path?(2, 0, 1)).to be_falsey
|
119
|
+
expect(array.has_key_path?(3, 0, 1, 2)).to be_falsey
|
120
|
+
expect(array.has_key_path?(3, 0, 1, 999)).to be_falsey
|
121
|
+
expect(array.has_key_path?(3, 0, 1, 2, 3)).to be_falsey
|
122
|
+
expect(array.has_key_path?(4)).to be_truthy
|
123
|
+
expect(array.has_key_path?(4, 0)).to be_truthy
|
124
|
+
expect(array.has_key_path?(4, 0, 1)).to be_falsey
|
125
|
+
expect(array.has_key_path?(5)).to be_truthy
|
126
|
+
expect(array.has_key_path?(5, 0)).to be_falsey
|
127
|
+
expect(array.has_key_path?(5, 0, 1)).to be_falsey
|
130
128
|
end
|
131
129
|
|
132
130
|
it "with a nil value" do
|
133
|
-
|
134
|
-
|
131
|
+
expect { array.fetch_path(nil) }.to raise_error(ArgumentError)
|
132
|
+
expect { array.fetch_path(3, nil, 0) }.to raise_error(ArgumentError)
|
135
133
|
end
|
136
134
|
|
137
135
|
it "with invalid values" do
|
138
|
-
|
136
|
+
expect { array.has_key_path? }.to raise_error(ArgumentError)
|
139
137
|
end
|
140
138
|
|
141
139
|
include_examples "core_ext/array/nested will not modify arguments", :has_key_path?
|
@@ -144,28 +142,28 @@ shared_examples_for "core_ext/array/nested" do
|
|
144
142
|
context "#delete_path" do
|
145
143
|
it "on a nested array" do
|
146
144
|
array.delete_path(3, 0, 0, 0)
|
147
|
-
array[3].
|
145
|
+
expect(array[3]).to eq([[[]]])
|
148
146
|
end
|
149
147
|
|
150
148
|
it "with an invalid path" do
|
151
149
|
array.delete_path(3, 0, 5)
|
152
|
-
array[3].
|
150
|
+
expect(array[3]).to eq([[[3]]])
|
153
151
|
end
|
154
152
|
|
155
153
|
include_examples "core_ext/array/nested will not modify arguments", :delete_path
|
156
154
|
end
|
157
155
|
|
158
156
|
it "#delete_blank_paths" do
|
159
|
-
array.delete_blank_paths.
|
157
|
+
expect(array.delete_blank_paths).to eq([1, [2], [[[3]]]])
|
160
158
|
end
|
161
159
|
|
162
160
|
context "#find_path" do
|
163
161
|
it "with a real value" do
|
164
|
-
array.find_path(3).
|
162
|
+
expect(array.find_path(3)).to eq([3, 0, 0, 0])
|
165
163
|
end
|
166
164
|
|
167
165
|
it "with non-existent value" do
|
168
|
-
array.find_path(42).
|
166
|
+
expect(array.find_path(42)).to eq([])
|
169
167
|
end
|
170
168
|
end
|
171
169
|
end
|
@@ -177,9 +175,9 @@ describe Array do
|
|
177
175
|
[2],
|
178
176
|
[[[3]]],
|
179
177
|
Array.new(4),
|
180
|
-
described_class.new
|
178
|
+
described_class.new
|
181
179
|
]
|
182
180
|
end
|
183
181
|
|
184
182
|
include_examples "core_ext/array/nested"
|
185
|
-
end
|
183
|
+
end
|
@@ -1,5 +1,3 @@
|
|
1
|
-
require_relative "../../spec_helper"
|
2
|
-
|
3
1
|
describe Array do
|
4
2
|
around do |example|
|
5
3
|
old_seed = srand(12072)
|
@@ -8,16 +6,16 @@ describe Array do
|
|
8
6
|
end
|
9
7
|
|
10
8
|
it '#random_index' do
|
11
|
-
20.times.collect { [].random_index }.uniq.sort.
|
12
|
-
20.times.collect { %w{a}.random_index }.uniq.sort.
|
13
|
-
20.times.collect { %w{a b}.random_index }.uniq.sort.
|
14
|
-
20.times.collect { %w{a b c d}.random_index }.uniq.sort.
|
9
|
+
expect(20.times.collect { [].random_index }.uniq.sort).to eq([nil])
|
10
|
+
expect(20.times.collect { %w{a}.random_index }.uniq.sort).to eq([0])
|
11
|
+
expect(20.times.collect { %w{a b}.random_index }.uniq.sort).to eq([0, 1])
|
12
|
+
expect(20.times.collect { %w{a b c d}.random_index }.uniq.sort).to eq([0, 1, 2, 3])
|
15
13
|
end
|
16
14
|
|
17
15
|
it '#random_element' do
|
18
|
-
20.times.collect { [].random_element }.uniq.sort.
|
19
|
-
20.times.collect { %w{a}.random_element }.uniq.sort.
|
20
|
-
20.times.collect { %w{a b}.random_element }.uniq.sort.
|
21
|
-
20.times.collect { %w{a b c d}.random_element }.uniq.sort.
|
16
|
+
expect(20.times.collect { [].random_element }.uniq.sort).to eq([nil])
|
17
|
+
expect(20.times.collect { %w{a}.random_element }.uniq.sort).to eq(%w{a})
|
18
|
+
expect(20.times.collect { %w{a b}.random_element }.uniq.sort).to eq(%w{a b})
|
19
|
+
expect(20.times.collect { %w{a b c d}.random_element }.uniq.sort).to eq(%w{a b c d})
|
22
20
|
end
|
23
21
|
end
|
@@ -1,5 +1,3 @@
|
|
1
|
-
require_relative "../../spec_helper"
|
2
|
-
|
3
1
|
describe Array do
|
4
2
|
STRETCH_CASES = [
|
5
3
|
# 2 parameter cases
|
@@ -28,8 +26,8 @@ describe Array do
|
|
28
26
|
it "where #{msg}" do
|
29
27
|
result = Array.stretch(*test_case)
|
30
28
|
result.each_with_index do |r, i|
|
31
|
-
r.
|
32
|
-
r.
|
29
|
+
expect(r).not_to equal(test_case[i])
|
30
|
+
expect(r).to eq(expected[i])
|
33
31
|
end
|
34
32
|
end
|
35
33
|
end
|
@@ -40,8 +38,8 @@ describe Array do
|
|
40
38
|
it "where #{msg}" do
|
41
39
|
result = Array.stretch!(*test_case)
|
42
40
|
result.each_with_index do |r, i|
|
43
|
-
r.
|
44
|
-
r.
|
41
|
+
expect(r).to equal(test_case[i])
|
42
|
+
expect(r).to eq(expected[i])
|
45
43
|
end
|
46
44
|
end
|
47
45
|
end
|
@@ -52,8 +50,8 @@ describe Array do
|
|
52
50
|
it "where #{msg}" do
|
53
51
|
receiver, params = test_case[0], test_case[1..-1]
|
54
52
|
result = receiver.stretch(*params)
|
55
|
-
result.
|
56
|
-
result.
|
53
|
+
expect(result).not_to equal(receiver)
|
54
|
+
expect(result).to eq(expected[0])
|
57
55
|
end
|
58
56
|
end
|
59
57
|
end
|
@@ -63,8 +61,8 @@ describe Array do
|
|
63
61
|
it "where #{msg}" do
|
64
62
|
receiver, params = test_case[0].dup, test_case[1..-1]
|
65
63
|
result = receiver.stretch!(*params)
|
66
|
-
result.
|
67
|
-
result.
|
64
|
+
expect(result).to equal(receiver)
|
65
|
+
expect(result).to eq(expected[0])
|
68
66
|
end
|
69
67
|
end
|
70
68
|
end
|
@@ -89,7 +87,7 @@ describe Array do
|
|
89
87
|
context '#zip_stretched' do
|
90
88
|
ZIP_STRETCHED_CASES.each_slice(4) do |msg, receiver, params, expected|
|
91
89
|
it "where #{msg}" do
|
92
|
-
receiver.zip_stretched(*params).
|
90
|
+
expect(receiver.zip_stretched(*params)).to eq(expected)
|
93
91
|
end
|
94
92
|
end
|
95
93
|
end
|