/
start_and_end_with.rb
57 lines (46 loc) · 1.33 KB
/
start_and_end_with.rb
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
module RSpec
module Matchers
module BuiltIn
class StartAndEndWith < BaseMatcher
def initialize(*expected)
@expected = expected.length == 1 ? expected.first : expected
end
def matches?(actual)
@actual = actual
raise invalid_actual_object unless actual.respond_to?(:[])
begin
return subset_matches? if expected.respond_to?(:length)
element_matches?
rescue ArgumentError
raise actual_is_unordered
end
end
private
def invalid_actual_object
ArgumentError.new("#{actual.inspect} does not respond to :[]")
end
def actual_is_unordered
ArgumentError.new("#{actual.inspect} does not have ordered elements")
end
end
class StartWith < StartAndEndWith
private
def subset_matches?
values_match?(expected, actual[0, expected.length])
end
def element_matches?
values_match?(expected, actual[0])
end
end
class EndWith < StartAndEndWith
private
def subset_matches?
values_match?(expected, actual[-expected.length, expected.length])
end
def element_matches?
values_match?(expected, actual[-1])
end
end
end
end
end