summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--LICENSE.md2
-rw-r--r--README.md2
-rw-r--r--ext/ruby2d/ruby2d-opal.rb2
-rw-r--r--ext/ruby2d/ruby2d.c3
-rw-r--r--lib/ruby2d.rb2
-rw-r--r--lib/ruby2d/circle.rb15
-rw-r--r--lib/ruby2d/color.rb22
-rw-r--r--lib/ruby2d/dsl.rb2
-rw-r--r--lib/ruby2d/exceptions.rb2
-rw-r--r--lib/ruby2d/image.rb8
-rw-r--r--lib/ruby2d/line.rb13
-rw-r--r--lib/ruby2d/music.rb8
-rw-r--r--lib/ruby2d/quad.rb7
-rw-r--r--lib/ruby2d/rectangle.rb14
-rw-r--r--lib/ruby2d/renderable.rb6
-rw-r--r--lib/ruby2d/sound.rb3
-rw-r--r--lib/ruby2d/sprite.rb2
-rw-r--r--lib/ruby2d/square.rb8
-rw-r--r--lib/ruby2d/text.rb6
-rw-r--r--lib/ruby2d/triangle.rb6
-rw-r--r--lib/ruby2d/version.rb2
-rw-r--r--lib/ruby2d/window.rb12
-rw-r--r--test/circle_spec.rb2
-rw-r--r--test/color_spec.rb16
-rw-r--r--test/dsl_spec.rb10
-rw-r--r--test/events_spec.rb6
-rw-r--r--test/image_spec.rb13
-rw-r--r--test/line_spec.rb11
-rw-r--r--test/music_spec.rb4
-rw-r--r--test/quad_spec.rb23
-rw-r--r--test/rectangle_spec.rb8
-rw-r--r--test/sound_spec.rb4
-rw-r--r--test/spec_helper.rb4
-rw-r--r--test/sprite_spec.rb6
-rw-r--r--test/text_spec.rb19
-rw-r--r--test/triangle_spec.rb25
-rw-r--r--test/window_spec.rb2
37 files changed, 161 insertions, 139 deletions
diff --git a/LICENSE.md b/LICENSE.md
index 0a35006..4e4941e 100644
--- a/LICENSE.md
+++ b/LICENSE.md
@@ -1,6 +1,6 @@
The MIT License (MIT)
-Copyright (c) 2017 Tom Black ([blacktm.com](http://www.blacktm.com))
+Copyright © 2018 Tom Black ([blacktm.com](http://www.blacktm.com))
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
diff --git a/README.md b/README.md
index 406e960..909622b 100644
--- a/README.md
+++ b/README.md
@@ -67,7 +67,7 @@ Whether adding a feature or fixing a bug, try to do the following to ensure your
- **Check if there is an existing issue, and if not, open a new one to start a discussion.** Before dedicating time and energy to an idea or fix, let's make sure it's consistent with the principles and goals of the project, and that we have a solid strategy in place to implement and test.
-- **Use a subset of Ruby that works everywhere.** Ruby 2D applications are, of course, written in Ruby. Some users may choose to harness the full power of the language, standard library, and ecosystem of gems by writing interpreted apps targeting the standard implementation, [MRI](https://en.wikipedia.org/wiki/Ruby_MRI). Others may want to target the web via [Opal](http://opalrb.org), so their app can be run in any browser. And others still may want to compile their app to native code via [MRuby](http://mruby.org), so they can embed it on platforms like the [Raspberry Pi](https://www.raspberrypi.org) and [CHIP](https://getchip.com/pages/chip), or run it on mobile platforms like iOS and Android, or on the big screen with Apple TV or Amazon Fire TV. Or even further, some may want to do all three! Ruby 2D aims to support all of these use cases, even with the same app codebase. Your code contribution to this gem has to support a subset of Ruby that is compatible and behaves similarly across MRI, MRuby, and Opal. Beyond reading the documentation for each Ruby implementation, you can also try out code snippets on the command line using their respective REPLs: `irb` (MRI), `mirb` (MRuby), and `opal-repl` (Opal).
+- **Use a subset of Ruby that works everywhere.** Ruby 2D applications are, of course, written in Ruby. Some users may choose to harness the full power of the language, standard library, and ecosystem of gems by writing interpreted apps targeting the standard implementation, [MRI](https://en.wikipedia.org/wiki/Ruby_MRI). Others may want to target the web via [Opal](http://opalrb.org), so their app can be run in any browser. And others still may want to compile their app to native code via [MRuby](http://mruby.org), so they can embed it on platforms like the [Raspberry Pi](https://www.raspberrypi.org), or run it on mobile platforms like iOS and Android, or on the big screen with Apple TV or Amazon Fire TV. Or even further, some may want to do all three! Ruby 2D aims to support all of these use cases, even with the same app codebase. Your code contribution to this gem has to support a subset of Ruby that is compatible and behaves similarly across MRI, MRuby, and Opal. Beyond reading the documentation for each Ruby implementation, you can also try out code snippets on the command line using their respective REPLs: `irb` (MRI), `mirb` (MRuby), and `opal-repl` (Opal).
- **Comprehensively test your change.** Unlike other Ruby libraries, not everything here can be easily covered with unit tests alone. We also need to make sure things look and sound right, inputs work as expected, and behavior is consistent across all [platforms Ruby 2D supports](http://www.ruby2d.com/platforms).
diff --git a/ext/ruby2d/ruby2d-opal.rb b/ext/ruby2d/ruby2d-opal.rb
index 1699b36..e888328 100644
--- a/ext/ruby2d/ruby2d-opal.rb
+++ b/ext/ruby2d/ruby2d-opal.rb
@@ -1,4 +1,4 @@
-# ruby2d-opal.rb
+# Web extension for Opal
# Ruby 2D window
$R2D_WINDOW = nil
diff --git a/ext/ruby2d/ruby2d.c b/ext/ruby2d/ruby2d.c
index d70381d..12e868e 100644
--- a/ext/ruby2d/ruby2d.c
+++ b/ext/ruby2d/ruby2d.c
@@ -1,4 +1,4 @@
-// ruby2d.c – Native C extension for Ruby and MRuby
+// Native C extension for Ruby and MRuby
// Simple 2D includes
#if RUBY2D_IOS_TVOS
@@ -936,6 +936,7 @@ static R_VAL ruby2d_window_ext_show(R_VAL self) {
#endif
ruby2d_window = self;
+ // Set Simple 2D diagnostics
if (r_test(r_iv_get(self, "@diagnostics"))) {
S2D_Diagnostics(true);
}
diff --git a/lib/ruby2d.rb b/lib/ruby2d.rb
index 6dc72dc..1d48ab8 100644
--- a/lib/ruby2d.rb
+++ b/lib/ruby2d.rb
@@ -1,4 +1,4 @@
-# ruby2d.rb
+# Ruby2D module and native extension loader, adds DSL
require 'ruby2d/renderable'
require 'ruby2d/exceptions'
diff --git a/lib/ruby2d/circle.rb b/lib/ruby2d/circle.rb
index 4f87828..90b86df 100644
--- a/lib/ruby2d/circle.rb
+++ b/lib/ruby2d/circle.rb
@@ -1,4 +1,4 @@
-# circle.rb
+# Ruby2D::Circle
module Ruby2D
class Circle
@@ -8,12 +8,12 @@ module Ruby2D
attr_accessor :x, :y, :radius, :sectors
def initialize(opts = {})
- @x = opts[:x] || 25
- @y = opts[:y] || 25
- @radius = opts[:radius] || 25
- @sectors = opts[:sectors] || 20
- @z = opts[:z] || 0
- self.color = opts[:color] || 'white'
+ @x = opts[:x] || 25
+ @y = opts[:y] || 25
+ @z = opts[:z] || 0
+ @radius = opts[:radius] || 25
+ @sectors = opts[:sectors] || 20
+ self.color = opts[:color] || 'white'
add
end
@@ -24,5 +24,6 @@ module Ruby2D
def contains?(x, y)
Math.sqrt((x - @x)**2 + (y - @y)**2) <= @radius
end
+
end
end
diff --git a/lib/ruby2d/color.rb b/lib/ruby2d/color.rb
index a41e6c7..26d176f 100644
--- a/lib/ruby2d/color.rb
+++ b/lib/ruby2d/color.rb
@@ -1,11 +1,12 @@
-# color.rb
+# Ruby2D::Color
module Ruby2D
class Color
+
# Color::Set represents an array of colors
class Set
def initialize(colors)
- @colors = colors.map{|c| Color.new(c)}
+ @colors = colors.map { |c| Color.new(c) }
end
def [](i)
@@ -83,31 +84,28 @@ module Ruby2D
# Array of Floats from 0.0..1.0
c.class == Array && c.length == 4 &&
- c.all? { |el|
- el.is_a?(Numeric)
- }
+ c.all? { |el| el.is_a?(Numeric) }
end
+ # Create a color from whatever is provided
def self.from(input)
- # If a valid array of colors, return a Color::Set with those colors
- # Else return single color
+ # If a valid array of colors, return a `Color::Set` with those colors
if input.is_a? Array and input.all? { |el| Color.is_valid? el }
Color::Set.new(input)
+ # Otherwise, return single color
else
Color.new(input)
end
end
+ # Convenience methods to alias `opacity` to `@a`
def opacity; @a end
-
- def opacity=(opacity)
- @a = opacity
- end
+ def opacity=(opacity); @a = opacity end
private
- # TODO: Only `Number` supported in JS
# Convert from Fixnum (0..255) to Float (0.0..1.0)
+ # TODO: Only `Number` is supported in JS
def to_f(a)
b = []
a.each do |n|
diff --git a/lib/ruby2d/dsl.rb b/lib/ruby2d/dsl.rb
index 993b5ca..a6818fa 100644
--- a/lib/ruby2d/dsl.rb
+++ b/lib/ruby2d/dsl.rb
@@ -1,4 +1,4 @@
-# dsl.rb
+# Ruby2D::DSL
module Ruby2D::DSL
diff --git a/lib/ruby2d/exceptions.rb b/lib/ruby2d/exceptions.rb
index 687ab11..595c328 100644
--- a/lib/ruby2d/exceptions.rb
+++ b/lib/ruby2d/exceptions.rb
@@ -1,4 +1,4 @@
-# exceptions.rb
+# Ruby2D::Error
module Ruby2D
class Error < StandardError
diff --git a/lib/ruby2d/image.rb b/lib/ruby2d/image.rb
index 5490d53..9386f6b 100644
--- a/lib/ruby2d/image.rb
+++ b/lib/ruby2d/image.rb
@@ -1,4 +1,4 @@
-# image.rb
+# Ruby2D::Image
module Ruby2D
class Image
@@ -19,10 +19,9 @@ module Ruby2D
@x = opts[:x] || 0
@y = opts[:y] || 0
@z = opts[:z] || 0
- @width = opts[:width] || nil
+ @width = opts[:width] || nil
@height = opts[:height] || nil
- @rotate = 0
-
+ @rotate = opts[:rotate] || 0
self.color = opts[:color] || 'white'
ext_init(@path)
@@ -36,5 +35,6 @@ module Ruby2D
def contains?(x, y)
@x < x and @x + @width > x and @y < y and @y + @height > y
end
+
end
end
diff --git a/lib/ruby2d/line.rb b/lib/ruby2d/line.rb
index 23fb03d..e817acb 100644
--- a/lib/ruby2d/line.rb
+++ b/lib/ruby2d/line.rb
@@ -1,4 +1,4 @@
-# line.rb
+# Ruby2D::Line
module Ruby2D
class Line
@@ -14,7 +14,6 @@ module Ruby2D
@width = opts[:width] || 2
@z = opts[:z] || 0
self.color = opts[:color] || 'white'
-
add
end
@@ -23,13 +22,15 @@ module Ruby2D
update_color(@color)
end
+ # Return the length of the line
def length
points_distance(@x1, @y1, @x2, @y2)
end
- # Line contains a point if the point is closer than the length of line from both ends
- # and if the distance from point to line is smaller than half of the width.
- # Check https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line for reference
+ # Line contains a point if the point is closer than the length of line from
+ # both ends and if the distance from point to line is smaller than half of
+ # the width. For reference:
+ # https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line
def contains?(x, y)
points_distance(x1, y1, x, y) < length and
points_distance(x2, y2, x, y) < length and
@@ -38,6 +39,7 @@ module Ruby2D
private
+ # Calculate the distance between two points
def points_distance(x1, y1, x2, y2)
Math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
end
@@ -59,5 +61,6 @@ module Ruby2D
@c4 = c
end
end
+
end
end
diff --git a/lib/ruby2d/music.rb b/lib/ruby2d/music.rb
index 0ace141..41b8901 100644
--- a/lib/ruby2d/music.rb
+++ b/lib/ruby2d/music.rb
@@ -1,4 +1,4 @@
-# music.rb
+# Ruby2D::Music
module Ruby2D
class Music
@@ -19,24 +19,30 @@ module Ruby2D
ext_init(path)
end
+ # Play the music
def play
ext_play
end
+ # Pause the music
def pause
ext_pause
end
+ # Resume paused music
def resume
ext_resume
end
+ # Stop playing the music, start at beginning
def stop
ext_stop
end
+ # Fade out music over provided milliseconds
def fadeout(ms)
ext_fadeout(ms)
end
+
end
end
diff --git a/lib/ruby2d/quad.rb b/lib/ruby2d/quad.rb
index 8ffc194..e514a39 100644
--- a/lib/ruby2d/quad.rb
+++ b/lib/ruby2d/quad.rb
@@ -1,4 +1,4 @@
-# quad.rb
+# Ruby2D::Quad
module Ruby2D
class Quad
@@ -25,9 +25,7 @@ module Ruby2D
@y3 = opts[:y3] || 100
@x4 = opts[:x4] || 0
@y4 = opts[:y4] || 100
-
- @z = opts[:z] || 0
-
+ @z = opts[:z] || 0
self.color = opts[:color] || 'white'
add
end
@@ -74,5 +72,6 @@ module Ruby2D
@c4 = c
end
end
+
end
end
diff --git a/lib/ruby2d/rectangle.rb b/lib/ruby2d/rectangle.rb
index 4b9db5e..43b0db5 100644
--- a/lib/ruby2d/rectangle.rb
+++ b/lib/ruby2d/rectangle.rb
@@ -1,4 +1,4 @@
-# rectangle.rb
+# Ruby2D::Rectangle
module Ruby2D
class Rectangle < Quad
@@ -6,15 +6,13 @@ module Ruby2D
attr_reader :x, :y, :width, :height
def initialize(opts = {})
- @x = opts[:x] || 0
- @y = opts[:y] || 0
- @z = opts[:z] || 0
- @width = opts[:width] || 200
+ @x = opts[:x] || 0
+ @y = opts[:y] || 0
+ @z = opts[:z] || 0
+ @width = opts[:width] || 200
@height = opts[:height] || 100
-
- update_coords(@x, @y, @width, @height)
-
self.color = opts[:color] || 'white'
+ update_coords(@x, @y, @width, @height)
add
end
diff --git a/lib/ruby2d/renderable.rb b/lib/ruby2d/renderable.rb
index d704c17..6a6761d 100644
--- a/lib/ruby2d/renderable.rb
+++ b/lib/ruby2d/renderable.rb
@@ -1,5 +1,8 @@
+# Ruby2D::Renderable
+
module Ruby2D
module Renderable
+
attr_reader :z
def z=(z)
@@ -29,7 +32,8 @@ module Ruby2D
end
def contains?(x, y)
- raise "Not implemented yet"
+ raise Error, "\`#contains?\` not implemented for this class yet"
end
+
end
end
diff --git a/lib/ruby2d/sound.rb b/lib/ruby2d/sound.rb
index 3ead3fa..c1d4060 100644
--- a/lib/ruby2d/sound.rb
+++ b/lib/ruby2d/sound.rb
@@ -1,4 +1,4 @@
-# sound.rb
+# Ruby2D::Sound
module Ruby2D
class Sound
@@ -18,6 +18,7 @@ module Ruby2D
ext_init(path)
end
+ # Play the sound
def play
ext_play
end
diff --git a/lib/ruby2d/sprite.rb b/lib/ruby2d/sprite.rb
index 68b6ace..37753fc 100644
--- a/lib/ruby2d/sprite.rb
+++ b/lib/ruby2d/sprite.rb
@@ -1,4 +1,4 @@
-# sprite.rb
+# Ruby2D::Sprite
module Ruby2D
class Sprite
diff --git a/lib/ruby2d/square.rb b/lib/ruby2d/square.rb
index 1a862b9..83cefb0 100644
--- a/lib/ruby2d/square.rb
+++ b/lib/ruby2d/square.rb
@@ -1,4 +1,4 @@
-# square.rb
+# Ruby2D::Square
module Ruby2D
class Square < Rectangle
@@ -10,18 +10,18 @@ module Ruby2D
@y = opts[:y] || 0
@z = opts[:z] || 0
@width = @height = @size = opts[:size] || 100
-
self.color = opts[:color] || 'white'
-
update_coords(@x, @y, @size, @size)
-
add
end
+ # Set the size of the square
def size=(s)
self.width = self.height = @size = s
end
+ # Make the inherited width and height attribute accessors private
private :width=, :height=
+
end
end
diff --git a/lib/ruby2d/text.rb b/lib/ruby2d/text.rb
index 9a66b6d..1b2207d 100644
--- a/lib/ruby2d/text.rb
+++ b/lib/ruby2d/text.rb
@@ -1,4 +1,4 @@
-# text.rb
+# Ruby2D::Text
module Ruby2D
class Text
@@ -11,9 +11,9 @@ module Ruby2D
@x = opts[:x] || 0
@y = opts[:y] || 0
@z = opts[:z] || 0
- @text = (opts[:text] || "Hello World!").to_s
+ @text = (opts[:text] || "Hello Ruby!").to_s
@size = opts[:size] || 20
- @rotate = 0
+ @rotate = opts[:rotate] || 0
@font = opts[:font]
unless RUBY_ENGINE == 'opal'
diff --git a/lib/ruby2d/triangle.rb b/lib/ruby2d/triangle.rb
index 75615cf..0888994 100644
--- a/lib/ruby2d/triangle.rb
+++ b/lib/ruby2d/triangle.rb
@@ -1,4 +1,4 @@
-# triangle.rb
+# Ruby2D::Triangle
module Ruby2D
class Triangle
@@ -16,8 +16,7 @@ module Ruby2D
@y2 = opts[:y2] || 100
@x3 = opts[:x3] || 0
@y3 = opts[:y3] || 100
- @z = opts[:z] || 0
-
+ @z = opts[:z] || 0
self.color = opts[:color] || 'white'
add
end
@@ -61,5 +60,6 @@ module Ruby2D
@c3 = c
end
end
+
end
end
diff --git a/lib/ruby2d/version.rb b/lib/ruby2d/version.rb
index 46d7714..19ae871 100644
--- a/lib/ruby2d/version.rb
+++ b/lib/ruby2d/version.rb
@@ -1,4 +1,4 @@
-# version.rb
+# Ruby2D::VERSION
module Ruby2D
VERSION = '0.5.1'
diff --git a/lib/ruby2d/window.rb b/lib/ruby2d/window.rb
index 6ed6677..385070b 100644
--- a/lib/ruby2d/window.rb
+++ b/lib/ruby2d/window.rb
@@ -1,4 +1,4 @@
-# Ruby 2D Window class
+# Ruby2D::Window
# Represents a window on screen, responsible for storing renderable graphics,
# event handlers, the update loop, showing and closing the window.
@@ -28,12 +28,12 @@ module Ruby2D
@icon = nil
# Window size and characteristics
- @width = args[:width] || 640
- @height = args[:height] || 480
- @resizable = false
+ @width = args[:width] || 640
+ @height = args[:height] || 480
+ @resizable = false
@borderless = false
@fullscreen = false
- @highdpi = false
+ @highdpi = false
# Size of the window's viewport (the drawable area)
@viewport_width, @viewport_height = nil, nil
@@ -46,7 +46,7 @@ module Ruby2D
# Frames per second upper limit, and the actual FPS
@fps_cap = args[:fps_cap] || 60
- @fps = @fps_cap
+ @fps = @fps_cap
# Vertical synchronization, set to prevent screen tearing (recommended)
@vsync = args[:vsync] || true
diff --git a/test/circle_spec.rb b/test/circle_spec.rb
index f100cde..dd49e2c 100644
--- a/test/circle_spec.rb
+++ b/test/circle_spec.rb
@@ -2,7 +2,7 @@ require 'ruby2d'
RSpec.describe Ruby2D::Circle do
- describe '#new' do
+ describe "#new" do
it "creates a white circle by default" do
circle = Circle.new
expect(circle.color).to be_a(Ruby2D::Color)
diff --git a/test/color_spec.rb b/test/color_spec.rb
index c2f2cfa..4ed21aa 100644
--- a/test/color_spec.rb
+++ b/test/color_spec.rb
@@ -2,32 +2,32 @@ require 'ruby2d'
RSpec.describe Ruby2D::Color do
- describe '#is_valid?' do
- it 'determines if a color string is valid' do
+ describe "#is_valid?" do
+ it "determines if a color string is valid" do
expect(Ruby2D::Color.is_valid? 'red').to eq true
expect(Ruby2D::Color.is_valid? 'balloons').to eq false
end
- it 'determines if a color string is a valid hex value' do
+ it "determines if a color string is a valid hex value" do
expect(Ruby2D::Color.is_valid? '#c0c0c0').to eq true
expect(Ruby2D::Color.is_valid? '#00000').to eq false
expect(Ruby2D::Color.is_valid? '123456').to eq false
end
- it 'determines if an array is a valid color' do
+ it "determines if an array is a valid color" do
expect(Ruby2D::Color.is_valid? [1, 0, 0.0, 1.0]).to eq true
expect(Ruby2D::Color.is_valid? [1.0, 0, 0]).to eq false
end
end
- describe '#new' do
- it 'raises error on bad color' do
+ describe "#new" do
+ it "raises error on bad color" do
expect { Ruby2D::Color.new 42 }.to raise_error Ruby2D::Error
end
end
- describe '#opacity' do
- it 'sets and returns the opacity' do
+ describe "#opacity" do
+ it "sets and returns the opacity" do
s1 = Square.new
s1.opacity = 0.5
s2 = Square.new(color: ['red', 'green', 'blue', 'yellow'])
diff --git a/test/dsl_spec.rb b/test/dsl_spec.rb
index 4337046..dd85f77 100644
--- a/test/dsl_spec.rb
+++ b/test/dsl_spec.rb
@@ -3,23 +3,23 @@ include Ruby2D::DSL
RSpec.describe Ruby2D::DSL do
- describe '#get' do
- it 'gets the default window attributes' do
+ describe "#get" do
+ it "gets the default window attributes" do
expect(get :width).to eq 640
expect(get :height).to eq 480
expect(get :title).to eq "Ruby 2D"
end
end
- describe '#set' do
- it 'sets a single window attribute' do
+ describe "#set" do
+ it "sets a single window attribute" do
set width: 300
expect(get :width).to eq 300
expect(get :height).to eq 480
expect(get :title).to eq "Ruby 2D"
end
- it 'sets multiple window attributes at a time' do
+ it "sets multiple window attributes at a time" do
set width: 800, height: 600, title: "Hello tests!"
expect(get :width).to eq 800
expect(get :height).to eq 600
diff --git a/test/events_spec.rb b/test/events_spec.rb
index a5fa279..ab07209 100644
--- a/test/events_spec.rb
+++ b/test/events_spec.rb
@@ -1,7 +1,8 @@
require 'ruby2d'
-RSpec.describe Window do
- describe 'on :bad_event' do
+RSpec.describe Ruby2D::Window do
+
+ describe "on :bad_event" do
it "raises exception if a bad event type is given" do
window = Ruby2D::Window.new
expect { window.on(:bad_event) }.to raise_error(Ruby2D::Error)
@@ -130,4 +131,5 @@ RSpec.describe Window do
end
end
end
+
end
diff --git a/test/image_spec.rb b/test/image_spec.rb
index 5f57c26..f175a01 100644
--- a/test/image_spec.rb
+++ b/test/image_spec.rb
@@ -1,20 +1,20 @@
require 'ruby2d'
RSpec.describe Ruby2D::Image do
- describe '#new' do
+
+ describe "#new" do
it "raises exception if image file doesn't exist" do
- expect { Image.new(path: 'bad_image.png') }.to raise_error(Ruby2D::Error)
+ expect { Image.new(path: "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
+ describe "#contains?" do
+ it "returns true if point is inside the image" do
image = Image.new(path: "test/media/image.bmp")
expect(image.contains?(50, 50)).to be true
end
- it "returns true if point is not inside image" do
+ it "returns true if point is outside the image" do
image = Image.new(path: "test/media/image.bmp")
expect(image.contains?(-50, 50)).to be false
expect(image.contains?(50, -50)).to be false
@@ -22,4 +22,5 @@ RSpec.describe Ruby2D::Image do
expect(image.contains?(150, 50)).to be false
end
end
+
end
diff --git a/test/line_spec.rb b/test/line_spec.rb
index 669d440..ac6d005 100644
--- a/test/line_spec.rb
+++ b/test/line_spec.rb
@@ -1,15 +1,18 @@
require 'ruby2d'
-RSpec.describe Ruby2D::Triangle do
- describe '#contains?' do
- it "returns true if point is inside line" do
+RSpec.describe Ruby2D::Line do
+
+ describe "#contains?" do
+ it "returns true if point is inside the line" do
line = Line.new(x1: 0, y1: 0, x2: 100, y2: 100)
expect(line.contains?(25, 25)).to be true
end
- it "returns true if point is inside text" do
+
+ it "returns false if point is outside the line" do
line = Line.new(x1: 0, y1: 0, x2: 100, y2: 100)
expect(line.contains?(0, 10)).to be false
expect(line.contains?(10, 0)).to be false
end
end
+
end
diff --git a/test/music_spec.rb b/test/music_spec.rb
index 4b4e432..c3a1173 100644
--- a/test/music_spec.rb
+++ b/test/music_spec.rb
@@ -2,9 +2,9 @@ require 'ruby2d'
RSpec.describe Ruby2D::Music do
- describe '#new' 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)
+ expect { Music.new("bad_music.mp3") }.to raise_error(Ruby2D::Error)
end
end
diff --git a/test/quad_spec.rb b/test/quad_spec.rb
index 249d8a7..1819fc0 100644
--- a/test/quad_spec.rb
+++ b/test/quad_spec.rb
@@ -1,18 +1,19 @@
require 'ruby2d'
RSpec.describe Ruby2D::Quad do
- describe '#new' do
+
+ describe "#new" do
it "creates a quad with white color by default" do
quad = Quad.new
- expect(quad.color).to be_a(Ruby2D::Color)
+ expect(quad.color).to be_a(Ruby2D::Color)
expect(quad.color.r).to eq(1)
expect(quad.color.g).to eq(1)
expect(quad.color.b).to eq(1)
expect(quad.color.a).to eq(1)
end
- it 'creates a new quad with one color via string' do
- quad = Quad.new(color: "red")
+ it "creates a new quad with one color via string" do
+ quad = Quad.new(color: 'red')
expect(quad.color).to be_a(Ruby2D::Color)
end
@@ -22,7 +23,7 @@ RSpec.describe Ruby2D::Quad do
end
it "creates a new quad with 4 colors via array of 4 strings" do
- quad = Quad.new(color: ["red", "green", "blue", "black"])
+ quad = Quad.new(color: ['red', 'green', 'blue', 'black'])
expect(quad.color).to be_a(Ruby2D::Color::Set)
end
@@ -35,25 +36,24 @@ RSpec.describe Ruby2D::Quad do
[0.4, 0.6, 0.8, 1.0]
]
)
-
expect(quad.color).to be_a(Ruby2D::Color::Set)
end
it "throws an error when array of 3 strings is passed" do
expect do
- Quad.new(color: ["red", "green", "blue"])
+ Quad.new(color: ['red', 'green', 'blue'])
end.to raise_error("Quads require 4 colors, one for each vertex. 3 were given.")
end
it "throws an error when array of 5 strings is passed" do
expect do
- Quad.new(color: ["red", "green", "blue", "black", "fuchsia"])
+ Quad.new(color: ['red', 'green', 'blue', 'black', 'fuchsia'])
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
+ describe "#contains?" do
+ it "returns true if point is inside the quad" do
quad = Quad.new(
x1: -25, y1: 0,
x2: 0, y2: -25,
@@ -63,7 +63,7 @@ RSpec.describe Ruby2D::Quad do
expect(quad.contains?(0, 0)).to be true
end
- it "returns true if point is not inside quad" do
+ it "returns true if point is outside the quad" do
quad = Quad.new(
x1: -25, y1: 0,
x2: 0, y2: -25,
@@ -76,4 +76,5 @@ RSpec.describe Ruby2D::Quad do
expect(quad.contains?(-20, -20)).to be false
end
end
+
end
diff --git a/test/rectangle_spec.rb b/test/rectangle_spec.rb
index 940b6cc..bbf1616 100644
--- a/test/rectangle_spec.rb
+++ b/test/rectangle_spec.rb
@@ -1,13 +1,14 @@
require 'ruby2d'
RSpec.describe Ruby2D::Rectangle do
- describe '#contains?' do
- it "returns true if point is inside rectangle" do
+
+ describe "#contains?" do
+ it "returns true if point is inside the rectangle" do
rectangle = Rectangle.new(x: 0, y: 0, width: 50, height: 50)
expect(rectangle.contains?(25, 25)).to be true
end
- it "returns true if point is not inside rectangle" do
+ it "returns true if point is outside the rectangle" do
rectangle = Rectangle.new(x: 0, y: 0, width: 50, height: 50)
expect(rectangle.contains?(-25, 25)).to be false
expect(rectangle.contains?( 25, -25)).to be false
@@ -15,4 +16,5 @@ RSpec.describe Ruby2D::Rectangle do
expect(rectangle.contains?( 50, 25)).to be false
end
end
+
end
diff --git a/test/sound_spec.rb b/test/sound_spec.rb
index 72141a7..5458abe 100644
--- a/test/sound_spec.rb
+++ b/test/sound_spec.rb
@@ -2,9 +2,9 @@ require 'ruby2d'
RSpec.describe Ruby2D::Sound do
- describe '#new' 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)
+ expect { Sound.new("bad_sound.wav") }.to raise_error(Ruby2D::Error)
end
end
diff --git a/test/spec_helper.rb b/test/spec_helper.rb
index b69dfa6..3960a15 100644
--- a/test/spec_helper.rb
+++ b/test/spec_helper.rb
@@ -12,8 +12,8 @@ RSpec.configure do |config|
original_stdout = $stdout
config.before(:all) do
# Redirect stderr and stdout
- $stderr = File.open(File::NULL, "w")
- $stdout = File.open(File::NULL, "w")
+ $stderr = File.open(File::NULL, 'w')
+ $stdout = File.open(File::NULL, 'w')
end
config.after(:all) do
# Restore
diff --git a/test/sprite_spec.rb b/test/sprite_spec.rb
index a9cbf31..c4ad61e 100644
--- a/test/sprite_spec.rb
+++ b/test/sprite_spec.rb
@@ -2,16 +2,14 @@ require 'ruby2d'
RSpec.describe Ruby2D::Sprite do
- describe '#new' do
-
+ describe "#new" do
it "raises exception if file doesn't exist" do
expect { Sprite.new("bad_sprite_sheet.png") }.to raise_error(Ruby2D::Error)
end
- it 'creates a new sprite' do
+ it "creates a new sprite" do
Sprite.new("test/media/coin.png")
end
-
end
end
diff --git a/test/text_spec.rb b/test/text_spec.rb
index a787380..3e15435 100644
--- a/test/text_spec.rb
+++ b/test/text_spec.rb
@@ -1,20 +1,21 @@
require 'ruby2d'
RSpec.describe Ruby2D::Text do
- describe '#new' do
+
+ describe "#new" do
it "raises exception if font file doesn't exist" do
- expect { Text.new(font: 'bad_font.ttf') }.to raise_error(Ruby2D::Error)
+ expect { Text.new(font: "bad_font.ttf") }.to raise_error(Ruby2D::Error)
end
end
- describe '#text=' do
+ describe "#text=" do
it 'maps Time to string' do
t = Text.new(font: "test/media/bitstream_vera/vera.ttf")
t.text = Time.new(1, 1, 1, 1, 1, 1, 1)
expect(t.text).to eq "0001-01-01 01:01:01 +0000"
end
- it 'maps Number to string' do
+ it "maps Number to string" do
t = Text.new(font: "test/media/bitstream_vera/vera.ttf")
t.text = 0
expect(t.text).to eq "0"
@@ -24,7 +25,8 @@ RSpec.describe Ruby2D::Text do
describe "#width" do
it "is known after creation" do
t = Text.new(font: "test/media/bitstream_vera/vera.ttf")
- expect(t.width).to eq(123)
+ # expect(t.width).to eq(123)
+ expect(t.width).to eq(116)
end
it "is known after updating" do
@@ -47,14 +49,14 @@ RSpec.describe Ruby2D::Text do
end
end
- describe '#contains?' do
- it "returns true if point is inside text" do
+ describe "#contains?" do
+ it "returns true if point is inside the text" do
t = Text.new(font: "test/media/bitstream_vera/vera.ttf")
t.text = "Hello world!"
expect(t.contains?(t.width / 2, t.height / 2)).to be true
end
- it "returns true if point is not inside text" do
+ it "returns false if point is outside the text" do
t = Text.new(font: "test/media/bitstream_vera/vera.ttf")
t.text = "Hello world!"
expect(t.contains?( - t.width / 2, t.height / 2)).to be false
@@ -63,4 +65,5 @@ RSpec.describe Ruby2D::Text do
expect(t.contains?( t.width / 2, 3 * t.height / 2)).to be false
end
end
+
end
diff --git a/test/triangle_spec.rb b/test/triangle_spec.rb
index 045f9f3..1f9dc5d 100644
--- a/test/triangle_spec.rb
+++ b/test/triangle_spec.rb
@@ -1,18 +1,19 @@
require 'ruby2d'
RSpec.describe Ruby2D::Triangle do
- describe '#new' do
- it "creates a triangle with white color by default" do
+
+ describe "#new" do
+ it "creates a white triangle by default" do
triangle = Triangle.new
- expect(triangle.color).to be_a(Ruby2D::Color)
+ expect(triangle.color).to be_a(Ruby2D::Color)
expect(triangle.color.r).to eq(1)
expect(triangle.color.g).to eq(1)
expect(triangle.color.b).to eq(1)
expect(triangle.color.a).to eq(1)
end
- it 'creates a new triangle with one color via string' do
- triangle = Triangle.new(color: "black")
+ it "creates a new triangle with one color via string" do
+ triangle = Triangle.new(color: 'black')
expect(triangle.color).to be_a(Ruby2D::Color)
end
@@ -22,7 +23,7 @@ RSpec.describe Ruby2D::Triangle do
end
it "creates a new triangle with 3 colors via array of 3 strings" do
- triangle = Triangle.new(color: ["red", "green", "blue"])
+ triangle = Triangle.new(color: ['red', 'green', 'blue'])
expect(triangle.color).to be_a(Ruby2D::Color::Set)
end
@@ -39,20 +40,19 @@ RSpec.describe Ruby2D::Triangle do
it "throws an error when array of 2 strings is passed" do
expect do
- Triangle.new(color: ["red", "green"])
+ Triangle.new(color: ['red', 'green'])
end.to raise_error("Triangles require 3 colors, one for each vertex. 2 were given.")
end
it "throws an error when array of 4 strings is passed" do
expect do
- Triangle.new(color: ["red", "green", "blue", "fuchsia"])
+ Triangle.new(color: ['red', 'green', 'blue', 'fuchsia'])
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
+ describe "#contains?" do
+ it "returns true if point is inside the triangle" do
triangle = Triangle.new(
x1: 0, y1: 0,
x2: 0, y2: 100,
@@ -61,7 +61,7 @@ RSpec.describe Ruby2D::Triangle do
expect(triangle.contains?(25, 25)).to be true
end
- it "returns true if point is inside text" do
+ it "returns false if point is outside the triangle" do
triangle = Triangle.new(
x1: 0, y1: 0,
x2: 0, y2: 100,
@@ -72,4 +72,5 @@ RSpec.describe Ruby2D::Triangle do
expect(triangle.contains?(100, 100)).to be false
end
end
+
end
diff --git a/test/window_spec.rb b/test/window_spec.rb
index c49b824..7be4e2b 100644
--- a/test/window_spec.rb
+++ b/test/window_spec.rb
@@ -2,7 +2,7 @@ require 'ruby2d'
RSpec.describe Ruby2D::Window do
- it 'returns class values' do
+ it "returns class values" do
expect(Window.current).to be_a(Ruby2D::Window)
expect(Window.width).to eq(640)
set width: 200