| Module | BareTest::IRBMode::IRBContext |
| In: |
lib/baretest/irb_mode.rb
|
The class used to recreate the failed/errored assertion‘s context. Adds several methods over plain Assertion.
| __status__ | [RW] | Provides access the assertions’ original status |
Prints the original assertion‘s backtrace
# File lib/baretest/irb_mode.rb, line 119
119: def bt!(size=nil)
120: if @__status__.exception then
121: size ||= caller.size+3
122: puts @__status__.exception.backtrace[0..-size]
123: else
124: puts "No exception occurred, therefore no backtrace is available"
125: end
126: end
Prints the code of the assertion Be aware that this relies on your code being properly indented.
# File lib/baretest/irb_mode.rb, line 170
170: def code!
171: if code = @__assertion__.code then
172: puts(insert_line_numbers(code, @__assertion__.line-1))
173: else
174: puts "Code could not be extracted"
175: end
176: end
Returns an array of all class variable names
# File lib/baretest/irb_mode.rb, line 134
134: def cv!
135: puts *self.class.class_variables.sort
136: end
Prints a string of the original assertion‘s nesting within suites
# File lib/baretest/irb_mode.rb, line 159
159: def description!
160: puts @__assertion__.description
161: end
Prints the original assertion‘s error message and backtrace
# File lib/baretest/irb_mode.rb, line 102
102: def e!
103: em!
104: bt!(caller.size+3)
105: end
Prints the original assertion‘s error message
# File lib/baretest/irb_mode.rb, line 108
108: def em!
109: if @__status__.exception then
110: puts @__status__.exception.message
111: elsif @__status__.failure_reason
112: puts @__status__.failure_reason
113: else
114: puts "No exception or failure reason available"
115: end
116: end
# File lib/baretest/irb_mode.rb, line 178
178: def eval!(from, number=nil, explicit_binding=nil)
179: if code = @__assertion__.code then
180: if from.is_a?(Range) then
181: number = from.end-from.begin+1
182: from = from.begin
183: end
184: number ||= 1
185: total_lines = code.chomp.count("\n")-1
186: first_line = @__assertion__.line
187: if !from.between?(first_line, first_line+total_lines-1)
188: puts "From must be between #{first_line} and #{first_line+total_lines-1}"
189: elsif !number.between?(0, total_lines)
190: puts "Number must be between 1 and #{total_lines}"
191: else
192: from -= first_line-1
193: puts "Evaluating: ", *code.split(/\n/)[from, number]
194: eval(code.split(/\n/)[from, number].join("\n"), explicit_binding || context.workspace.binding) # this 'context' comes from irb
195: end
196: else
197: puts "Code could not be extracted"
198: end
199: end
Returns the original assertion‘s file
# File lib/baretest/irb_mode.rb, line 144
144: def file!
145: puts @__assertion__.file
146: end
Returns an array of all global variable names
# File lib/baretest/irb_mode.rb, line 139
139: def gv!(remove_standard=true)
140: puts *(global_variables-(remove_standard ? IRBMode::RemoveGlobals : [])).sort
141: end
Prints a list of available helper methods
# File lib/baretest/irb_mode.rb, line 56
56: def help
57: puts "Available methods:",
58: "s! - the assertions' original status",
59: "sc! - the assertions' original status code",
60: "e! - prints the error message and full backtrace",
61: "em! - prints the error message",
62: "bt! - prints the full backtrace",
63: "lv! - lists all available local variables",
64: "iv! - lists all available instance variables",
65: "cv! - lists all available class variables",
66: "gv! - lists all available global variables, per default dropping rubys",
67: " standard globals (use gv!(false) to avoid that)",
68: "file! - the file this assertion was defined in",
69: "line! - the line number in the file where this assertion's definition",
70: " starts",
71: "nesting! - a >-separated list of suite descriptions this assertion is nested",
72: "description! - this assertion's description",
73: "code! - the code of this assertion",
74: "eval! - eval (from_line, number_of_lines) or (from_line..to_line)",
75: #"restart! - Restart this irb session, resetting everything",
76: "irb_help - irb's original help",
77: "q - Quit - alias to irb's exit",
78: "help - this text you're reading right now"
79: end
Returns an array of all instance variable names
# File lib/baretest/irb_mode.rb, line 129
129: def iv!
130: puts *instance_variables.sort
131: end
Returns the original assertion‘s line
# File lib/baretest/irb_mode.rb, line 149
149: def line!
150: puts @__assertion__.line
151: end
Prints a string of the original assertion‘s nesting within suites
# File lib/baretest/irb_mode.rb, line 164
164: def nesting!
165: puts @__assertion__.suite.ancestors[0..-2].reverse.map { |s| s.description }.join(' > ')
166: end
Returns the original assertion‘s line
# File lib/baretest/irb_mode.rb, line 154
154: def open!
155: `bbedit '#{@__assertion__.file}:#{@__assertion__.line}'`
156: end