nutella 0.11.1 → 0.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/nutella/core_ext/module.rb +41 -0
- data/lib/nutella/core_ext/string/heredoc.rb +5 -1
- data/lib/nutella/version.rb +1 -1
- data/nutella.gemspec +2 -2
- data/spec/nutella/core_ext/enumerable_spec.rb +39 -39
- data/spec/nutella/core_ext/file_spec.rb +19 -19
- data/spec/nutella/core_ext/hash_spec.rb +5 -5
- data/spec/nutella/core_ext/integer_spec.rb +55 -55
- data/spec/nutella/core_ext/module_spec.rb +23 -0
- data/spec/nutella/core_ext/numeric_spec.rb +17 -17
- data/spec/nutella/core_ext/object_spec.rb +18 -18
- data/spec/nutella/core_ext/string_spec.rb +31 -4
- data/spec/support/alias.rb +1 -1
- data/spec/support/initializer_testers.rb +20 -0
- data/spec/support/matchers.rb +17 -0
- metadata +13 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ef8876f7754fa5b0b422e9ab7ca092e9298ee436
|
4
|
+
data.tar.gz: 63636995f8ba8417326f52026f07ff0c5fc33272
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 77c51d26b2d0af83884a6e188993e5c4e58d3b4e71bd3bb59310660820c37082dbcdba45e31529c02c50c0346d6927ea09374c9aaa29c5e3d4b2e250e50ed68b
|
7
|
+
data.tar.gz: bd3e50152263964033c4cf070c8d7c4c2208b2f32d319f56f47164026cf85d07aabace679a7816d7bd264c5acfb7e58d7d8a79ef3795c7d40611720295915ece
|
@@ -0,0 +1,41 @@
|
|
1
|
+
class Module
|
2
|
+
# A DRYer way to create a constructor where all of its arguments are assigned
|
3
|
+
# to instance variables of the same name.
|
4
|
+
#
|
5
|
+
# @example Comparison of constructor creation with and without Nutella
|
6
|
+
# # Pure Ruby
|
7
|
+
# class Test
|
8
|
+
# def initialize(a, b, c, d)
|
9
|
+
# @a, @b, @c, @d = a, b, c, d
|
10
|
+
# end
|
11
|
+
# end
|
12
|
+
# Test.new(0, 0, 0, 0).instance_variables # => [:@a, :@b, :@c, :@d]
|
13
|
+
#
|
14
|
+
# # Ruby with Nutella
|
15
|
+
# class Test
|
16
|
+
# initializer *%i[a b c d]
|
17
|
+
# end
|
18
|
+
# Test.new(0, 0, 0, 0).instance_variables # => [:@a, :@b, :@c, :@d]
|
19
|
+
#
|
20
|
+
# # It can also take a block with additional code to run
|
21
|
+
# class Test
|
22
|
+
# initializer :a, :b do
|
23
|
+
# @in_block = 0
|
24
|
+
# end
|
25
|
+
# end
|
26
|
+
# Test.new(0, 0).instance_variables # => [:@a, :@b, :@in_block]
|
27
|
+
#
|
28
|
+
# @param [Array] args the names of the arguments to the constructor as symbols
|
29
|
+
# @yield code to be executed on construction
|
30
|
+
def initializer(*args, &block)
|
31
|
+
define_method :initialize do |*input|
|
32
|
+
(input.last.is_a?(Hash) \
|
33
|
+
? input[-1]
|
34
|
+
: Hash[args.zip input]).each do |name, value|
|
35
|
+
instance_variable_set "@#{name}", value
|
36
|
+
end
|
37
|
+
|
38
|
+
instance_eval(&block) if block_given?
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -13,8 +13,12 @@ class String
|
|
13
13
|
#
|
14
14
|
# @return [String] the string with excess leading whitespace stripped
|
15
15
|
def heredoc
|
16
|
-
gsub /^[ \t]{#{scan(/^[ \t]*(?=\S)/).min.size}}/, ""
|
16
|
+
gsub /^[ \t]{#{scan(/^[ \t]*(?=\S)/).min.size || 0}}/, ""
|
17
17
|
end
|
18
18
|
alias format_heredoc heredoc
|
19
19
|
alias strip_heredoc heredoc
|
20
|
+
|
21
|
+
def in_heredoc indentation
|
22
|
+
lines[1..-1].map { |line| line.prepend " " * indentation }.join "\n"
|
23
|
+
end
|
20
24
|
end
|
data/lib/nutella/version.rb
CHANGED
data/nutella.gemspec
CHANGED
@@ -20,10 +20,10 @@ Gem::Specification.new do |gem|
|
|
20
20
|
]
|
21
21
|
|
22
22
|
gem.required_ruby_version = "~> 2.0"
|
23
|
-
gem.add_development_dependency "fuubar", "~>
|
23
|
+
gem.add_development_dependency "fuubar", "~> 2.0"
|
24
24
|
gem.add_development_dependency "rake", "~> 10.0"
|
25
25
|
gem.add_development_dependency "redcarpet", "~> 2.2"
|
26
26
|
gem.add_development_dependency "rdoc", "~> 3.12"
|
27
|
-
gem.add_development_dependency "rspec", "~>
|
27
|
+
gem.add_development_dependency "rspec", "~> 3.1"
|
28
28
|
gem.add_development_dependency "yard", "~> 0.8"
|
29
29
|
end
|
@@ -15,19 +15,19 @@ describe Enumerable do
|
|
15
15
|
|
16
16
|
it "works for a function that takes one argument" do
|
17
17
|
testers.apply :increment_value
|
18
|
-
testers.map(&:value).
|
18
|
+
expect(testers.map(&:value)).to eq([6] * 5)
|
19
19
|
end
|
20
20
|
|
21
21
|
context "with a function that takes two arguments" do
|
22
22
|
it "allows multi-dimensional arrays" do
|
23
23
|
multi = testers.zip([2] * 5)
|
24
24
|
multi.apply :increment_value
|
25
|
-
multi.each { |arr| arr.first.value.
|
25
|
+
multi.each { |arr| expect(arr.first.value).to eq(7) }
|
26
26
|
end
|
27
27
|
|
28
28
|
it "allows multiple arguments" do
|
29
29
|
testers.apply :increment_value, 2
|
30
|
-
testers.map(&:value).
|
30
|
+
expect(testers.map(&:value)).to eq([7] * 5)
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
@@ -35,58 +35,58 @@ describe Enumerable do
|
|
35
35
|
it "allows multi-dimensional arrays" do
|
36
36
|
multi = testers.map { |elem| [elem, 2, 2] }
|
37
37
|
multi.apply :increment_value
|
38
|
-
multi.each { |arr| arr.first.value.
|
38
|
+
multi.each { |arr| expect(arr.first.value).to eq(9) }
|
39
39
|
end
|
40
40
|
|
41
41
|
it "allows multiple arguments" do
|
42
42
|
testers.apply :increment_value, 2, 2
|
43
|
-
testers.map(&:value).
|
43
|
+
expect(testers.map(&:value)).to eq([9] * 5)
|
44
44
|
end
|
45
45
|
|
46
46
|
it "allows multi-dimensional arrays with multiple arguments" do
|
47
47
|
multi = testers.zip([2] * 5)
|
48
48
|
multi.apply :increment_value, 2
|
49
|
-
multi.each { |arr| arr.first.value.
|
49
|
+
multi.each { |arr| expect(arr.first.value).to eq(9) }
|
50
50
|
end
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
54
54
|
describe "#exclude?" do
|
55
55
|
it "returns true if the collection does not contain the input" do
|
56
|
-
[1, 2, 3].exclude?(4).
|
56
|
+
expect([1, 2, 3].exclude?(4)).to be_truthy
|
57
57
|
end
|
58
58
|
|
59
59
|
it "returns false if the collection contains the input" do
|
60
|
-
[1, 2, 3].exclude?(2).
|
60
|
+
expect([1, 2, 3].exclude?(2)).to be_falsey
|
61
61
|
end
|
62
62
|
end
|
63
63
|
|
64
64
|
describe "#group" do
|
65
65
|
it "groups elements" do
|
66
|
-
[].group(2).
|
67
|
-
[1, 2].group(2).
|
68
|
-
(1..4).group(2).
|
69
|
-
(1..9).group(3).
|
66
|
+
expect([].group(2)).to eq([])
|
67
|
+
expect([1, 2].group(2)).to eq([[1, 2]])
|
68
|
+
expect((1..4).group(2)).to eq([[1, 2], [3, 4]])
|
69
|
+
expect((1..9).group(3)).to eq([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
|
70
70
|
end
|
71
71
|
|
72
72
|
it "puts all excess into the last group" do
|
73
|
-
[1, 2].group(4).
|
74
|
-
(1..8).group(3).
|
73
|
+
expect([1, 2].group(4)).to eq([[1, 2]])
|
74
|
+
expect((1..8).group(3)).to eq([[1, 2, 3], [4, 5, 6], [7, 8]])
|
75
75
|
end
|
76
76
|
|
77
77
|
it "discards all excess if instructed to" do
|
78
|
-
(1..3).group(2, true).
|
79
|
-
(1..8).group(3, true).
|
78
|
+
expect((1..3).group(2, true)).to eq([[1, 2]])
|
79
|
+
expect((1..8).group(3, true)).to eq([[1, 2, 3], [4, 5, 6]])
|
80
80
|
end
|
81
81
|
|
82
82
|
it "does not discard anything if there is no excess" do
|
83
|
-
(1..4).group(2, true).
|
83
|
+
expect((1..4).group(2, true)).to eq([[1, 2], [3, 4]])
|
84
84
|
end
|
85
85
|
|
86
86
|
it "does not modify in place" do
|
87
87
|
arr = (1..10).to_a
|
88
88
|
arr.group 2
|
89
|
-
arr.
|
89
|
+
expect(arr).to eq((1..10).to_a)
|
90
90
|
end
|
91
91
|
end
|
92
92
|
|
@@ -98,18 +98,18 @@ describe Enumerable do
|
|
98
98
|
end
|
99
99
|
|
100
100
|
it "returns the modified string" do
|
101
|
-
arr.group!(2).
|
101
|
+
expect(arr.group!(2)).to eq(arr)
|
102
102
|
end
|
103
103
|
|
104
104
|
it "returns nil if nothing was modified" do
|
105
|
-
[].group!(2).
|
105
|
+
expect([].group!(2)).to be_nil
|
106
106
|
end
|
107
107
|
end
|
108
108
|
|
109
109
|
describe "#map" do
|
110
110
|
it "takes a symbol and maps that method" do
|
111
|
-
[1, 2, 3].map(:succ).
|
112
|
-
(1..10).map(:succ).
|
111
|
+
expect([1, 2, 3].map(:succ)).to eq([2, 3, 4])
|
112
|
+
expect((1..10).map(:succ)).to eq((2..11).to_a)
|
113
113
|
end
|
114
114
|
|
115
115
|
it "passes additional arguments into the method in the first argument" do
|
@@ -117,49 +117,49 @@ describe Enumerable do
|
|
117
117
|
alpha = ("a".."z")
|
118
118
|
args = [/[aeiou]/, "x"]
|
119
119
|
|
120
|
-
arr.map(:gsub, *args).
|
121
|
-
alpha.map(:gsub, *args).
|
120
|
+
expect(arr.map(:gsub, *args)).to eq(arr.map { |e| e.gsub *args })
|
121
|
+
expect(alpha.map(:gsub, *args)).to eq(alpha.map { |e| e.gsub *args })
|
122
122
|
end
|
123
123
|
|
124
124
|
context "when using as the original #map" do
|
125
125
|
it "doesn't break passing in blocks" do
|
126
|
-
[1, 2, 3].map { |n| n + 1 }.
|
126
|
+
expect([1, 2, 3].map { |n| n + 1 }).to eq([2, 3, 4])
|
127
127
|
end
|
128
128
|
|
129
129
|
it "doesn't break when passing in Proc" do
|
130
|
-
[1, 2, 3].map(&:succ).
|
130
|
+
expect([1, 2, 3].map(&:succ)).to eq([2, 3, 4])
|
131
131
|
end
|
132
132
|
end
|
133
133
|
end
|
134
134
|
|
135
135
|
describe "#sum" do
|
136
136
|
it "returns the sum of elements" do
|
137
|
-
[].sum.
|
138
|
-
[5].sum.
|
139
|
-
[1, 2, 3].sum.
|
140
|
-
(1..4).sum.
|
137
|
+
expect([].sum).to eq(0)
|
138
|
+
expect([5].sum).to eq(5)
|
139
|
+
expect([1, 2, 3].sum).to eq(6)
|
140
|
+
expect((1..4).sum).to eq(10)
|
141
141
|
end
|
142
142
|
|
143
143
|
it "flattens the elements before adding" do
|
144
|
-
[[4, 5], 1].sum.
|
145
|
-
[[1, 2, 3], [4, 5]].sum.
|
144
|
+
expect([[4, 5], 1].sum).to eq(10)
|
145
|
+
expect([[1, 2, 3], [4, 5]].sum).to eq(15)
|
146
146
|
end
|
147
147
|
|
148
148
|
it "filters out all non-numbers" do
|
149
|
-
{ a: 2, b: 4 }.sum.
|
150
|
-
[1, "str", 4].sum.
|
151
|
-
["no numbers"].sum.
|
149
|
+
expect({ a: 2, b: 4 }.sum).to eq(6)
|
150
|
+
expect([1, "str", 4].sum).to eq(5)
|
151
|
+
expect(["no numbers"].sum).to eq(0)
|
152
152
|
end
|
153
153
|
|
154
154
|
context "with a block" do
|
155
155
|
it "returns the sum of the elements that pass the filter block" do
|
156
|
-
(1..10).sum(&:even?).
|
157
|
-
(1..10).sum { |n| n % 3 == 0 }.
|
156
|
+
expect((1..10).sum(&:even?)).to eq(30)
|
157
|
+
expect((1..10).sum { |n| n % 3 == 0 }).to eq(18)
|
158
158
|
end
|
159
159
|
|
160
160
|
it "still filters out all non-numbers" do
|
161
|
-
{ a: 1, b: 2, c: 3 }.sum(&:odd?).
|
162
|
-
[2, "str", 4, 7].sum(&:even?).
|
161
|
+
expect({ a: 1, b: 2, c: 3 }.sum(&:odd?)).to eq(4)
|
162
|
+
expect([2, "str", 4, 7].sum(&:even?)).to eq(6)
|
163
163
|
end
|
164
164
|
end
|
165
165
|
end
|
@@ -6,41 +6,41 @@ describe File do
|
|
6
6
|
describe "::human_size" do
|
7
7
|
context "when given a Numeric" do
|
8
8
|
it "does not change bytes" do
|
9
|
-
File.human_size(150.bytes).
|
9
|
+
expect(File.human_size(150.bytes)).to eq("150 Bytes")
|
10
10
|
end
|
11
11
|
|
12
12
|
it "changes kilobytes" do
|
13
|
-
File.human_size(1.5.kilobytes).
|
13
|
+
expect(File.human_size(1.5.kilobytes)).to eq("1.5 KB")
|
14
14
|
end
|
15
15
|
|
16
16
|
it "changes megabytes" do
|
17
|
-
File.human_size(1.5.megabytes).
|
17
|
+
expect(File.human_size(1.5.megabytes)).to eq("1.5 MB")
|
18
18
|
end
|
19
19
|
|
20
20
|
it "changes gigabytes" do
|
21
|
-
File.human_size(1.5.gigabytes).
|
21
|
+
expect(File.human_size(1.5.gigabytes)).to eq("1.5 GB")
|
22
22
|
end
|
23
23
|
|
24
24
|
it "rounds down properly to the tenths" do
|
25
|
-
File.human_size(1.72.megabytes).
|
25
|
+
expect(File.human_size(1.72.megabytes)).to eq("1.7 MB")
|
26
26
|
end
|
27
27
|
|
28
28
|
it "rounds up properly to the tenths" do
|
29
|
-
File.human_size(1.78.megabytes).
|
29
|
+
expect(File.human_size(1.78.megabytes)).to eq("1.8 MB")
|
30
30
|
end
|
31
31
|
|
32
32
|
it "has a singular unit for 1 byte" do
|
33
|
-
File.human_size(1.byte).
|
33
|
+
expect(File.human_size(1.byte)).to eq("1 Byte")
|
34
34
|
end
|
35
35
|
|
36
36
|
it "returns 'Empty' for an empty file or a blank parameter" do
|
37
|
-
File.human_size(0).
|
38
|
-
File.human_size(nil).
|
37
|
+
expect(File.human_size(0)).to eq("Empty")
|
38
|
+
expect(File.human_size(nil)).to eq("Empty")
|
39
39
|
end
|
40
40
|
|
41
41
|
it "displays whole numbers without a decimal" do
|
42
|
-
File.human_size(1.megabyte).
|
43
|
-
File.human_size(24.gigabytes).
|
42
|
+
expect(File.human_size(1.megabyte)).to eq("1 MB")
|
43
|
+
expect(File.human_size(24.gigabytes)).to eq("24 GB")
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
@@ -48,7 +48,7 @@ describe File do
|
|
48
48
|
it "detects empty files properly" do
|
49
49
|
begin
|
50
50
|
f = File.new("temp", "w")
|
51
|
-
File.human_size(f).
|
51
|
+
expect(File.human_size(f)).to eq("Empty")
|
52
52
|
ensure
|
53
53
|
File.delete "temp"
|
54
54
|
end
|
@@ -57,10 +57,10 @@ describe File do
|
|
57
57
|
it "detects the file size properly" do
|
58
58
|
begin
|
59
59
|
File.truncate(f = File.new("temp", "w"), 1.5.kilobytes)
|
60
|
-
File.human_size(f).
|
60
|
+
expect(File.human_size(f)).to eq("1.5 KB")
|
61
61
|
|
62
62
|
File.truncate(f = File.new("temp", "w"), 3.kilobytes)
|
63
|
-
File.human_size(f).
|
63
|
+
expect(File.human_size(f)).to eq("3 KB")
|
64
64
|
ensure
|
65
65
|
File.delete "temp"
|
66
66
|
end
|
@@ -71,10 +71,10 @@ describe File do
|
|
71
71
|
it "detects the file size properly" do
|
72
72
|
begin
|
73
73
|
File.truncate(f = File.new("temp", "w"), 1.5.kilobytes)
|
74
|
-
File.human_size("temp").
|
74
|
+
expect(File.human_size("temp")).to eq("1.5 KB")
|
75
75
|
|
76
76
|
File.truncate(f = File.new("temp", "w"), 3.kilobytes)
|
77
|
-
File.human_size("temp").
|
77
|
+
expect(File.human_size("temp")).to eq("3 KB")
|
78
78
|
ensure
|
79
79
|
File.delete "temp"
|
80
80
|
end
|
@@ -85,7 +85,7 @@ describe File do
|
|
85
85
|
describe "#human_size" do
|
86
86
|
it "detects empty files properly" do
|
87
87
|
begin
|
88
|
-
File.new("temp", "w").human_size.
|
88
|
+
expect(File.new("temp", "w").human_size).to eq("Empty")
|
89
89
|
ensure
|
90
90
|
File.delete "temp"
|
91
91
|
end
|
@@ -94,10 +94,10 @@ describe File do
|
|
94
94
|
it "detects the file size properly" do
|
95
95
|
begin
|
96
96
|
File.truncate(f = File.new("temp", "w"), 1.5.kilobytes)
|
97
|
-
f.human_size.
|
97
|
+
expect(f.human_size).to eq("1.5 KB")
|
98
98
|
|
99
99
|
File.truncate(f = File.new("temp", "w"), 3.kilobytes)
|
100
|
-
f.human_size.
|
100
|
+
expect(f.human_size).to eq("3 KB")
|
101
101
|
ensure
|
102
102
|
File.delete "temp"
|
103
103
|
end
|
@@ -11,17 +11,17 @@ describe Hash do
|
|
11
11
|
|
12
12
|
describe "#grab" do
|
13
13
|
it "selects the given items from a hash" do
|
14
|
-
hash.slice(:a, :c).
|
14
|
+
expect(hash.slice(:a, :c)).to eq({a: 1, c: 3})
|
15
15
|
end
|
16
16
|
|
17
17
|
it "skips items that do not exist in the hash" do
|
18
|
-
hash.slice(:a, :d, :f).
|
18
|
+
expect(hash.slice(:a, :d, :f)).to eq({a: 1, d: 4})
|
19
19
|
end
|
20
20
|
|
21
21
|
it "does not modify in place" do
|
22
22
|
start = hash
|
23
23
|
hash.slice :a, :b
|
24
|
-
hash.
|
24
|
+
expect(hash).to eq(start)
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
@@ -31,11 +31,11 @@ describe Hash do
|
|
31
31
|
end
|
32
32
|
|
33
33
|
it "returns the removed pairs" do
|
34
|
-
hash.slice!(:a, :c).
|
34
|
+
expect(hash.slice!(:a, :c)).to eq({b: 2, d: 4})
|
35
35
|
end
|
36
36
|
|
37
37
|
it "does not return pairs that did not affect the hash" do
|
38
|
-
hash.slice!(:a, :c, :g).
|
38
|
+
expect(hash.slice!(:a, :c, :g)).to eq({b: 2, d: 4})
|
39
39
|
end
|
40
40
|
end
|
41
41
|
end
|
@@ -9,147 +9,147 @@ describe Integer do
|
|
9
9
|
|
10
10
|
describe "#digits" do
|
11
11
|
it "returns an array of the digits in the integer" do
|
12
|
-
(0..9).each { |n| n.digits.
|
13
|
-
10.digits.
|
14
|
-
349.digits.
|
15
|
-
1000.digits.
|
12
|
+
(0..9).each { |n| expect(n.digits).to eq([n]) }
|
13
|
+
expect(10.digits).to eq([1, 0])
|
14
|
+
expect(349.digits).to eq([3, 4, 9])
|
15
|
+
expect(1000.digits).to eq([1, 0, 0, 0])
|
16
16
|
end
|
17
17
|
|
18
18
|
it "trims the '-' from negative numbers" do
|
19
|
-
-3.digits.
|
20
|
-
-20.digits.
|
19
|
+
expect(-3.digits).to eq([3])
|
20
|
+
expect(-20.digits).to eq([2, 0])
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
24
|
describe "#ordinalize" do
|
25
25
|
NUMBER_FORMATS.each do |cardinal, ordinal|
|
26
26
|
it "returns the ordinal #{ordinal} for the integer #{cardinal}" do
|
27
|
-
cardinal.ordinalize.
|
27
|
+
expect(cardinal.ordinalize).to eq(ordinal)
|
28
28
|
end
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
32
|
describe "#goes_into?" do
|
33
33
|
it "returns true if the number goes evenly into the argument" do
|
34
|
-
5.goes_into?(10).
|
35
|
-
3.goes_into?(21).
|
36
|
-
25.goes_into?(100).
|
34
|
+
expect(5.goes_into?(10)).to be_truthy
|
35
|
+
expect(3.goes_into?(21)).to be_truthy
|
36
|
+
expect(25.goes_into?(100)).to be_truthy
|
37
37
|
end
|
38
38
|
|
39
39
|
it "returns false if the number does not go evenly in" do
|
40
|
-
3.goes_into?(10).
|
41
|
-
9.goes_into?(40).
|
42
|
-
10.goes_into?(5).
|
40
|
+
expect(3.goes_into?(10)).to be_falsey
|
41
|
+
expect(9.goes_into?(40)).to be_falsey
|
42
|
+
expect(10.goes_into?(5)).to be_falsey
|
43
43
|
end
|
44
44
|
|
45
45
|
context "when passing in zero" do
|
46
46
|
it "returns false if one tries to divide by zero" do
|
47
|
-
0.goes_into?(20).
|
48
|
-
0.goes_into?(30).
|
47
|
+
expect(0.goes_into?(20)).to be_falsey
|
48
|
+
expect(0.goes_into?(30)).to be_falsey
|
49
49
|
end
|
50
50
|
|
51
51
|
it "allows zero to go into zero" do
|
52
|
-
0.goes_into?(0).
|
52
|
+
expect(0.goes_into?(0)).to be_truthy
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
56
|
context "with multiple arguments" do
|
57
57
|
it "returns true if all arguments succeed" do
|
58
|
-
5.goes_into?(10, 15, 50).
|
59
|
-
2.goes_into?(2, 4, 10).
|
58
|
+
expect(5.goes_into?(10, 15, 50)).to be_truthy
|
59
|
+
expect(2.goes_into?(2, 4, 10)).to be_truthy
|
60
60
|
end
|
61
61
|
|
62
62
|
it "returns false if only some arguments succeed" do
|
63
|
-
5.goes_into?(10, 12, 15).
|
64
|
-
8.goes_into?(4, 16).
|
63
|
+
expect(5.goes_into?(10, 12, 15)).to be_falsey
|
64
|
+
expect(8.goes_into?(4, 16)).to be_falsey
|
65
65
|
end
|
66
66
|
|
67
67
|
it "returns false if no arguments succeed" do
|
68
|
-
3.goes_into?(8, 16, 20).
|
69
|
-
6.goes_into?(5, 10, 15).
|
68
|
+
expect(3.goes_into?(8, 16, 20)).to be_falsey
|
69
|
+
expect(6.goes_into?(5, 10, 15)).to be_falsey
|
70
70
|
end
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
74
74
|
describe "#goes_into_any?" do
|
75
75
|
it "returns true if all arguments succeed" do
|
76
|
-
5.goes_into_any?(10, 15, 50).
|
77
|
-
2.goes_into_any?(2, 4, 10).
|
76
|
+
expect(5.goes_into_any?(10, 15, 50)).to be_truthy
|
77
|
+
expect(2.goes_into_any?(2, 4, 10)).to be_truthy
|
78
78
|
end
|
79
79
|
|
80
80
|
it "returns true if only some arguments succeed" do
|
81
|
-
5.goes_into_any?(10, 12, 15).
|
82
|
-
8.goes_into_any?(4, 16).
|
81
|
+
expect(5.goes_into_any?(10, 12, 15)).to be_truthy
|
82
|
+
expect(8.goes_into_any?(4, 16)).to be_truthy
|
83
83
|
end
|
84
84
|
|
85
85
|
it "returns false if no arguments succeed" do
|
86
|
-
3.goes_into_any?(8, 16, 20).
|
87
|
-
6.goes_into_any?(5, 10, 15).
|
86
|
+
expect(3.goes_into_any?(8, 16, 20)).to be_falsey
|
87
|
+
expect(6.goes_into_any?(5, 10, 15)).to be_falsey
|
88
88
|
end
|
89
89
|
end
|
90
90
|
|
91
91
|
describe "#multiple_of?" do
|
92
92
|
it "returns true if the number is evenly divisible" do
|
93
|
-
5.
|
94
|
-
15.
|
95
|
-
10.
|
96
|
-
6000.
|
93
|
+
expect(5).to be_multiple_of(5)
|
94
|
+
expect(15).to be_multiple_of(5)
|
95
|
+
expect(10).to be_multiple_of(2)
|
96
|
+
expect(6000).to be_multiple_of(6)
|
97
97
|
end
|
98
98
|
|
99
99
|
it "returns false if the number is not evenly divisible" do
|
100
|
-
20.
|
101
|
-
4.
|
102
|
-
5.
|
103
|
-
100.
|
100
|
+
expect(20).not_to be_multiple_of(7)
|
101
|
+
expect(4).not_to be_multiple_of(3)
|
102
|
+
expect(5).not_to be_multiple_of(15)
|
103
|
+
expect(100).not_to be_multiple_of(3)
|
104
104
|
end
|
105
105
|
|
106
106
|
context "when passing in zero" do
|
107
107
|
it "returns false if one tries to divide by zero" do
|
108
|
-
20.
|
109
|
-
30.
|
108
|
+
expect(20).not_to be_multiple_of(0)
|
109
|
+
expect(30).not_to be_multiple_of(0)
|
110
110
|
end
|
111
111
|
|
112
112
|
it "allows zero to go into zero" do
|
113
|
-
0.
|
113
|
+
expect(0).to be_multiple_of(0)
|
114
114
|
end
|
115
115
|
end
|
116
116
|
|
117
117
|
context "with multiple arguments" do
|
118
118
|
it "returns true if evenly divisible by all arguments" do
|
119
|
-
15.
|
120
|
-
100.
|
121
|
-
0.
|
119
|
+
expect(15).to be_multiple_of(3, 15)
|
120
|
+
expect(100).to be_multiple_of(2, 5, 25)
|
121
|
+
expect(0).to be_multiple_of(0, 1, 2)
|
122
122
|
end
|
123
123
|
|
124
124
|
it "returns false if evenly divisible by only some arguments" do
|
125
|
-
15.
|
126
|
-
12.
|
127
|
-
10.
|
125
|
+
expect(15).not_to be_multiple_of(2, 3)
|
126
|
+
expect(12).not_to be_multiple_of(3, 4, 6, 8)
|
127
|
+
expect(10).not_to be_multiple_of(0, 5)
|
128
128
|
end
|
129
129
|
|
130
130
|
it "returns false if evenly divisible by none of the arguments" do
|
131
|
-
6.
|
132
|
-
17.
|
131
|
+
expect(6).not_to be_multiple_of(4, 5)
|
132
|
+
expect(17).not_to be_multiple_of(2, 4)
|
133
133
|
end
|
134
134
|
end
|
135
135
|
end
|
136
136
|
|
137
137
|
describe "#multiple_of_any?" do
|
138
138
|
it "returns true if evenly divisible by all arguments" do
|
139
|
-
15.
|
140
|
-
100.
|
141
|
-
0.
|
139
|
+
expect(15).to be_multiple_of_any(3, 15)
|
140
|
+
expect(100).to be_multiple_of_any(2, 5, 25)
|
141
|
+
expect(0).to be_multiple_of_any(0, 1, 2)
|
142
142
|
end
|
143
143
|
|
144
144
|
it "returns true if evenly divisible by only some arguments" do
|
145
|
-
15.
|
146
|
-
12.
|
147
|
-
10.
|
145
|
+
expect(15).to be_multiple_of_any(2, 3)
|
146
|
+
expect(12).to be_multiple_of_any(3, 4, 6, 8)
|
147
|
+
expect(10).to be_multiple_of_any(0, 5)
|
148
148
|
end
|
149
149
|
|
150
150
|
it "returns false if evenly divisible by none of the arguments" do
|
151
|
-
6.
|
152
|
-
17.
|
151
|
+
expect(6).not_to be_multiple_of_any(4, 5)
|
152
|
+
expect(17).not_to be_multiple_of_any(2, 4)
|
153
153
|
end
|
154
154
|
end
|
155
155
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "nutella/core_ext/module"
|
3
|
+
|
4
|
+
describe Module do
|
5
|
+
describe "initializer" do
|
6
|
+
it "defines the instance variables" do
|
7
|
+
expect(InitTest.new *INIT_VALUES).
|
8
|
+
to have_instance_variables(*INIT_INSTANCE_VARS)
|
9
|
+
end
|
10
|
+
|
11
|
+
context "with a block" do
|
12
|
+
let!(:t) { InitBlockTest.new *INIT_VALUES }
|
13
|
+
|
14
|
+
it "defines the instance variables" do
|
15
|
+
expect(t).to have_instance_variables(*INIT_INSTANCE_VARS)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "runs the block" do
|
19
|
+
expect(t.block_run?).to be true
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -11,57 +11,57 @@ describe Numeric do
|
|
11
11
|
|
12
12
|
describe "#percent" do
|
13
13
|
it "returns the percent for an integer" do
|
14
|
-
5.percent.
|
15
|
-
15.percent.
|
16
|
-
50.percent.
|
17
|
-
125.percent.
|
14
|
+
expect(5.percent).to eq(0.05)
|
15
|
+
expect(15.percent).to eq(0.15)
|
16
|
+
expect(50.percent).to eq(0.5)
|
17
|
+
expect(125.percent).to eq(1.25)
|
18
18
|
end
|
19
19
|
|
20
20
|
it "returns an integer for multiples of 100" do
|
21
|
-
100.percent.
|
22
|
-
500.percent.
|
21
|
+
expect(100.percent).to eq(1)
|
22
|
+
expect(500.percent).to eq(5)
|
23
23
|
end
|
24
24
|
|
25
25
|
it "returns the percent for a floating point" do
|
26
|
-
0.5.percent.
|
27
|
-
5.5.percent.
|
28
|
-
10.5.percent.
|
26
|
+
expect(0.5.percent).to eq(0.005)
|
27
|
+
expect(5.5.percent).to eq(0.055)
|
28
|
+
expect(10.5.percent).to eq(0.105)
|
29
29
|
end
|
30
30
|
|
31
31
|
it "returns an integer for floating point multiples of 100" do
|
32
|
-
100.0.percent.
|
32
|
+
expect(100.0.percent).to eq(1)
|
33
33
|
end
|
34
34
|
|
35
35
|
it "returns zero for 0" do
|
36
|
-
0.percent.
|
36
|
+
expect(0.percent).to be_zero
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
40
|
describe "#sanity_check_min" do
|
41
41
|
it "returns the parameter if the number is lower" do
|
42
|
-
5.sanity_check_min(7).
|
42
|
+
expect(5.sanity_check_min(7)).to eq(7)
|
43
43
|
end
|
44
44
|
|
45
45
|
it "doesn't change anything if the number is equal" do
|
46
|
-
5.sanity_check_min(5).
|
46
|
+
expect(5.sanity_check_min(5)).to eq(5)
|
47
47
|
end
|
48
48
|
|
49
49
|
it "returns the number if the number is higher" do
|
50
|
-
5.sanity_check_min(2).
|
50
|
+
expect(5.sanity_check_min(2)).to eq(5)
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
54
54
|
describe "#sanity_check_max" do
|
55
55
|
it "returns the parameter if the number is higher" do
|
56
|
-
5.sanity_check_max(2).
|
56
|
+
expect(5.sanity_check_max(2)).to eq(2)
|
57
57
|
end
|
58
58
|
|
59
59
|
it "doesn't change anything if the number is equal" do
|
60
|
-
5.sanity_check_max(5).
|
60
|
+
expect(5.sanity_check_max(5)).to eq(5)
|
61
61
|
end
|
62
62
|
|
63
63
|
it "returns the number if the number is lower" do
|
64
|
-
5.sanity_check_max(7).
|
64
|
+
expect(5.sanity_check_max(7)).to eq(5)
|
65
65
|
end
|
66
66
|
end
|
67
67
|
end
|
@@ -8,44 +8,44 @@ describe Object do
|
|
8
8
|
|
9
9
|
describe "#blank?" do
|
10
10
|
it "is blank when nil" do
|
11
|
-
nil.
|
11
|
+
expect(nil).to be_blank
|
12
12
|
end
|
13
13
|
|
14
14
|
it "is blank when false" do
|
15
|
-
false.
|
15
|
+
expect(false).to be_blank
|
16
16
|
end
|
17
17
|
|
18
18
|
it "is not blank when true" do
|
19
|
-
true.
|
19
|
+
expect(true).not_to be_blank
|
20
20
|
end
|
21
21
|
|
22
22
|
it "is not blank when numeric" do
|
23
|
-
[0, 1].each { |n| n.
|
23
|
+
[0, 1].each { |n| expect(n).not_to be_blank }
|
24
24
|
end
|
25
25
|
|
26
26
|
context "when a string" do
|
27
27
|
it "is blank if the string is empty" do
|
28
|
-
"".
|
28
|
+
expect("").to be_blank
|
29
29
|
end
|
30
30
|
|
31
31
|
it "is blank if only whitespace" do
|
32
|
-
[" ", "\n \t"].each { |str| str.
|
33
|
-
" something here ".
|
32
|
+
[" ", "\n \t"].each { |str| expect(str).to be_blank }
|
33
|
+
expect(" something here ").not_to be_blank
|
34
34
|
end
|
35
35
|
|
36
36
|
it "is not blank if the string has content" do
|
37
|
-
"string".
|
37
|
+
expect("string").not_to be_blank
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
41
|
context "when a collection" do
|
42
42
|
it "is blank if the collection is empty" do
|
43
|
-
[[], {}].each { |collection| collection.
|
43
|
+
[[], {}].each { |collection| expect(collection).to be_blank }
|
44
44
|
end
|
45
45
|
|
46
46
|
it "is not blank if there are elements in the collection" do
|
47
47
|
[[1, 2, 3], { 1 => 2, 3 => 4 }].each do |collection|
|
48
|
-
collection.
|
48
|
+
expect(collection).not_to be_blank
|
49
49
|
end
|
50
50
|
end
|
51
51
|
end
|
@@ -53,22 +53,22 @@ describe Object do
|
|
53
53
|
|
54
54
|
describe "#present?" do
|
55
55
|
it "is the inverse of #blank?" do
|
56
|
-
[0, 1].each { |n| n.
|
57
|
-
[[], {}].each { |collection| collection.
|
58
|
-
["", " "].each { |str| str.
|
59
|
-
["str", " str "].each { |str| str.
|
56
|
+
[0, 1].each { |n| expect(n).to be_present }
|
57
|
+
[[], {}].each { |collection| expect(collection).not_to be_present }
|
58
|
+
["", " "].each { |str| expect(str).not_to be_present }
|
59
|
+
["str", " str "].each { |str| expect(str).to be_present }
|
60
60
|
end
|
61
61
|
end
|
62
62
|
|
63
63
|
describe "#presence" do
|
64
64
|
it "returns the object if the object is present" do
|
65
|
-
1.presence.
|
66
|
-
"str".presence.
|
65
|
+
expect(1.presence).to eq(1)
|
66
|
+
expect("str".presence).to eq("str")
|
67
67
|
end
|
68
68
|
|
69
69
|
it "returns nil if the object is not present" do
|
70
|
-
"".presence.
|
71
|
-
false.presence.
|
70
|
+
expect("".presence).to be_nil
|
71
|
+
expect(false.presence).to be_nil
|
72
72
|
end
|
73
73
|
end
|
74
74
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
require "nutella/core_ext/string"
|
3
3
|
|
4
|
-
describe
|
4
|
+
describe String do
|
5
5
|
describe "aliases" do
|
6
6
|
test_alias String, :contains?, :include?
|
7
7
|
test_alias String, :includes?, :include?
|
@@ -15,11 +15,11 @@ describe Enumerable do
|
|
15
15
|
|
16
16
|
describe "#exclude?" do
|
17
17
|
it "returns true if the string does not contain the input string" do
|
18
|
-
"hello".exclude?("test").
|
18
|
+
expect("hello".exclude?("test")).to be_truthy
|
19
19
|
end
|
20
20
|
|
21
21
|
it "returns false if the string contains the input string" do
|
22
|
-
"hello".exclude?("llo").
|
22
|
+
expect("hello".exclude?("llo")).to be_falsey
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
@@ -35,7 +35,7 @@ describe Enumerable do
|
|
35
35
|
Left-aligned again.
|
36
36
|
EOS
|
37
37
|
|
38
|
-
test.
|
38
|
+
expect(test).to eq <<EOS
|
39
39
|
This is a test of String#heredoc.
|
40
40
|
This text should be left-aligned.
|
41
41
|
This text is indented by four spaces.
|
@@ -45,5 +45,32 @@ This text should be left-aligned.
|
|
45
45
|
Left-aligned again.
|
46
46
|
EOS
|
47
47
|
end
|
48
|
+
|
49
|
+
=begin
|
50
|
+
it "works with string interpolation" do
|
51
|
+
short = "Test"
|
52
|
+
inner = <<-EOS.heredoc.strip
|
53
|
+
This text should be left-aligned.
|
54
|
+
This is indented by two.
|
55
|
+
Last line of the inner string, left-aligned.
|
56
|
+
EOS
|
57
|
+
|
58
|
+
test = <<-EOS.heredoc
|
59
|
+
Testing short in-line: #{short}
|
60
|
+
Testing next-line:
|
61
|
+
#{inner.in_heredoc 8}
|
62
|
+
Ending interpolation test.
|
63
|
+
EOS
|
64
|
+
|
65
|
+
test.should == <<EOS
|
66
|
+
Testing short in-line: Test
|
67
|
+
Testing next-line:
|
68
|
+
This text should be left-aligned.
|
69
|
+
This is indented by two.
|
70
|
+
Last line of the inner string, left-aligned.
|
71
|
+
Ending interpolation test.
|
72
|
+
EOS
|
73
|
+
end
|
74
|
+
=end
|
48
75
|
end
|
49
76
|
end
|
data/spec/support/alias.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
def test_alias(cls, method_a, method_b)
|
3
3
|
describe "##{method_a.to_s}" do
|
4
4
|
it "is an alias of ##{method_b.to_s}" do
|
5
|
-
cls.instance_method
|
5
|
+
expect(cls.instance_method method_a).to eq(cls.instance_method(method_b))
|
6
6
|
end
|
7
7
|
end
|
8
8
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "nutella/core_ext/module"
|
2
|
+
|
3
|
+
INIT_ARGS = %i[a b c]
|
4
|
+
INIT_INSTANCE_VARS = INIT_ARGS.map { |arg| arg.to_s.prepend("@").to_sym }
|
5
|
+
|
6
|
+
INIT_VALUES = [0] * INIT_ARGS.size
|
7
|
+
|
8
|
+
class InitTest
|
9
|
+
initializer *INIT_ARGS
|
10
|
+
end
|
11
|
+
|
12
|
+
class InitBlockTest
|
13
|
+
initializer *INIT_ARGS do
|
14
|
+
@block_run = true
|
15
|
+
end
|
16
|
+
|
17
|
+
def block_run?
|
18
|
+
@block_run
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
RSpec::Matchers.define :have_instance_variables do |*vars|
|
2
|
+
match do |target|
|
3
|
+
(vars - target.instance_variables).empty?
|
4
|
+
end
|
5
|
+
|
6
|
+
failure_message do |target|
|
7
|
+
"expected #{target.inspect} to have all of the following instance variables: " +
|
8
|
+
"\n #{vars.inspect}" +
|
9
|
+
"\nInstead it contains:\n #{target.instance_variables.inspect}"
|
10
|
+
end
|
11
|
+
|
12
|
+
failure_message_when_negated do |target|
|
13
|
+
"expected #{target.inspect} to not have any of the following instance variables: " +
|
14
|
+
"\n #{vars.inspect}" +
|
15
|
+
"\nIt contains:\n #{target.instance_variables.inspect}"
|
16
|
+
end
|
17
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nutella
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: '0.12'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vinny Diehl
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: fuubar
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ~>
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '2.0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ~>
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '2.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -72,14 +72,14 @@ dependencies:
|
|
72
72
|
requirements:
|
73
73
|
- - ~>
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '
|
75
|
+
version: '3.1'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - ~>
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '
|
82
|
+
version: '3.1'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: yard
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -104,6 +104,7 @@ files:
|
|
104
104
|
- lib/nutella/version.rb
|
105
105
|
- lib/nutella/input.rb
|
106
106
|
- lib/nutella/core_ext/enumerable.rb
|
107
|
+
- lib/nutella/core_ext/module.rb
|
107
108
|
- lib/nutella/core_ext/string.rb
|
108
109
|
- lib/nutella/core_ext/string/exclude.rb
|
109
110
|
- lib/nutella/core_ext/string/colour.rb
|
@@ -130,6 +131,7 @@ files:
|
|
130
131
|
- lib/nutella.rb
|
131
132
|
- spec/nutella/core_ext/file_spec.rb
|
132
133
|
- spec/nutella/core_ext/string_spec.rb
|
134
|
+
- spec/nutella/core_ext/module_spec.rb
|
133
135
|
- spec/nutella/core_ext/enumerable_spec.rb
|
134
136
|
- spec/nutella/core_ext/numeric_spec.rb
|
135
137
|
- spec/nutella/core_ext/hash_spec.rb
|
@@ -138,6 +140,8 @@ files:
|
|
138
140
|
- spec/support/alias.rb
|
139
141
|
- spec/support/number_formats.rb
|
140
142
|
- spec/support/apply_tester.rb
|
143
|
+
- spec/support/matchers.rb
|
144
|
+
- spec/support/initializer_testers.rb
|
141
145
|
- spec/spec_helper.rb
|
142
146
|
- .rspec
|
143
147
|
- LICENSE
|
@@ -171,6 +175,7 @@ summary: Spread some Nutella on Ruby to sweeten up its functionality.
|
|
171
175
|
test_files:
|
172
176
|
- spec/nutella/core_ext/file_spec.rb
|
173
177
|
- spec/nutella/core_ext/string_spec.rb
|
178
|
+
- spec/nutella/core_ext/module_spec.rb
|
174
179
|
- spec/nutella/core_ext/enumerable_spec.rb
|
175
180
|
- spec/nutella/core_ext/numeric_spec.rb
|
176
181
|
- spec/nutella/core_ext/hash_spec.rb
|
@@ -179,5 +184,7 @@ test_files:
|
|
179
184
|
- spec/support/alias.rb
|
180
185
|
- spec/support/number_formats.rb
|
181
186
|
- spec/support/apply_tester.rb
|
187
|
+
- spec/support/matchers.rb
|
188
|
+
- spec/support/initializer_testers.rb
|
182
189
|
- spec/spec_helper.rb
|
183
190
|
has_rdoc:
|