Skip to content

Commit 3a1f4a9

Browse files
committed
The Interactor needed to be part of the SassSpec module
1 parent 40f1884 commit 3a1f4a9

File tree

2 files changed

+51
-49
lines changed

2 files changed

+51
-49
lines changed

lib/sass_spec/interactor.rb

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,62 @@
1-
class Interactor
1+
module SassSpec
2+
class Interactor
23

3-
class Choice < Struct.new(:label, :message, :block)
4-
def run_block!
5-
block.call if block
4+
class Choice < Struct.new(:label, :message, :block)
5+
def run_block!
6+
block.call if block
7+
end
68
end
7-
end
89

9-
def initialize
10-
@choices = []
11-
@choice = nil
12-
end
13-
14-
def prompt(message)
15-
@prompt = message
16-
end
17-
18-
def choice(label, message, &block)
19-
@choices << Choice.new(label, message, block)
20-
end
10+
def initialize
11+
@choices = []
12+
@choice = nil
13+
end
2114

22-
def restart!
23-
@choice = nil
24-
end
15+
def prompt(message)
16+
@prompt = message
17+
end
2518

26-
def display_choices!
27-
puts
28-
puts @prompt if @prompt
29-
@choices.each_with_index do |c, i|
30-
puts "#{i + 1}. #{c.message}"
19+
def choice(label, message, &block)
20+
@choices << Choice.new(label, message, block)
3121
end
32-
print "Please select an option > "
33-
end
3422

35-
def run!
36-
if @choices.size == 0
37-
raise ArgumentError, "No choices to run."
23+
def restart!
24+
@choice = nil
3825
end
39-
@choice = nil
40-
until @choice
41-
display_choices!
42-
input = $stdin.gets
26+
27+
def display_choices!
4328
puts
44-
@choice = input ? input.strip.to_i : 0
45-
if @choice > 0 && @choice <= @choices.size
46-
@choices[@choice - 1].run_block! # We run this in the loop so restart! can be invoked.
47-
else
48-
@choice = nil
29+
puts @prompt if @prompt
30+
@choices.each_with_index do |c, i|
31+
puts "#{i + 1}. #{c.message}"
4932
end
33+
print "Please select an option > "
5034
end
51-
@choices[@choice - 1].label # we return the label so re-ordering choices doesn't change result
52-
end
5335

54-
def self.interact
55-
interactor = new
56-
yield interactor
57-
interactor.run!
36+
def run!
37+
if @choices.size == 0
38+
raise ArgumentError, "No choices to run."
39+
end
40+
@choice = nil
41+
until @choice
42+
display_choices!
43+
input = $stdin.gets
44+
puts
45+
@choice = input ? input.strip.to_i : 0
46+
if @choice > 0 && @choice <= @choices.size
47+
@choices[@choice - 1].run_block! # We run this in the loop so restart! can be invoked.
48+
else
49+
@choice = nil
50+
end
51+
end
52+
@choices[@choice - 1].label # we return the label so re-ordering choices doesn't change result
53+
end
54+
55+
def self.interact
56+
interactor = new
57+
yield interactor
58+
interactor.run!
59+
end
5860
end
5961
end
6062

lib/sass_spec/test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def run_spec_test(test_case, options = {})
3636

3737
def interact(test_case, default, &block)
3838
if test_case.interactive?
39-
return Interactor.interact(&block)
39+
return SassSpec::Interactor.interact(&block)
4040
else
4141
return default
4242
end
@@ -235,7 +235,7 @@ def handle_unexpected_pass!(test_case, status, error, options)
235235
if test_case.should_fail?
236236
output, _, error, status = test_case.output
237237
if status == 0 && test_case.interactive?
238-
choice = Interactor.interact do |i|
238+
choice = SassSpec::Interactor.interact do |i|
239239
i.prompt "In #{test_case.name}\n" +
240240
"A failure was expected but it compiled instead."
241241
i.choice(:show_source, "Show me the input.") do
@@ -279,7 +279,7 @@ def handle_unexpected_error!(test_case, status, error, options)
279279
unless test_case.should_fail?
280280
_output, _clean_output, error, status = test_case.output
281281
if status != 0 && test_case.interactive?
282-
choice = Interactor.interact do |i|
282+
choice = SassSpec::Interactor.interact do |i|
283283
i.prompt "In #{test_case.name}\n" +
284284
"An unexpected compiler error was encountered."
285285
i.choice(:show_source, "Show me the input.") do
@@ -320,7 +320,7 @@ def handle_unexpected_error!(test_case, status, error, options)
320320

321321
def delete_test!(test_case)
322322
files = Dir.glob(File.join(test_case.folder, "**", "*"))
323-
result = Interactor.interact do |i|
323+
result = SassSpec::Interactor.interact do |i|
324324
i.prompt("The following files will be removed:\n * " + files.join("\n * "))
325325
i.choice(:proceed, "Delete them.") do
326326
FileUtils.rm_rf(test_case.folder)

0 commit comments

Comments
 (0)