Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specs Init #28

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.env
TODO.md
.rspec_status
*.gem
2 changes: 1 addition & 1 deletion .mdlrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
rules "~MD029", "~MD033", "~MD013"
rules "~MD029", "~MD033", "~MD013", "~MD034"
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--format documentation
--color
--require spec_helper
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Change log

## [master]

## [0.1.0]

- Initial release 👾

[@fargelus]: https://github.com/fargelus
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
49 changes: 23 additions & 26 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
8 changes: 4 additions & 4 deletions lib/space_invaders/aliens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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

Expand Down
8 changes: 2 additions & 6 deletions lib/space_invaders/base/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 8 additions & 15 deletions space_invaders.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
32 changes: 32 additions & 0 deletions spec/base/helpers_spec.rb
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions spec/base/image_object_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Binary file added spec/fixtures/files/ruby.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 2 additions & 4 deletions spec/space_invaders_spec.rb → spec/version_spec.rb
Original file line number Diff line number Diff line change
@@ -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