Skip to content

Commit 5573afb

Browse files
committed
Fix bug where TextWrapper didn't cope gracefully with negative columnWidth. Also fix up old broken tests
1 parent 6224f26 commit 5573afb

File tree

3 files changed

+149
-125
lines changed

3 files changed

+149
-125
lines changed

src/CommandLine/Text/TextWrapper.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public TextWrapper(string input)
4141
/// <returns>this</returns>
4242
public TextWrapper WordWrap(int columnWidth)
4343
{
44-
44+
//ensure we always use at least 1 column even if the client has told us there's no space available
45+
columnWidth = Math.Max(1, columnWidth);
4546
lines= lines
4647
.SelectMany(line => WordWrapLine(line, columnWidth))
4748
.ToArray();
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using CommandLine.Tests.Fakes;
1+
using System;
2+
using System.Linq;
23
using CommandLine.Text;
34
using FluentAssertions;
45
using Xunit;
@@ -11,110 +12,120 @@ private string NormalizeLineBreaks(string str)
1112
{
1213
return str.Replace("\r", "");
1314
}
14-
private void EnsureEquivalent(string a,string b)
15+
16+
private void EnsureEquivalent(string a, string b)
1517
{
1618
//workaround build system line-end inconsistencies
1719
NormalizeLineBreaks(a).Should().Be(NormalizeLineBreaks(b));
1820
}
1921

20-
22+
2123
[Fact]
22-
public void IndentWorksCorrectly()
24+
public void ExtraSpacesAreTreatedAsNonBreaking()
2325
{
26+
var input =
27+
"here is some text with some extra spacing";
28+
var expected = @"here is some text
29+
with some extra
30+
spacing";
31+
var wrapper = new TextWrapper(input);
32+
EnsureEquivalent(wrapper.WordWrap(20).ToText(), expected);
33+
}
2434

35+
36+
[Fact]
37+
public void IndentWorksCorrectly()
38+
{
2539
var input =
2640
@"line1
2741
line2";
2842
var expected = @" line1
2943
line2";
3044
var wrapper = new TextWrapper(input);
31-
EnsureEquivalent(wrapper.Indent(2).ToText(),expected);
32-
45+
EnsureEquivalent(wrapper.Indent(2).ToText(), expected);
3346
}
3447

3548
[Fact]
36-
public void SimpleWrappingIsAsExpected()
49+
public void LongWordsAreBroken()
3750
{
38-
3951
var input =
40-
@"here is some text that needs wrapping";
41-
var expected = @"here is
42-
some text
43-
that needs
44-
wrapping";
52+
"here is some text that contains a veryLongWordThatWontFitOnASingleLine";
53+
var expected = @"here is some text
54+
that contains a
55+
veryLongWordThatWont
56+
FitOnASingleLine";
4557
var wrapper = new TextWrapper(input);
46-
EnsureEquivalent(wrapper.WordWrap(10).ToText(),expected);
47-
58+
EnsureEquivalent(wrapper.WordWrap(20).ToText(), expected);
4859
}
4960

5061
[Fact]
51-
public void WrappingAvoidsBreakingWords()
62+
public void NegativeColumnWidthStillProducesOutput()
5263
{
53-
54-
var input =
55-
@"here hippopotamus is some text that needs wrapping";
56-
var expected = @"here
57-
hippopotamus is
58-
some text that
59-
needs wrapping";
64+
var input = @"test";
65+
var expected = string.Join(Environment.NewLine, input.Select(c => c.ToString()));
6066
var wrapper = new TextWrapper(input);
61-
EnsureEquivalent(wrapper.WordWrap(15).ToText(),expected);
62-
67+
EnsureEquivalent(wrapper.WordWrap(-1).ToText(), expected);
6368
}
6469

6570
[Fact]
66-
public void WrappingObeysLineBreaksOfAllStyles()
71+
public void SimpleWrappingIsAsExpected()
6772
{
68-
6973
var input =
70-
"here is some text\nthat needs\r\nwrapping";
71-
var expected = @"here is some text
74+
@"here is some text that needs wrapping";
75+
var expected = @"here is
76+
some text
7277
that needs
7378
wrapping";
7479
var wrapper = new TextWrapper(input);
75-
EnsureEquivalent(wrapper.WordWrap(20).ToText(),expected);
76-
80+
EnsureEquivalent(wrapper.WordWrap(10).ToText(), expected);
7781
}
7882

79-
8083
[Fact]
81-
public void WrappingPreservesSubIndentation()
84+
public void SingleColumnStillProducesOutputForSubIndentation()
8285
{
83-
84-
var input =
85-
"here is some text\n that needs wrapping where we want the wrapped part to preserve indentation\nand this part to not be indented";
86-
var expected = @"here is some text
87-
that needs
88-
wrapping where we
89-
want the wrapped
90-
part to preserve
91-
indentation
92-
and this part to not
93-
be indented";
86+
var input = @"test
87+
ind";
88+
89+
var expected = @"t
90+
e
91+
s
92+
t
93+
i
94+
n
95+
d";
9496
var wrapper = new TextWrapper(input);
95-
EnsureEquivalent(wrapper.WordWrap(20).ToText(),expected);
96-
97+
EnsureEquivalent(wrapper.WordWrap(-1).ToText(), expected);
9798
}
9899

99100
[Fact]
100-
public void LongWordsAreBroken()
101+
public void SpacesWithinStringAreRespected()
101102
{
102-
103103
var input =
104-
"here is some text that contains a veryLongWordThatWontFitOnASingleLine";
105-
var expected = @"here is some text
106-
that contains a
107-
veryLongWordThatWont
108-
FitOnASingleLine";
104+
"here is some text with some extra spacing";
105+
var expected = @"here is some
106+
text with some extra
107+
spacing";
109108
var wrapper = new TextWrapper(input);
110-
EnsureEquivalent(wrapper.WordWrap(20).ToText(),expected);
109+
EnsureEquivalent(wrapper.WordWrap(20).ToText(), expected);
110+
}
111111

112+
[Fact]
113+
public void SubIndentationCorrectlyWrapsWhenColumnWidthRequiresIt()
114+
{
115+
var input = @"test
116+
indented";
117+
var expected = @"test
118+
in
119+
de
120+
nt
121+
ed";
122+
var wrapper = new TextWrapper(input);
123+
EnsureEquivalent(wrapper.WordWrap(6).ToText(), expected);
112124
}
113125

114126
[Fact]
115127
public void SubIndentationIsPreservedWhenBreakingWords()
116128
{
117-
118129
var input =
119130
"here is some text that contains \n a veryLongWordThatWontFitOnASingleLine";
120131
var expected = @"here is some text
@@ -123,59 +134,64 @@ that contains
123134
veryLongWordThatWo
124135
ntFitOnASingleLine";
125136
var wrapper = new TextWrapper(input);
126-
EnsureEquivalent(wrapper.WordWrap(20).ToText(),expected);
127-
137+
EnsureEquivalent(wrapper.WordWrap(20).ToText(), expected);
128138
}
129139

130140
[Fact]
131-
public void SpacesWithinStringAreRespected()
141+
public void WrappingAvoidsBreakingWords()
132142
{
133-
134143
var input =
135-
"here is some text with some extra spacing";
136-
var expected = @"here is some
137-
text with some extra
138-
spacing";
144+
@"here hippopotamus is some text that needs wrapping";
145+
var expected = @"here
146+
hippopotamus is
147+
some text that
148+
needs wrapping";
139149
var wrapper = new TextWrapper(input);
140-
EnsureEquivalent(wrapper.WordWrap(20).ToText(),expected);
141-
150+
EnsureEquivalent(wrapper.WordWrap(15).ToText(), expected);
142151
}
143152

144-
145-
[Fact]
146-
public void ExtraSpacesAreTreatedAsNonBreaking()
147-
{
148153

149-
var input =
150-
"here is some text with some extra spacing";
151-
var expected = @"here is some text
152-
with some extra
153-
spacing";
154-
var wrapper = new TextWrapper(input);
155-
EnsureEquivalent(wrapper.WordWrap(20).ToText(),expected);
156-
157-
}
158-
159-
160154
[Fact]
161155
public void WrappingExtraSpacesObeySubIndent()
162156
{
163-
164157
var input =
165158
"here is some\n text with some extra spacing";
166159
var expected = @"here is some
167160
text
168161
with some extra
169162
spacing";
170163
var wrapper = new TextWrapper(input);
171-
EnsureEquivalent(wrapper.WordWrap(20).ToText(),expected);
172-
164+
EnsureEquivalent(wrapper.WordWrap(20).ToText(), expected);
173165
}
174166

167+
[Fact]
168+
public void WrappingObeysLineBreaksOfAllStyles()
169+
{
170+
var input =
171+
"here is some text\nthat needs\r\nwrapping";
172+
var expected = @"here is some text
173+
that needs
174+
wrapping";
175+
var wrapper = new TextWrapper(input);
176+
EnsureEquivalent(wrapper.WordWrap(20).ToText(), expected);
177+
}
175178

176179

180+
[Fact]
181+
public void WrappingPreservesSubIndentation()
182+
{
183+
var input =
184+
"here is some text\n that needs wrapping where we want the wrapped part to preserve indentation\nand this part to not be indented";
185+
var expected = @"here is some text
186+
that needs
187+
wrapping where we
188+
want the wrapped
189+
part to preserve
190+
indentation
191+
and this part to not
192+
be indented";
193+
var wrapper = new TextWrapper(input);
194+
EnsureEquivalent(wrapper.WordWrap(20).ToText(), expected);
195+
}
177196
}
178-
179-
180-
181197
}

0 commit comments

Comments
 (0)