blob: 614020e5509a4ff295886531d389a37c8f49f6bc (
plain)
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
|
class MatchYAMLMatcher
def initialize(expected)
if valid_yaml?(expected)
@expected = expected
else
@expected = expected.to_yaml
end
end
def matches?(actual)
@actual = actual
clean_yaml(@actual) == clean_yaml(@expected)
end
def failure_message
["Expected #{@actual.inspect}", " to match #{@expected.inspect}"]
end
def negative_failure_message
["Expected #{@actual.inspect}", " to match #{@expected.inspect}"]
end
protected
def clean_yaml(yaml)
yaml.gsub(/([^-]|^---)\s+\n/, "\\1\n").sub(/\n\.\.\.\n$/, "\n")
end
def valid_yaml?(obj)
require 'yaml'
begin
YAML.unsafe_load(obj)
rescue
false
else
true
end
end
end
module MSpecMatchers
private def match_yaml(expected)
MatchYAMLMatcher.new(expected)
end
end
|