lib/mattock/testing/mock-command-line.rb in mattock-0.3.0 vs lib/mattock/testing/mock-command-line.rb in mattock-0.3.1

- old
+ new

@@ -22,47 +22,45 @@ attr_reader :exit_code, :streams alias exit_status exit_code end + class CommandLine + def self.execute(*args) + fail "Command line executed in specs without 'expect_command' or 'expect_some_commands'" + end + end + module CommandLineExampleGroup - def self.included(group) - group.class_eval do - let :pairs do - [] - end + module MockingExecute + def execute + Mattock::CommandLine.execute(command) + end + end - before :each do - Mattock::CommandLine.should_receive(:execute) do |cmd| - pattern, res = pairs.shift - pattern.should =~ cmd - Mattock::MockCommandResult.create(*res) - end.any_number_of_times - end - after :each do - pairs.should have_all_been_called + def self.included(group) + group.before :each do + @original_execute = Mattock::CommandLine.instance_method(:execute) + unless MockingExecute > Mattock::CommandLine + Mattock::CommandLine.send(:include, MockingExecute) end + Mattock::CommandLine.send(:remove_method, :execute) end + + group.after :each do + Mattock::CommandLine.send(:define_method, :execute, @original_execute) + end end - def expect_command(cmd, *result) - raise ArgumentError, "Regexp expected: not #{cmd.inspect}" unless Regexp === cmd - pairs << [cmd, result] + #Registers indifference as to exactly what commands get called + def expect_some_commands + Mattock::CommandLine.should_receive(:execute).any_number_of_times.and_return(MockCommandResult.create(0)) end - module Matchers - extend RSpec::Matchers::DSL - - define :have_all_been_called do - match do |list| - list.empty? - end - - failure_message_for_should do |list| - "Expected all commands to be run, but: #{list.map{|item| item[0].source.inspect}.join(", ")} #{list.length > 1 ? "were" : "was"} not." - end - end + #Registers an expectation about a command being run - expectations are + #ordered + def expect_command(cmd, *result) + Mattock::CommandLine.should_receive(:execute, :expected_from => caller(1)[0]).with(cmd).ordered.and_return(MockCommandResult.create(*result)) end - include Matchers end end