Module: BareTest::IRBMode::IRBContext

Defined in:
lib/baretest/irb_mode.rb

Overview

The class used to recreate the failed/errored assertion’s context. Adds several methods over plain Assertion.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#__status__Object

Provides access the assertions’ original status



53
54
55
# File 'lib/baretest/irb_mode.rb', line 53

def __status__
  @__status__
end

Instance Method Details

#bt!(size = nil) ⇒ Object

Prints the original assertion’s backtrace



119
120
121
122
123
124
125
126
# File 'lib/baretest/irb_mode.rb', line 119

def bt!(size=nil)
  if @__status__.exception then
    size ||= caller.size+3
    puts @__status__.exception.backtrace[0..-size]
  else
    puts "No exception occurred, therefore no backtrace is available"
  end
end

#code!Object

Prints the code of the assertion Be aware that this relies on your code being properly indented.



170
171
172
173
174
175
176
# File 'lib/baretest/irb_mode.rb', line 170

def code!
  if code = @__assertion__.code then
    puts(insert_line_numbers(code, @__assertion__.line-1))
  else
    puts "Code could not be extracted"
  end
end

#cv!Object

Returns an array of all class variable names



134
135
136
# File 'lib/baretest/irb_mode.rb', line 134

def cv!
  puts *self.class.class_variables.sort
end

#description!Object

Prints a string of the original assertion’s nesting within suites



159
160
161
# File 'lib/baretest/irb_mode.rb', line 159

def description!
  puts @__assertion__.description
end

#e!Object

Prints the original assertion’s error message and backtrace



102
103
104
105
# File 'lib/baretest/irb_mode.rb', line 102

def e!
  em!
  bt!(caller.size+3)
end

#em!Object

Prints the original assertion’s error message



108
109
110
111
112
113
114
115
116
# File 'lib/baretest/irb_mode.rb', line 108

def em!
  if @__status__.exception then
    puts @__status__.exception.message
  elsif @__status__.failure_reason
    puts @__status__.failure_reason
  else
    puts "No exception or failure reason available"
  end
end

#eval!(from, number = nil, explicit_binding = nil) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/baretest/irb_mode.rb', line 178

def eval!(from, number=nil, explicit_binding=nil)
  if code = @__assertion__.code then
    if from.is_a?(Range) then
      number = from.end-from.begin+1
      from   = from.begin
    end
    number    ||= 1
    total_lines = code.chomp.count("\n")-1
    first_line  = @__assertion__.line
    if !from.between?(first_line, first_line+total_lines-1)
      puts "From must be between #{first_line} and #{first_line+total_lines-1}"
    elsif !number.between?(0, total_lines)
      puts "Number must be between 1 and #{total_lines}"
    else
      from -= first_line-1
      puts "Evaluating: ", *code.split(/\n/)[from, number]
      eval(code.split(/\n/)[from, number].join("\n"), explicit_binding || context.workspace.binding) # this 'context' comes from irb
    end
  else
    puts "Code could not be extracted"
  end
end

#file!Object

Returns the original assertion’s file



144
145
146
# File 'lib/baretest/irb_mode.rb', line 144

def file!
  puts @__assertion__.file
end

#gv!(remove_standard = true) ⇒ Object

Returns an array of all global variable names



139
140
141
# File 'lib/baretest/irb_mode.rb', line 139

def gv!(remove_standard=true)
  puts *(global_variables-(remove_standard ? IRBMode::RemoveGlobals : [])).sort
end

#helpObject Also known as: help!

Prints a list of available helper methods



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/baretest/irb_mode.rb', line 56

def help
  puts "Available methods:",
       "s!           - the assertions' original status",
       "sc!          - the assertions' original status code",
       "e!           - prints the error message and full backtrace",
       "em!          - prints the error message",
       "bt!          - prints the full backtrace",
       "lv!          - lists all available local variables",
       "iv!          - lists all available instance variables",
       "cv!          - lists all available class variables",
       "gv!          - lists all available global variables, per default dropping rubys",
       "               standard globals (use gv!(false) to avoid that)",
       "file!        - the file this assertion was defined in",
       "line!        - the line number in the file where this assertion's definition",
       "               starts",
       "nesting!     - a >-separated list of suite descriptions this assertion is nested",
       "description! - this assertion's description",
       "code!        - the code of this assertion",
       "eval!        - eval (from_line, number_of_lines) or (from_line..to_line)",
       #"restart! - Restart this irb session, resetting everything",
       "irb_help     - irb's original help",
       "q            - Quit - alias to irb's exit",
       "help         - this text you're reading right now"
end

#insert_line_numbers(code, start_line = 1) ⇒ Object

Prepend the line number in front of ever line



202
203
204
205
206
# File 'lib/baretest/irb_mode.rb', line 202

def insert_line_numbers(code, start_line=1) # :nodoc:
  digits       = Math.log10(start_line+code.count("\n")).floor+1
  current_line = start_line-1
  code.gsub(/^/) { sprintf '  %0*d  ', digits, current_line+=1 }
end

#iv!Object

Returns an array of all instance variable names



129
130
131
# File 'lib/baretest/irb_mode.rb', line 129

def iv!
  puts *instance_variables.sort
end

#line!Object

Returns the original assertion’s line



149
150
151
# File 'lib/baretest/irb_mode.rb', line 149

def line!
  puts @__assertion__.line
end

#nesting!Object

Prints a string of the original assertion’s nesting within suites



164
165
166
# File 'lib/baretest/irb_mode.rb', line 164

def nesting!
  puts @__assertion__.suite.ancestors[0..-2].reverse.map { |s| s.description }.join(' > ')
end

#open!Object

Returns the original assertion’s line



154
155
156
# File 'lib/baretest/irb_mode.rb', line 154

def open!
  `bbedit '#{@__assertion__.file}:#{@__assertion__.line}'`
end

#qObject

Quit - an alias to irb’s exit



87
88
89
# File 'lib/baretest/irb_mode.rb', line 87

def q
  exit
end

#s!Object

Returns the original assertion’s status



92
93
94
# File 'lib/baretest/irb_mode.rb', line 92

def s!
  @__status__
end

#sc!Object

Returns the original assertion’s status code



97
98
99
# File 'lib/baretest/irb_mode.rb', line 97

def sc!
  @__status__.status
end

#to_sObject

:nodoc:



82
83
84
# File 'lib/baretest/irb_mode.rb', line 82

def to_s # :nodoc:
  "Context"
end