Class: Pry::Testable::PryTester

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/pry/testable/pry_tester.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Forwardable

def_private_delegators

Constructor Details

#initialize(target = TOPLEVEL_BINDING, options = {}) ⇒ PryTester

Returns a new instance of PryTester.



12
13
14
15
16
17
# File 'lib/pry/testable/pry_tester.rb', line 12

def initialize(target = TOPLEVEL_BINDING, options = {})
  @pry = Pry.new(options.merge(target: target))
  @history = options[:history]
  @pry.inject_sticky_locals!
  reset_output
end

Instance Attribute Details

#outObject (readonly)

Returns the value of attribute out.



9
10
11
# File 'lib/pry/testable/pry_tester.rb', line 9

def out
  @out
end

#pryObject (readonly)

Returns the value of attribute pry.



9
10
11
# File 'lib/pry/testable/pry_tester.rb', line 9

def pry
  @pry
end

Instance Method Details

#eval(*strs) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pry/testable/pry_tester.rb', line 19

def eval(*strs)
  reset_output
  result = nil

  strs.flatten.each do |str|
    # Check for space prefix. See #1369.
    str = "#{str.strip}\n" if str !~ /^\s\S/
    @history.push str if @history

    result =
      if @pry.process_command(str)
        last_command_result_or_output
      else
        # Check if this is a multiline paste.
        begin
          complete_expr = Pry::Code.complete_expression?(str)
        rescue SyntaxError => exception
          @pry.output.puts(
            "SyntaxError: #{exception.message.sub(/.*syntax error, */m, '')}"
          )
        end
        @pry.evaluate_ruby(str) if complete_expr
      end
  end

  result
end

#last_command_resultObject



66
67
68
69
# File 'lib/pry/testable/pry_tester.rb', line 66

def last_command_result
  result = Pry.current[:pry_cmd_result]
  result.retval if result
end

#last_outputObject



57
58
59
# File 'lib/pry/testable/pry_tester.rb', line 57

def last_output
  @out.string if @out
end

#process_command(command_str) ⇒ Object



61
62
63
64
# File 'lib/pry/testable/pry_tester.rb', line 61

def process_command(command_str)
  @pry.process_command(command_str) || raise("Not a valid command")
  last_command_result_or_output
end

#push(*lines) ⇒ Object



47
48
49
50
51
# File 'lib/pry/testable/pry_tester.rb', line 47

def push(*lines)
  Array(lines).flatten.each do |line|
    @pry.eval(line)
  end
end

#push_binding(context) ⇒ Object



53
54
55
# File 'lib/pry/testable/pry_tester.rb', line 53

def push_binding(context)
  @pry.push_binding context
end