Sha256: fff278edd6bcc3b84dfabdf41aecaa5ea8146142cd155cb518d962f8dd24b234

Contents?: true

Size: 1.73 KB

Versions: 5

Compression:

Stored size: 1.73 KB

Contents

require 'mattock/command-line'

module Mattock
  class MockCommandResult < CommandLine::CommandRunResult
    def self.create(*args)
      if args.length == 1
        args = [args[0], {1 => ""}]
      end

      if String == args[1]
        args[1] = {1 => args[1]}
      end

      return self.new(*args)
    end

    def initialize(code, streams)
      @streams = streams
      @exit_code = code
    end

    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
    include CommandLineDSL
    module MockingExecute
      def execute
        Mattock::CommandLine.execute(command)
      end
    end


    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

    #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

    #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
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mattock-0.7.1 lib/mattock/testing/mock-command-line.rb
mattock-0.7.0 lib/mattock/testing/mock-command-line.rb
mattock-0.5.3 lib/mattock/testing/mock-command-line.rb
mattock-0.5.2 lib/mattock/testing/mock-command-line.rb
mattock-0.5.0 lib/mattock/testing/mock-command-line.rb