Class: PryTester

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of PryTester.



8
9
10
11
12
13
14
# File 'lib/pry_tester.rb', line 8

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.



4
5
6
# File 'lib/pry_tester.rb', line 4

def out
  @out
end

#pryObject (readonly)

Returns the value of attribute pry.



4
5
6
# File 'lib/pry_tester.rb', line 4

def pry
  @pry
end

Instance Method Details

#eval(*strs) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pry_tester.rb', line 16

def eval(*strs)
  reset_output
  result = nil

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

    if @pry.process_command(str)
      result = last_command_result_or_output
    else
      result = @pry.evaluate_ruby(str)
    end
  end

  result
end

#last_command_resultObject



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

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

#last_outputObject



47
48
49
# File 'lib/pry_tester.rb', line 47

def last_output
  @out.string if @out
end

#process_command(command_str) ⇒ Object



51
52
53
54
# File 'lib/pry_tester.rb', line 51

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

#push(*lines) ⇒ Object



37
38
39
40
41
# File 'lib/pry_tester.rb', line 37

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

#push_binding(context) ⇒ Object



43
44
45
# File 'lib/pry_tester.rb', line 43

def push_binding(context)
  @pry.push_binding context
end