1 require File.dirname(__FILE__) + "/spec_helper.rb"
2 require 'debugger/output'
5 describe "Output::Columns#initialize" do
6 it "raises an ArgumentError if an invalid format specification is used" do
7 cols = Debugger::Output::Columns.new(["%-5s"])
8 lambda { cols = Debugger::Output::Columns.new(["%5-s"]) }.should raise_error(ArgumentError)
13 describe "Output::Columns#wrap" do
15 @cols = Debugger::Output::Columns.new 1
18 it "returns a single element array containing the input string if arg length is less than the wrap length" do
19 @cols.wrap(nil, 40).should == [nil]
20 @cols.wrap("", 40).should == [""]
21 @cols.wrap("here is a short string", 40).should == ["here is a short string"]
24 it "returns nil if wrap == 0" do
25 @cols.wrap(nil, 0).should == [nil]
26 @cols.wrap("here is a string", 0).should == [nil]
29 it "raises an ArgumentError if wrap < 0" do
30 lambda{ @cols.wrap(nil, -1) }.should raise_error(ArgumentError)
31 lambda{ @cols.wrap("here is a string", -1) }.should raise_error(ArgumentError)
34 it "returns an array of lines when an input string is provided" do
35 @cols.wrap("", 1).should == [""]
36 @cols.wrap("a", 1).should == ["a"]
37 @cols.wrap("ab", 1).should == ["a","b"]
38 @cols.wrap("here is a short string", 20).should == ["here is a short", "string"]
41 it "breaks long lines on whitespace, comma, or dash first, and then on non-word characters" do
42 @cols.wrap("here is a test-45 string", 14).should == ["here is a", "test-45 string"]
43 @cols.wrap("here is a test-45 string", 15).should == ["here is a test-", "45 string"]
44 @cols.wrap("1,2", 2).should == ["1,", "2"]
45 @cols.wrap("here is a CompiledMethod#decode test", 20).should == ["here is a", "CompiledMethod", "#decode test"]
48 it "forces a break at width if no other break position exists" do
49 @cols.wrap("abcdefghijklmnop", 10).should == ["abcdefghij","klmnop"]
50 @cols.wrap("abcdefghijklmnop", 5).should == ["abcde", "fghij","klmno", "p"]
51 @cols.wrap("$LOADED_FEATURES", 15).should == ["$LOADED_", "FEATURES"]
54 it "trims leading and trailing whitespace from split lines" do
55 @cols.wrap(" 45 ", 2).should == ["","45"]
56 @cols.wrap(" 45 54 63 ", 2).should == ["","45", "54", "63"]
59 it "pads lines with spaces if an alignment is specified" do
60 @cols.wrap("abc", 5, :left).should == ["abc "]
61 @cols.wrap("abc", 5, :center).should == [" abc "]
62 @cols.wrap("abc", 5, :right).should == [" abc"]
67 describe "Output::Columns#format_str" do
68 it "aligns columns according to a left or right justification specification" do
69 cols = Debugger::Output::Columns.new(["%-5s"])
70 cols.format_str(["a"]).should == ["a "]
71 cols = Debugger::Output::Columns.new(["%5s"])
72 cols.format_str(["a"]).should == [" a"]
73 cols = Debugger::Output::Columns.new(["%|5s"])
74 cols.format_str(["a"]).should == [" a "]
75 cols = Debugger::Output::Columns.new(["%5s", "%5s"], "|")
76 cols.format_str(["a","b"]).should == [" a| b"]
79 it "wraps a cell value onto multiple lines if the content of a cell is wider than the cell width" do
80 cols = Debugger::Output::Columns.new(["%10s"])
81 cols.format_str(["abcdefghij"]).should == ["abcdefghij"]
82 cols.format_str(["abcdefghijklmnop"]).should == ["abcdefghij"," klmnop"]
84 cols = Debugger::Output::Columns.new(["%-5s","%10s"], "|")
85 cols.format_str(["a", "abcdefghijklmnop"]).should == ["a |abcdefghij", " | klmnop"]
86 cols = Debugger::Output::Columns.new(["%-5s","%10s"], "|")
87 cols.update_widths(["a", "abcdefghijklmnop"])
88 cols.format_str(["a", "abcdefghijklmnop"]).should == ["a |abcdefghijklmnop"]
91 it "handles columns that calculate a 0 width" do
92 cols = Debugger::Output::Columns.new(["%04d:", "%-s ", "%-s"])
93 cols.update_widths([41, :push_self, ""])
94 cols.format_str([41, :push_self, ""]).should == ["0041: push_self "]
99 describe "Output::Columns#redistribute_widths" do
100 it "makes no changes and returns true if page width is nil" do
101 cols = Debugger::Output::Columns.new(["%-5s","%10s"])
102 cols.update_widths(["a", "abcdefghijklmnop"])
103 cols.widths.should == [5, 16]
104 cols.redistribute_widths(nil).should == true
105 cols.widths.should == [5, 16]
108 it "makes no changes and returns true if page width > content width" do
109 cols = Debugger::Output::Columns.new(["%-5s","%10s"])
110 cols.update_widths(["a", "abcdefghijklmnop"])
111 cols.widths.should == [5, 16]
112 cols.redistribute_widths(80).should == true
113 cols.widths.should == [5, 16]
116 it "first reduces fixed width columns to specified widths if page width < content width" do
117 cols = Debugger::Output::Columns.new(["%-5s","%10s"])
118 cols.update_widths(["abcdefg", "abcdefghijklmnop"])
119 cols.widths.should == [7, 16]
120 cols.redistribute_widths(22).should == true
121 cols.widths.should == [5, 10]
122 cols = Debugger::Output::Columns.new(["%-5s","%10s"])
123 cols.update_widths(["abcdefg", "abcdefghijklmnop"])
124 cols.widths.should == [7, 16]
125 cols.redistribute_widths(22).should == true
126 cols.widths.should == [5, 10]
129 it "then reduces variable width columns to specified widths if page width < content width" do
130 cols = Debugger::Output::Columns.new(["%-5s","%*s"])
131 cols.update_widths(["abcdefg", "abcdefghijklmnop"])
132 cols.widths.should == [7, 16]
133 cols.redistribute_widths(23).should == true
134 cols.widths.should == [5, 16]
135 cols.redistribute_widths(20).should == true
136 cols.widths.should == [5, 14]
139 it "only reduces variable column widths until content width == page width" do
140 cols = Debugger::Output::Columns.new(["%-5s","%*s", "%*s"])
141 cols.update_widths(["abcdefg", "abcdefghij", "klmnop"])
142 cols.widths.should == [7, 10, 6]
143 cols.redistribute_widths(23).should == true
144 cols.widths.should == [5, 10, 6]
145 cols.redistribute_widths(22).should == true
146 cols.widths.should == [5, 9, 6]
149 it "reduces multiple variable width columns in proportion to their content widths" do
150 cols = Debugger::Output::Columns.new(["%-5s","%*s", "%*s"])
151 cols.update_widths(["abcdefg", "abcdefghijklmnop", "abcdefgh"])
152 cols.widths.should == [7, 16, 8]
153 cols.redistribute_widths(30).should == true
154 cols.widths.should == [5, 15, 8]