Skip to content

Commit 94ef134

Browse files
committed
Make Console more testable with some overrides
1 parent 6798fe2 commit 94ef134

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

lib/remedy/console.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ def output
2323
@output ||= $stdout
2424
end
2525

26+
def input= new_input
27+
@input = new_input
28+
end
29+
30+
def output= new_output
31+
@output = new_output
32+
end
33+
2634
def raw
2735
raw!
2836
result = yield
@@ -52,14 +60,20 @@ def rows
5260
alias_method :height, :rows
5361

5462
def size
63+
return @size_override if @size_override
64+
5565
str = [0, 0, 0, 0].pack('SSSS')
56-
if input.ioctl(TIOCGWINSZ, str) >= 0 then
66+
if input.respond_to?(:ioctl) && input.ioctl(TIOCGWINSZ, str) >= 0 then
5767
Size.new str.unpack('SSSS').first 2
5868
else
5969
raise UnknownConsoleSize, "Unable to get console size"
6070
end
6171
end
6272

73+
def size_override= new_size
74+
@size_override = new_size
75+
end
76+
6377
def interactive?
6478
input.isatty
6579
end

spec/viewport_spec.rb

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
1-
require_relative 'spec_helper'
2-
require 'remedy/viewport'
1+
require_relative "spec_helper"
2+
require "remedy/viewport"
33

44
describe Remedy::Viewport do
5+
let(:console){ ::Remedy::Console }
6+
let(:size_override){ Size 20, 40 }
7+
let(:stringio){ StringIO.new }
8+
9+
before(:each) do
10+
console.input = stringio
11+
console.output = stringio
12+
console.size_override = size_override
13+
end
14+
15+
after(:each) do
16+
console.input = $stdin
17+
console.output = $stdout
18+
console.size_override = nil
19+
end
20+
521
it 'should be able to execute the example code from the readme' do
6-
@stdout = $stdout
22+
expected = "\\e[H\\e[JQ: What's the difference between a duck?\\e[1B\\e[0GA: Purple, because ice cream has no bone\\e[1B\\e[0G"
23+
724
joke = ::Remedy::Partial.new
825
joke << "Q: What's the difference between a duck?"
926
joke << "A: Purple, because ice cream has no bones!"
1027

1128
screen = ::Remedy::Viewport.new
12-
sio = StringIO.new
13-
$stdout = sio
14-
screen.draw joke unless ENV['CI']
15-
expect(sio.string).to include("ice cream")
16-
ensure
17-
$stdout = @stdout
29+
screen.draw joke
30+
31+
actual = stringio.string.inspect[1..-2]
32+
expect(actual).to eq expected
1833
end
1934
end

0 commit comments

Comments
 (0)