Skip to content

Commit c49209d

Browse files
committed
use pp instead of inspect for values
1 parent 1eb18c1 commit c49209d

File tree

4 files changed

+52
-19
lines changed

4 files changed

+52
-19
lines changed

examples.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def initialize(*args)
9797

9898
example do
9999
hash = {:flavor => "vanilla"}
100-
exception_with_newlines = Exception.new(hash.to_yaml)
100+
exception_with_newlines = Exception.new(hash.to_yaml.chomp)
101101
assert("showing indentation of details") { rescuing { raise exception_with_newlines }.message.include?(":flavor: chocolate") }
102102
end
103103

lib/wrong/chunk.rb

+28-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'ruby_parser'
22
require 'ruby2ruby'
3+
require 'pp'
34

45
def require_optionally(library)
56
begin
@@ -175,32 +176,23 @@ def details
175176
end
176177

177178
def pretty_value(value, starting_col = 0, indent_wrapped_lines = 6, width = Chunk.terminal_width)
178-
inspected = value.inspect
179+
# inspected = value.inspect
180+
181+
# note that if the first line overflows due to the starting column then pp won't wrap it right
182+
inspected = PP.pp(value, "", width - starting_col).chomp
183+
184+
# this bit might be redundant with the pp call now
179185
indented = indent_all(6, inspected)
180186
if width
181-
indented.split("\n").map do |line|
182-
s = ""
183-
first_line = true
184-
width -= starting_col
185-
while line.length > width
186-
s << line[0...width]
187-
s << newline(indent_wrapped_lines)
188-
line = line[width..-1]
189-
if first_line
190-
width += starting_col - indent_wrapped_lines
191-
first_line = false
192-
end
193-
end
194-
s << line
195-
s
196-
end.join("\n")
187+
wrap_and_indent(indented, starting_col, indent_wrapped_lines, width)
197188
else
198189
indented
199190
end
200191
end
201192

202193
private
203194

195+
# todo: move to FailureMessage?
204196
def build_details
205197
require "wrong/rainbow" if Wrong.config[:color]
206198
s = ""
@@ -263,6 +255,25 @@ def indent_all(amount, s)
263255
s.gsub("\n", "\n#{indent(amount)}")
264256
end
265257

258+
def wrap_and_indent(indented, starting_col, indent_wrapped_lines, full_width)
259+
first_line = true
260+
width = full_width - starting_col # the first line is essentially shorter
261+
indented.split("\n").map do |line|
262+
s = ""
263+
while line.length > width
264+
s << line[0...width]
265+
s << newline(indent_wrapped_lines)
266+
line = line[width..-1]
267+
if first_line
268+
width += starting_col - indent_wrapped_lines
269+
first_line = false
270+
end
271+
end
272+
s << line
273+
s
274+
end.join("\n")
275+
end
276+
266277
# Returns [width, height] of terminal when detected, nil if not detected.
267278
# Think of this as a simpler version of Highline's Highline::SystemExtensions.terminal_size()
268279
# Lifted from https://github.com/cldwalker/hirb/blob/master/lib/hirb/util.rb#L59

test/chunk_test.rb

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
here = File.expand_path(File.dirname(__FILE__))
22
require "#{here}/test_helper"
33
require "wrong/chunk"
4+
require 'yaml'
45

56
unless Object.const_defined?(:Chunk)
67
Chunk = Wrong::Chunk
@@ -256,13 +257,21 @@ def details(&block)
256257
assert d == "\n" + ' x is "flavor:\tvanilla"' + "\n"
257258
end
258259

260+
it "splits lower-down details correctly (bug)" do
261+
hash = {:flavor => "vanilla"}
262+
exception_with_newlines = Exception.new(hash.to_yaml.chomp)
263+
d = details {
264+
rescuing { raise exception_with_newlines }.message.include?(":flavor: chocolate")
265+
}
266+
assert d.include? "exception_with_newlines is #<Exception: --- \n :flavor: vanilla>"
267+
end
268+
259269
it "skips assignments" do
260270
y = 14
261271
d = details { x = 7; y }
262272
assert d !~ /x = 7/
263273
assert d =~ /y is 14/
264274
end
265-
266275

267276
class Weirdo
268277
def initialize(inspected_value)
@@ -335,6 +344,16 @@ def inspect
335344
DONE
336345
end
337346

347+
it "wraps correctly" do
348+
hash = {:flavor => "vanilla"}
349+
object = Weirdo.new(hash.to_yaml.chomp)
350+
pretty = @chunk.pretty_value(object, 2, 3, 80)
351+
assert pretty == <<-DONE.chomp
352+
---
353+
:flavor: vanilla
354+
DONE
355+
end
356+
338357
end
339358

340359
end

todo.txt

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
Use RSpec 2's new framework extension API for rspec adapter
22

33
Switch matchers from predicated to sexp
4+
5+
We may also want to have Wrong's assert method detect when it is passed a parameter and no block, and act like the old assert in that case rather than erroring out. I think that'll fix some other integration hassles/bugs.
6+

0 commit comments

Comments
 (0)