Class: Wrong::Chunk
- Inherits:
-
Object
- Object
- Wrong::Chunk
- Defined in:
- lib/wrong/chunk.rb
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#line_number ⇒ Object
readonly
Returns the value of attribute line_number.
Class Method Summary collapse
Instance Method Summary collapse
-
#claim ⇒ Object
The claim is the part of the assertion inside the curly braces.
- #code ⇒ Object
- #details ⇒ Object
-
#initialize(file, line_number, block = nil) ⇒ Chunk
constructor
line parameter is 1-based.
- #line_index ⇒ Object
- #location ⇒ Object
-
#parse ⇒ Object
Algorithm: try to parse the starting line if it parses OK, then we’re done! if not, then glom the next line and try again repeat until it parses or we’re out of lines.
- #parts(sexp = nil) ⇒ Object
Constructor Details
#initialize(file, line_number, block = nil) ⇒ Chunk
line parameter is 1-based
22 23 24 25 26 |
# File 'lib/wrong/chunk.rb', line 22 def initialize(file, line_number, block = nil) @file = file @line_number = line_number.to_i @block = block end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
19 20 21 |
# File 'lib/wrong/chunk.rb', line 19 def block @block end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
19 20 21 |
# File 'lib/wrong/chunk.rb', line 19 def file @file end |
#line_number ⇒ Object (readonly)
Returns the value of attribute line_number.
19 20 21 |
# File 'lib/wrong/chunk.rb', line 19 def line_number @line_number end |
Class Method Details
.from_block(block, depth = 0) ⇒ Object
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/wrong/chunk.rb', line 8 def self.from_block(block, depth = 0) file, line = if block.to_proc.respond_to? :source_location # in Ruby 1.9, it reads the source location from the block block.to_proc.source_location else # in Ruby 1.8, it reads the source location from the call stack caller[depth].split(":") end new(file, line, block) end |
Instance Method Details
#claim ⇒ Object
The claim is the part of the assertion inside the curly braces. E.g. for “assert { x == 5 }” the claim is “x == 5”
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/wrong/chunk.rb', line 66 def claim parse() if @sexp.nil? raise "Could not parse #{location}" else assertion = @sexp.assertion statement = assertion && assertion[3] if statement.nil? @sexp # raise "Could not find assertion in #{location}\n\t#{@chunk.strip}\n\t#{@sexp}" else statement end end end |
#code ⇒ Object
83 84 85 |
# File 'lib/wrong/chunk.rb', line 83 def code self.claim.to_ruby end |
#details ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/wrong/chunk.rb', line 111 def details require "wrong/rainbow" if Wrong.config[:color] s = "" parts = self.parts parts.shift # remove the first part, since it's the same as the code details = [] if parts.size > 0 parts.each do |part| begin value = eval(part, block.binding) unless part == value.inspect # this skips literals or tautologies if part =~ /\n/m part.gsub!(/\n/, newline(2)) part += newline(3) end value = indent_all(3, value.inspect) if Wrong.config[:color] part = part.color(:blue) value = value.color(:magenta) end details << indent(2, part, " is ", value) end rescue Exception => e raises = "raises #{e.class}" if Wrong.config[:color] part = part.color(:blue) raises = raises.bold.color(:red) end details << indent(2, part, " ", raises, ": ", indent_all(3, e.)) end end end details.uniq! if details.empty? "" else "\n" + details.join("\n") + "\n" end end |
#line_index ⇒ Object
28 29 30 |
# File 'lib/wrong/chunk.rb', line 28 def line_index @line_number - 1 end |
#location ⇒ Object
32 33 34 |
# File 'lib/wrong/chunk.rb', line 32 def location "#{@file}:#{@line_number}" end |
#parse ⇒ Object
Algorithm: try to parse the starting line if it parses OK, then we’re done! if not, then glom the next line and try again repeat until it parses or we’re out of lines
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/wrong/chunk.rb', line 41 def parse source = if @file == "(irb)" IRB.CurrentContext.all_lines else File.read(@file) end lines = source.split("\n") @parser ||= RubyParser.new @chunk = nil c = 0 @sexp = nil while @sexp.nil? && line_index + c < lines.size begin @chunk = lines[line_index..line_index+c].join("\n") @sexp = @parser.parse(@chunk) rescue Racc::ParseError => e # loop and try again c += 1 end end @sexp end |
#parts(sexp = nil) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/wrong/chunk.rb', line 87 def parts(sexp = nil) if sexp.nil? parts(self.claim).compact.uniq else # todo: extract some of this into Sexp parts_list = [] begin unless sexp.first == :arglist code = sexp.to_ruby.strip parts_list << code unless code == "" || parts_list.include?(code) end rescue => e puts "#{e.class}: #{e.}" puts e.backtrace.join("\n") end sexp.each do |sub| if sub.is_a?(Sexp) parts_list += parts(sub) end end parts_list end end |