maux_robot 0.0.5 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fcae61d13656d506d2d2b851f744ad89ab52019b
4
- data.tar.gz: 55ccb6a55a0ecac20ed9a32ed4b4894e6396d928
3
+ metadata.gz: 961e2b5489e001d3676b9a8f4360b212b62e677b
4
+ data.tar.gz: '028a03a16e37befedecb9336b8d1a5f6c3cd78e1'
5
5
  SHA512:
6
- metadata.gz: c4bcf5f0d29bc5e2e06534f26d3ad31275829bb9f310c98a70fba8c6b2ee6081999c884c2a91fb5dee7bf8dbf8b2a33a1b04436319a64a21b9bb952bf8ff3b9e
7
- data.tar.gz: c0e40e5920f39688a15e5018d7545fd6a81e6639791bd0394e182b2390b35198d68bd53089f180bd7aa990f390910d292f7f4554a33a64a3b10d2e7b71e3c9f4
6
+ metadata.gz: 1676475a44d162ae4038dc3862e380f81527caf621e3ea74e942ce3d431553476635373cffe05e9f4e468afea18ce5035e2c7d297bbfd3d57d645bf6fb1def11
7
+ data.tar.gz: ff3e5c81d4c9d4d348b90d7e99e76e683fb14064c2bf4431203498b479de0e60a4d1c2da186a4e4b59f63590e2868c4c1d25e0a63d6bff37dd6abb5ebb591819
data/README.md CHANGED
@@ -24,7 +24,8 @@ PLACE X,Y,F
24
24
  MOVE
25
25
  LEFT
26
26
  RIGHT
27
- REPORT
27
+ REPORT [csv|json]
28
+ VERBOSE
28
29
  ```
29
30
 
30
31
  * `PLACE` will put the toy robot on the table in position X,Y
@@ -40,7 +41,8 @@ in the direction it is currently facing.
40
41
  * `LEFT` and `RIGHT` will rotate the robot 90 degrees
41
42
  in the specified direction
42
43
  without changing the position of the robot.
43
- * `REPORT` will announce the X,Y and F of the robot.
44
+ * `REPORT` will announce the X,Y and F of the robot. If `csv` or `json` is passed, it formats the whereabouts accordingly. The default format is `csv`.
45
+ * `VERBOSE` will turn the error messages (talkative mode) on or off. The robot starts with talkative mode set to off.
44
46
 
45
47
  * A robot that is not on the table ignores the `MOVE`, `LEFT`, `RIGHT` and `REPORT` commands.
46
48
 
@@ -17,7 +17,11 @@ module MauxRobot
17
17
  def run_script(all_input)
18
18
  all_input.split("\n").each do |line_input|
19
19
  command = parse(line_input)
20
- execute(command) if command
20
+ begin
21
+ execute(command) if command
22
+ rescue MauxRobot::RobotError => e
23
+ puts e if @robot.talkative
24
+ end
21
25
  end
22
26
  end
23
27
 
@@ -46,7 +46,7 @@ module MauxRobot
46
46
  Position.new(x, y, @face)
47
47
  end
48
48
 
49
- def report(format_type: :csv)
49
+ def report(format_type=:csv)
50
50
  formatter = MauxRobot::Formatter.from(format_type)
51
51
  puts formatter.generate(self)
52
52
  end
@@ -4,31 +4,89 @@ require 'forwardable'
4
4
 
5
5
  module MauxRobot
6
6
 
7
+ class RobotError < StandardError; end
8
+
9
+ # Error to notify that the robot was not placed yet
10
+ class RobotNotPlacedYet < RobotError
11
+ def initialize
12
+ super('The robot was not placed yet!')
13
+ end
14
+ end
15
+
16
+ # Error to notify that the position is/would be outside the table
17
+ class NotOkToGo < RobotError
18
+ def initialize(position)
19
+ super("It's not ok to go to #{position.x},#{position.y}")
20
+ end
21
+ end
22
+
7
23
  # The main class. It executes the actions using its table and position
8
24
  class Robot
9
- attr_reader :position
10
-
11
- extend Forwardable
12
- def_delegators :@position, :left, :right, :report
25
+ attr_reader :position, :talkative
13
26
 
14
27
  def initialize(table=MauxRobot::Table.new)
15
28
  @table = table
16
29
  @position = MauxRobot::NullPosition.new
30
+ @talkative = false
17
31
  end
18
32
 
19
33
  def place(x:, y:, face:)
20
34
  position = MauxRobot::Position.new(x, y, face)
21
35
 
22
- if position.valid_direction? && @table.contains?(position)
36
+ ok_to_go?(position) do
23
37
  @position = position
24
38
  end
25
39
  end
26
40
 
27
41
  def move
28
- next_position = @position.forward_position
42
+ robot_placed? do
43
+ next_position = @position.forward_position
44
+
45
+ ok_to_go?(next_position) do
46
+ @position = next_position
47
+ end
48
+ end
49
+ end
50
+
51
+ def left
52
+ robot_placed? do
53
+ @position.left
54
+ end
55
+ end
56
+
57
+ def right
58
+ robot_placed? do
59
+ @position.right
60
+ end
61
+ end
62
+
63
+ def report(format_type: :csv)
64
+ robot_placed? do
65
+ @position.report(format_type)
66
+ end
67
+ end
68
+
69
+ def verbose
70
+ @talkative = !@talkative
71
+ is_not = @talkative ? 'is' : 'is not'
72
+ puts "Robot #{is_not} talkative now."
73
+ end
74
+
75
+ private
76
+
77
+ def ok_to_go?(position)
78
+ if @table.contains?(position)
79
+ yield
80
+ else
81
+ raise NotOkToGo.new(position)
82
+ end
83
+ end
29
84
 
30
- if @table.contains?(next_position)
31
- @position = next_position
85
+ def robot_placed?
86
+ unless @position.class == MauxRobot::NullPosition
87
+ yield
88
+ else
89
+ raise RobotNotPlacedYet
32
90
  end
33
91
  end
34
92
  end
@@ -10,7 +10,8 @@ module MauxRobot
10
10
  end
11
11
 
12
12
  def contains?(position)
13
- position.x.between?(@x[0], @x[1]) &&
13
+ position.valid_direction? &&
14
+ position.x.between?(@x[0], @x[1]) &&
14
15
  position.y.between?(@y[0], @y[1])
15
16
  end
16
17
  end
@@ -3,7 +3,7 @@
3
3
  module MauxRobot
4
4
  # This module holds the MauxRobot version information.
5
5
  module Version
6
- STRING = '0.0.5'.freeze
6
+ STRING = '0.1.0'.freeze
7
7
 
8
8
  MSG = '%s (using Parser %s, running on %s %s %s)'.freeze
9
9
 
@@ -14,28 +14,22 @@ describe MauxRobot::Robot do
14
14
  end
15
15
 
16
16
  context 'given an invalid place' do
17
- it 'should not have a position' do
18
- robot.place(x: 5, y: 0, face: :south)
19
-
20
- expect(robot.position).to eq null_position
17
+ it 'should raise NotOkToGo error' do
18
+ expect { robot.place(x: 5, y: 0, face: :south) }.to raise_error MauxRobot::NotOkToGo
21
19
  end
22
20
  end
23
21
 
24
22
  context 'given an invalid direction' do
25
- it 'should not have a position' do
26
- robot.place(x: 0, y: 2, face: :south_east)
27
-
28
- expect(robot.position).to eq null_position
23
+ it 'should raise NotOkToGo error' do
24
+ expect { robot.place(x: 0, y: 2, face: :south_east) }.to raise_error MauxRobot::NotOkToGo
29
25
  end
30
26
  end
31
27
  end
32
28
 
33
29
  describe '#left' do
34
30
  context 'robot not placed' do
35
- it 'should silently ignore' do
36
- robot.place(x: 0, y: 2, face: :south_east)
37
- robot.left
38
-
31
+ it 'should raise RobotNotPlacedYet error' do
32
+ expect { robot.left }.to raise_error MauxRobot::RobotNotPlacedYet
39
33
  expect(robot.position).to eq null_position
40
34
  end
41
35
  end
@@ -52,9 +46,8 @@ describe MauxRobot::Robot do
52
46
 
53
47
  describe '#right' do
54
48
  context 'robot not placed' do
55
- it 'should silently ignore' do
56
- robot.place(x: 0, y: 2, face: :north_west)
57
- robot.right
49
+ it 'should raise RobotNotPlacedYet error' do
50
+ expect { robot.right }.to raise_error MauxRobot::RobotNotPlacedYet
58
51
 
59
52
  expect(robot.position).to eq null_position
60
53
  end
@@ -72,9 +65,8 @@ describe MauxRobot::Robot do
72
65
 
73
66
  describe '#move' do
74
67
  context 'robot not placed' do
75
- it 'should silently ignore' do
76
- robot.move
77
-
68
+ it 'should raise RobotNotPlacedYet error' do
69
+ expect { robot.right }.to raise_error MauxRobot::RobotNotPlacedYet
78
70
  expect(robot.position).to eq null_position
79
71
  end
80
72
  end
@@ -93,7 +85,8 @@ describe MauxRobot::Robot do
93
85
  context 'to outside the table' do
94
86
  it 'should stay where it is' do
95
87
  robot.place(x: 0, y: 0, face: :south)
96
- robot.move
88
+
89
+ expect { robot.move }.to raise_error MauxRobot::NotOkToGo
97
90
 
98
91
  expect(robot.position.x).to eq(0)
99
92
  expect(robot.position.y).to eq(0)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: maux_robot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mauricio Vieira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-27 00:00:00.000000000 Z
11
+ date: 2017-09-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec