From f69598768e9a3002d9e7c2acbed109016fac6ae9 Mon Sep 17 00:00:00 2001 From: Tom Black Date: Fri, 19 May 2017 22:24:02 -0400 Subject: Check if audio file exists for `Sound` and `Music` --- lib/ruby2d/music.rb | 8 ++++++- lib/ruby2d/sound.rb | 8 ++++++- test/audio.rb | 63 ++++++++++++++++++++++------------------------------- test/music_spec.rb | 11 ++++++++++ test/sound_spec.rb | 11 ++++++++++ 5 files changed, 62 insertions(+), 39 deletions(-) create mode 100644 test/music_spec.rb create mode 100644 test/sound_spec.rb 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 -- cgit v1.2.3