-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add provisional CommandHelpers module
- Loading branch information
Showing
3 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |