toyrobot 0.0.1 → 0.0.2

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.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/CONTRIBUTING.md +2 -2
  3. data/README.md +166 -10
  4. data/lib/toy_robot/application.rb +1 -13
  5. data/lib/toy_robot/board.rb +1 -1
  6. data/lib/toy_robot/command/parser.rb +6 -3
  7. data/lib/toy_robot/command/parser/base.rb +4 -25
  8. data/lib/toy_robot/command/parser/left.rb +12 -0
  9. data/lib/toy_robot/command/parser/move.rb +12 -0
  10. data/lib/toy_robot/command/parser/place.rb +3 -4
  11. data/lib/toy_robot/command/parser/report.rb +12 -0
  12. data/lib/toy_robot/command/parser/right.rb +12 -0
  13. data/lib/toy_robot/controller.rb +32 -0
  14. data/lib/toy_robot/factory.rb +6 -18
  15. data/lib/toy_robot/pose.rb +19 -5
  16. data/lib/toy_robot/robot.rb +11 -15
  17. data/lib/toy_robot/view.rb +10 -12
  18. data/test/board_double.rb +5 -0
  19. data/test/integration/test_factory.rb +2 -2
  20. data/test/integration/test_toy_robot.rb +1 -1
  21. data/test/pose_double.rb +23 -0
  22. data/test/test_board.rb +7 -14
  23. data/test/test_board_double.rb +13 -0
  24. data/test/test_board_interface.rb +15 -0
  25. data/test/{test_command_matcher.rb → test_command_parser.rb} +6 -6
  26. data/test/{test_command_matcher_interface.rb → test_command_parser_interface.rb} +5 -5
  27. data/test/test_command_parser_left.rb +20 -0
  28. data/test/{test_command_matcher_move.rb → test_command_parser_move.rb} +5 -5
  29. data/test/{test_command_matcher_place.rb → test_command_parser_place.rb} +10 -10
  30. data/test/{test_command_matcher_report.rb → test_command_parser_report.rb} +5 -5
  31. data/test/test_command_parser_right.rb +20 -0
  32. data/test/test_controller.rb +64 -0
  33. data/test/test_pose.rb +16 -20
  34. data/test/test_pose_double.rb +15 -0
  35. data/test/test_pose_interface.rb +26 -0
  36. data/test/test_reporter_interface.rb +13 -1
  37. data/test/test_robot.rb +70 -27
  38. data/toyrobot.gemspec +1 -1
  39. metadata +35 -26
  40. data/lib/toy_robot/command/base.rb +0 -5
  41. data/lib/toy_robot/command/token.rb +0 -11
  42. data/lib/toy_robot/placement.rb +0 -34
  43. data/lib/toy_robot/robot_controller.rb +0 -35
  44. data/test/test_command.rb +0 -22
  45. data/test/test_command_matcher_left.rb +0 -20
  46. data/test/test_command_matcher_right.rb +0 -20
  47. data/test/test_placement.rb +0 -94
  48. data/test/test_robot_controller.rb +0 -62
@@ -1,14 +1,14 @@
1
1
  require_relative '../lib/toy_robot/command/parser/place'
2
- require_relative 'test_command_matcher_interface'
2
+ require_relative 'test_command_parser_interface'
3
3
  require 'minitest/autorun'
4
4
 
5
5
  module ToyRobot
6
- class CommandMatcherPlaceTest < MiniTest::Unit::TestCase
7
- include CommandMatcherInterfaceTest
6
+ class CommandParserPlaceTest < MiniTest::Unit::TestCase
7
+ include CommandParserInterfaceTest
8
8
 
9
9
  def setup
10
10
  @object = Command::Parser::Place
11
- @object_token = Command::Token::PLACE
11
+ @object_msg = :place
12
12
  @object_valid_str = 'PLACE 0,0,NORTH'
13
13
  @object_invalid_strs = [
14
14
  'PLACE',
@@ -25,32 +25,32 @@ module ToyRobot
25
25
 
26
26
  def test_that_response_args_know_x
27
27
  assert_equal 123,
28
- @object.build_with_match('PLACE 123,0,NORTH').args.x
28
+ @object.build_with_match('PLACE 123,0,NORTH').args[:x]
29
29
  end
30
30
 
31
31
  def test_that_response_args_know_y
32
32
  assert_equal 123,
33
- @object.build_with_match('PLACE 0,123,NORTH').args.y
33
+ @object.build_with_match('PLACE 0,123,NORTH').args[:y]
34
34
  end
35
35
 
36
36
  def test_that_response_args_knows_north_orientation
37
37
  assert_equal Pose::Orientation::NORTH,
38
- @object.build_with_match('PLACE 0,123,NORTH').args.orientation
38
+ @object.build_with_match('PLACE 0,123,NORTH').args[:orientation]
39
39
  end
40
40
 
41
41
  def test_that_response_args_knows_south_orientation
42
42
  assert_equal Pose::Orientation::SOUTH,
43
- @object.build_with_match('PLACE 0,123,SOUTH').args.orientation
43
+ @object.build_with_match('PLACE 0,123,SOUTH').args[:orientation]
44
44
  end
45
45
 
46
46
  def test_that_response_args_knows_east_orientation
47
47
  assert_equal Pose::Orientation::EAST,
48
- @object.build_with_match('PLACE 0,123,EAST').args.orientation
48
+ @object.build_with_match('PLACE 0,123,EAST').args[:orientation]
49
49
  end
50
50
 
51
51
  def test_that_response_args_knows_west_orientation
52
52
  assert_equal Pose::Orientation::WEST,
53
- @object.build_with_match('PLACE 0,123,WEST').args.orientation
53
+ @object.build_with_match('PLACE 0,123,WEST').args[:orientation]
54
54
  end
55
55
  end
56
56
  end
@@ -1,14 +1,14 @@
1
- require_relative '../lib/toy_robot/command/parser/base'
2
- require_relative 'test_command_matcher_interface'
1
+ require_relative '../lib/toy_robot/command/parser/report'
2
+ require_relative 'test_command_parser_interface'
3
3
  require 'minitest/autorun'
4
4
 
5
5
  module ToyRobot
6
- class CommandMatcherReportTest < MiniTest::Unit::TestCase
7
- include CommandMatcherInterfaceTest
6
+ class CommandParserReportTest < MiniTest::Unit::TestCase
7
+ include CommandParserInterfaceTest
8
8
 
9
9
  def setup
10
10
  @object = Command::Parser::Report
11
- @object_token = Command::Token::REPORT
11
+ @object_msg = :report
12
12
  @object_valid_str = 'REPORT'
13
13
  @object_invalid_strs = [
14
14
  'REPORT ',
@@ -0,0 +1,20 @@
1
+ require_relative '../lib/toy_robot/command/parser/right'
2
+ require_relative 'test_command_parser_interface'
3
+ require 'minitest/autorun'
4
+
5
+ module ToyRobot
6
+ class CommandParserRightTest < MiniTest::Unit::TestCase
7
+ include CommandParserInterfaceTest
8
+
9
+ def setup
10
+ @object = Command::Parser::Right
11
+ @object_msg = :right
12
+ @object_valid_str = 'RIGHT'
13
+ @object_invalid_strs = [
14
+ 'RIGHT ',
15
+ 'RIGHTY',
16
+ 'RIGHT 180'
17
+ ]
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,64 @@
1
+ require_relative '../lib/toy_robot/controller'
2
+ require 'minitest/autorun'
3
+
4
+ module ToyRobot
5
+ class ControllerTest < MiniTest::Unit::TestCase
6
+ def setup
7
+ @robot = MiniTest::Mock.new
8
+ @view = MiniTest::Mock.new
9
+ @controller = Controller.new(
10
+ robot: @robot,
11
+ view: @view
12
+ )
13
+ end
14
+
15
+ def test_respond_to_public_interface
16
+ assert_respond_to @controller, :place
17
+ assert_respond_to @controller, :move
18
+ assert_respond_to @controller, :right
19
+ assert_respond_to @controller, :left
20
+ assert_respond_to @controller, :report
21
+ end
22
+
23
+ def test_it_delegates_a_place_command_to_robot
24
+ args = Object.new
25
+ @robot.expect(:place, nil, [args])
26
+
27
+ @controller.place(args)
28
+
29
+ @robot.verify
30
+ end
31
+
32
+ def test_it_delegates_a_move_command_to_robot
33
+ @robot.expect(:move, nil)
34
+
35
+ @controller.move(nil)
36
+
37
+ @robot.verify
38
+ end
39
+
40
+ def test_it_delegates_a_left_command_to_robot
41
+ @robot.expect(:left, nil)
42
+
43
+ @controller.left(nil)
44
+
45
+ @robot.verify
46
+ end
47
+
48
+ def test_it_delegates_a_right_command_to_robot
49
+ @robot.expect(:right, nil)
50
+
51
+ @controller.right(nil)
52
+
53
+ @robot.verify
54
+ end
55
+
56
+ def test_it_delegates_a_report_command_to_view
57
+ @view.expect(:report, nil)
58
+
59
+ @controller.report(nil)
60
+
61
+ @view.verify
62
+ end
63
+ end
64
+ end
data/test/test_pose.rb CHANGED
@@ -1,9 +1,12 @@
1
1
  require_relative '../lib/toy_robot/pose'
2
+ require_relative 'test_pose_interface'
2
3
  require_relative 'test_reporter_interface'
4
+
3
5
  require 'minitest/autorun'
4
6
 
5
7
  module ToyRobot
6
8
  class PoseTest < MiniTest::Unit::TestCase
9
+ include PoseInterfaceTest
7
10
  include ReporterInterfaceTest
8
11
 
9
12
  def setup
@@ -16,25 +19,18 @@ module ToyRobot
16
19
  )
17
20
  end
18
21
 
19
- def test_public_interface
20
- assert_respond_to @pose, :adjacent
21
- assert_respond_to @pose, :rotate!
22
- assert_respond_to @pose, :report
23
- assert_respond_to @pose, :x
24
- assert_respond_to @pose, :y
25
- assert_respond_to @pose, :orientation
26
- end
27
-
28
22
  def test_it_can_be_initialized_without_arguments
29
23
  assert Pose.new
30
24
  end
31
25
 
32
- def test_that_adjacent_returns_a_pose
33
- assert_instance_of Pose, @pose.adjacent
34
- end
26
+ def test_that_it_can_mutate
27
+ mutated_pose_attrs = {
28
+ x: 5,
29
+ y: 5,
30
+ orientation: 'random'
31
+ }
35
32
 
36
- def test_that_adjacent_pose_is_not_the_reciever
37
- refute_equal @pose, @pose.adjacent
33
+ assert_pose mutated_pose_attrs, @pose.mutate!(mutated_pose_attrs)
38
34
  end
39
35
 
40
36
  def test_that_knows_adjacent_when_facing_east
@@ -125,16 +121,16 @@ module ToyRobot
125
121
  end
126
122
  end
127
123
 
128
- def test_that_it_reports_x
129
- assert_includes @pose.report, :x
124
+ def test_that_hash_like_access_to_x_returns_instance_variable_x
125
+ assert_equal @pose.instance_variable_get(:@x), @pose[:x]
130
126
  end
131
127
 
132
- def test_that_it_reports_y
133
- assert_includes @pose.report, :y
128
+ def test_that_hash_like_access_to_y_returns_instance_variable_y
129
+ assert_equal @pose.instance_variable_get(:@y), @pose[:y]
134
130
  end
135
131
 
136
- def test_that_it_reports_orientation
137
- assert_includes @pose.report, :orientation
132
+ def test_that_hash_like_access_to_orientation_returns_instance_variable_orientation
133
+ assert_equal @pose.instance_variable_get(:@orientation), @pose[:orientation]
138
134
  end
139
135
 
140
136
  def assert_pose(expected_pose_hash, actual)
@@ -0,0 +1,15 @@
1
+ require_relative 'pose_double'
2
+ require_relative 'test_pose_interface'
3
+ require_relative 'test_reporter_interface'
4
+ require 'minitest/autorun'
5
+
6
+ module ToyRobot
7
+ class PoseDoubleTest < MiniTest::Unit::TestCase
8
+ include PoseInterfaceTest
9
+ include ReporterInterfaceTest
10
+
11
+ def setup
12
+ @object = PoseDouble.new
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,26 @@
1
+ module ToyRobot
2
+ module PoseInterfaceTest
3
+ def test_pose_public_interface
4
+ assert_respond_to @object, :mutate!
5
+ assert_respond_to @object, :adjacent
6
+ assert_respond_to @object, :rotate!
7
+ assert_respond_to @object, :[]
8
+ end
9
+
10
+ def test_that_mutate_returns_the_receiver
11
+ assert_equal @object, @object.mutate!
12
+ end
13
+
14
+ def test_that_adjacent_returns_a_pose_object
15
+ assert_instance_of @object.class, @object.adjacent
16
+ end
17
+
18
+ def test_that_adjacent_does_not_return_the_receiver
19
+ refute_equal @object, @object.adjacent
20
+ end
21
+
22
+ def test_that_rotate_returns_the_receiver
23
+ assert_equal @object, @object.rotate!(1337)
24
+ end
25
+ end
26
+ end
@@ -1,7 +1,19 @@
1
1
  module ToyRobot
2
2
  module ReporterInterfaceTest
3
- def test_it_implement_the_reproter_interface
3
+ def test_it_implements_the_reproter_interface
4
4
  assert_respond_to @object, :report
5
5
  end
6
+
7
+ def test_that_it_reports_x
8
+ assert_includes @object.report, :x
9
+ end
10
+
11
+ def test_that_it_reports_y
12
+ assert_includes @object.report, :y
13
+ end
14
+
15
+ def test_that_it_reports_orientation
16
+ assert_includes @object.report, :orientation
17
+ end
6
18
  end
7
19
  end
data/test/test_robot.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require_relative '../lib/toy_robot/robot'
2
2
  require_relative 'test_reporter_interface'
3
+ require_relative 'board_double'
4
+ require_relative 'pose_double'
3
5
  require 'minitest/autorun'
4
6
 
5
7
  module ToyRobot
@@ -7,64 +9,105 @@ module ToyRobot
7
9
  include ReporterInterfaceTest
8
10
 
9
11
  def setup
10
- @observer = MiniTest::Mock.new.expect(:on_board?, true)
11
- @robot = @object = Robot.new(placement: @observer)
12
+ @board = BoardDouble.new
13
+ @pose = PoseDouble.new
14
+ @robot = @object = Robot.new(board: @board, pose: @pose)
15
+ @mock = MiniTest::Mock.new
12
16
  end
13
17
 
14
18
  def teardown
15
- @observer = nil
16
- @robot = nil
19
+ @board, @pose, @robot, @mock = nil, nil, nil, nil
17
20
  end
18
21
 
19
- def test_respond_to_public_interface
22
+ def test_that_it_implements_robot_public_interface
20
23
  assert_respond_to @robot, :place
21
24
  assert_respond_to @robot, :move
22
25
  assert_respond_to @robot, :right
23
26
  assert_respond_to @robot, :left
24
27
  end
25
28
 
29
+ def test_that_place_returns_nil
30
+ assert_nil @robot.place(nil)
31
+ end
32
+
33
+ def test_that_move_returns_nil
34
+ assert_nil @robot.move
35
+ end
36
+
37
+ def test_that_right_returns_nil
38
+ assert_nil @robot.right
39
+ end
40
+
41
+ def test_that_left_returns_nil
42
+ assert_nil @robot.left
43
+ end
44
+
26
45
  def test_that_it_places
27
- placement = MiniTest::Mock.new.expect(:update, nil, [nil])
28
- @robot.instance_variable_set(:@placement, placement)
46
+ @board.stub :valid_pose?, true do
47
+ @robot.instance_variable_set(:@pose, @mock)
48
+ @mock.expect(:mutate!, nil, ['a valid pose'])
49
+
50
+ @robot.place('a valid pose')
29
51
 
30
- @robot.place(nil)
31
- placement.verify
52
+ @mock.verify
53
+ end
32
54
  end
33
55
 
34
- def test_that_it_moves
35
- @observer.expect(:advance, nil)
36
- @robot.move
37
- @observer.verify
56
+ def test_that_it_does_not_place_if_pose_is_invalid
57
+ @board.stub :valid_pose?, false do
58
+ @robot.instance_variable_set(:@pose, @mock)
59
+
60
+ @robot.place('a invalid pose')
61
+
62
+ @mock.verify
63
+ end
64
+ end
65
+
66
+ def test_that_it_moves_one_step_at_a_time
67
+ @board.stub :valid_pose?, true do
68
+ @robot.instance_variable_set(:@pose, @mock)
69
+ @mock.expect(:adjacent, 'a adjacent pose')
70
+ @mock.expect(:mutate!, nil, ['a adjacent pose'])
71
+
72
+ @robot.move
73
+
74
+ @mock.verify
75
+ end
76
+ end
77
+
78
+ def test_that_it_does_not_move_if_next_to_border
79
+ @board.stub :valid_pose?, false do
80
+ @robot.instance_variable_set(:@pose, @mock)
81
+
82
+ @robot.move
83
+
84
+ @mock.verify
85
+ end
38
86
  end
39
87
 
40
88
  def test_that_it_turns_right
41
- @observer.expect(:rotate, nil, [90])
89
+ @robot.instance_variable_set(:@pose, @mock)
90
+ @mock.expect(:rotate!, nil, [90])
42
91
  @robot.right
43
- @observer.verify
92
+ @mock.verify
44
93
  end
45
94
 
46
95
  def test_that_it_turns_left
47
- @observer.expect(:rotate, nil, [-90])
96
+ @robot.instance_variable_set(:@pose, @mock)
97
+ @mock.expect(:rotate!, nil, [-90])
48
98
  @robot.left
49
- @observer.verify
50
- end
51
-
52
- def test_that_it_reports
53
- @observer.expect(:report, nil)
54
- @robot.report
55
- @observer.verify
99
+ @mock.verify
56
100
  end
57
101
 
58
102
  def test_that_it_ignores_commands_if_unplaced
59
- observer = MiniTest::Mock.new
60
- @robot.instance_variable_set(:@placement, observer)
103
+ @robot.instance_variable_set(:@board, nil)
104
+ @robot.instance_variable_set(:@pose, @mock)
61
105
 
62
106
  [:move, :right, :left, :report].each do |command|
63
- observer.expect(:on_board?, false)
64
107
  @robot.send(command)
65
108
  end
66
109
 
67
- observer.verify
110
+ @mock.verify
68
111
  end
69
112
  end
70
113
  end
data/toyrobot.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = 'toyrobot'
3
- spec.version = '0.0.1'
3
+ spec.version = '0.0.2'
4
4
  spec.authors = ['Matias Anaya']
5
5
  spec.email = ['matiasanaya@gmail.com']
6
6
  spec.summary = %q{A toy robot simulator}