diff options
| author | Tom Black <[email protected]> | 2017-05-19 22:24:02 -0400 |
|---|---|---|
| committer | Tom Black <[email protected]> | 2017-05-20 18:15:53 -0400 |
| commit | f69598768e9a3002d9e7c2acbed109016fac6ae9 (patch) | |
| tree | c716af50d3d98676704518ce9ab84f9afb7368f4 | |
| parent | 8ce4acf745b27c3f1209b02cc6aeec2c40b5aa1b (diff) | |
| download | ruby2d-f69598768e9a3002d9e7c2acbed109016fac6ae9.tar.gz ruby2d-f69598768e9a3002d9e7c2acbed109016fac6ae9.zip | |
Check if audio file exists for `Sound` and `Music`
| -rw-r--r-- | lib/ruby2d/music.rb | 8 | ||||
| -rw-r--r-- | lib/ruby2d/sound.rb | 8 | ||||
| -rw-r--r-- | test/audio.rb | 63 | ||||
| -rw-r--r-- | test/music_spec.rb | 11 | ||||
| -rw-r--r-- | test/sound_spec.rb | 11 |
5 files changed, 62 insertions, 39 deletions
diff --git a/lib/ruby2d/music.rb b/lib/ruby2d/music.rb index 410c82f..e0fdf11 100644 --- a/lib/ruby2d/music.rb +++ b/lib/ruby2d/music.rb @@ -7,7 +7,13 @@ module Ruby2D attr_reader :path def initialize(path) - # TODO: Check if file exists + + unless RUBY_ENGINE == 'opal' + unless File.exists? path + raise Error, "Cannot find audio file `#{path}`" + end + end + init(path) @path = path @loop = false diff --git a/lib/ruby2d/sound.rb b/lib/ruby2d/sound.rb index 1049eee..4e0c385 100644 --- a/lib/ruby2d/sound.rb +++ b/lib/ruby2d/sound.rb @@ -7,7 +7,13 @@ module Ruby2D attr_reader :path def initialize(path) - # TODO: Check if file exists + + unless RUBY_ENGINE == 'opal' + unless File.exists? path + raise Error, "Cannot find audio file `#{path}`" + end + end + init(path) @path = path end diff --git a/test/audio.rb b/test/audio.rb index e57160c..7d39569 100644 --- a/test/audio.rb +++ b/test/audio.rb @@ -8,46 +8,35 @@ end set width: 300, height: 200, title: "Ruby 2D — Audio" -on key: 'escape' do - close -end - snd = Sound.new("#{media}/sound.wav") mus = Music.new("#{media}/music.wav") -on key_down: 'p' do - puts "Playing sound..." - snd.play -end - -on key_down: 'm' do - puts "Playing music..." - mus.play -end - -on key_down: 'l' do - puts "Loop music true..." - mus.loop = true -end - -on key_down: 'a' do - puts "Pause music..." - mus.pause -end - -on key_down: 'r' do - puts "Resume music..." - mus.resume -end - -on key_down: 's' do - puts "Stop music..." - mus.stop -end - -on key_down: 'f' do - puts "fade out music..." - mus.fadeout 2000 +on :key_down do |event| + case event.key + when 'p' + puts "Playing sound..." + snd.play + when 'm' + puts "Playing music..." + mus.play + when 'l' + puts "Loop music true..." + mus.loop = true + when 'a' + puts "Pause music..." + mus.pause + when 'r' + puts "Resume music..." + mus.resume + when 's' + puts "Stop music..." + mus.stop + when 'f' + puts "fade out music..." + mus.fadeout 2000 + when 'escape' + close + end end show diff --git a/test/music_spec.rb b/test/music_spec.rb new file mode 100644 index 0000000..4b4e432 --- /dev/null +++ b/test/music_spec.rb @@ -0,0 +1,11 @@ +require 'ruby2d' + +RSpec.describe Ruby2D::Music do + + describe '#new' do + it "raises exception if audio file doesn't exist" do + expect { Music.new('bad_music.mp3') }.to raise_error(Ruby2D::Error) + end + end + +end diff --git a/test/sound_spec.rb b/test/sound_spec.rb new file mode 100644 index 0000000..72141a7 --- /dev/null +++ b/test/sound_spec.rb @@ -0,0 +1,11 @@ +require 'ruby2d' + +RSpec.describe Ruby2D::Sound do + + describe '#new' do + it "raises exception if audio file doesn't exist" do + expect { Sound.new('bad_sound.wav') }.to raise_error(Ruby2D::Error) + end + end + +end |
