gobstones 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.rubocop.yml +133 -0
- data/.rubocop_todo.yml +75 -0
- data/.simplecov +1 -3
- data/.tool-versions +1 -0
- data/.travis.yml +9 -2
- data/Gemfile +11 -5
- data/Gemfile.lock +65 -39
- data/README.md +8 -1
- data/Rakefile +1 -1
- data/bin/gobstones +1 -1
- data/gobstones.gemspec +12 -11
- data/lib/gobstones/cli/printer.rb +41 -28
- data/lib/gobstones/cli/runner.rb +50 -15
- data/lib/gobstones/extensions/all.rb +1 -1
- data/lib/gobstones/extensions/boolean.rb +1 -5
- data/lib/gobstones/extensions/{fixnum.rb → integer.rb} +2 -4
- data/lib/gobstones/extensions/string.rb +1 -3
- data/lib/gobstones/lang/all.rb +3 -3
- data/lib/gobstones/lang/commands/all.rb +11 -10
- data/lib/gobstones/lang/commands/boom.rb +26 -0
- data/lib/gobstones/lang/commands/command_block.rb +23 -23
- data/lib/gobstones/lang/commands/conditional.rb +26 -0
- data/lib/gobstones/lang/commands/if.rb +13 -0
- data/lib/gobstones/lang/commands/if_then_else.rb +26 -0
- data/lib/gobstones/lang/commands/ir_al_origen.rb +15 -0
- data/lib/gobstones/lang/commands/mover.rb +23 -0
- data/lib/gobstones/lang/commands/multiple_assignment.rb +34 -21
- data/lib/gobstones/lang/commands/poner.rb +31 -0
- data/lib/gobstones/lang/commands/procedure_call.rb +8 -8
- data/lib/gobstones/lang/commands/repeat_with.rb +69 -0
- data/lib/gobstones/lang/commands/sacar.rb +29 -0
- data/lib/gobstones/lang/commands/single_assignment.rb +15 -20
- data/lib/gobstones/lang/commands/skip.rb +15 -0
- data/lib/gobstones/lang/commands/vaciar_tablero.rb +15 -0
- data/lib/gobstones/lang/commands/while.rb +20 -0
- data/lib/gobstones/lang/definitions/definition.rb +16 -23
- data/lib/gobstones/lang/definitions/definition_call.rb +7 -15
- data/lib/gobstones/lang/definitions/function.rb +1 -7
- data/lib/gobstones/lang/definitions/main.rb +1 -8
- data/lib/gobstones/lang/definitions/no_return_statement.rb +2 -8
- data/lib/gobstones/lang/definitions/procedure.rb +1 -7
- data/lib/gobstones/lang/definitions/return_from_function.rb +6 -13
- data/lib/gobstones/lang/definitions/return_from_main.rb +15 -10
- data/lib/gobstones/lang/definitions/var_tuple.rb +10 -12
- data/lib/gobstones/lang/expressions/arithmetic_expressions.rb +17 -27
- data/lib/gobstones/lang/expressions/boolean_expressions.rb +4 -10
- data/lib/gobstones/lang/expressions/comparison_expressions.rb +0 -16
- data/lib/gobstones/lang/expressions/enclosed_by_parens_expression.rb +3 -5
- data/lib/gobstones/lang/expressions/expression.rb +18 -0
- data/lib/gobstones/lang/expressions/function_call.rb +9 -6
- data/lib/gobstones/lang/expressions/one_arg_expression.rb +8 -16
- data/lib/gobstones/lang/expressions/primitive_functions.rb +24 -16
- data/lib/gobstones/lang/expressions/two_arg_expression.rb +9 -16
- data/lib/gobstones/lang/expressions/type_bound_functions.rb +30 -25
- data/lib/gobstones/lang/expressions/var_name.rb +9 -13
- data/lib/gobstones/lang/literals/all.rb +3 -3
- data/lib/gobstones/lang/literals/{booleans.rb → boolean.rb} +24 -22
- data/lib/gobstones/lang/literals/{colors.rb → color.rb} +4 -14
- data/lib/gobstones/lang/literals/{directions.rb → direction.rb} +3 -13
- data/lib/gobstones/lang/literals/literal.rb +35 -27
- data/lib/gobstones/lang/literals/number.rb +6 -8
- data/lib/gobstones/lang/program.rb +9 -16
- data/lib/gobstones/modules/equality_definition.rb +23 -0
- data/lib/gobstones/parser/ast/ast.rb +48 -70
- data/lib/gobstones/parser/parse_error.rb +2 -7
- data/lib/gobstones/parser/treetop_parser.rb +9 -14
- data/lib/gobstones/runner/board.rb +12 -15
- data/lib/gobstones/runner/cell.rb +14 -20
- data/lib/gobstones/runner/errors/all.rb +1 -1
- data/lib/gobstones/runner/errors/boom_error.rb +0 -5
- data/lib/gobstones/runner/errors/definition_not_found_error.rb +7 -7
- data/lib/gobstones/runner/errors/empty_cell_error.rb +0 -5
- data/lib/gobstones/runner/errors/gobstones_runtime_error.rb +0 -5
- data/lib/gobstones/runner/errors/gobstones_type_error.rb +2 -7
- data/lib/gobstones/runner/errors/out_of_board_error.rb +0 -5
- data/lib/gobstones/runner/errors/undefined_variable_error.rb +6 -4
- data/lib/gobstones/runner/errors/wrong_arguments_error.rb +0 -5
- data/lib/gobstones/runner/execution_context.rb +16 -33
- data/lib/gobstones/runner/head.rb +17 -20
- data/lib/gobstones/runner/program_result.rb +12 -0
- data/lib/gobstones/type_check_result.rb +0 -4
- data/spec/lang/commands/boom_spec.rb +7 -0
- data/spec/lang/commands/command_block_spec.rb +15 -0
- data/spec/lang/commands/if_spec.rb +32 -0
- data/spec/lang/commands/if_then_else_spec.rb +19 -0
- data/spec/lang/commands/ir_al_origen_spec.rb +11 -0
- data/spec/lang/commands/mover_spec.rb +30 -0
- data/spec/lang/commands/multiple_assignment_spec.rb +39 -22
- data/spec/lang/commands/poner_spec.rb +27 -0
- data/spec/lang/commands/procedure_call_spec.rb +26 -28
- data/spec/lang/commands/procedure_spec.rb +32 -32
- data/spec/lang/commands/repeat_with_spec.rb +64 -0
- data/spec/lang/commands/sacar_spec.rb +32 -0
- data/spec/lang/commands/single_assignment_spec.rb +5 -6
- data/spec/lang/commands/skip_spec.rb +10 -0
- data/spec/lang/commands/vaciar_tablero_spec.rb +10 -0
- data/spec/lang/commands/while_spec.rb +39 -0
- data/spec/lang/definitions/main_spec.rb +34 -0
- data/spec/lang/definitions/no_return_statement_spec.rb +4 -5
- data/spec/lang/definitions/var_tuple_spec.rb +4 -7
- data/spec/lang/expressions/arithmetic_expressions_spec.rb +37 -64
- data/spec/lang/expressions/boolean_expressions_spec.rb +25 -34
- data/spec/lang/expressions/comparison_expressions_spec.rb +109 -155
- data/spec/lang/expressions/enclosed_by_parens_expression_spec.rb +3 -7
- data/spec/lang/expressions/function_call_spec.rb +24 -20
- data/spec/lang/expressions/primitive_functions_spec.rb +28 -39
- data/spec/lang/expressions/type_bound_functions_spec.rb +10 -18
- data/spec/lang/expressions/var_name_spec.rb +8 -12
- data/spec/lang/literals/{booleans_spec.rb → boolean_spec.rb} +3 -5
- data/spec/lang/literals/color_spec.rb +19 -0
- data/spec/lang/literals/direction_spec.rb +46 -0
- data/spec/lang/literals/{numbers_spec.rb → number_spec.rb} +3 -4
- data/spec/matchers/parse_matcher.rb +9 -11
- data/spec/parser/arithmetic_expressions_spec.rb +51 -61
- data/spec/parser/assignments_spec.rb +21 -31
- data/spec/parser/boolean_expressions_spec.rb +35 -43
- data/spec/parser/command_block_spec.rb +18 -20
- data/spec/parser/data_types_spec.rb +9 -19
- data/spec/parser/function_calls_spec.rb +19 -19
- data/spec/parser/function_definitions_spec.rb +17 -22
- data/spec/parser/gobstones_program_spec.rb +30 -30
- data/spec/parser/if_command_spec.rb +19 -28
- data/spec/parser/main_definition_spec.rb +13 -16
- data/spec/parser/nested_expressions_spec.rb +20 -24
- data/spec/parser/primitive_expressions_spec.rb +33 -38
- data/spec/parser/procedure_calls_spec.rb +17 -18
- data/spec/parser/procedure_definitions_spec.rb +12 -15
- data/spec/parser/repeat_with_command_spec.rb +10 -10
- data/spec/parser/simple_commands_spec.rb +23 -37
- data/spec/parser/treetop_parser_spec.rb +87 -83
- data/spec/parser/var_tuple_spec.rb +8 -12
- data/spec/parser/while_command_spec.rb +11 -14
- data/spec/runner/board_spec.rb +28 -33
- data/spec/runner/cell_spec.rb +21 -28
- data/spec/runner/execution_context_spec.rb +18 -35
- data/spec/runner/head_spec.rb +54 -60
- data/spec/spec_helper.rb +10 -1
- data/spec/support/board_assertions.rb +18 -0
- data/spec/{gobstones_lang_test_objects.rb → support/gobstones_lang_test_objects.rb} +6 -4
- data/spec/type_checker_spec.rb +19 -25
- metadata +80 -56
- data/.ruby-version +0 -1
- data/lib/gobstones/lang/commands/boom_cmd.rb +0 -31
- data/lib/gobstones/lang/commands/conditional_cmd.rb +0 -31
- data/lib/gobstones/lang/commands/if_cmd.rb +0 -38
- data/lib/gobstones/lang/commands/ir_al_origen_cmd.rb +0 -19
- data/lib/gobstones/lang/commands/mover_cmd.rb +0 -27
- data/lib/gobstones/lang/commands/poner_cmd.rb +0 -35
- data/lib/gobstones/lang/commands/repeat_with_cmd.rb +0 -77
- data/lib/gobstones/lang/commands/sacar_cmd.rb +0 -33
- data/lib/gobstones/lang/commands/skip_cmd.rb +0 -19
- data/lib/gobstones/lang/commands/vaciar_tablero_cmd.rb +0 -19
- data/lib/gobstones/lang/commands/while_cmd.rb +0 -25
- data/lib/gobstones/modules/equal_by_class.rb +0 -13
- data/spec/lang/commands/boom_cmd_spec.rb +0 -8
- data/spec/lang/commands/cmd_block_spec.rb +0 -21
- data/spec/lang/commands/if_cmd_spec.rb +0 -50
- data/spec/lang/commands/ir_al_origen_cmd_spec.rb +0 -16
- data/spec/lang/commands/mover_cmd_spec.rb +0 -36
- data/spec/lang/commands/poner_cmd_spec.rb +0 -28
- data/spec/lang/commands/repeat_with_cmd_spec.rb +0 -60
- data/spec/lang/commands/sacar_cmd_spec.rb +0 -35
- data/spec/lang/commands/skip_cmd_spec.rb +0 -12
- data/spec/lang/commands/vaciar_tablero_cmd_spec.rb +0 -14
- data/spec/lang/commands/while_cmd_spec.rb +0 -43
- data/spec/lang/literals/colors_spec.rb +0 -13
- data/spec/lang/literals/directions_spec.rb +0 -45
data/spec/runner/cell_spec.rb
CHANGED
@@ -1,26 +1,19 @@
|
|
1
|
-
describe Cell do
|
1
|
+
RSpec.describe Cell do
|
2
|
+
subject(:cell) { described_class.new }
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
it "answers that there are no balls of a given color" do
|
7
|
-
expect(cell.are_there_balls?(azul)).to be false
|
8
|
-
expect(cell.are_there_balls?(negro)).to be false
|
9
|
-
expect(cell.are_there_balls?(rojo)).to be false
|
10
|
-
expect(cell.are_there_balls?(verde)).to be false
|
4
|
+
it 'answers that there are no balls of a given color' do
|
5
|
+
expect_no_balls(*colors, on: cell)
|
11
6
|
end
|
12
7
|
|
13
|
-
it
|
8
|
+
it 'answers that there are balls of a given color when adding some' do
|
14
9
|
cell.put azul
|
15
10
|
cell.put rojo
|
16
11
|
|
17
|
-
|
18
|
-
|
19
|
-
expect(cell.are_there_balls?(rojo)).to be true
|
20
|
-
expect(cell.are_there_balls?(verde)).to be false
|
12
|
+
expect_balls(azul, rojo, on: cell)
|
13
|
+
expect_no_balls(verde, negro, on: cell)
|
21
14
|
end
|
22
15
|
|
23
|
-
it
|
16
|
+
it 'answers the number of balls of a given color' do
|
24
17
|
5.times { cell.put verde }
|
25
18
|
|
26
19
|
expect(cell.number_of_balls(azul)).to eq(0)
|
@@ -29,11 +22,11 @@ describe Cell do
|
|
29
22
|
expect(cell.number_of_balls(verde)).to eq(5)
|
30
23
|
end
|
31
24
|
|
32
|
-
it
|
25
|
+
it 'allows to take out some balls' do
|
33
26
|
5.times { cell.put azul }
|
34
27
|
3.times { cell.take_out azul }
|
35
28
|
|
36
|
-
expect(cell.are_there_balls?(azul)).to be
|
29
|
+
expect(cell.are_there_balls?(azul)).to be(true)
|
37
30
|
expect(cell.number_of_balls(azul)).to eq(2)
|
38
31
|
end
|
39
32
|
|
@@ -41,28 +34,28 @@ describe Cell do
|
|
41
34
|
expect { cell.take_out rojo }.to raise_error(EmptyCellError)
|
42
35
|
end
|
43
36
|
|
44
|
-
it
|
45
|
-
expect { cell.put(
|
46
|
-
expect { cell.take_out(42) }.to raise_error
|
47
|
-
expect { cell.are_there_balls?(Norte) }.to raise_error
|
48
|
-
expect { cell.number_of_balls(nil) }.to raise_error
|
37
|
+
it 'fails passing something that is not a color' do
|
38
|
+
expect { cell.put('not a color') }.to raise_error(/is not a color/)
|
39
|
+
expect { cell.take_out(42) }.to raise_error(/is not a color/)
|
40
|
+
expect { cell.are_there_balls?(Norte) }.to raise_error(/is not a color/)
|
41
|
+
expect { cell.number_of_balls(nil) }.to raise_error(/is not a color/)
|
49
42
|
end
|
50
43
|
|
51
|
-
it
|
44
|
+
it 'clears its contents' do
|
52
45
|
colors.each { |color| cell.put color }
|
53
46
|
|
54
47
|
cell.empty!
|
55
48
|
|
56
|
-
colors
|
49
|
+
expect_no_balls(*colors, on: cell)
|
57
50
|
end
|
58
51
|
|
59
52
|
it "is empty if it doesn't have any balls" do
|
60
|
-
expect(cell
|
53
|
+
expect(cell).to be_empty
|
61
54
|
end
|
62
55
|
|
63
|
-
it
|
56
|
+
it 'is not empty it it has some balls' do
|
64
57
|
colors.each { |color| cell.put color }
|
65
|
-
expect(cell.empty?).to be false
|
66
|
-
end
|
67
58
|
|
59
|
+
expect(cell).not_to be_empty
|
60
|
+
end
|
68
61
|
end
|
@@ -1,16 +1,14 @@
|
|
1
|
-
describe ExecutionContext do
|
2
|
-
|
1
|
+
RSpec.describe ExecutionContext do
|
3
2
|
let(:context) { clean_context }
|
4
3
|
|
5
|
-
describe
|
6
|
-
|
7
|
-
it "allows to set/get a variable" do
|
4
|
+
describe 'variables context' do
|
5
|
+
it 'allows to set/get a variable' do
|
8
6
|
context.set 'myColor', negro
|
9
7
|
|
10
8
|
expect(context.get('myColor')).to eq(negro)
|
11
9
|
end
|
12
10
|
|
13
|
-
it
|
11
|
+
it 'allows to set/get more than one variable' do
|
14
12
|
context.set 'dir', norte
|
15
13
|
context.set 'bool', true_value
|
16
14
|
|
@@ -18,61 +16,46 @@ describe ExecutionContext do
|
|
18
16
|
expect(context.get('bool')).to eq(true_value)
|
19
17
|
end
|
20
18
|
|
21
|
-
it
|
19
|
+
it 'raises an error when trying to get an undefined variable' do
|
22
20
|
expect { context.get('undefined') }.to raise_error(UndefinedVariableError)
|
23
21
|
end
|
24
|
-
|
25
22
|
end
|
26
23
|
|
27
24
|
describe ProgramExecutionContext do
|
28
|
-
|
29
|
-
it "is the program context itself" do
|
25
|
+
it 'is the program context itself' do
|
30
26
|
expect(context.program_context).to eq(context)
|
31
27
|
end
|
32
|
-
|
33
|
-
it "has a head" do
|
34
|
-
expect(context.head).to be_a Head
|
35
|
-
end
|
36
|
-
|
37
|
-
it "has a board" do
|
38
|
-
expect(context.board).to be_a Board
|
39
|
-
end
|
40
|
-
|
41
28
|
end
|
42
29
|
|
43
30
|
describe ProcedureExecutionContext do
|
31
|
+
let(:procedure_context) { described_class.based_on(context) }
|
44
32
|
|
45
|
-
|
46
|
-
|
47
|
-
it "returns the program context in which it is based" do
|
33
|
+
it 'returns the program context in which it is based' do
|
48
34
|
expect(procedure_context.program_context).to eq(context)
|
49
35
|
end
|
50
36
|
|
51
|
-
it
|
37
|
+
it 'shares the head with the outer context' do
|
52
38
|
expect(procedure_context.head).to eq(context.head)
|
53
39
|
end
|
54
|
-
|
55
40
|
end
|
56
41
|
|
57
42
|
describe FunctionExecutionContext do
|
43
|
+
it 'returns the program context in which it is based' do
|
44
|
+
function_context = described_class.based_on(context)
|
58
45
|
|
59
|
-
it "returns the program context in which it is based" do
|
60
|
-
function_context = FunctionExecutionContext.based_on context
|
61
46
|
expect(function_context.program_context).to eq(context)
|
62
47
|
end
|
63
48
|
|
64
49
|
it "has a new head, a copy of the outer context's head" do
|
65
|
-
context.head.put
|
66
|
-
function_context =
|
67
|
-
function_context.head.put
|
50
|
+
context.head.put(azul)
|
51
|
+
function_context = described_class.based_on(context)
|
52
|
+
function_context.head.put(verde)
|
68
53
|
|
69
54
|
expect(function_context.head).not_to eq(context.head)
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
55
|
+
expect_balls(azul)
|
56
|
+
expect_no_balls(verde)
|
57
|
+
expect_balls(azul, on: function_context.head)
|
58
|
+
expect_balls(verde, on: function_context.head)
|
74
59
|
end
|
75
|
-
|
76
60
|
end
|
77
|
-
|
78
61
|
end
|
data/spec/runner/head_spec.rb
CHANGED
@@ -1,130 +1,124 @@
|
|
1
|
-
describe Head do
|
1
|
+
RSpec.describe Head do
|
2
|
+
subject(:head) { described_class.new }
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
it "has a position, default 0;0" do
|
4
|
+
it 'has a position, default 0;0' do
|
6
5
|
expect(head.x_pos).to eq(0)
|
7
6
|
expect(head.y_pos).to eq(0)
|
8
7
|
end
|
9
8
|
|
10
|
-
it
|
11
|
-
expect(
|
12
|
-
expect(
|
9
|
+
it 'answers the max size' do
|
10
|
+
expect(described_class::MAX_ROWS).to eq(9)
|
11
|
+
expect(described_class::MAX_COLS).to eq(9)
|
13
12
|
end
|
14
13
|
|
15
|
-
it
|
16
|
-
head =
|
14
|
+
it 'allows to be created at a random position' do
|
15
|
+
head = described_class.at_random
|
17
16
|
|
18
|
-
|
19
|
-
|
17
|
+
expect(head.x_pos.between?(0, described_class::MAX_ROWS - 1)).to be(true)
|
18
|
+
expect(head.y_pos.between?(0, described_class::MAX_COLS - 1)).to be(true)
|
20
19
|
end
|
21
20
|
|
22
|
-
describe
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
expect(head.can_move?(norte)).to be true
|
28
|
-
expect { head.move(norte) }.to_not raise_error
|
21
|
+
describe 'movements' do
|
22
|
+
context 'valid' do
|
23
|
+
it 'moves north' do
|
24
|
+
expect(head.can_move?(norte)).to be(true)
|
25
|
+
expect { head.move(norte) }.not_to raise_error
|
29
26
|
expect(head.x_pos).to eq(0)
|
30
27
|
expect(head.y_pos).to eq(1)
|
31
28
|
end
|
32
29
|
|
33
|
-
it
|
30
|
+
it 'moves south' do
|
34
31
|
head.move norte
|
35
|
-
|
36
|
-
expect
|
32
|
+
|
33
|
+
expect(head.can_move?(sur)).to be(true)
|
34
|
+
expect { head.move(sur) }.not_to raise_error
|
37
35
|
expect(head.x_pos).to eq(0)
|
38
36
|
expect(head.y_pos).to eq(0)
|
39
37
|
end
|
40
38
|
|
41
|
-
it
|
42
|
-
expect(head.can_move?(este)).to be
|
43
|
-
expect { head.move(este) }.
|
39
|
+
it 'moves east' do
|
40
|
+
expect(head.can_move?(este)).to be(true)
|
41
|
+
expect { head.move(este) }.not_to raise_error
|
44
42
|
expect(head.x_pos).to eq(1)
|
45
43
|
expect(head.y_pos).to eq(0)
|
46
44
|
end
|
47
45
|
|
48
|
-
it
|
46
|
+
it 'moves west' do
|
49
47
|
head.move este
|
50
|
-
|
51
|
-
expect
|
48
|
+
|
49
|
+
expect(head.can_move?(oeste)).to be(true)
|
50
|
+
expect { head.move(oeste) }.not_to raise_error
|
52
51
|
expect(head.x_pos).to eq(0)
|
53
52
|
expect(head.y_pos).to eq(0)
|
54
53
|
end
|
55
54
|
|
56
|
-
it
|
55
|
+
it 'goes to the origin' do
|
57
56
|
head.move este
|
58
57
|
head.move norte
|
59
58
|
head.go_to_origin
|
59
|
+
|
60
60
|
expect(head.x_pos).to eq(0)
|
61
61
|
expect(head.y_pos).to eq(0)
|
62
62
|
end
|
63
|
-
|
64
63
|
end
|
65
64
|
|
66
|
-
context
|
65
|
+
context 'non valid' do
|
66
|
+
it 'fails moving north' do
|
67
|
+
(Head::MAX_COLS - 1).times { head.move norte }
|
67
68
|
|
68
|
-
|
69
|
-
(Head::MAX_COLS-1).times { head.move norte }
|
70
|
-
|
71
|
-
expect(head.can_move?(norte)).to be false
|
69
|
+
expect(head.can_move?(norte)).to be(false)
|
72
70
|
expect { head.move norte }.to raise_error(OutOfBoardError)
|
73
71
|
end
|
74
72
|
|
75
|
-
it
|
76
|
-
expect(head.can_move?(sur)).to be
|
73
|
+
it 'fails moving south' do
|
74
|
+
expect(head.can_move?(sur)).to be(false)
|
77
75
|
expect { head.move sur }.to raise_error(OutOfBoardError)
|
78
76
|
end
|
79
77
|
|
80
|
-
it
|
81
|
-
(
|
78
|
+
it 'fails moving east' do
|
79
|
+
(described_class::MAX_ROWS - 1).times { head.move este }
|
82
80
|
|
83
|
-
expect(head.can_move?(este)).to be
|
81
|
+
expect(head.can_move?(este)).to be(false)
|
84
82
|
expect { head.move este }.to raise_error(OutOfBoardError)
|
85
83
|
end
|
86
84
|
|
87
|
-
it
|
88
|
-
expect(head.can_move?(oeste)).to be
|
85
|
+
it 'fails moving west' do
|
86
|
+
expect(head.can_move?(oeste)).to be(false)
|
89
87
|
expect { head.move oeste }.to raise_error(OutOfBoardError)
|
90
88
|
end
|
91
89
|
|
92
|
-
it
|
93
|
-
expect { head.move azul }.
|
94
|
-
|
95
|
-
expect { head.move
|
96
|
-
|
97
|
-
expect { head.move true_value }.
|
98
|
-
to raise_error(GobstonesTypeError, /is not a direction/)
|
99
|
-
expect { head.move 42 }.
|
100
|
-
to raise_error(GobstonesTypeError, /is not a direction/)
|
90
|
+
it 'fails if the argument is not a direction' do
|
91
|
+
expect { head.move azul }.to raise_error(GobstonesTypeError, /is not a direction/)
|
92
|
+
expect { head.move 'not a direction' }.to raise_error(GobstonesTypeError, /is not a direction/)
|
93
|
+
expect { head.move true_value }.to raise_error(GobstonesTypeError, /is not a direction/)
|
94
|
+
expect { head.move 42 }.to raise_error(GobstonesTypeError, /is not a direction/)
|
101
95
|
end
|
102
|
-
|
103
96
|
end
|
104
|
-
|
105
97
|
end
|
106
98
|
|
107
|
-
describe
|
108
|
-
|
109
|
-
it "puts balls across the board" do
|
99
|
+
describe 'board actions' do
|
100
|
+
it 'puts balls across the board' do
|
110
101
|
3.times { head.put negro }
|
111
|
-
|
102
|
+
|
103
|
+
expect(head.are_there_balls?(negro)).to be(true)
|
112
104
|
expect(head.number_of_balls(negro)).to eq(3)
|
105
|
+
|
113
106
|
head.move norte
|
114
107
|
2.times { head.put negro }
|
108
|
+
|
115
109
|
expect(head.number_of_balls(negro)).to eq(2)
|
110
|
+
|
116
111
|
head.move este
|
117
112
|
5.times { head.put negro }
|
113
|
+
|
118
114
|
expect(head.number_of_balls(negro)).to eq(5)
|
119
115
|
end
|
120
116
|
|
121
|
-
it
|
117
|
+
it 'takes out balls across the board' do
|
122
118
|
4.times { head.put negro }
|
123
119
|
4.times { head.take_out negro }
|
124
120
|
|
125
|
-
expect(head.are_there_balls?(negro)).to be
|
121
|
+
expect(head.are_there_balls?(negro)).to be(false)
|
126
122
|
end
|
127
|
-
|
128
123
|
end
|
129
|
-
|
130
124
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -2,6 +2,8 @@ require 'coveralls'
|
|
2
2
|
Coveralls.wear!
|
3
3
|
|
4
4
|
require 'simplecov'
|
5
|
+
require 'test_prof'
|
6
|
+
require 'test_prof/recipes/rspec/let_it_be'
|
5
7
|
|
6
8
|
require 'matchers/parse_matcher'
|
7
9
|
|
@@ -11,8 +13,15 @@ require 'gobstones/runner/all'
|
|
11
13
|
require 'gobstones/parser/treetop_parser'
|
12
14
|
|
13
15
|
include Gobstones::Lang
|
16
|
+
include Gobstones::Lang::Commands
|
14
17
|
include Gobstones::Runner
|
15
18
|
include Gobstones::Parser
|
16
19
|
|
17
|
-
require 'gobstones_lang_test_objects'
|
20
|
+
require 'support/gobstones_lang_test_objects'
|
18
21
|
include GobstonesLangTestObjects
|
22
|
+
require 'support/board_assertions'
|
23
|
+
include BoardAssertions
|
24
|
+
|
25
|
+
RSpec.configure(&:disable_monkey_patching!)
|
26
|
+
|
27
|
+
TestProf::BeforeAll.adapter = OpenStruct.new(begin_transaction: nil, rollback_transaction: nil)
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module BoardAssertions
|
2
|
+
def expect_balls(*colors, on: context.head)
|
3
|
+
colors.each do |color|
|
4
|
+
expect(on.are_there_balls?(color)).to be(true)
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
def expect_no_balls(*colors, on: context.head)
|
9
|
+
colors.each do |color|
|
10
|
+
expect(on.are_there_balls?(color)).to be(false)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def expect_positioned_at(x_pos, y_pos)
|
15
|
+
expect(context.head.x_pos).to eq(x_pos)
|
16
|
+
expect(context.head.y_pos).to eq(y_pos)
|
17
|
+
end
|
18
|
+
end
|
@@ -1,17 +1,16 @@
|
|
1
1
|
module GobstonesLangTestObjects
|
2
|
-
|
3
2
|
# Programs, contexts and definitions
|
4
3
|
|
5
4
|
def clean_context
|
6
|
-
program_context_for
|
5
|
+
program_context_for(empty_program)
|
7
6
|
end
|
8
7
|
|
9
8
|
def program_context_for(program)
|
10
|
-
ProgramExecutionContext.for
|
9
|
+
ProgramExecutionContext.for(program)
|
11
10
|
end
|
12
11
|
|
13
12
|
def empty_program
|
14
|
-
Program.new
|
13
|
+
Program.new(no_definitions, no_return_statement)
|
15
14
|
end
|
16
15
|
|
17
16
|
def no_definitions
|
@@ -72,4 +71,7 @@ module GobstonesLangTestObjects
|
|
72
71
|
Oeste.new
|
73
72
|
end
|
74
73
|
|
74
|
+
def colors
|
75
|
+
[azul, negro, rojo, verde]
|
76
|
+
end
|
75
77
|
end
|
data/spec/type_checker_spec.rb
CHANGED
@@ -1,37 +1,31 @@
|
|
1
|
-
describe Gobstones,
|
1
|
+
RSpec.describe Gobstones, 'type checker' do
|
2
|
+
describe 'board expressions' do
|
3
|
+
describe 'puedeMover()' do
|
4
|
+
it 'allows a direction as argument' do
|
5
|
+
[norte, sur, este, oeste].each do |dir|
|
6
|
+
result = PuedeMover.new(dir).type_check
|
2
7
|
|
3
|
-
|
8
|
+
expect(result.ok?).to be(true)
|
9
|
+
end
|
10
|
+
end
|
4
11
|
|
5
|
-
|
12
|
+
it 'does not allow a number as argument' do
|
13
|
+
result = PuedeMover.new(42.to_gbs_num).type_check
|
6
14
|
|
7
|
-
|
8
|
-
[norte, sur, este, oeste].each do |dir|
|
9
|
-
puede_mover_dir = PuedeMover.new dir
|
10
|
-
result = puede_mover_dir.type_check
|
11
|
-
expect(result.ok?).to be true
|
12
|
-
end
|
15
|
+
expect(result.ok?).to be(false)
|
13
16
|
end
|
14
17
|
|
15
|
-
it
|
16
|
-
|
17
|
-
result = puede_mover_number.type_check
|
18
|
-
expect(result.ok?).to be false
|
19
|
-
end
|
18
|
+
it 'does not allow a color as argument' do
|
19
|
+
result = PuedeMover.new(verde).type_check
|
20
20
|
|
21
|
-
|
22
|
-
puede_mover_color = PuedeMover.new verde
|
23
|
-
result = puede_mover_color.type_check
|
24
|
-
expect(result.ok?).to be false
|
21
|
+
expect(result.ok?).to be(false)
|
25
22
|
end
|
26
23
|
|
27
|
-
it
|
28
|
-
|
29
|
-
result = puede_mover_bool.type_check
|
30
|
-
expect(result.ok?).to be false
|
31
|
-
end
|
24
|
+
it 'does not allow a boolean as argument' do
|
25
|
+
result = PuedeMover.new(true_value).type_check
|
32
26
|
|
27
|
+
expect(result.ok?).to be(false)
|
28
|
+
end
|
33
29
|
end
|
34
|
-
|
35
30
|
end
|
36
|
-
|
37
31
|
end
|