diff options
| author | lstrzebinczyk <[email protected]> | 2017-03-21 14:20:54 +0100 |
|---|---|---|
| committer | Tom Black <[email protected]> | 2017-03-28 22:23:34 -0400 |
| commit | e5131435e9f78d37b8dd02be551299dac7f547d9 (patch) | |
| tree | b792765453421fd07f2463f4b3fbf9f72802933c /test | |
| parent | 79c50dab7f06e4ec288e79e61d0631fdf07b7264 (diff) | |
| download | ruby2d-e5131435e9f78d37b8dd02be551299dac7f547d9.tar.gz ruby2d-e5131435e9f78d37b8dd02be551299dac7f547d9.zip | |
implement Color#opacity=
Diffstat (limited to 'test')
| -rw-r--r-- | test/quad_spec.rb | 92 | ||||
| -rw-r--r-- | test/testcard.rb | 8 | ||||
| -rw-r--r-- | test/triangle_spec.rb | 82 |
3 files changed, 182 insertions, 0 deletions
diff --git a/test/quad_spec.rb b/test/quad_spec.rb new file mode 100644 index 0000000..58f2dfd --- /dev/null +++ b/test/quad_spec.rb @@ -0,0 +1,92 @@ +require 'ruby2d' + +RSpec.describe Ruby2D::Quad do + describe '#new' do + it "creates a quad with white color by default" do + quad = Quad.new( + 300, 200, + 350, 200, + 300, 250, + 250, 250 + ) + + 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( + 300, 200, + 350, 200, + 300, 250, + 250, 250, + "red" + ) + + expect(quad.color).to be_a(Ruby2D::Color) + end + + it "creates a new triangle with one color via array of numbers" do + quad = Quad.new( + 300, 200, + 350, 200, + 300, 250, + 250, 250, + [0.1, 0.3, 0.5, 0.7] + ) + + expect(quad.color).to be_a(Ruby2D::Color) + end + + it "creates a new quad with 4 colors via array of 4 strings" do + quad = Quad.new( + 300, 200, + 350, 200, + 300, 250, + 250, 250, + ["red", "green", "blue", "black"] + ) + + expect(quad.color).to be_a(Ruby2D::Color::Set) + end + + it "creates a new quad with 4 colors via array of 4 arrays of arrays of numbers" do + quad = Quad.new( + 300, 200, + 350, 200, + 300, 250, + 250, 250, + [[0.1, 0.3, 0.5, 0.7], [0.2, 0.4, 0.6, 0.8], [0.3, 0.5, 0.7, 0.9], [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( + 300, 200, + 350, 200, + 300, 250, + 250, 250, + ["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( + 300, 200, + 350, 200, + 300, 250, + 250, 250, + ["red", "green", "blue", "black", "fuchsia"] + ) + end.to raise_error("Quads require 4 colors, one for each vertex. 5 were given.") + end + end +end diff --git a/test/testcard.rb b/test/testcard.rb index 44cc817..b25813b 100644 --- a/test/testcard.rb +++ b/test/testcard.rb @@ -175,6 +175,10 @@ pointer = Square.new(0, 0, 10, [1, 1, 1, 1]) pointer_outline = Square.new(0, 0, 18, [0, 1, 0, 0]) flash = 0 +# Updating opacity +opacity_square = Square.new(500, 255, 50, ["red", "green", "blue", "yellow"]) +time_start = Time.now + on key: 'escape' do close end @@ -202,6 +206,10 @@ update do if (get :frames) % 20 == 0 fps.text = "FPS: #{(get :fps).round(3)}" end + + elapsed_time = Time.now - time_start + opacity = Math.sin(3 * elapsed_time.to_f).abs + opacity_square.color.opacity = opacity end show diff --git a/test/triangle_spec.rb b/test/triangle_spec.rb new file mode 100644 index 0000000..f71b097 --- /dev/null +++ b/test/triangle_spec.rb @@ -0,0 +1,82 @@ +require 'ruby2d' + +RSpec.describe Ruby2D::Triangle do + describe '#new' do + it "creates a triangle with white color by default" do + triangle = Triangle.new( + 320, 50, + 540, 430, + 100, 430 + ) + 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( + 320, 50, + 540, 430, + 100, 430, + "black" + ) + + expect(triangle.color).to be_a(Ruby2D::Color) + end + + it "creates a new triangle with one color via array of numbers" do + triangle = Triangle.new( + 320, 50, + 540, 430, + 100, 430, + [0.1, 0.3, 0.5, 0.7] + ) + + expect(triangle.color).to be_a(Ruby2D::Color) + end + + it "creates a new triangle with 3 colors via array of 3 strings" do + triangle = Triangle.new( + 320, 50, + 540, 430, + 100, 430, + ["red", "green", "blue"] + ) + expect(triangle.color).to be_a(Ruby2D::Color::Set) + end + + it "creates a new triangle with 3 colors via array of 3 arrays of arrays of numbers" do + triangle = Triangle.new( + 320, 50, + 540, 430, + 100, 430, + [[0.1, 0.3, 0.5, 0.7], [0.2, 0.4, 0.6, 0.8], [0.3, 0.5, 0.7, 0.9]] + ) + expect(triangle.color).to be_a(Ruby2D::Color::Set) + end + + it "throws an error when array of 2 strings is passed" do + expect do + Triangle.new( + 320, 50, + 540, 430, + 100, 430, + ["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( + 320, 50, + 540, 430, + 100, 430, + ["red", "green", "blue", "fuchsia"] + ) + end.to raise_error("Triangles require 3 colors, one for each vertex. 4 were given.") + end + end +end |
