Skip to content

Commit bcea704

Browse files
authored
Add sound observer to prompt (#179)
1 parent 003dfaa commit bcea704

File tree

5 files changed

+67
-5
lines changed

5 files changed

+67
-5
lines changed

exe/retest

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ if options.help?
1313
return
1414
end
1515

16-
repository = Retest::Repository.new(files: Retest::VersionControl.files)
16+
prompt = Retest::Prompt.new
17+
repository = Retest::Repository.new(files: Retest::VersionControl.files, prompt: prompt)
1718
command = Retest::Command.for_options(options)
1819
runner = Retest::Runners.runner_for(command.to_s)
1920
sounds = Retest::Sounds.for(options)
2021

2122
sounds.play(:start)
2223
runner.add_observer(sounds)
24+
prompt.add_observer(sounds)
2325

2426
program = Retest::Program.new(
2527
repository: repository,

lib/retest/prompt.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module Retest
22
class Prompt
3+
include Observable
4+
35
def self.ask_which_test_to_use(path, files)
46
new.ask_which_test_to_use(path, files)
57
end
@@ -15,6 +17,9 @@ def initialize(input: nil, output: nil)
1517
end
1618

1719
def ask_which_test_to_use(path, files)
20+
changed
21+
notify_observers(:question)
22+
1823
output.puts(<<~QUESTION)
1924
We found few tests matching: #{path}
2025

lib/retest/sounds.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ def play(sound)
2626
['afplay', '/System/Library/Sounds/Funk.aiff']
2727
when :start
2828
['afplay', '/System/Library/Sounds/Blow.aiff']
29+
when :question
30+
['afplay', '/System/Library/Sounds/Glass.aiff']
2931
else
3032
raise ArgumentError.new("No sounds were found for type: #{sound}.")
3133
end
@@ -40,15 +42,18 @@ def play(sound)
4042
# List of Mac Audio Files:
4143
# afplay /System/Library/Sounds/Basso.aiff
4244
# afplay /System/Library/Sounds/Bottle.aiff
43-
# afplay /System/Library/Sounds/Funk.aiff
4445
# afplay /System/Library/Sounds/Hero.aiff
4546
# afplay /System/Library/Sounds/Ping.aiff
4647
# afplay /System/Library/Sounds/Purr.aiff
4748
# afplay /System/Library/Sounds/Submarine.aiff
48-
# afplay /System/Library/Sounds/Blow.aiff
4949
# afplay /System/Library/Sounds/Frog.aiff
50-
# afplay /System/Library/Sounds/Glass.aiff
5150
# afplay /System/Library/Sounds/Morse.aiff
5251
# afplay /System/Library/Sounds/Pop.aiff
53-
# afplay /System/Library/Sounds/Sosumi.aiff
5452
# afplay /System/Library/Sounds/Tink.aiff
53+
54+
# USED
55+
56+
# fail: afplay /System/Library/Sounds/Sosumi.aiff
57+
# pass: afplay /System/Library/Sounds/Funk.aiff
58+
# start: afplay /System/Library/Sounds/Blow.aiff
59+
# question: afplay /System/Library/Sounds/Glass.aiff

test/retest/prompt_test.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
module Retest
44
class PromptTest < MiniTest::Test
5+
MethodCall = Struct.new(:name, :args, keyword_init: true)
6+
class FakeObserver
7+
attr_reader :notepad
8+
9+
def initialize
10+
@notepad = []
11+
end
12+
13+
def update(*args)
14+
@notepad << MethodCall.new(name: __method__, args: args)
15+
end
16+
end
517

618
def setup
719
@subject = Prompt.new(output: StringIO.new)
@@ -45,5 +57,25 @@ def test_puts
4557
out, _ = capture_subprocess_io { Prompt.puts "hello world\n" }
4658
assert_equal "hello world\n", out
4759
end
60+
61+
def test_observers_receive_correct_update_on_ask_which_test_to_use
62+
observer = FakeObserver.new
63+
64+
@subject.add_observer(observer)
65+
66+
files = %w(
67+
test/models/taxation/holdings_test.rb
68+
test/models/schedule/holdings_test.rb
69+
test/models/holdings_test.rb
70+
test/models/performance/holdings_test.rb
71+
test/lib/csv_report/holdings_test.rb
72+
)
73+
74+
@subject.input = StringIO.new("1\n")
75+
76+
@subject.ask_which_test_to_use("app/models/valuation/holdings.rb", files)
77+
78+
assert_includes observer.notepad, MethodCall.new(name: :update, args: [:question])
79+
end
4880
end
4981
end

test/retest/sounds/mac_os_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,24 @@ def test_play_tests_fail
3333

3434
kernel.verify
3535
end
36+
37+
def test_play_question
38+
kernel = MiniTest::Mock.new
39+
kernel.expect(:system, true, ['afplay', '/System/Library/Sounds/Glass.aiff'])
40+
41+
MacOS.new(kernel: kernel, thread: FakeThread).play(:question)
42+
43+
kernel.verify
44+
end
45+
46+
def test_play_start
47+
kernel = MiniTest::Mock.new
48+
kernel.expect(:system, true, ['afplay', '/System/Library/Sounds/Blow.aiff'])
49+
50+
MacOS.new(kernel: kernel, thread: FakeThread).play(:start)
51+
52+
kernel.verify
53+
end
3654
end
3755
end
3856
end

0 commit comments

Comments
 (0)