From 7f5ba20f10bf8613397fd750868ca235e1fd23ce Mon Sep 17 00:00:00 2001 From: kawasakikou Date: Mon, 27 Jul 2015 15:41:26 +0900 Subject: [PATCH 1/4] Added Sound#stop and Wave#stop --- lib/dxruby_sdl/sound.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/dxruby_sdl/sound.rb b/lib/dxruby_sdl/sound.rb index c019e05..1eaa80f 100644 --- a/lib/dxruby_sdl/sound.rb +++ b/lib/dxruby_sdl/sound.rb @@ -21,7 +21,7 @@ def initialize(filename) end end - def_delegators :@sound, :play, :set_volume + def_delegators :@sound, :play, :set_volume, :stop alias_method :setVolume, :set_volume @@ -39,6 +39,10 @@ def play def set_volume(volume, time = 0) raise NotImplementedError, 'Sound#set_volume(volume, time) with MIDI' end + + def stop + SDL::Mixer.halt_music + end end private_constant :Music @@ -65,6 +69,10 @@ def set_volume(volume, time = 0) end @wave.set_volume(volume) end + + def stop + SDL::Mixer.halt(@last_played_channel) + end end private_constant :Wave end From c248e17df65089fe971aa40eceb32c879f4c0fee Mon Sep 17 00:00:00 2001 From: kawasakikou Date: Mon, 27 Jul 2015 16:52:27 +0900 Subject: [PATCH 2/4] Added test of Music#stop and Wave#stop --- spec/lib/dxruby_sdl/sound_spec.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/spec/lib/dxruby_sdl/sound_spec.rb b/spec/lib/dxruby_sdl/sound_spec.rb index e3ebea5..2fece53 100644 --- a/spec/lib/dxruby_sdl/sound_spec.rb +++ b/spec/lib/dxruby_sdl/sound_spec.rb @@ -76,4 +76,32 @@ end end end + + describe '#stop' do + context 'WAVE形式のファイルの場合' do + let(:path) { fixture_path('sound.wav') } + let(:sound) { DXRubySDL::Sound.new(path) } + + subject { sound.stop } + + it 'SDL::Mixer.halt_musicが呼び出される' do + sound.play + expect(SDL::Mixer).to receive(:halt) + subject + end + end + + context 'MIDI形式のファイルの場合' do + let(:path) { fixture_path('bgm.mid') } + let(:sound) { DXRubySDL::Sound.new(path) } + + subject { sound.stop } + + it 'SDL::Mixer.halt_musicが呼び出される' do + sound.play + expect(SDL::Mixer).to receive(:halt_music) + subject + end + end + end end From 0b0e186c907391a492bfb6180e400093393ad562 Mon Sep 17 00:00:00 2001 From: Kouji Takao Date: Tue, 28 Jul 2015 10:17:24 +0900 Subject: [PATCH 3/4] refactored spec. --- spec/lib/dxruby_sdl/sound_spec.rb | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/spec/lib/dxruby_sdl/sound_spec.rb b/spec/lib/dxruby_sdl/sound_spec.rb index 2fece53..406435f 100644 --- a/spec/lib/dxruby_sdl/sound_spec.rb +++ b/spec/lib/dxruby_sdl/sound_spec.rb @@ -78,30 +78,38 @@ end describe '#stop' do - context 'WAVE形式のファイルの場合' do + context 'WAVE file' do let(:path) { fixture_path('sound.wav') } let(:sound) { DXRubySDL::Sound.new(path) } subject { sound.stop } - it 'SDL::Mixer.halt_musicが呼び出される' do + before do + allow(SDL::Mixer).to receive(:halt) sound.play - expect(SDL::Mixer).to receive(:halt) subject end + + describe SDL::Mixer do + it { expect(SDL::Mixer).to have_received(:halt).with(0).once } + end end - context 'MIDI形式のファイルの場合' do + context 'MIDI file' do let(:path) { fixture_path('bgm.mid') } let(:sound) { DXRubySDL::Sound.new(path) } subject { sound.stop } - it 'SDL::Mixer.halt_musicが呼び出される' do + before do + allow(SDL::Mixer).to receive(:halt_music) sound.play - expect(SDL::Mixer).to receive(:halt_music) subject end + + describe SDL::Mixer do + it { expect(SDL::Mixer).to have_received(:halt_music).with(no_args).once } + end end end end From 19e26b5d448c268f0718e6aa9ba93f57ba98686a Mon Sep 17 00:00:00 2001 From: Kouji Takao Date: Tue, 28 Jul 2015 10:20:48 +0900 Subject: [PATCH 4/4] refactored spec. --- spec/lib/dxruby_sdl/sound_spec.rb | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/spec/lib/dxruby_sdl/sound_spec.rb b/spec/lib/dxruby_sdl/sound_spec.rb index 406435f..bd879c7 100644 --- a/spec/lib/dxruby_sdl/sound_spec.rb +++ b/spec/lib/dxruby_sdl/sound_spec.rb @@ -2,6 +2,16 @@ require 'spec_helper' describe DXRubySDL::Sound, '音を表すクラス' do + shared_context 'WAVE file', wave: true do + let(:path) { fixture_path('sound.wav') } + let(:sound) { DXRubySDL::Sound.new(path) } + end + + shared_context 'MIDI file', midi: true do + let(:path) { fixture_path('bgm.mid') } + let(:sound) { DXRubySDL::Sound.new(path) } + end + describe '.new' do shared_context '.new' do subject { DXRubySDL::Sound.new(fixture_path(filename)) } @@ -25,10 +35,7 @@ end describe '#play' do - context 'WAVE形式のファイルの場合' do - let(:path) { fixture_path('sound.wav') } - let(:sound) { DXRubySDL::Sound.new(path) } - + context 'WAVE file', wave: true do subject { sound.play } it 'SDL::Mixer.play_channelを呼び出す' do @@ -62,10 +69,7 @@ end end - context 'MIDI形式のファイルの場合' do - let(:path) { fixture_path('bgm.mid') } - let(:sound) { DXRubySDL::Sound.new(path) } - + context 'MIDI file', midi: true do subject { sound.play } it 'SDL::Mixer.play_musicを呼び出す' do @@ -78,7 +82,7 @@ end describe '#stop' do - context 'WAVE file' do + context 'WAVE file', wave: true do let(:path) { fixture_path('sound.wav') } let(:sound) { DXRubySDL::Sound.new(path) } @@ -95,7 +99,7 @@ end end - context 'MIDI file' do + context 'MIDI file', midi: true do let(:path) { fixture_path('bgm.mid') } let(:sound) { DXRubySDL::Sound.new(path) }