-
-
Notifications
You must be signed in to change notification settings - Fork 397
/
start_and_end_with.rb
50 lines (42 loc) · 1.54 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
module RSpec
module Matchers
module BuiltIn
class StartAndEndWith
include BaseMatcher
def initialize(*expected)
@expected = expected.length == 1 ? expected.first : expected
end
def matches?(actual)
@actual = actual.respond_to?(:[]) ? actual : (raise ArgumentError.new("#{actual.inspect} does not respond to :[]"))
begin
@expected.respond_to?(:length) ? subset_matches?(@expected, @actual) : element_matches?(@expected, @actual)
rescue ArgumentError
raise ArgumentError.new("#{actual.inspect} does not have ordered elements")
end
end
def failure_message_for_should
"expected #{@actual.inspect} to #{self.class.name.split('::').last.sub(/With/,'').downcase} with #{@expected.inspect}"
end
def failure_message_for_should_not
"expected #{@actual.inspect} not to #{self.class.name.split('::').last.sub(/With/,'').downcase} with #{@expected.inspect}"
end
end
class StartWith < StartAndEndWith
def subset_matches?(expected, actual)
actual[0, expected.length] == expected
end
def element_matches?(expected, actual)
@actual[0] == @expected
end
end
class EndWith < StartAndEndWith
def subset_matches?(expected, actual)
actual[-expected.length, expected.length] == expected
end
def element_matches?(expected, actual)
actual[-1] == expected
end
end
end
end
end