forked from travis-ci/dpl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
have_run.rb
64 lines (51 loc) · 1.59 KB
/
have_run.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
58
59
60
61
62
63
64
module Support
module Matchers
module RecordCmds
def self.included(base)
base.before(:context) { @expected_cmds = [] }
end
end
class HaveRun < Struct.new(:cmds, :str)
include Shared
def matches?(cmd)
cmds << str if cmds
@cmd = cmd
cmd.ctx.cmds.any? { |cmd, opts| match?(cmd) }
end
def description
"run #{str.inspect}"
end
def failure_message
"Expected the command\n\n #{str}\n\nto have run, but it didn't. Instead, we have run the commands:\n\n #{indent(cmd.ctx.cmds.join("\n"))}"
end
def failure_message_when_negated
"Expected the command\n\n #{str}\n\nto not have run, but it did:\n\n #{indent(cmd.ctx.cmds.join("\n"))}"
end
end
# needs record: true on the context
class HaveRunInOrder < Struct.new(:expected, :example)
include Shared
attr_reader :actual
def matches?(cmd)
@actual = cmd.ctx.cmds.map { |str| expected.detect { |cmd| match?(str, cmd) } }.compact.uniq
expected == actual
end
def description
'have run commands in order'
end
def failure_message
"Expected the commands\n\n#{indent(expected.join("\n"))}\n\nto have run in this order, but they have run as follows:\n\n#{indent(actual.join("\n"))}"
end
def indent(strs)
strs.lines.map { |line| " #{line}" }.join
end
end
def have_run(str)
HaveRun.new(@expected_cmds, str)
end
def have_run_in_order
# p example_group
HaveRunInOrder.new(@expected_cmds)
end
end
end