maux_robot 0.0.3 → 0.0.4

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: 776efb576b9d292a6d18f2ab3428c45b223399a3
4
- data.tar.gz: 8f65894e13ff0bb52a95a61b0654d58200a53cb8
3
+ metadata.gz: 9bf2d0fed313bcc6043144745aa33467fa7c8ab0
4
+ data.tar.gz: a63febfee434feda427515e282367fb974b223c7
5
5
  SHA512:
6
- metadata.gz: 20aef8da93c3cb1f28378607a95020a8201fef566adc14522432d3a1ab0723b7c597dab066bc57c2359a51d83ea9785cd1bb28602d32b95418b5b16b122bd130
7
- data.tar.gz: d09ae6bb62e2bdbc5d9b2f5f110ae78d0ebc0d86b66c8371d42984dea9823e138cc0eff556cd0b94fd04cb6f030e9079fbd0d3a3afded3ca5b6d5e8ad70582c7
6
+ metadata.gz: f04f82e9a2914420a34898662e02231d4f744bfc6035e51302b091af5058eba85c74beaf32bcb69d0954643494cf9624571ee6ae68cbec58bcb437e75a514213
7
+ data.tar.gz: 0304f7d1f1110a3ca0b048bce64dc59bbd87087903345c3e97696fe0b42052dcc72d93349084ca66413b319f32514f1b89b10cec330f1bfd40dd59acc3cc7b76
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ [![Build Status](https://travis-ci.org/mauriciovieira/maux_robot.svg?branch=master)](https://travis-ci.org/mauriciovieira/maux_robot)
2
+
1
3
  # maux_robot
2
4
 
3
5
  Maux version of a Toy Robot Simulator in Ruby
@@ -4,8 +4,6 @@ module MauxRobot
4
4
 
5
5
  # The CLI is a class responsible of handling command line interface
6
6
  class CLI
7
- ALLOWED_ORDERS = %i[left right move place report].freeze
8
-
9
7
  attr_reader :robot
10
8
  def initialize
11
9
  @robot = Robot.new
@@ -24,38 +22,15 @@ module MauxRobot
24
22
  end
25
23
 
26
24
  def parse(line_input)
27
- clean_input = line_input.strip.squeeze(' ').split(' ')
28
- order = sanitize_order(clean_input.shift)
29
- return unless order
30
-
31
- command = { order: order }
32
-
33
- if clean_input.any?
34
- arguments = sanitize_arguments(clean_input.join(''))
35
- command[:arguments] = arguments
36
- end
37
-
38
- command
25
+ Parser.new.parse(line_input)
39
26
  end
40
27
 
41
28
  def execute(command)
42
- if command[:arguments].nil?
43
- @robot.send(command[:order])
44
- else
29
+ if command.has_key?(:arguments)
45
30
  @robot.send(command[:order], command[:arguments])
31
+ else
32
+ @robot.send(command[:order])
46
33
  end
47
34
  end
48
-
49
- private
50
-
51
- def sanitize_order(input_string)
52
- order = input_string.downcase.to_sym
53
- order if ALLOWED_ORDERS.include?(order)
54
- end
55
-
56
- def sanitize_arguments(arguments)
57
- arguments = arguments.delete(' ').split(',')
58
- { x: arguments[0], y: arguments[1], face: arguments[2] }
59
- end
60
35
  end
61
36
  end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MauxRobot
4
+
5
+ class Parser
6
+ ALLOWED_ORDERS = %i[left right move place report verbose].freeze
7
+
8
+ def parse(line_input)
9
+ clean_input = line_input.strip.squeeze(' ').split(' ')
10
+ order = sanitize_order(clean_input.shift)
11
+ return unless order
12
+
13
+ command = { order: order }
14
+
15
+ if clean_input.any?
16
+ arguments = sanitize_arguments(order, clean_input.join(''))
17
+ command[:arguments] = arguments
18
+ end
19
+
20
+ command
21
+ end
22
+
23
+
24
+ private
25
+
26
+ def sanitize_order(input_string)
27
+ order = input_string.downcase.to_sym
28
+ order if ALLOWED_ORDERS.include?(order)
29
+ end
30
+
31
+ def sanitize_arguments(order, arguments)
32
+ case order
33
+ when :place
34
+ arguments = arguments.delete(' ').split(',')
35
+ { x: arguments[0], y: arguments[1], face: arguments[2] }
36
+ when :report
37
+ { format: arguments.downcase.to_sym }
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+
44
+
@@ -26,17 +26,21 @@ module MauxRobot
26
26
  POSSIBLE_DIRECTIONS.include?(@face)
27
27
  end
28
28
 
29
- def turn_left
29
+ def left
30
+ return self unless valid_direction?
30
31
  next_direction_index = ( POSSIBLE_DIRECTIONS.index(@face) + 1 ) % 4
31
32
  @face = POSSIBLE_DIRECTIONS[next_direction_index]
32
33
  end
33
34
 
34
- def turn_right
35
+ def right
36
+ return self unless valid_direction?
35
37
  next_direction_index = ( POSSIBLE_DIRECTIONS.index(@face) - 1 )
36
38
  @face = POSSIBLE_DIRECTIONS[next_direction_index]
37
39
  end
38
40
 
39
41
  def forward_position
42
+ return self unless valid_direction?
43
+
40
44
  x = @x + POSSIBLE_MOVEMENTS[@face][:x]
41
45
  y = @y + POSSIBLE_MOVEMENTS[@face][:y]
42
46
  Position.new(x, y, @face)
@@ -45,5 +49,15 @@ module MauxRobot
45
49
  def to_s
46
50
  "#{@x},#{@y},#{@face.upcase.to_s}"
47
51
  end
52
+
53
+ def ==(other)
54
+ other.x == x && other.y == y && other.face == face
55
+ end
56
+ end
57
+
58
+ class NullPosition < Position
59
+ def initialize
60
+ super(nil, nil, nil)
61
+ end
48
62
  end
49
63
  end
@@ -1,31 +1,29 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'forwardable'
4
+
3
5
  module MauxRobot
4
6
 
5
7
  # The main class. It executes the actions using its table and position
6
8
  class Robot
7
9
  attr_reader :position
8
10
 
11
+ extend Forwardable
12
+ def_delegators :@position, :left, :right
13
+
9
14
  def initialize(table=MauxRobot::Table.new)
10
15
  @table = table
16
+ @position = MauxRobot::NullPosition.new
11
17
  end
12
18
 
13
19
  def place(x:, y:, face:)
14
20
  position = MauxRobot::Position.new(x, y, face)
15
21
 
16
- if position.valid_direction? and @table.contains?(position)
22
+ if position.valid_direction? && @table.contains?(position)
17
23
  @position = position
18
24
  end
19
25
  end
20
26
 
21
- def left
22
- @position&.turn_left
23
- end
24
-
25
- def right
26
- @position&.turn_right
27
- end
28
-
29
27
  def move
30
28
  next_position = @position.forward_position
31
29
 
@@ -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.3'.freeze
6
+ STRING = '0.0.4'.freeze
7
7
 
8
8
  MSG = '%s (using Parser %s, running on %s %s %s)'.freeze
9
9
 
data/lib/maux_robot.rb CHANGED
@@ -1,5 +1,9 @@
1
1
  require 'maux_robot/version'
2
2
  require 'maux_robot/cli'
3
+ require 'maux_robot/parser'
3
4
  require 'maux_robot/position'
4
5
  require 'maux_robot/table'
5
- require 'maux_robot/robot'
6
+ require 'maux_robot/robot'
7
+
8
+ module MauxRobot
9
+ end
@@ -3,66 +3,17 @@
3
3
  require 'maux_robot'
4
4
 
5
5
  describe MauxRobot::CLI do
6
- describe '#parse' do
7
- context 'valid orders' do
8
- it '#report' do
9
- expect(subject.parse('REPORT')).to eq(order: :report)
10
- end
11
-
12
- it '#move' do
13
- expect(subject.parse('MOVE')).to eq(order: :move)
14
- end
15
-
16
- it '#left' do
17
- expect(subject.parse('LEFT')).to eq(order: :left)
18
- end
19
-
20
- it '#right' do
21
- expect(subject.parse('RIGHT')).to eq(order: :right)
22
- end
23
-
24
- it '#place' do
25
- expect(subject.parse('PLACE 0,3,WEST')).to eq(order: :place, arguments: { x: '0', y: '3', face: 'WEST' })
26
- end
27
- end
28
-
29
- context 'orders with spaces and downcase' do
30
- it '#report' do
31
- expect(subject.parse(' repoRT')).to eq(order: :report)
32
- end
33
-
34
- it '#move' do
35
- expect(subject.parse('move ')).to eq(order: :move)
36
- end
37
-
38
- it '#left' do
39
- expect(subject.parse(' LefT ')).to eq(order: :left)
40
- end
41
-
42
- it '#right' do
43
- expect(subject.parse(' rigHt')).to eq(order: :right)
44
- end
45
-
46
- it '#place' do
47
- expect(subject.parse('PLACE 2 , 1 , NOrth')).to eq(order: :place, arguments: { x: '2', y: '1', face: 'NOrth' })
48
- end
49
- end
50
-
51
- it 'ignores anything else' do
52
- expect(subject.parse('blablabla balbla')).to be_nil
53
- end
54
- end
55
6
 
56
7
  describe '#execute' do
57
- context 'commands without arguments' do
58
- it 'sends proper message to robot' do
8
+ context 'parsed input without arguments' do
9
+ it 'should send proper message to robot' do
59
10
  expect(subject.robot).to receive(:move)
60
11
  subject.execute(order: :move)
61
12
  end
62
13
  end
63
14
 
64
- context 'place' do
65
- it 'send place command along with arguments' do
15
+ context 'parsed place command with arguments' do
16
+ it 'should send proper message to robot' do
66
17
  expect(subject.robot).to receive(:place).with(x: '0', y: '2', face: 'EAST')
67
18
  subject.execute(order: :place, arguments: { x: '0', y: '2', face: 'EAST' })
68
19
  end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'maux_robot'
4
+
5
+ describe MauxRobot::Parser do
6
+ describe '#parse' do
7
+ context 'valid orders' do
8
+ it '#report' do
9
+ expect(subject.parse('REPORT')).to eq(order: :report)
10
+ end
11
+
12
+ it '#report csv' do
13
+ expect(subject.parse('REPORT CSV')).to eq(order: :report, arguments: { format: :csv })
14
+ end
15
+
16
+ it '#report json' do
17
+ expect(subject.parse('REPORT JSON')).to eq(order: :report, arguments: { format: :json })
18
+ end
19
+
20
+ it '#move' do
21
+ expect(subject.parse('MOVE')).to eq(order: :move)
22
+ end
23
+
24
+ it '#left' do
25
+ expect(subject.parse('LEFT')).to eq(order: :left)
26
+ end
27
+
28
+ it '#right' do
29
+ expect(subject.parse('RIGHT')).to eq(order: :right)
30
+ end
31
+
32
+ it '#verbose' do
33
+ expect(subject.parse('VERBOSE')).to eq(order: :verbose)
34
+ end
35
+
36
+ it '#place' do
37
+ expect(subject.parse('PLACE 0,3,WEST')).to eq(order: :place, arguments: { x: '0', y: '3', face: 'WEST' })
38
+ end
39
+ end
40
+
41
+ context 'orders with spaces and downcase' do
42
+ it '#report' do
43
+ expect(subject.parse(' repoRT')).to eq(order: :report)
44
+ end
45
+
46
+ it '#move' do
47
+ expect(subject.parse('move ')).to eq(order: :move)
48
+ end
49
+
50
+ it '#left' do
51
+ expect(subject.parse(' LefT ')).to eq(order: :left)
52
+ end
53
+
54
+ it '#right' do
55
+ expect(subject.parse(' rigHt')).to eq(order: :right)
56
+ end
57
+
58
+ it '#place' do
59
+ expect(subject.parse('PLACE 2 , 1 , NOrth')).to eq(order: :place, arguments: { x: '2', y: '1', face: 'NOrth' })
60
+ end
61
+ end
62
+
63
+ context 'invalid orders' do
64
+ it 'ignores anything else' do
65
+ expect(subject.parse('blablabla balbla')).to be_nil
66
+ end
67
+ end
68
+ end
69
+ end
@@ -36,34 +36,34 @@ describe MauxRobot::Position do
36
36
  describe 'rotation' do
37
37
  let(:position) { MauxRobot::Position.new(2, 2, :north) }
38
38
 
39
- context '#turn_left' do
39
+ context '#left' do
40
40
  it 'should rotate counter-clockwise' do
41
- position.turn_left
41
+ position.left
42
42
  expect(position.face).to eq(:west)
43
43
 
44
- position.turn_left
44
+ position.left
45
45
  expect(position.face).to eq(:south)
46
46
 
47
- position.turn_left
47
+ position.left
48
48
  expect(position.face).to eq(:east)
49
49
 
50
- position.turn_left
50
+ position.left
51
51
  expect(position.face).to eq(:north)
52
52
  end
53
53
  end
54
54
 
55
- context '#turn_right' do
55
+ context '#right' do
56
56
  it 'should rotate clockwise' do
57
- position.turn_right
57
+ position.right
58
58
  expect(position.face).to eq(:east)
59
59
 
60
- position.turn_right
60
+ position.right
61
61
  expect(position.face).to eq(:south)
62
62
 
63
- position.turn_right
63
+ position.right
64
64
  expect(position.face).to eq(:west)
65
65
 
66
- position.turn_right
66
+ position.right
67
67
  expect(position.face).to eq(:north)
68
68
  end
69
69
  end
@@ -4,13 +4,14 @@ require 'maux_robot'
4
4
 
5
5
  describe MauxRobot::Robot do
6
6
  subject(:robot) { described_class.new }
7
+ let(:null_position) { MauxRobot::NullPosition.new }
7
8
 
8
9
  describe '#place' do
9
10
  context 'given a valid place' do
10
11
  it 'should have a position' do
11
12
  robot.place(x: 0, y: 0, face: :north)
12
13
 
13
- expect(robot.position).not_to be_nil
14
+ expect(robot.position).not_to eq null_position
14
15
  end
15
16
  end
16
17
 
@@ -18,7 +19,7 @@ describe MauxRobot::Robot do
18
19
  it 'should not have a position' do
19
20
  robot.place(x: 5, y: 0, face: :south)
20
21
 
21
- expect(robot.position).to be_nil
22
+ expect(robot.position).to eq null_position
22
23
  end
23
24
  end
24
25
 
@@ -26,7 +27,7 @@ describe MauxRobot::Robot do
26
27
  it 'should not have a position' do
27
28
  robot.place(x: 0, y: 2, face: :south_east)
28
29
 
29
- expect(robot.position).to be_nil
30
+ expect(robot.position).to eq null_position
30
31
  end
31
32
  end
32
33
  end
@@ -37,7 +38,7 @@ describe MauxRobot::Robot do
37
38
  robot.place(x: 0, y: 2, face: :south_east)
38
39
  robot.left
39
40
 
40
- expect(robot.position).to be_nil
41
+ expect(robot.position).to eq null_position
41
42
  end
42
43
  end
43
44
 
@@ -57,7 +58,7 @@ describe MauxRobot::Robot do
57
58
  robot.place(x: 0, y: 2, face: :north_west)
58
59
  robot.right
59
60
 
60
- expect(robot.position).to be_nil
61
+ expect(robot.position).to eq null_position
61
62
  end
62
63
  end
63
64
 
@@ -72,6 +73,14 @@ describe MauxRobot::Robot do
72
73
  end
73
74
 
74
75
  describe '#move' do
76
+ context 'robot not placed' do
77
+ it 'should silently ignore' do
78
+ robot.move
79
+
80
+ expect(robot.position).to eq null_position
81
+ end
82
+ end
83
+
75
84
  context 'to a place on the table' do
76
85
  it 'should update its position' do
77
86
  robot.place(x: 3, y: 2, face: :north)
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.3
4
+ version: 0.0.4
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-19 00:00:00.000000000 Z
11
+ date: 2017-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -44,11 +44,13 @@ files:
44
44
  - bin/maux_robot
45
45
  - lib/maux_robot.rb
46
46
  - lib/maux_robot/cli.rb
47
+ - lib/maux_robot/parser.rb
47
48
  - lib/maux_robot/position.rb
48
49
  - lib/maux_robot/robot.rb
49
50
  - lib/maux_robot/table.rb
50
51
  - lib/maux_robot/version.rb
51
52
  - spec/maux_robot/cli_spec.rb
53
+ - spec/maux_robot/parser_spec.rb
52
54
  - spec/maux_robot/position_spec.rb
53
55
  - spec/maux_robot/robot_spec.rb
54
56
  - spec/maux_robot/table_spec.rb