summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAndrew Havens <[email protected]>2017-12-29 09:26:26 -0800
committerTom Black <[email protected]>2018-01-02 20:35:39 -0800
commit50b36b6a8aaf9512a4699734c160fcd82c0c2ea6 (patch)
treead99e261dc37a67128c42434989c94bd4d3742a3
parent038cd2f1a5b99dbc523b8b2b42551e0261e83aa7 (diff)
downloadruby2d-50b36b6a8aaf9512a4699734c160fcd82c0c2ea6.tar.gz
ruby2d-50b36b6a8aaf9512a4699734c160fcd82c0c2ea6.zip
Check for valid file path to prevent segfault when file does not exist.
-rw-r--r--lib/ruby2d/sprite.rb8
-rw-r--r--test/sprite_spec.rb6
2 files changed, 10 insertions, 4 deletions
diff --git a/lib/ruby2d/sprite.rb b/lib/ruby2d/sprite.rb
index e6a6efa..7c66f97 100644
--- a/lib/ruby2d/sprite.rb
+++ b/lib/ruby2d/sprite.rb
@@ -8,9 +8,11 @@ module Ruby2D
def initialize(x, y, path, z=0)
- # unless File.exists? path
- # raise Error, "Cannot find image file `#{path}`"
- # end
+ unless RUBY_ENGINE == 'opal'
+ unless File.exists? path
+ raise Error, "Cannot find sprite image file `#{path}`"
+ end
+ end
@x, @y, @path = x, y, path
@clip_x, @clip_y, @clip_w, @clip_h = 0, 0, 0, 0
diff --git a/test/sprite_spec.rb b/test/sprite_spec.rb
index 441ca6e..e44ea62 100644
--- a/test/sprite_spec.rb
+++ b/test/sprite_spec.rb
@@ -3,8 +3,12 @@ require 'ruby2d'
RSpec.describe Ruby2D::Sprite do
describe '#new' do
+ it "raises exception if file doesn't exist" do
+ expect { Sprite.new(0, 0, "bad_sprite_sheet.png") }.to raise_error(Ruby2D::Error)
+ end
+
it 'creates a new sprite' do
- Ruby2D::Sprite.new(0, 0, "tests/media/sprite_sheet.png")
+ Sprite.new(0, 0, "test/media/sprite_sheet.png")
end
end