maux_robot 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9bf2d0fed313bcc6043144745aa33467fa7c8ab0
4
- data.tar.gz: a63febfee434feda427515e282367fb974b223c7
3
+ metadata.gz: fcae61d13656d506d2d2b851f744ad89ab52019b
4
+ data.tar.gz: 55ccb6a55a0ecac20ed9a32ed4b4894e6396d928
5
5
  SHA512:
6
- metadata.gz: f04f82e9a2914420a34898662e02231d4f744bfc6035e51302b091af5058eba85c74beaf32bcb69d0954643494cf9624571ee6ae68cbec58bcb437e75a514213
7
- data.tar.gz: 0304f7d1f1110a3ca0b048bce64dc59bbd87087903345c3e97696fe0b42052dcc72d93349084ca66413b319f32514f1b89b10cec330f1bfd40dd59acc3cc7b76
6
+ metadata.gz: c4bcf5f0d29bc5e2e06534f26d3ad31275829bb9f310c98a70fba8c6b2ee6081999c884c2a91fb5dee7bf8dbf8b2a33a1b04436319a64a21b9bb952bf8ff3b9e
7
+ data.tar.gz: c0e40e5920f39688a15e5018d7545fd6a81e6639791bd0394e182b2390b35198d68bd53089f180bd7aa990f390910d292f7f4554a33a64a3b10d2e7b71e3c9f4
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MauxRobot::Formatter
4
+
5
+ class Csv
6
+ def generate(position)
7
+ "#{position.x},#{position.y},#{position.face.upcase.to_s}"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module MauxRobot::Formatter
6
+
7
+ class Json
8
+ def generate(position)
9
+ JSON.generate({x: position.x, y: position.y, face: position.face.upcase.to_s})
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ module MauxRobot
2
+
3
+ module Formatter
4
+ def self.from(format_type)
5
+ case format_type
6
+ when :json
7
+ Formatter::Json.new
8
+ else
9
+ Formatter::Csv.new
10
+ end
11
+ end
12
+ end
13
+ end
@@ -34,7 +34,7 @@ module MauxRobot
34
34
  arguments = arguments.delete(' ').split(',')
35
35
  { x: arguments[0], y: arguments[1], face: arguments[2] }
36
36
  when :report
37
- { format: arguments.downcase.to_sym }
37
+ { format_type: arguments.downcase.to_sym }
38
38
  end
39
39
  end
40
40
  end
@@ -46,8 +46,9 @@ module MauxRobot
46
46
  Position.new(x, y, @face)
47
47
  end
48
48
 
49
- def to_s
50
- "#{@x},#{@y},#{@face.upcase.to_s}"
49
+ def report(format_type: :csv)
50
+ formatter = MauxRobot::Formatter.from(format_type)
51
+ puts formatter.generate(self)
51
52
  end
52
53
 
53
54
  def ==(other)
@@ -9,7 +9,7 @@ module MauxRobot
9
9
  attr_reader :position
10
10
 
11
11
  extend Forwardable
12
- def_delegators :@position, :left, :right
12
+ def_delegators :@position, :left, :right, :report
13
13
 
14
14
  def initialize(table=MauxRobot::Table.new)
15
15
  @table = table
@@ -31,9 +31,5 @@ module MauxRobot
31
31
  @position = next_position
32
32
  end
33
33
  end
34
-
35
- def report
36
- puts @position if @position
37
- end
38
34
  end
39
35
  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.4'.freeze
6
+ STRING = '0.0.5'.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,8 @@
1
1
  require 'maux_robot/version'
2
2
  require 'maux_robot/cli'
3
+ require 'maux_robot/formatter'
4
+ require 'maux_robot/formatter/csv'
5
+ require 'maux_robot/formatter/json'
3
6
  require 'maux_robot/parser'
4
7
  require 'maux_robot/position'
5
8
  require 'maux_robot/table'
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe MauxRobot::Formatter::Csv do
4
+ let (:position) {MauxRobot::Position.new(1,2,:west)}
5
+
6
+ context '#generate' do
7
+ it "Formats a position as CSV" do
8
+ expect(subject.generate(position)).to eq('1,2,WEST')
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe MauxRobot::Formatter::Json do
4
+ let (:position) {MauxRobot::Position.new(1,2,:west)}
5
+
6
+ context '#generate' do
7
+ it "Formats a position as JSON" do
8
+ expect(subject.generate(position)).to eq('{"x":1,"y":2,"face":"WEST"}')
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe MauxRobot::Formatter do
4
+ context "#from" do
5
+ describe "valid format_types" do
6
+ it "returns csv formatter" do
7
+ expect(subject.from(:csv).class).to eq(MauxRobot::Formatter::Csv)
8
+ end
9
+
10
+ it 'returns json formatter' do
11
+ expect(subject.from(:json).class).to eq(MauxRobot::Formatter::Json)
12
+ end
13
+ end
14
+
15
+ describe 'invalid format_types' do
16
+ it 'defaults to csv formatter' do
17
+ expect(subject.from(:anything).class).to eq(MauxRobot::Formatter::Csv)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'maux_robot'
4
-
5
3
  describe MauxRobot::Parser do
6
4
  describe '#parse' do
7
5
  context 'valid orders' do
@@ -10,11 +8,11 @@ describe MauxRobot::Parser do
10
8
  end
11
9
 
12
10
  it '#report csv' do
13
- expect(subject.parse('REPORT CSV')).to eq(order: :report, arguments: { format: :csv })
11
+ expect(subject.parse('REPORT CSV')).to eq(order: :report, arguments: { format_type: :csv })
14
12
  end
15
13
 
16
14
  it '#report json' do
17
- expect(subject.parse('REPORT JSON')).to eq(order: :report, arguments: { format: :json })
15
+ expect(subject.parse('REPORT JSON')).to eq(order: :report, arguments: { format_type: :json })
18
16
  end
19
17
 
20
18
  it '#move' do
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'maux_robot'
4
-
5
3
  describe MauxRobot::Position do
6
4
  describe '#initialize' do
7
5
  context 'given text input' do
@@ -119,25 +117,26 @@ describe MauxRobot::Position do
119
117
  end
120
118
  end
121
119
 
122
- describe '#to_s' do
120
+ describe '#report' do
123
121
  context 'if x, y and face are defined' do
124
122
  let(:position) { MauxRobot::Position.new(5, 0, :north) }
125
- it 'returns X,Y,FACE' do
126
- expect(position.to_s).to eq('5,0,NORTH')
123
+
124
+ it 'prints X,Y,FACE' do
125
+ expect { position.report }.to output("5,0,NORTH\n").to_stdout
127
126
  end
128
127
  end
129
128
 
130
129
  context 'if only face is defined' do
131
130
  let(:position) { MauxRobot::Position.new(nil, nil, 'SOUTH') }
132
- it 'returns 0,0,FACE' do
133
- expect(position.to_s).to eq('0,0,SOUTH')
131
+ it 'prints 0,0,FACE' do
132
+ expect { position.report }.to output("0,0,SOUTH\n").to_stdout
134
133
  end
135
134
  end
136
135
 
137
136
  context 'if face is not defined' do
138
137
  let(:position) { MauxRobot::Position.new(5, 0, nil) }
139
- it 'returns X,Y,INVALID' do
140
- expect(position.to_s).to eq('5,0,INVALID')
138
+ it 'prints X,Y,INVALID' do
139
+ expect { position.report }.to output("5,0,INVALID\n").to_stdout
141
140
  end
142
141
  end
143
142
  end
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'maux_robot'
4
-
5
3
  describe MauxRobot::Robot do
6
4
  subject(:robot) { described_class.new }
7
5
  let(:null_position) { MauxRobot::NullPosition.new }
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'maux_robot'
4
-
5
3
  describe MauxRobot::Table do
6
4
  subject(:table) { described_class.new(x: [-1, 2], y: [3, 6]) }
7
5
 
@@ -0,0 +1,3 @@
1
+ $LOAD_PATH.unshift("#{__dir__}/../lib")
2
+
3
+ require 'maux_robot'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: maux_robot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mauricio Vieira
@@ -44,16 +44,23 @@ files:
44
44
  - bin/maux_robot
45
45
  - lib/maux_robot.rb
46
46
  - lib/maux_robot/cli.rb
47
+ - lib/maux_robot/formatter.rb
48
+ - lib/maux_robot/formatter/csv.rb
49
+ - lib/maux_robot/formatter/json.rb
47
50
  - lib/maux_robot/parser.rb
48
51
  - lib/maux_robot/position.rb
49
52
  - lib/maux_robot/robot.rb
50
53
  - lib/maux_robot/table.rb
51
54
  - lib/maux_robot/version.rb
52
55
  - spec/maux_robot/cli_spec.rb
56
+ - spec/maux_robot/formatter/csv_spec.rb
57
+ - spec/maux_robot/formatter/json_spec.rb
58
+ - spec/maux_robot/formatter_spec.rb
53
59
  - spec/maux_robot/parser_spec.rb
54
60
  - spec/maux_robot/position_spec.rb
55
61
  - spec/maux_robot/robot_spec.rb
56
62
  - spec/maux_robot/table_spec.rb
63
+ - spec/spec_helper.rb
57
64
  homepage: https://github.com/mauriciovieira/maux_robot
58
65
  licenses:
59
66
  - GPL-3.0