Skip to content

Commit

Permalink
Refactor helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexB52 committed Aug 11, 2024
1 parent 939ed86 commit 50bad24
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 29 deletions.
32 changes: 4 additions & 28 deletions test/retest/prompt_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,6 @@ def test_ask_which_test_to_use
EXPECTED
end

class ListeningIO
attr_accessor :io
def initialize
@io = StringIO.new
end

def gets
loop do
sleep 0.0001
line = @io.gets.to_s
break line unless line.empty?
end
end
end

def test_question_asked_when_asking_question
files = %w(
test/models/taxation/holdings_test.rb
Expand All @@ -73,26 +58,17 @@ def test_question_asked_when_asking_question
test/lib/csv_report/holdings_test.rb
)

@subject.input = stdin = ListeningIO.new
@subject.input = stdin = BlockingInput.new

th = Thread.new do
@subject.ask_which_test_to_use("app/models/valuation/holdings.rb", files)
end

attempts = 0
begin
assert @subject.question_asked?
rescue Minitest::Assertion => e
raise e if attempts >= 10
sleep 0.0001
attempts += 1
end

stdin.io = StringIO.new("1\n")
th.join
wait_until { assert @subject.question_asked? }

refute @subject.question_asked?
stdin.puts("1\n")
assert_equal "test/models/schedule/holdings_test.rb", th.value
refute @subject.question_asked?
end

def test_read_output
Expand Down
40 changes: 39 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,48 @@
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)

require "retest"
require "byebug"
require "minitest/autorun"

WAITING_TIME = 0.0001

def wait(time = WAITING_TIME)
sleep time
end

FakeFS = Struct.new(:files) do
def exist?(value)
files.include? value
end
end
end

class BlockingInput
attr_accessor :io
def initialize
@io = StringIO.new
end

def puts(value)
@io = StringIO.new(value)
end

def gets
loop do
wait
line = @io.gets.to_s
break line unless line.empty?
end
end
end

def wait_until(max_attempts: 10)
attempts = 0
begin
yield
rescue Minitest::Assertion => e
raise e if attempts >= max_attempts
wait
attempts += 1
end
end

0 comments on commit 50bad24

Please sign in to comment.