toy 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.document +4 -0
- data/.gitignore +19 -0
- data/.rspec +1 -0
- data/.travis.yml +8 -0
- data/ChangeLog.md +3 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +46 -0
- data/LICENSE.txt +20 -0
- data/README.md +49 -0
- data/Rakefile +41 -0
- data/bin/toy +7 -0
- data/lib/toy.rb +9 -0
- data/lib/toy/controller.rb +99 -0
- data/lib/toy/direction.rb +38 -0
- data/lib/toy/robot.rb +66 -0
- data/lib/toy/table.rb +52 -0
- data/lib/toy/unit.rb +44 -0
- data/lib/toy/unit_collection.rb +62 -0
- data/lib/toy/version.rb +5 -0
- data/spec/controller_spec.rb +162 -0
- data/spec/direction_spec.rb +35 -0
- data/spec/robot_spec.rb +143 -0
- data/spec/spec_helper.rb +8 -0
- data/spec/table_spec.rb +37 -0
- data/spec/toy_spec.rb +9 -0
- data/spec/unit_collection_spec.rb +125 -0
- data/spec/unit_spec.rb +67 -0
- data/toy.gemspec +24 -0
- metadata +136 -0
data/spec/spec_helper.rb
ADDED
data/spec/table_spec.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Toy::Table do
|
6
|
+
let(:table) { Toy::Table.new }
|
7
|
+
|
8
|
+
describe '#unit_collection' do
|
9
|
+
specify { expect(table.unit_collection.all.size).to eq(25) }
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#hold!' do
|
13
|
+
let!(:object1) { "Object1" }
|
14
|
+
let!(:object2) { "Object2" }
|
15
|
+
let!(:unit1) { table.unit_collection.find_by_coordinates(0, 0) }
|
16
|
+
let!(:unit2) { table.unit_collection.find_by_coordinates(0, 1) }
|
17
|
+
|
18
|
+
context 'when unit is available' do
|
19
|
+
before { table.hold!(unit1.x, unit1.y, object1) }
|
20
|
+
|
21
|
+
specify { expect(table.unit_collection.find_by_object(object1)).to eq(unit1) }
|
22
|
+
specify do
|
23
|
+
table.hold!(unit2.x, unit2.y, object1)
|
24
|
+
expect(unit1.object).to eq(nil)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'when unit is not available' do
|
29
|
+
before { table.hold!(unit1.x, unit1.y, object1) }
|
30
|
+
|
31
|
+
specify do
|
32
|
+
expect { table.hold!(unit1.x, unit1.y, object2) }.to \
|
33
|
+
raise_error(Toy::Table::Error::TableError, 'Unit is not available.')
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/spec/toy_spec.rb
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Toy::UnitCollection do
|
6
|
+
let(:unit_collection) { Toy::UnitCollection.new }
|
7
|
+
let(:table1) { Toy::Table.new }
|
8
|
+
let!(:unit1) { Toy::Unit.new(table=table1, 0, 0) }
|
9
|
+
let!(:unit2) { Toy::Unit.new(table=table1, 0, 1) }
|
10
|
+
let!(:unit3) { Toy::Unit.new(table=table1, 1, 1) }
|
11
|
+
let!(:unit4) { Toy::Unit.new(table=table1, 2, 1) }
|
12
|
+
|
13
|
+
context 'when valid arguments' do
|
14
|
+
describe '#add' do
|
15
|
+
before do
|
16
|
+
unit_collection.add(unit1)
|
17
|
+
unit_collection.add(unit2)
|
18
|
+
end
|
19
|
+
|
20
|
+
specify do
|
21
|
+
expect(unit_collection.all).to match_array([unit1, unit2])
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '#find_by_coordinates' do
|
26
|
+
before do
|
27
|
+
unit_collection.add(unit1)
|
28
|
+
unit_collection.add(unit2)
|
29
|
+
end
|
30
|
+
|
31
|
+
specify do
|
32
|
+
expect(unit_collection.find_by_coordinates(0, 1)).to eq(unit2)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '#find_by_object' do
|
37
|
+
let!(:object) { "I'm an object" }
|
38
|
+
before do
|
39
|
+
unit1.hold!(object)
|
40
|
+
unit_collection.add(unit1)
|
41
|
+
unit_collection.add(unit2)
|
42
|
+
end
|
43
|
+
|
44
|
+
specify do
|
45
|
+
expect(unit_collection.find_by_object(object)).to eq(unit1)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe '#find_north_of' do
|
50
|
+
before do
|
51
|
+
unit_collection.add(unit1)
|
52
|
+
unit_collection.add(unit2)
|
53
|
+
end
|
54
|
+
|
55
|
+
specify do
|
56
|
+
expect(unit_collection.find_north_of(unit1.x, unit1.y)).to eq(unit2)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe '#find_east_of' do
|
61
|
+
before do
|
62
|
+
unit_collection.add(unit3)
|
63
|
+
unit_collection.add(unit4)
|
64
|
+
end
|
65
|
+
|
66
|
+
specify do
|
67
|
+
expect(unit_collection.find_east_of(unit3.x, unit3.y)).to eq(unit4)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe '#find_south_of' do
|
72
|
+
before do
|
73
|
+
unit_collection.add(unit1)
|
74
|
+
unit_collection.add(unit2)
|
75
|
+
end
|
76
|
+
|
77
|
+
specify do
|
78
|
+
expect(unit_collection.find_south_of(unit2.x, unit2.y)).to eq(unit1)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe '#find_west_of' do
|
83
|
+
before do
|
84
|
+
unit_collection.add(unit3)
|
85
|
+
unit_collection.add(unit4)
|
86
|
+
end
|
87
|
+
|
88
|
+
specify do
|
89
|
+
expect(unit_collection.find_west_of(unit4.x, unit4.y)).to eq(unit3)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
describe '#find_by_direction_of' do
|
94
|
+
before do
|
95
|
+
unit_collection.add(unit3)
|
96
|
+
unit_collection.add(unit4)
|
97
|
+
end
|
98
|
+
|
99
|
+
specify do
|
100
|
+
expect(unit_collection.find_by_direction_of(unit4.x, unit4.y, :west)).to eq(unit3)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
context 'when invalid arguments' do
|
106
|
+
describe '#add' do
|
107
|
+
specify do
|
108
|
+
expect { unit_collection.add("I'm trying to be Unit") }.to \
|
109
|
+
raise_error(Toy::UnitCollection::Error::UnitError, 'Argument must be of type Unit!')
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
describe '#find_by_direction_of' do
|
114
|
+
before do
|
115
|
+
unit_collection.add(unit3)
|
116
|
+
unit_collection.add(unit4)
|
117
|
+
end
|
118
|
+
|
119
|
+
specify do
|
120
|
+
expect { unit_collection.find_by_direction_of(unit4.x, unit4.y, :invalid) }.to \
|
121
|
+
raise_error(Toy::UnitCollection::Error::DirectionError, 'Invalid direction!')
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
data/spec/unit_spec.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Toy::Unit do
|
6
|
+
context 'when valid arguments' do
|
7
|
+
let(:unit) { Toy::Unit.new(0, 1) }
|
8
|
+
|
9
|
+
describe '#table' do
|
10
|
+
specify { expect(unit.table.is_a?(Table)).to eq(true) }
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#x' do
|
14
|
+
specify { expect(unit.x).to eq(0) }
|
15
|
+
end
|
16
|
+
|
17
|
+
describe '#y' do
|
18
|
+
specify { expect(unit.y).to eq(1) }
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '#object' do
|
22
|
+
let!(:object) { "I'm an object" }
|
23
|
+
|
24
|
+
before { unit.hold!(object) }
|
25
|
+
|
26
|
+
specify { expect(unit.object).to eq(object) }
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '#available?' do
|
30
|
+
context 'when object is not present' do
|
31
|
+
specify { expect(unit).to be_available }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe '#hold!' do
|
36
|
+
before { unit.hold!("I'm an object") }
|
37
|
+
|
38
|
+
specify { expect(unit.object).to eq("I'm an object") }
|
39
|
+
end
|
40
|
+
|
41
|
+
describe '#release!' do
|
42
|
+
before do
|
43
|
+
unit.hold!("I'm an object")
|
44
|
+
unit.release!
|
45
|
+
end
|
46
|
+
|
47
|
+
specify { expect(unit.object).to be_nil }
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context 'when invalid arguments' do
|
52
|
+
specify do
|
53
|
+
expect { Toy::Unit.new("invalid", 0) }.to \
|
54
|
+
raise_error(Toy::Unit::Error::CoordinateError, 'Coordinates must be of type Fixnum!')
|
55
|
+
end
|
56
|
+
|
57
|
+
specify do
|
58
|
+
expect { Toy::Unit.new(0, "invalid") }.to \
|
59
|
+
raise_error(Toy::Unit::Error::CoordinateError, 'Coordinates must be of type Fixnum!')
|
60
|
+
end
|
61
|
+
|
62
|
+
specify do
|
63
|
+
expect { Toy::Unit.new(0, 0, table=0) }.to \
|
64
|
+
raise_error(Toy::Unit::Error::TableError, 'Table must be of type Table!')
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
data/toy.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require File.expand_path('../lib/toy/version', __FILE__)
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.name = 'toy'
|
7
|
+
gem.version = Toy::VERSION
|
8
|
+
gem.summary = %q{Summary}
|
9
|
+
gem.description = %q{Description}
|
10
|
+
gem.license = 'MIT'
|
11
|
+
gem.authors = ['Ninoslav Milenovic']
|
12
|
+
gem.email = 'coffee@rubyengineer.com'
|
13
|
+
gem.homepage = 'https://github.com/rubyengineer/toy#readme'
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ['lib']
|
19
|
+
|
20
|
+
gem.add_development_dependency 'rdoc', '~> 4.1'
|
21
|
+
gem.add_development_dependency 'rspec', '~> 3.1'
|
22
|
+
gem.add_development_dependency 'rubygems-tasks', '~> 0.2'
|
23
|
+
gem.add_development_dependency 'codeclimate-test-reporter'
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: toy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ninoslav Milenovic
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rdoc
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.1'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.1'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rubygems-tasks
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.2'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.2'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: codeclimate-test-reporter
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Description
|
70
|
+
email: coffee@rubyengineer.com
|
71
|
+
executables:
|
72
|
+
- toy
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".document"
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rspec"
|
79
|
+
- ".travis.yml"
|
80
|
+
- ChangeLog.md
|
81
|
+
- Gemfile
|
82
|
+
- Gemfile.lock
|
83
|
+
- LICENSE.txt
|
84
|
+
- README.md
|
85
|
+
- Rakefile
|
86
|
+
- bin/toy
|
87
|
+
- lib/toy.rb
|
88
|
+
- lib/toy/controller.rb
|
89
|
+
- lib/toy/direction.rb
|
90
|
+
- lib/toy/robot.rb
|
91
|
+
- lib/toy/table.rb
|
92
|
+
- lib/toy/unit.rb
|
93
|
+
- lib/toy/unit_collection.rb
|
94
|
+
- lib/toy/version.rb
|
95
|
+
- spec/controller_spec.rb
|
96
|
+
- spec/direction_spec.rb
|
97
|
+
- spec/robot_spec.rb
|
98
|
+
- spec/spec_helper.rb
|
99
|
+
- spec/table_spec.rb
|
100
|
+
- spec/toy_spec.rb
|
101
|
+
- spec/unit_collection_spec.rb
|
102
|
+
- spec/unit_spec.rb
|
103
|
+
- toy.gemspec
|
104
|
+
homepage: https://github.com/rubyengineer/toy#readme
|
105
|
+
licenses:
|
106
|
+
- MIT
|
107
|
+
metadata: {}
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 2.2.2
|
125
|
+
signing_key:
|
126
|
+
specification_version: 4
|
127
|
+
summary: Summary
|
128
|
+
test_files:
|
129
|
+
- spec/controller_spec.rb
|
130
|
+
- spec/direction_spec.rb
|
131
|
+
- spec/robot_spec.rb
|
132
|
+
- spec/spec_helper.rb
|
133
|
+
- spec/table_spec.rb
|
134
|
+
- spec/toy_spec.rb
|
135
|
+
- spec/unit_collection_spec.rb
|
136
|
+
- spec/unit_spec.rb
|