diff options
| author | lstrzebinczyk <[email protected]> | 2017-05-21 21:26:12 +0200 |
|---|---|---|
| committer | Tom Black <[email protected]> | 2017-05-31 23:36:04 -0400 |
| commit | 65187cd5126227778146ec33c2857a391fbab620 (patch) | |
| tree | 328c21a2bdf9365d5c018326c229760986cf412f /test | |
| parent | 09ed2811ad3ccb9af50028447fabbcee5b5c5ae2 (diff) | |
| download | ruby2d-65187cd5126227778146ec33c2857a391fbab620.tar.gz ruby2d-65187cd5126227778146ec33c2857a391fbab620.zip | |
Implement #contains? for all renderables
Diffstat (limited to 'test')
| -rw-r--r-- | test/contains.rb | 32 | ||||
| -rw-r--r-- | test/image_spec.rb | 16 | ||||
| -rw-r--r-- | test/line_spec.rb | 23 | ||||
| -rw-r--r-- | test/quad_spec.rb | 26 | ||||
| -rw-r--r-- | test/rectangle_spec.rb | 18 | ||||
| -rw-r--r-- | test/text_spec.rb | 15 | ||||
| -rw-r--r-- | test/triangle_spec.rb | 24 |
7 files changed, 153 insertions, 1 deletions
diff --git a/test/contains.rb b/test/contains.rb new file mode 100644 index 0000000..5a11064 --- /dev/null +++ b/test/contains.rb @@ -0,0 +1,32 @@ +require 'ruby2d' + +set title: "Ruby 2D — Contains", height: 350 + +if RUBY_ENGINE == 'opal' + media = "../test/media" + font = "sans-serif" +else + media = "media" + font = "#{media}/bitstream_vera/vera.ttf" +end + +objects = [] +objects.push Square.new(50, 50, 100) +objects.push Rectangle.new(200, 50, 100, 75) +objects.push Quad.new(350, 50, 500, 75, 450, 150, 375, 125) +objects.push Triangle.new(550, 50, 600, 125, 500, 150) +objects.push Line.new(225, 175, 375, 225, 20) +objects.push Image.new(50, 200, "#{media}/colors.png") +objects.push Text.new(450, 200, "Hello", 50, font) + +on :key_down do |event| + close if event.key == 'escape' +end + +update do + objects.each do |o| + o.contains?(get(:mouse_x), get(:mouse_y)) ? o.opacity = 1.0 : o.opacity = 0.5 + end +end + +show diff --git a/test/image_spec.rb b/test/image_spec.rb index 3eb5d86..33dee35 100644 --- a/test/image_spec.rb +++ b/test/image_spec.rb @@ -1,11 +1,25 @@ require 'ruby2d' RSpec.describe Ruby2D::Image do - describe '#new' do it "raises exception if image file doesn't exist" do expect { Image.new(0, 0, 'bad_image.png') }.to raise_error(Ruby2D::Error) end end + # Image has 100 width and 100 height + describe '#contains?' do + it "returns true if point is inside image" do + image = Image.new(0, 0, "test/media/image.bmp") + expect(image.contains?(50, 50)).to be true + end + + it "returns true if point is not inside image" do + image = Image.new(0, 0, "test/media/image.bmp") + expect(image.contains?(-50, 50)).to be false + expect(image.contains?(50, -50)).to be false + expect(image.contains?(50, 150)).to be false + expect(image.contains?(150, 50)).to be false + end + end end diff --git a/test/line_spec.rb b/test/line_spec.rb new file mode 100644 index 0000000..04445d1 --- /dev/null +++ b/test/line_spec.rb @@ -0,0 +1,23 @@ +require 'ruby2d' + +RSpec.describe Ruby2D::Triangle do + describe '#contains?' do + it "returns true if point is inside line" do + line = Line.new( + 0, 0, + 100, 100 + ) + expect(line.contains?(25, 25)).to be true + end + + it "returns true if point is inside text" do + line = Line.new( + 0, 0, + 100, 100 + ) + + expect(line.contains?(0, 10)).to be false + expect(line.contains?(10, 0)).to be false + end + end +end diff --git a/test/quad_spec.rb b/test/quad_spec.rb index 58f2dfd..a6cfca4 100644 --- a/test/quad_spec.rb +++ b/test/quad_spec.rb @@ -89,4 +89,30 @@ RSpec.describe Ruby2D::Quad do end.to raise_error("Quads require 4 colors, one for each vertex. 5 were given.") end end + + describe '#contains?' do + it "returns true if point is inside quad" do + quad = Quad.new( + -25, 0, + 0, -25, + 25, 0, + 0, 25 + ) + expect(quad.contains?(0, 0)).to be true + end + + it "returns true if point is not inside quad" do + quad = Quad.new( + -25, 0, + 0, -25, + 25, 0, + 0, 25 + ) + + expect(quad.contains?( 20, 20)).to be false + expect(quad.contains?(-20, 20)).to be false + expect(quad.contains?( 20, -20)).to be false + expect(quad.contains?(-20, -20)).to be false + end + end end diff --git a/test/rectangle_spec.rb b/test/rectangle_spec.rb new file mode 100644 index 0000000..493f3d1 --- /dev/null +++ b/test/rectangle_spec.rb @@ -0,0 +1,18 @@ +require 'ruby2d' + +RSpec.describe Ruby2D::Rectangle do + describe '#contains?' do + it "returns true if point is inside rectangle" do + rectangle = Rectangle.new(0, 0, 50, 50) + expect(rectangle.contains?(25, 25)).to be true + end + + it "returns true if point is not inside rectangle" do + rectangle = Rectangle.new(0, 0, 50, 50) + expect(rectangle.contains?(-25, 25)).to be false + expect(rectangle.contains?(25, -25)).to be false + expect(rectangle.contains?(25, 50)).to be false + expect(rectangle.contains?(50, 25)).to be false + end + end +end diff --git a/test/text_spec.rb b/test/text_spec.rb index ad12cbf..15038d2 100644 --- a/test/text_spec.rb +++ b/test/text_spec.rb @@ -41,4 +41,19 @@ RSpec.describe Ruby2D::Text do expect(t.height).to eq(48) end end + + describe '#contains?' do + it "returns true if point is inside text" do + text = Text.new(0, 0, "Hello world!", 40, "test/media/bitstream_vera/vera.ttf") + expect(text.contains?(text.width / 2, text.height / 2)).to be true + end + + it "returns true if point is not inside text" do + text = Text.new(0, 0, "Hello world!", 40, "test/media/bitstream_vera/vera.ttf") + expect(text.contains?( - text.width / 2, text.height / 2)).to be false + expect(text.contains?( text.width / 2, - text.height / 2)).to be false + expect(text.contains?(3 * text.width / 2, text.height / 2)).to be false + expect(text.contains?( text.width / 2, 3 * text.height / 2)).to be false + end + end end diff --git a/test/triangle_spec.rb b/test/triangle_spec.rb index f71b097..b562e59 100644 --- a/test/triangle_spec.rb +++ b/test/triangle_spec.rb @@ -79,4 +79,28 @@ RSpec.describe Ruby2D::Triangle do end.to raise_error("Triangles require 3 colors, one for each vertex. 4 were given.") end end + + + describe '#contains?' do + it "returns true if point is inside triangle" do + triangle = Triangle.new( + 0, 0, + 0, 100, + 100, 0 + ) + expect(triangle.contains?(25, 25)).to be true + end + + it "returns true if point is inside text" do + triangle = Triangle.new( + 0, 0, + 0, 100, + 100, 0 + ) + + expect(triangle.contains?(25, -25)).to be false + expect(triangle.contains?(-25, 25)).to be false + expect(triangle.contains?(100, 100)).to be false + end + end end |
