Skip to content
Permalink
Browse files

Optimise Table methods, update table_spec

  • Loading branch information...
SelenaSmall committed Aug 27, 2017
1 parent 5240942 commit 5152d5937e471c3be0bb639f8690a2b759306054
Showing with 10 additions and 72 deletions.
  1. +4 −14 lib/table.rb
  2. +6 −58 spec/table_spec.rb
@@ -1,24 +1,14 @@
# Table Class
class Table
attr_reader :x, :y

def initialize(x = 5, y = 5)
@x = x
@y = y
@x = (0..(x - 1))
@y = (0..(y - 1))
end

# Validate x & y axis are within 5x5 area
def valid_position?(x, y)
valid_x?(x) && valid_y?(y)
end

private

def valid_x?(x)
x >= 0 && x <= @x - 1
end

def valid_y?(y)
y >= 0 && y <= @y - 1
@x.cover?(x) && @y.cover?(y)
end
end
@@ -4,18 +4,18 @@

describe Table do
describe '#initialize' do
it 'should have an x attribute which is an integer' do
it 'should have an x attribute which is a Range' do
instance = Table.new

expect(instance.x).to be_a Integer
expect(instance.x).to eq 5
expect(instance.x).to be_a Range
expect(instance.x).to eq 0..4
end

it 'should have a y attribute which is an integer' do
it 'should have a y attribute which is a Range' do
instance = Table.new

expect(instance.y).to be_a Integer
expect(instance.y).to eq 5
expect(instance.y).to be_a Range
expect(instance.y).to eq 0..4
end
end

@@ -62,56 +62,4 @@
expect(instance.valid_position?(0, -2)).to be false
end
end

describe '#valid_x?' do
it 'should return true when x is not greater than or equal to 0' do
instance = Table.new

expect(instance.send(:valid_x?, 0)).to be true
end

it 'should return true when x is less than or equal to 4' do
instance = Table.new

expect(instance.send(:valid_x?, 4)).to be true
end

it 'should return false when x is less 0' do
instance = Table.new

expect(instance.send(:valid_x?, -1)).to be false
end

it 'should return false when x greater than 4' do
instance = Table.new

expect(instance.send(:valid_x?, 6)).to be false
end
end

describe '#valid_y?' do
it 'should return true when y is not greater than or equal to 0' do
instance = Table.new

expect(instance.send(:valid_y?, 0)).to be true
end

it 'should return true when y is less than or equal to 4' do
instance = Table.new

expect(instance.send(:valid_y?, 4)).to be true
end

it 'should return false when y is less 0' do
instance = Table.new

expect(instance.send(:valid_y?, -1)).to be false
end

it 'should return false when y greater than 4' do
instance = Table.new

expect(instance.send(:valid_y?, 6)).to be false
end
end
end

0 comments on commit 5152d59

Please sign in to comment.
You can’t perform that action at this time.