From 964910e87dfae2266f30b43d543a767fcc0281d5 Mon Sep 17 00:00:00 2001 From: fargelus Date: Thu, 12 May 2022 16:16:14 +0300 Subject: [PATCH] First specs --- .gitignore | 2 + .mdlrc | 2 +- .rspec | 3 ++ CHANGELOG.md | 9 ++++ Gemfile | 4 ++ Gemfile.lock | 49 ++++++++---------- lib/space_invaders/aliens.rb | 8 +-- lib/space_invaders/base/helpers.rb | 8 +-- space_invaders.gemspec | 23 +++----- spec/base/helpers_spec.rb | 32 ++++++++++++ spec/base/image_object_spec.rb | 29 +++++++++++ spec/fixtures/files/ruby.png | Bin 0 -> 5563 bytes spec/spec_helper.rb | 1 - ...space_invaders_spec.rb => version_spec.rb} | 6 +-- 14 files changed, 119 insertions(+), 57 deletions(-) create mode 100644 .rspec create mode 100644 CHANGELOG.md create mode 100644 spec/base/helpers_spec.rb create mode 100644 spec/base/image_object_spec.rb create mode 100644 spec/fixtures/files/ruby.png rename spec/{space_invaders_spec.rb => version_spec.rb} (69%) diff --git a/.gitignore b/.gitignore index ce9c3c5..0313bb7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .env TODO.md +.rspec_status +*.gem diff --git a/.mdlrc b/.mdlrc index 68975e8..e350d3d 100644 --- a/.mdlrc +++ b/.mdlrc @@ -1 +1 @@ -rules "~MD029", "~MD033", "~MD013" \ No newline at end of file +rules "~MD029", "~MD033", "~MD013", "~MD034" diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..34c5164 --- /dev/null +++ b/.rspec @@ -0,0 +1,3 @@ +--format documentation +--color +--require spec_helper diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..3be69e2 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,9 @@ +# Change log + +## [master] + +## [0.1.0] + +- Initial release 👾 + +[@fargelus]: https://github.com/fargelus \ No newline at end of file diff --git a/Gemfile b/Gemfile index 946445d..d394f1c 100644 --- a/Gemfile +++ b/Gemfile @@ -4,3 +4,7 @@ source 'https://rubygems.org' # Specify your gem's dependencies in space_invaders.gemspec gemspec + +gem 'dotenv' +gem 'mdl' +gem 'rspec', '~> 3.0' diff --git a/Gemfile.lock b/Gemfile.lock index f9f98f4..ab9c206 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,18 +2,20 @@ PATH remote: . specs: space_invaders (0.1.0) + gosu + mongo (~> 2.2.5) GEM remote: https://rubygems.org/ specs: - bson (4.8.2) - byebug (11.1.3) - chef-utils (17.9.46) + bson (4.15.0) + chef-utils (17.10.0) concurrent-ruby - concurrent-ruby (1.1.9) - diff-lcs (1.3) - dotenv (2.7.5) - kramdown (2.3.1) + concurrent-ruby (1.1.10) + diff-lcs (1.5.0) + dotenv (2.7.6) + gosu (1.4.3) + kramdown (2.4.0) rexml kramdown-parser-gfm (1.1.0) kramdown (~> 2.0) @@ -26,39 +28,34 @@ GEM mixlib-cli (2.1.8) mixlib-config (3.0.9) tomlrb - mixlib-shellout (3.2.5) + mixlib-shellout (3.2.7) chef-utils mongo (2.2.7) bson (~> 4.0) - rake (13.0.1) rexml (3.2.5) - rspec (3.9.0) - rspec-core (~> 3.9.0) - rspec-expectations (~> 3.9.0) - rspec-mocks (~> 3.9.0) - rspec-core (3.9.1) - rspec-support (~> 3.9.1) - rspec-expectations (3.9.1) + rspec (3.11.0) + rspec-core (~> 3.11.0) + rspec-expectations (~> 3.11.0) + rspec-mocks (~> 3.11.0) + rspec-core (3.11.0) + rspec-support (~> 3.11.0) + rspec-expectations (3.11.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.9.0) - rspec-mocks (3.9.1) + rspec-support (~> 3.11.0) + rspec-mocks (3.11.1) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.9.0) - rspec-support (3.9.2) + rspec-support (~> 3.11.0) + rspec-support (3.11.0) tomlrb (2.0.1) PLATFORMS - ruby + arm64-darwin-21 DEPENDENCIES - bundler (~> 2.0) - byebug dotenv mdl - mongo (~> 2.2.5) - rake (~> 13.0) rspec (~> 3.0) space_invaders! BUNDLED WITH - 2.1.4 + 2.3.9 diff --git a/lib/space_invaders/aliens.rb b/lib/space_invaders/aliens.rb index a53d81f..5195176 100644 --- a/lib/space_invaders/aliens.rb +++ b/lib/space_invaders/aliens.rb @@ -109,10 +109,10 @@ def above_enemy_alien closest = find(@enemy.x + @enemy.w) return closest if closest - further_x = last_column_x(@aliens) + further_x = max_x_coord(@aliens) return find(further_x) if further_x < @enemy.x - find(first_column_x(@aliens)) + find(min_x_coord(@aliens)) end def can_move? @@ -146,8 +146,8 @@ def next_move_direction def available_directions extreme_right_aliens = WIDTH - ALIENS_WIDTH - ALIENS_MARGIN { - right: first_column_x(@aliens) < ALIENS_MARGIN, - left: last_column_x(@aliens) > extreme_right_aliens + right: min_x_coord(@aliens) < ALIENS_MARGIN, + left: max_x_coord(@aliens) > extreme_right_aliens } end diff --git a/lib/space_invaders/base/helpers.rb b/lib/space_invaders/base/helpers.rb index f049c90..43b4982 100644 --- a/lib/space_invaders/base/helpers.rb +++ b/lib/space_invaders/base/helpers.rb @@ -5,15 +5,11 @@ module SpaceInvaders module Helpers - def horizontal_center(obj) - Settings::WIDTH / 2 - obj.w / 2 - end - - def last_column_x(collection) + def max_x_coord(collection) collection.max_by(&:x).x end - def first_column_x(collection) + def min_x_coord(collection) collection.min_by(&:x).x end end diff --git a/space_invaders.gemspec b/space_invaders.gemspec index 4acfd53..0e02b32 100644 --- a/space_invaders.gemspec +++ b/space_invaders.gemspec @@ -14,25 +14,18 @@ Gem::Specification.new do |spec| spec.description = 'Space Invaders classic 2d game' spec.homepage = 'https://github.com/fargelus/space-invaders' spec.license = 'MIT' - - spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'" + spec.required_ruby_version = '>= 2.6.0' spec.metadata['homepage_uri'] = spec.homepage + spec.metadata['changelog_uri'] = 'https://github.com/fargelus/space-invaders/CHANGELOG.md' # Specify which files should be added to the gem when it is released. - # The `git ls-files -z` loads the files in the RubyGem that have been added into git. - spec.files = Dir.chdir(File.expand_path(__dir__)) do - `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } - end - spec.bindir = 'exe' - spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } + spec.files = Dir.glob('lib/**/*') + Dir.glob('bin/**/*') + %w[README.md CHANGELOG.md] + spec.bindir = 'bin' + spec.executables = 'space_invaders' spec.require_paths = ['lib'] - spec.add_development_dependency 'bundler', '~> 2.0' - spec.add_development_dependency 'dotenv' - spec.add_development_dependency 'byebug' - spec.add_development_dependency 'mongo', '~> 2.2.5' - spec.add_development_dependency 'rake', '~> 13.0' - spec.add_development_dependency 'rspec', '~> 3.0' - spec.add_development_dependency 'mdl' + spec.add_dependency 'gosu' + # TODO Change to SQLite + spec.add_dependency 'mongo', '~> 2.2.5' end diff --git a/spec/base/helpers_spec.rb b/spec/base/helpers_spec.rb new file mode 100644 index 0000000..b16ce34 --- /dev/null +++ b/spec/base/helpers_spec.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +require 'space_invaders/base/helpers' +require 'ostruct' + +RSpec.describe SpaceInvaders::Helpers do + include SpaceInvaders::Helpers + + let(:collection) do + [ + OpenStruct.new(x: 2, y: 35), + OpenStruct.new(x: 50, y: 0), + OpenStruct.new(x: -100, y: 200) + ] + end + + describe '#max_x_coord' do + subject { max_x_coord(collection) } + + it 'returns max x value' do + expect(subject).to eql 50 + end + end + + describe '#min_x_coord' do + subject { min_x_coord(collection) } + + it 'returns min x value' do + expect(subject).to eql(-100) + end + end +end diff --git a/spec/base/image_object_spec.rb b/spec/base/image_object_spec.rb new file mode 100644 index 0000000..ec55e58 --- /dev/null +++ b/spec/base/image_object_spec.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +require 'space_invaders/base/image_object' + +RSpec.describe SpaceInvaders::ImageObject do + let(:image_path) { File.join(Dir.pwd, 'spec', 'fixtures', 'files', 'ruby.png') } + + subject(:image_object) { described_class.new(0, 0, image_path) } + + describe '#set' do + before { image_object.set(100, 200) } + + it 'updates x and y coordinates' do + expect(image_object.x).to eql 100 + expect(image_object.y).to eql 200 + end + end + + describe '#draw' do + before do + allow_any_instance_of(Gosu::Image).to receive(:draw).and_return nil + end + + it 'draws an image at predefined coords' do + expect_any_instance_of(Gosu::Image).to receive(:draw).with(0, 0, 0) + image_object.draw + end + end +end diff --git a/spec/fixtures/files/ruby.png b/spec/fixtures/files/ruby.png new file mode 100644 index 0000000000000000000000000000000000000000..9d88d4db7cb4a4be9492e2f859d03b9069c407c1 GIT binary patch literal 5563 zcmV;s6-4TZP)5w4HcFE0FDn9^I~4?02b;N zBJx{T^Iu!*02AyE9FG71?qOd2?(XXV3+n(B(-a@y6(HMDPyYJ(?k_mS6CL{1)|UVb zq8T6WPF47-sQ9_N?mkBJj*_kr8TO>7rVkj_O-%Kgo%gS@xiU2EA}#sD#_?=%x)B@i zLrd^lVEp0Y*c2byvb6Geefi1D!#q9kR$TLghoUJj&Yz*FPgCZ~%h#-~x@~Z(L`kkE zEA!;# z)&KzO*Vy!QaJ>Kq^m};PM@99GiOE-6vkY~X000zLNkldD%Pa#d%l`j=vE8k!CH3*TEy=L;ZJ0rF=~cg@)U7d$ zp8(G6^7!cH^m=#WWEtUPG)JSgPQPF9(C@E} z&>t4=4`%*<>8y0S-S&@5XOTa5PK*yuHxSicNHmUqXJnr2d8EhPa=F~@uKc2;#~*h_ z=4g$}m0~?IcP`1_`LR+i6Vlxu(_c&9v*UZj7C<&2Z9W~Nzvr~e9uVp8lV64Ocn>ML z183`VzCnNYvRe+&Ug>^282|M8zPuH2=wfvGGfLM# zf2e1BC-cz7=yzrjUH{CXb|yQzVvbPWaEu>`C2H&q13f-B*HY(;()F$()pfa2sgzeAzis~YHCMjMjU)^s zRCh(XMr>CfUftS_hHW=%pK{`y9bb~IQT}J@+9msB8OzzT+hxWD@hBnH%_C8B#J4d7R^D~37r)wPA zhmUC;v#?dmF3gh?Gh4oh@x;B=_cqq?RZ_>yF#nm&Y98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ksY7%cWcxIj;$jn1V%X+45N>GuwPM{ehx;sahx*x_%14!b30wt~* zXjV}s#c55)v`rPx*WX`nLnLlDiYkFlj&N?O)-!EW$KCV4AQHD53Zq7%D3dut*R;(7 z!#D3xdseH7M|yam6GfR6CvDRDb+`4b3erlgxqJV(rJ+U5X}#HBN3q$uO!l!9rS z3x?bwTQ*bDMHv^TpE7T8;Jo~uJ%NY=O%-M9Sv4)wHb1Z@5K*A1qKr~?il%LjVERb> zQ6ntMcG{+Gp1?j*GbV9fpo5}(Sk1t+%@s)E(e{iTXul|7Nii5Go|+cERNEy~uq@Y|5TB#^|t+7$~lDauxk zrfr@WzWMy8o5iV_k`!h8Cow;=fH#5E2Ej~;in6u80Mj;CkO^ck@Mg-@hV}PFnYMWX z581WaTU4)5QU3COm$x{;5YrvqrLzD002ov JPDHLkV1iU-;M4#B literal 0 HcmV?d00001 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c6aed2f..e900f0a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true require 'bundler/setup' -require 'space_invaders' RSpec.configure do |config| # Enable flags like --only-failures and --next-failure diff --git a/spec/space_invaders_spec.rb b/spec/version_spec.rb similarity index 69% rename from spec/space_invaders_spec.rb rename to spec/version_spec.rb index b6b2f27..59946c2 100644 --- a/spec/space_invaders_spec.rb +++ b/spec/version_spec.rb @@ -1,11 +1,9 @@ # frozen_string_literal: true +require 'space_invaders/version' + RSpec.describe SpaceInvaders do it 'has a version number' do expect(SpaceInvaders::VERSION).not_to be nil end - - it 'does something useful' do - expect(false).to eq(true) - end end