toy_robot 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe ToyRobot::Roboto::Speech do
4
+
5
+ describe '#speak' do
6
+
7
+ it 'prints a message' do
8
+ expect { subject.speak('Hello sir!') }.to match_stdout('Hello sir!')
9
+ end
10
+
11
+ end
12
+
13
+ end
@@ -0,0 +1,216 @@
1
+ require 'spec_helper'
2
+
3
+ describe ToyRobot::Roboto do
4
+
5
+ it 'has a brain' do
6
+ expect(subject.brain).to be_an_instance_of(ToyRobot::Roboto::Brain)
7
+ end
8
+
9
+ it 'delegates speak to brain' do
10
+ expect(subject.brain).to receive(:speak).with('something')
11
+ subject.speak('something')
12
+ end
13
+
14
+ it 'delegates location to brain as motor_cortex' do
15
+ expect(subject.brain).to receive(:motor_cortex)
16
+ subject.location
17
+ end
18
+
19
+ it 'has a name' do
20
+ subject.name = 'slave roboto'
21
+ expect(subject.name).to eq 'slave roboto'
22
+ end
23
+
24
+ describe '#execute_order' do
25
+
26
+ context 'without arguments' do
27
+
28
+ it 'sends method' do
29
+ expect(subject).to receive(:report)
30
+ subject.execute_order(command: :report)
31
+ end
32
+
33
+ end
34
+
35
+ context 'with arguments' do
36
+
37
+ it 'sends method with arguments' do
38
+ expect(subject).to receive(:place).with(x: 1, y: 2, f: :north)
39
+ subject.execute_order(command: :place, args: { x: 1, y: 2, f: :north })
40
+ end
41
+
42
+ end
43
+
44
+ end
45
+
46
+ describe '#report' do
47
+
48
+ context 'when roboto is not placed yet' do
49
+
50
+ it 'advises' do
51
+ expect(subject.brain).to receive(:speak).with('Roboto not placed yet, sir!')
52
+ subject.report
53
+ end
54
+
55
+ end
56
+
57
+ context 'when roboto is placed' do
58
+
59
+ before do
60
+ allow(subject).to receive(:location).and_return(double(to_s: 'X: 1, Y: 2, F: north'))
61
+ end
62
+
63
+ it 'reports its position based on location' do
64
+ expect(subject.brain).to receive(:speak).with('X: 1, Y: 2, F: north')
65
+ subject.report
66
+ end
67
+
68
+ end
69
+
70
+ end
71
+
72
+ describe '#place' do
73
+
74
+ context 'when tries to place into an unsafe position' do
75
+
76
+ before do
77
+ expect(subject).to receive(:to_safe_location?).and_return(false)
78
+ end
79
+
80
+ it 'advises about danger' do
81
+ expect(subject.brain).to receive(:speak).with('No can do, sir! If Roboto move Roboto fall.')
82
+ subject.place(x: 1, y: 2, f: :north)
83
+ end
84
+
85
+ end
86
+
87
+ context 'when tries to place into a safe position' do
88
+
89
+ before do
90
+ expect(subject).to receive(:to_safe_location?).and_return(true)
91
+ end
92
+
93
+ it 'successfully changes roboto location' do
94
+ expect(subject.location).to receive(:reset)
95
+ subject.place(x: 1, y: 2, f: :north)
96
+ end
97
+
98
+ end
99
+
100
+ end
101
+
102
+ describe '#move' do
103
+
104
+ context 'when roboto is not placed yet' do
105
+
106
+ it 'advises' do
107
+ expect(subject.brain).to receive(:speak).with('Roboto not placed yet, sir!')
108
+ subject.move
109
+ end
110
+
111
+ end
112
+
113
+ context 'when roboto is placed' do
114
+
115
+ context 'trying to move to a safe position' do
116
+
117
+ before do
118
+ expect(subject).to receive(:to_safe_location?).twice.and_return(true)
119
+ subject.place(x: 1, y: 1, f: :north)
120
+ end
121
+
122
+ it 'moves one unit forward' do
123
+ subject.move
124
+ expect(subject.location.x).to eq 1
125
+ expect(subject.location.y).to eq 2
126
+ end
127
+
128
+ end
129
+
130
+ context 'trying to move to an unsafe position' do
131
+
132
+ before do
133
+ expect(subject).to receive(:to_safe_location?).and_return(true)
134
+ subject.place(x: 1, y: 1, f: :north)
135
+ end
136
+
137
+ it 'advises about danger' do
138
+ expect(subject).to receive(:to_safe_location?).and_return(false)
139
+ expect(subject.brain).to receive(:speak).with('No can do, sir! If Roboto move Roboto fall.')
140
+ subject.place(x: 1, y: 2, f: :north)
141
+ end
142
+
143
+ end
144
+
145
+ end
146
+
147
+ end
148
+
149
+ describe '#left' do
150
+
151
+ context 'when roboto is not placed yet' do
152
+
153
+ it 'advises' do
154
+ expect(subject.brain).to receive(:speak).with('Roboto not placed yet, sir!')
155
+ subject.left
156
+ end
157
+
158
+ end
159
+
160
+ context 'when roboto is placed' do
161
+
162
+ before do
163
+ expect(subject).to receive(:to_safe_location?).and_return(true)
164
+ subject.place(x: 1, y: 1, f: :north)
165
+ end
166
+
167
+ it 'rotates 90 degrees to the left' do
168
+ subject.left
169
+ expect(subject.location.f).to eq :west
170
+ end
171
+
172
+ end
173
+
174
+ end
175
+
176
+ describe '#right' do
177
+
178
+ context 'when roboto is not placed yet' do
179
+
180
+ it 'advises' do
181
+ expect(subject.brain).to receive(:speak).with('Roboto not placed yet, sir!')
182
+ subject.right
183
+ end
184
+
185
+ end
186
+
187
+ context 'when roboto is placed' do
188
+
189
+ before do
190
+ expect(subject).to receive(:to_safe_location?).and_return(true)
191
+ subject.place(x: 1, y: 1, f: :north)
192
+ end
193
+
194
+ it 'rotates 90 degrees to the right' do
195
+ subject.right
196
+ expect(subject.location.f).to eq :east
197
+ end
198
+
199
+ end
200
+
201
+ end
202
+
203
+ describe '#placed?' do
204
+
205
+ it 'returns true if roboto is placed' do
206
+ expect(subject.location).to receive(:nil?).and_return(false)
207
+ expect(subject.placed?).to be_truthy
208
+ end
209
+
210
+ it 'returns false if roboto is not placed' do
211
+ expect(subject.placed?).to be_falsy
212
+ end
213
+
214
+ end
215
+
216
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe ToyRobot::Simulator do
4
+
5
+ it 'has a table' do
6
+ expect(subject.table).to be_an_instance_of(ToyRobot::Table)
7
+ end
8
+
9
+ it 'has a default 5x5 table' do
10
+ expect(subject.table.x).to eq 5
11
+ expect(subject.table.y).to eq 5
12
+ end
13
+
14
+ it 'allows custom table' do
15
+ simulator = ToyRobot::Simulator.new(table_size: { x: 2, y: 3 } )
16
+ expect(simulator.table.x).to eq 2
17
+ expect(simulator.table.y).to eq 3
18
+ end
19
+
20
+ it 'has a roboto' do
21
+ expect(subject.roboto).to be_an_instance_of(ToyRobot::Roboto)
22
+ end
23
+
24
+ it 'makes sure roboto is aware of table' do
25
+ expect(subject.roboto.table_edges).to be_a(Hash)
26
+ end
27
+
28
+ describe '#run' do
29
+
30
+ it 'execute orders' do
31
+ subject.run(['MOVE', 'PLACE 1,2,SOUTH', 'MOVE', 'REPORT', 'LEFT', 'MOVE', 'RIGHT'])
32
+ expect { subject.roboto.report }.to match_stdout('X: 2, Y: 1, F: south')
33
+ end
34
+
35
+ end
36
+
37
+ end
@@ -0,0 +1,52 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
4
+ require_relative '../lib/toy_robot'
5
+
6
+ RSpec.configure do |config|
7
+
8
+ config.filter_run :focus
9
+ config.run_all_when_everything_filtered = true
10
+
11
+ config.order = "random"
12
+
13
+ end
14
+
15
+ RSpec::Matchers.define :have_constant do |const|
16
+ match do |owner|
17
+ owner.const_defined?(const)
18
+ end
19
+ end
20
+
21
+ RSpec::Matchers.define :match_stdout do |check|
22
+
23
+ @capture = nil
24
+
25
+ match do |block|
26
+
27
+ begin
28
+ stdout_saved = $stdout
29
+ $stdout = StringIO.new
30
+ block.call
31
+ ensure
32
+ @capture = $stdout
33
+ $stdout = stdout_saved
34
+ end
35
+
36
+ @capture.string.match check
37
+ end
38
+
39
+ failure_message do
40
+ "expected to #{description}"
41
+ end
42
+ failure_message_when_negated do
43
+ "expected not to #{description}"
44
+ end
45
+ description do
46
+ "match [#{check}] on stdout [#{@capture.string}]"
47
+ end
48
+
49
+ def supports_block_expectations?
50
+ true
51
+ end
52
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe ToyRobot::Table do
4
+
5
+ subject { ToyRobot::Table.new(x: 5, y: 3) }
6
+
7
+ describe '#edges' do
8
+
9
+ before do
10
+ expect(subject).to receive(:x_limits).and_return({ min: 0, max: 5})
11
+ expect(subject).to receive(:y_limits).and_return({ min: 1, max: 3})
12
+ end
13
+
14
+ it 'Returns the limits for X and Y axis' do
15
+ expect(subject.edges).to eq({ x: { min: 0, max: 5 }, y: { min: 1, max: 3 } })
16
+ end
17
+
18
+ end
19
+
20
+ describe '#x_limits' do
21
+
22
+ it 'returns the limits for X axis' do
23
+ expect(subject.x_limits).to eq({ min: 0, max: 4 })
24
+ end
25
+
26
+ it '0 is always the min' do
27
+ expect(subject.x_limits[:min]).to eq(0)
28
+ end
29
+
30
+ it 'subtracts one from max' do
31
+ expect(subject.x_limits[:max]).to eq(4)
32
+ end
33
+
34
+ end
35
+
36
+ describe '#y_limits' do
37
+
38
+ it 'returns the limits for Y axis' do
39
+ expect(subject.y_limits).to eq({ min: 0, max: 2 })
40
+ end
41
+
42
+ it '0 is always the min' do
43
+ expect(subject.y_limits[:min]).to eq(0)
44
+ end
45
+
46
+ it 'subtracts one from max' do
47
+ expect(subject.y_limits[:max]).to eq(2)
48
+ end
49
+
50
+ end
51
+
52
+
53
+
54
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'toy_robot/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "toy_robot"
8
+ spec.version = ToyRobot::VERSION
9
+ spec.authors = ["louman"]
10
+ spec.email = ["marcus.v.mansur@gmail.com"]
11
+
12
+ spec.summary = %q{Toy Robot Test}
13
+ spec.description = %q{Toy Robot Test}
14
+ spec.homepage = "https://github.com/louman/toy_robot"
15
+
16
+ spec.files = `git ls-files`.split("\n")
17
+ spec.bindir = "bin"
18
+ spec.executables = ["toy_robot"]
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.9"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.2.0"
24
+ spec.add_development_dependency "simplecov", "~> 0.9.2"
25
+
26
+ spec.add_dependency "terminal-table", "~> 1.4.5"
27
+ spec.add_dependency "asciiart"
28
+ spec.add_dependency "rainbow"
29
+ spec.add_dependency "pry-byebug"
30
+
31
+ end
metadata ADDED
@@ -0,0 +1,187 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: toy_robot
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.4
5
+ platform: ruby
6
+ authors:
7
+ - louman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.2.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.2.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.9.2
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.9.2
69
+ - !ruby/object:Gem::Dependency
70
+ name: terminal-table
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.4.5
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.4.5
83
+ - !ruby/object:Gem::Dependency
84
+ name: asciiart
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rainbow
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry-byebug
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Toy Robot Test
126
+ email:
127
+ - marcus.v.mansur@gmail.com
128
+ executables:
129
+ - toy_robot
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".gitignore"
134
+ - Gemfile
135
+ - LICENSE
136
+ - README.md
137
+ - Rakefile
138
+ - bin/toy_robot
139
+ - lib/toy_robot.rb
140
+ - lib/toy_robot/bin/io_utils.rb
141
+ - lib/toy_robot/bin/runner.rb
142
+ - lib/toy_robot/files/manifest.txt
143
+ - lib/toy_robot/files/roboto.jpg
144
+ - lib/toy_robot/manifest_reader.rb
145
+ - lib/toy_robot/roboto.rb
146
+ - lib/toy_robot/roboto/brain.rb
147
+ - lib/toy_robot/roboto/motor_cortex.rb
148
+ - lib/toy_robot/roboto/speech.rb
149
+ - lib/toy_robot/simulator.rb
150
+ - lib/toy_robot/table.rb
151
+ - lib/toy_robot/version.rb
152
+ - spec/bin/io_utils_spec.rb
153
+ - spec/bin/runner_spec.rb
154
+ - spec/fixtures/manifest_example.txt
155
+ - spec/manifest_reader_spec.rb
156
+ - spec/roboto/brain_spec.rb
157
+ - spec/roboto/motor_cortex_spec.rb
158
+ - spec/roboto/speech_spec.rb
159
+ - spec/roboto_spec.rb
160
+ - spec/simulator_spec.rb
161
+ - spec/spec_helper.rb
162
+ - spec/table_spec.rb
163
+ - toy_robot.gemspec
164
+ homepage: https://github.com/louman/toy_robot
165
+ licenses: []
166
+ metadata: {}
167
+ post_install_message:
168
+ rdoc_options: []
169
+ require_paths:
170
+ - lib
171
+ required_ruby_version: !ruby/object:Gem::Requirement
172
+ requirements:
173
+ - - ">="
174
+ - !ruby/object:Gem::Version
175
+ version: '0'
176
+ required_rubygems_version: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ requirements: []
182
+ rubyforge_project:
183
+ rubygems_version: 2.4.2
184
+ signing_key:
185
+ specification_version: 4
186
+ summary: Toy Robot Test
187
+ test_files: []