Skip to content

Commit

Permalink
Add provisional CommandHelpers module
Browse files Browse the repository at this point in the history
  • Loading branch information
tdg5 committed Mar 17, 2015
1 parent 89af2bb commit e945e0b
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
70 changes: 70 additions & 0 deletions lib/pry_test_case/command_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Mixin providing methods for executing Pry commands in various contexts and
# environments.
module PryTestCase::CommandHelpers
# Evaluates the given command string as the Pry CLI would evaluate it. Behaves
# more like executing the given command in a live Pry session would. This
# means that Pry will intercept some behaviors that may be valuable to your
# tests. For more direct access to the results and errors of a command
# execution, see {#command_exec_direct}.
#
# @param [String] command_string The command string to execute including any
# arguments that the command string might take.
# @param [Hash] options Optional arguments for manipulating the command
# execution environment.
# @option options [Binding] :target (TOPLEVEL_BINDING) The target Binding that the command should
# be executed in. Also aliased as *:context*.
# @option options [Boolean] :show_output (true) Flag indicating whether or not the
# output of the command should be displayed.
# @option options [IO] :output (Pry.config.output) The IO object that the
# output of the command should be written to. If *:show_output* is false,
# the given IO object will be ignored and a new StringIO object will be used
# instead.
# @option options [Pry::CommandSet] :commands (Pry.config.commands) The
# command set that the generated Pry instance should be created with.
# @return [Object] The result of the command execution. The exact value
# returned varies depending on the command.
# @see #command_exec_direct
def command_exec_cli(command_string, options = {})
Pry.run_command(command_string, options)
end

# Evaluates the given command string and runs the command directly without
# going through the Pry CLI eval cycle. Allows more direct access to errors
# and other things the CLI can make hard to get direct access to. To execute a
# command in an environment more similar to a live Pry session, see
# {#command_exec_cli}.
#
# @param [String] command_string The command string to execute including any
# arguments that the command string might take.
# @param [Hash] options Optional arguments for manipulating the command
# execution environment.
# @option options [Binding] :target (TOPLEVEL_BINDING) The target Binding that the command should
# be executed in. Also aliased as *:context*.
# @option options [IO] :output (Pry.config.output) The IO object that the
# output of the command should be written to.
# @option options [Pry::CommandSet] :command_set (Pry.config.commands) The
# command set that should be passed to the specified command when it is
# initialized.
# @option options [Pry] :pry_instance The Pry instance that should be passed
# to the specified command when it is initialized. By default a new Pry
# instance will be generated from the given options.
# @return [Object] The result of the command execution. The exact value
# returned varies depending on the command.
# @see #command_exec_cli
def command_exec_direct(command_string, options = {})
exec_options = {
:target => TOPLEVEL_BINDING,
:output => Pry.config.output,
:command_set => Pry.config.commands,
}.merge!(options)
exec_options[:eval_string] = command_string
exec_options[:pry_instance] ||= Pry.new({
:target => exec_options[:target],
:output => exec_options[:output],
:commands => exec_options[:command_set],
})
args = command_string.split(/\s+/)
match = args.shift
Pry.commands.run_command(exec_options, match, *args)
end
end
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "test_helpers/coverage" if ENV["CI"]
require "minitest/autorun"
require "mocha/setup"
require "pry"
require "pry_test_case"
require "test_helpers/test_case"
61 changes: 61 additions & 0 deletions test/unit/command_helpers_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require "test_helper"
require "pry_test_case/command_helpers"

module PryTestCase::Test
class CommandHelpersTest < TestCase
Subject = PryTestCase::CommandHelpers
TestSubject = Class.new { include Subject }

context Subject.name do
subject { TestSubject.new }

context "#command_exec_cli" do
should "pass the given command and options directly to Pry.run_command" do
command = "some-command"
options = { :fake => :options }
Pry.expects(:run_command).with(command, options)
subject.command_exec_cli(command, options)
end
end

context "#command_exec_direct" do
setup do
@command = "some-command"
@arg = "with_args"
@command_with_arg = "#{@command} #{@arg}"
end

should "pass the given command and options to Pry.commands.run_command" do
opts = {
:target => "target",
:output => "output",
:command_set => "command_set",
:pry_instance => "pry_instance",
}
expected_opts = opts.merge(:eval_string => @command_with_arg)
Pry.commands.expects(:run_command).with(expected_opts, @command, @arg)
subject.command_exec_direct(@command_with_arg, opts)
end

should "generate sane defaults for options not given" do
expected_opts = {
:command_set => Pry.config.commands,
:eval_string => @command_with_arg,
:output => Pry.config.output,
:target => TOPLEVEL_BINDING,
}
pry_instance_opts = {
:commands => expected_opts[:command_set],
:output => expected_opts[:output],
:target => expected_opts[:target],
}
pry_instance = Pry.new(pry_instance_opts.dup)
Pry.expects(:new).with(pry_instance_opts).returns(pry_instance)
expected_opts[:pry_instance] = pry_instance
Pry.commands.expects(:run_command).with(expected_opts, @command, @arg)
subject.command_exec_direct(@command_with_arg)
end
end
end
end
end

0 comments on commit e945e0b

Please sign in to comment.