-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrep_and_grep_v.rb
More file actions
63 lines (53 loc) · 1.7 KB
/
grep_and_grep_v.rb
File metadata and controls
63 lines (53 loc) · 1.7 KB
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
require "minitest/autorun"
# ruby -Ilib:test grep_and_grep_v.rb
describe Array do
describe "grep by exact match" do
describe "#monkey_grep" do
it "returns all elements that are equal to matchable" do
[1, 2, 3, 1].grep(1).must_equal [1, 1]
end
end
describe "#monkey_grep_v" do
it "returns all elements that are not equal to matchable" do
array = ['hello', 'world', 'I', 'am', 'here']
array.monkey_grep_v(/l/).must_equal ['I', 'am', 'here']
end
end
end
describe "grep by regexp" do
it "returns array of all elements that match regexp" do
array = ['hello', 'world', 'I', 'am', 'here']
array.monkey_grep(/l/).must_equal ['hello', 'world']
end
it "it ignores non-strings when matching regexp" do
["word", 9, {}, [], false, nil].grep(/o/).must_equal ["word"]
end
end
describe "grep by range" do
it "returns array of all elements that are included in range" do
[2, 4, 6, 8].monkey_grep(3..7).must_equal [4, 6]
end
end
end
describe Range do
describe "#monkey_grep" do
it "returns an array of all values that equal the argument" do
(1..3).monkey_grep(3).must_equal [3]
end
it "won't match the last value in a non-inclusive range" do
(1...3).monkey_grep(3).must_equal []
end
describe "grep by range" do
it "returns the intersection of the two ranges as an array" do
(-5..5).monkey_grep(0..10).must_equal [0,1,2,3,4,5]
end
end
end
end
describe Hash do
describe "#monkey_grep" do
it "returns an array of all key-value pairs that match the given key-value pair" do
{a: 1, b: 2, c: 3}.monkey_grep([:b, 2]).must_equal [[:b, 2]]
end
end
end