Skip to content

Commit

Permalink
Add sound observer to prompt (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexB52 authored Nov 19, 2023
1 parent 003dfaa commit bcea704
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 5 deletions.
4 changes: 3 additions & 1 deletion exe/retest
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ if options.help?
return
end

repository = Retest::Repository.new(files: Retest::VersionControl.files)
prompt = Retest::Prompt.new
repository = Retest::Repository.new(files: Retest::VersionControl.files, prompt: prompt)
command = Retest::Command.for_options(options)
runner = Retest::Runners.runner_for(command.to_s)
sounds = Retest::Sounds.for(options)

sounds.play(:start)
runner.add_observer(sounds)
prompt.add_observer(sounds)

program = Retest::Program.new(
repository: repository,
Expand Down
5 changes: 5 additions & 0 deletions lib/retest/prompt.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Retest
class Prompt
include Observable

def self.ask_which_test_to_use(path, files)
new.ask_which_test_to_use(path, files)
end
Expand All @@ -15,6 +17,9 @@ def initialize(input: nil, output: nil)
end

def ask_which_test_to_use(path, files)
changed
notify_observers(:question)

output.puts(<<~QUESTION)
We found few tests matching: #{path}
Expand Down
13 changes: 9 additions & 4 deletions lib/retest/sounds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def play(sound)
['afplay', '/System/Library/Sounds/Funk.aiff']
when :start
['afplay', '/System/Library/Sounds/Blow.aiff']
when :question
['afplay', '/System/Library/Sounds/Glass.aiff']
else
raise ArgumentError.new("No sounds were found for type: #{sound}.")
end
Expand All @@ -40,15 +42,18 @@ def play(sound)
# List of Mac Audio Files:
# afplay /System/Library/Sounds/Basso.aiff
# afplay /System/Library/Sounds/Bottle.aiff
# afplay /System/Library/Sounds/Funk.aiff
# afplay /System/Library/Sounds/Hero.aiff
# afplay /System/Library/Sounds/Ping.aiff
# afplay /System/Library/Sounds/Purr.aiff
# afplay /System/Library/Sounds/Submarine.aiff
# afplay /System/Library/Sounds/Blow.aiff
# afplay /System/Library/Sounds/Frog.aiff
# afplay /System/Library/Sounds/Glass.aiff
# afplay /System/Library/Sounds/Morse.aiff
# afplay /System/Library/Sounds/Pop.aiff
# afplay /System/Library/Sounds/Sosumi.aiff
# afplay /System/Library/Sounds/Tink.aiff

# USED

# fail: afplay /System/Library/Sounds/Sosumi.aiff
# pass: afplay /System/Library/Sounds/Funk.aiff
# start: afplay /System/Library/Sounds/Blow.aiff
# question: afplay /System/Library/Sounds/Glass.aiff
32 changes: 32 additions & 0 deletions test/retest/prompt_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

module Retest
class PromptTest < MiniTest::Test
MethodCall = Struct.new(:name, :args, keyword_init: true)
class FakeObserver
attr_reader :notepad

def initialize
@notepad = []
end

def update(*args)
@notepad << MethodCall.new(name: __method__, args: args)
end
end

def setup
@subject = Prompt.new(output: StringIO.new)
Expand Down Expand Up @@ -45,5 +57,25 @@ def test_puts
out, _ = capture_subprocess_io { Prompt.puts "hello world\n" }
assert_equal "hello world\n", out
end

def test_observers_receive_correct_update_on_ask_which_test_to_use
observer = FakeObserver.new

@subject.add_observer(observer)

files = %w(
test/models/taxation/holdings_test.rb
test/models/schedule/holdings_test.rb
test/models/holdings_test.rb
test/models/performance/holdings_test.rb
test/lib/csv_report/holdings_test.rb
)

@subject.input = StringIO.new("1\n")

@subject.ask_which_test_to_use("app/models/valuation/holdings.rb", files)

assert_includes observer.notepad, MethodCall.new(name: :update, args: [:question])
end
end
end
18 changes: 18 additions & 0 deletions test/retest/sounds/mac_os_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ def test_play_tests_fail

kernel.verify
end

def test_play_question
kernel = MiniTest::Mock.new
kernel.expect(:system, true, ['afplay', '/System/Library/Sounds/Glass.aiff'])

MacOS.new(kernel: kernel, thread: FakeThread).play(:question)

kernel.verify
end

def test_play_start
kernel = MiniTest::Mock.new
kernel.expect(:system, true, ['afplay', '/System/Library/Sounds/Blow.aiff'])

MacOS.new(kernel: kernel, thread: FakeThread).play(:start)

kernel.verify
end
end
end
end

0 comments on commit bcea704

Please sign in to comment.