pry 0.9.8pre2 → 0.9.8pre3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,53 +0,0 @@
1
- class Pry
2
- # Command contexts are the objects runing each command.
3
- # Helper modules can be mixed into this class.
4
- class CommandContext
5
-
6
- # represents a void return value for a command
7
- VOID_VALUE = Object.new
8
-
9
- # give it a nice inspect
10
- def VOID_VALUE.inspect() "void" end
11
-
12
- attr_accessor :command_name
13
- attr_accessor :output
14
- attr_accessor :target
15
- attr_accessor :target_self
16
- attr_accessor :captures
17
- attr_accessor :eval_string
18
- attr_accessor :arg_string
19
- attr_accessor :opts
20
- attr_accessor :command_set
21
- attr_accessor :command_processor
22
- attr_accessor :_pry_
23
-
24
- # Run a command from another command.
25
- # @param [String] command_string The string that invokes the command
26
- # @param [Array] args Further arguments to pass to the command
27
- # @example
28
- # run "show-input"
29
- # @example
30
- # run ".ls"
31
- # @example
32
- # run "amend-line", "5", 'puts "hello world"'
33
- def run(command_string, *args)
34
- complete_string = "#{command_string} #{args.join(" ")}"
35
- command_processor.process_commands(complete_string, eval_string, target)
36
- end
37
-
38
- def commands
39
- command_set.commands
40
- end
41
-
42
- def text
43
- Pry::Helpers::Text
44
- end
45
-
46
- def void
47
- VOID_VALUE
48
- end
49
-
50
- include Pry::Helpers::BaseHelpers
51
- include Pry::Helpers::CommandHelpers
52
- end
53
- end
@@ -1,194 +0,0 @@
1
- require 'forwardable'
2
-
3
- class Pry
4
- class CommandProcessor
5
-
6
- # Wraps the return result of process_commands, indicates if the
7
- # result IS a command and what kind of command (e.g void)
8
- class Result
9
- attr_reader :retval
10
-
11
- def initialize(is_command, keep_retval = false, retval = nil)
12
- @is_command, @keep_retval, @retval = is_command, keep_retval, retval
13
- end
14
-
15
- # Is the result a command?
16
- # @return [Boolean]
17
- def command?
18
- @is_command
19
- end
20
-
21
- # Is the result a command and if it is, is it a void command?
22
- # (one that does not return a value)
23
- # @return [Boolean]
24
- def void_command?
25
- (command? && !keep_retval?) || retval == CommandContext::VOID_VALUE
26
- end
27
-
28
- # Is the return value kept for this command? (i.e :keep_retval => true)
29
- # @return [Boolean]
30
- def keep_retval?
31
- @keep_retval
32
- end
33
- end
34
-
35
- extend Forwardable
36
-
37
- attr_accessor :pry_instance
38
-
39
- def initialize(pry_instance)
40
- @pry_instance = pry_instance
41
- end
42
-
43
- def_delegators :@pry_instance, :commands, :output
44
-
45
- # Is the string a valid command?
46
- # @param [String] val The string passed in from the Pry prompt.
47
- # @param [Binding] target The context where the string should be
48
- # interpolated in.
49
- # @return [Boolean] Whether the string is a valid command.
50
- def valid_command?(val, target=binding)
51
- !!(command_matched(val, target)[0])
52
- end
53
-
54
- # Convert the object to a form that can be interpolated into a
55
- # Regexp cleanly.
56
- # @return [String] The string to interpolate into a Regexp
57
- def convert_to_regex(obj)
58
- case obj
59
- when String
60
- Regexp.escape(obj)
61
- else
62
- obj
63
- end
64
- end
65
-
66
- # Revaluate the string (str) and perform interpolation.
67
- # @param [String] str The string to reevaluate with interpolation.
68
- # @param [Binding] target The context where the string should be
69
- # interpolated in.
70
- # @return [String] The reevaluated string with interpolations
71
- # applied (if any).
72
- def interpolate_string(str, target)
73
- dumped_str = str.dump
74
- dumped_str.gsub!(/\\\#\{/, '#{')
75
- target.eval(dumped_str)
76
- end
77
-
78
- # Determine whether a Pry command was matched and return command data
79
- # and argument string.
80
- # This method should not need to be invoked directly.
81
- # @param [String] val The line of input.
82
- # @param [Binding] target The binding to perform string
83
- # interpolation against.
84
- # @return [Array] The command data and arg string pair
85
- def command_matched(val, target)
86
- _, cmd_data = commands.commands.find do |name, data|
87
- prefix = convert_to_regex(Pry.config.command_prefix)
88
- prefix = "(?:#{prefix})?" unless data.options[:use_prefix]
89
-
90
- command_regex = /^#{prefix}#{convert_to_regex(name)}(?!\S)/
91
-
92
- if command_regex =~ val
93
- if data.options[:interpolate]
94
- val.replace(interpolate_string(val, target))
95
- command_regex =~ val # re-match with the interpolated string
96
- end
97
- true
98
- end
99
- end
100
-
101
- [cmd_data, (Regexp.last_match ? Regexp.last_match.captures : nil), (Regexp.last_match ? Regexp.last_match.end(0) : nil)]
102
- end
103
-
104
- # Display a warning if a command collides with a local/method in
105
- # the current scope.
106
- # @param [String] command_name_match The name of the colliding command.
107
- # @param [Binding] target The current binding context.
108
- def check_for_command_name_collision(command_name_match, target)
109
- if collision_type = target.eval("defined?(#{command_name_match})")
110
- pry_instance.output.puts "#{Pry::Helpers::Text.bold('WARNING:')} Command name collision with a #{collision_type}: '#{command_name_match}'\n\n"
111
- end
112
- rescue Pry::RescuableException
113
- end
114
-
115
- # Process Pry commands. Pry commands are not Ruby methods and are evaluated
116
- # prior to Ruby expressions.
117
- # Commands can be modified/configured by the user: see `Pry::Commands`
118
- # This method should not need to be invoked directly - it is called
119
- # by `Pry#r`.
120
- # @param [String] val The current line of input.
121
- # @param [String] eval_string The cumulative lines of input for
122
- # multi-line input.
123
- # @param [Binding] target The receiver of the commands.
124
- # @return [Pry::CommandProcessor::Result] A wrapper object
125
- # containing info about the result of the command processing
126
- # (indicating whether it is a command and if it is what kind of
127
- # command it is.
128
- def process_commands(val, eval_string, target)
129
-
130
- command, captures, pos = command_matched(val, target)
131
-
132
- # no command was matched, so return to caller
133
- return Result.new(false) if !command
134
-
135
- arg_string = val[pos..-1]
136
-
137
- check_for_command_name_collision(val[0..pos].rstrip, target) if Pry.config.collision_warning
138
-
139
- # remove the one leading space if it exists
140
- arg_string.slice!(0) if arg_string.start_with?(" ")
141
-
142
- if arg_string
143
- args = command.options[:shellwords] ? Shellwords.shellwords(arg_string) : arg_string.split(" ")
144
- else
145
- args = []
146
- end
147
-
148
- options = {
149
- :val => val,
150
- :arg_string => arg_string,
151
- :eval_string => eval_string,
152
- :commands => commands.commands,
153
- :captures => captures
154
- }
155
-
156
- ret = execute_command(target, command.name, options, *(captures + args))
157
-
158
- Result.new(true, command.options[:keep_retval], ret)
159
- end
160
-
161
- # Execute a Pry command.
162
- # This method should not need to be invoked directly.
163
- # @param [Binding] target The target of the Pry session.
164
- # @param [String] command The name of the command to be run.
165
- # @param [Hash] options The options to set on the Commands object.
166
- # @param [Array] args The command arguments.
167
- # @return [Object] The value returned by the command
168
- def execute_command(target, command, options, *args)
169
- context = CommandContext.new
170
-
171
- # set some useful methods to be used by the action blocks
172
- context.opts = options
173
- context.target = target
174
- context.target_self = target.eval('self')
175
- context.output = output
176
- context.captures = options[:captures]
177
- context.eval_string = options[:eval_string]
178
- context.arg_string = options[:arg_string]
179
- context.command_set = commands
180
- context._pry_ = @pry_instance
181
-
182
- context.command_processor = self
183
-
184
- ret = nil
185
- catch(:command_done) do
186
- ret = commands.run_command(context, command, *args)
187
- end
188
-
189
- options[:val].replace("")
190
-
191
- ret
192
- end
193
- end
194
- end
@@ -1,176 +0,0 @@
1
- require 'helper'
2
-
3
- describe "Pry::CommandProcessor" do
4
- before do
5
- @pry = Pry.new
6
- @pry.commands = Pry::CommandSet.new
7
- @command_processor = Pry::CommandProcessor.new(@pry)
8
- end
9
-
10
- after do
11
- @pry.commands = Pry::CommandSet.new
12
- end
13
-
14
- it 'should accurately determine if a command is valid' do
15
- @pry.commands.command("test-command") {}
16
- valid = @command_processor.valid_command? "test-command"
17
- valid.should == true
18
-
19
- valid = @command_processor.valid_command? "blah"
20
- valid.should == false
21
- end
22
-
23
- it 'should correctly match a simple string command' do
24
- @pry.commands.command("test-command") {}
25
- command, captures, pos = @command_processor.command_matched "test-command", binding
26
-
27
- command.name.should == "test-command"
28
- captures.should == []
29
- pos.should == "test-command".length
30
- end
31
-
32
- it 'should correctly match a simple string command with parameters' do
33
- @pry.commands.command("test-command") { |arg|}
34
- command, captures, pos = @command_processor.command_matched "test-command hello", binding
35
-
36
- command.name.should == "test-command"
37
- captures.should == []
38
- pos.should == "test-command".length
39
- end
40
-
41
- it 'should not match when the relevant command does not exist' do
42
- command, captures, pos = @command_processor.command_matched "test-command", binding
43
-
44
- command.should == nil
45
- captures.should == nil
46
- end
47
-
48
- it 'should correctly match a regex command' do
49
- @pry.commands.command(/rue(.?)/) { }
50
- command, captures, pos = @command_processor.command_matched "rue hello", binding
51
-
52
- command.name.should == /rue(.?)/
53
- captures.should == [""]
54
- pos.should == 3
55
- end
56
-
57
- it 'should correctly match a regex command and extract the capture groups' do
58
- @pry.commands.command(/rue(.?)/) { }
59
- command, captures, pos = @command_processor.command_matched "rue5 hello", binding
60
-
61
- command.name.should == /rue(.?)/
62
- captures.should == ["5"]
63
- pos.should == 4
64
- end
65
-
66
- it 'should correctly match a string command with spaces in its name' do
67
- @pry.commands.command("test command") {}
68
- command, captures, pos = @command_processor.command_matched "test command", binding
69
-
70
- command.name.should == "test command"
71
- captures.should == []
72
- pos.should == command.name.length
73
- end
74
-
75
- it 'should correctly match a string command with spaces in its name with parameters' do
76
- @pry.commands.command("test command") {}
77
- command, captures, pos = @command_processor.command_matched "test command param1 param2", binding
78
-
79
- command.name.should == "test command"
80
- captures.should == []
81
- pos.should == command.name.length
82
- end
83
-
84
- it 'should correctly match a command preceded by the command_prefix if one is defined' do
85
- Pry.config.command_prefix = "%"
86
-
87
- @pry.commands.command("test-command") {}
88
- command, captures, pos = @command_processor.command_matched "%test-command hello", binding
89
-
90
- command.name.should == "test-command"
91
- captures.should == []
92
- pos.should == "test-command".length + "%".length
93
-
94
- Pry.config.command_prefix = ''
95
- end
96
-
97
- it 'should not match a command not preceded by the command_prefix if one is defined' do
98
- Pry.config.command_prefix = "%"
99
-
100
- @pry.commands.command("test-command") {}
101
- command, captures, pos = @command_processor.command_matched "test-command hello", binding
102
-
103
- command.should == nil
104
- captures.should == nil
105
-
106
- Pry.config.command_prefix = ''
107
- end
108
-
109
- it 'should match a command preceded by the command_prefix when :use_prefix => false' do
110
- Pry.config.command_prefix = "%"
111
-
112
- @pry.commands.command("test-command", "", :use_prefix => false) {}
113
- command, captures, pos = @command_processor.command_matched "%test-command hello", binding
114
-
115
- command.name.should == "test-command"
116
- captures.should == []
117
- pos.should == "test-command".length + "%".length
118
-
119
- Pry.config.command_prefix = ''
120
- end
121
-
122
- it 'should match a command not preceded by the command_prefix when :use_prefix => false' do
123
- Pry.config.command_prefix = "%"
124
-
125
- @pry.commands.command("test-command", "", :use_prefix => false) {}
126
- command, captures, pos = @command_processor.command_matched "test-command hello", binding
127
-
128
- command.name.should == "test-command"
129
- captures.should == []
130
- pos.should == "test-command".length
131
-
132
- Pry.config.command_prefix = ''
133
- end
134
-
135
- it 'should correctly match a regex command with spaces in its name' do
136
- regex_command_name = /test\s+(.+)\s+command/
137
- @pry.commands.command(regex_command_name) {}
138
-
139
- sample_text = "test friendship command"
140
- command, captures, pos = @command_processor.command_matched sample_text, binding
141
-
142
- command.name.should == regex_command_name
143
- captures.should == ["friendship"]
144
- pos.should == sample_text.size
145
- end
146
-
147
- it 'should correctly match a complex regex command' do
148
- regex_command_name = /\.(.*)/
149
- @pry.commands.command(regex_command_name) {}
150
-
151
- sample_text = ".cd ~/pry"
152
- command, captures, pos = @command_processor.command_matched sample_text, binding
153
-
154
- command.name.should == regex_command_name
155
- captures.should == ["cd ~/pry"]
156
- pos.should == sample_text.size
157
- end
158
-
159
- it 'should not interpolate commands that have :interpolate => false (interpolate_string should *not* be called)' do
160
- @pry.commands.command("boast", "", :interpolate => false) {}
161
-
162
- # remember to use '' instead of "" when testing interpolation or
163
- # you'll cause yourself incredible confusion
164
- lambda { @command_processor.command_matched('boast #{c}', binding) }.should.not.raise NameError
165
- end
166
-
167
- it 'should only execute the contents of an interpolation once' do
168
- $obj = 'a'
169
-
170
- redirect_pry_io(InputTester.new('cat #{$obj.succ!}'), StringIO.new) do
171
- Pry.new.rep
172
- end
173
-
174
- $obj.should == 'b'
175
- end
176
- end