Skip to content

Commit 4a707b3

Browse files
committed
Add rspec coverage for cowsay. Achievement unlocked
1 parent 4604f8c commit 4a707b3

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

lib/rex/text.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,12 +1148,12 @@ def self.cowsay(text, width=39)
11481148
# text in the cowsay banner, so just do it by hand. This big mess wraps
11491149
# the provided text in an ASCII-cloud and then makes it look like the cloud
11501150
# is a thought/word coming from the ASCII-cow. Each line in the
1151-
# ASCII-cloud is no more than the specified number-characters long, and the cloud corners are
1152-
# made to look rounded
1153-
text_lines = text.scan(Regexp.new(".{1,#{width}}"))
1151+
# ASCII-cloud is no more than the specified number-characters long, and the
1152+
# cloud corners are made to look rounded
1153+
text_lines = text.scan(Regexp.new(".{1,#{width-4}}"))
11541154
max_length = text_lines.map(&:size).sort.last
11551155
cloud_parts = []
1156-
cloud_parts << " #{'_' * (max_length + 2)} "
1156+
cloud_parts << " #{'_' * (max_length + 2)}"
11571157
if text_lines.size == 1
11581158
cloud_parts << "< #{text} >"
11591159
else
@@ -1165,7 +1165,7 @@ def self.cowsay(text, width=39)
11651165
end
11661166
cloud_parts << "\\ #{text_lines.last.ljust(max_length, ' ')} /"
11671167
end
1168-
cloud_parts << " #{'-' * (max_length + 2)} "
1168+
cloud_parts << " #{'-' * (max_length + 2)}"
11691169
cloud_parts << <<EOS
11701170
\\ ,__,
11711171
\\ (oo)____

spec/lib/rex/text_spec.rb

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,61 @@
167167
end
168168
end
169169

170+
context ".cowsay" do
171+
172+
def moo(num)
173+
(%w(moo) * num).join(' ')
174+
end
175+
176+
it "should cowsay single lines correctly" do
177+
cowsaid = <<EOCOW
178+
_____________________
179+
< moo moo moo moo moo >
180+
---------------------
181+
\\ ,__,
182+
\\ (oo)____
183+
(__) )\\
184+
||--|| *
185+
EOCOW
186+
described_class.cowsay(moo(5)).should eq(cowsaid)
187+
end
188+
189+
it "should cowsay two lines correctly" do
190+
cowsaid = <<EOCOW
191+
_____________________________________
192+
/ moo moo moo moo moo moo moo moo moo \\
193+
\\ moo moo moo moo moo moo /
194+
-------------------------------------
195+
\\ ,__,
196+
\\ (oo)____
197+
(__) )\\
198+
||--|| *
199+
EOCOW
200+
described_class.cowsay(moo(15)).should eq(cowsaid)
201+
end
202+
203+
it "should cowsay three+ lines correctly" do
204+
cowsaid = <<EOCOW
205+
_____________________________________
206+
/ moo moo moo moo moo moo moo moo moo \\
207+
| moo moo moo moo moo moo moo moo mo |
208+
| o moo moo moo moo moo moo moo moo m |
209+
\\ oo moo moo moo /
210+
-------------------------------------
211+
\\ ,__,
212+
\\ (oo)____
213+
(__) )\\
214+
||--|| *
215+
EOCOW
216+
described_class.cowsay(moo(30)).should eq(cowsaid)
217+
end
218+
219+
it "should respect the wrap" do
220+
wrap = 40 + rand(100)
221+
cowsaid = described_class.cowsay(moo(1000), wrap)
222+
max_len = cowsaid.split(/\n/).map(&:length).sort.last
223+
max_len.should eq(wrap)
224+
end
225+
end
170226
end
171227
end
172-

0 commit comments

Comments
 (0)