gobstones 0.0.2 → 0.0.3
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 +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
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'gobstones/lang/expressions/one_arg_expression'
|
2
|
-
require 'gobstones/lang/commands/poner_cmd'
|
3
|
-
require 'gobstones/runner/errors/gobstones_type_error'
|
4
|
-
|
5
|
-
module Gobstones
|
6
|
-
|
7
|
-
module Lang
|
8
|
-
|
9
|
-
class Sacar < OneArgExpression
|
10
|
-
|
11
|
-
def evaluate(context)
|
12
|
-
with_evaluated_argument_in(context) do |result|
|
13
|
-
context.head.take_out result
|
14
|
-
end
|
15
|
-
rescue RuntimeError => e
|
16
|
-
raise Gobstones::Runner::GobstonesTypeError, e.message
|
17
|
-
end
|
18
|
-
|
19
|
-
def undo(context)
|
20
|
-
with_evaluated_argument_in(context) do |result|
|
21
|
-
context.head.put result
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def opposite
|
26
|
-
Poner.new argument
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
32
|
-
|
33
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
require 'gobstones/lang/commands/conditional_cmd'
|
2
|
-
require 'gobstones/runner/errors/gobstones_runtime_error'
|
3
|
-
|
4
|
-
module Gobstones
|
5
|
-
|
6
|
-
module Lang
|
7
|
-
|
8
|
-
class WhileCmd < ConditionalCmd
|
9
|
-
|
10
|
-
STACK_LIMIT = 10000
|
11
|
-
|
12
|
-
def evaluate(context)
|
13
|
-
stack_acc = 0
|
14
|
-
while evaluate_condition(context).is_true?
|
15
|
-
raise Gobstones::Runner::GobstonesRuntimeError, 'stack overflow' if stack_acc == STACK_LIMIT
|
16
|
-
then_block.evaluate context
|
17
|
-
stack_acc += 1
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
describe CommandBlock do
|
2
|
-
|
3
|
-
let(:context) { clean_context }
|
4
|
-
|
5
|
-
it "evaluates all inner commands" do
|
6
|
-
cmd_block = CommandBlock.new [
|
7
|
-
Poner.new(rojo), Poner.new(verde),
|
8
|
-
Poner.new(negro), Poner.new(azul)]
|
9
|
-
cmd_block.evaluate context
|
10
|
-
expect(context.head.are_there_balls?(azul)).to be true
|
11
|
-
expect(context.head.are_there_balls?(negro)).to be true
|
12
|
-
expect(context.head.are_there_balls?(rojo)).to be true
|
13
|
-
expect(context.head.are_there_balls?(verde)).to be true
|
14
|
-
end
|
15
|
-
|
16
|
-
it "builds an empty command block" do
|
17
|
-
expect(CommandBlock.new []).to be_empty
|
18
|
-
expect(CommandBlock.empty).to be_empty
|
19
|
-
end
|
20
|
-
|
21
|
-
end
|
@@ -1,50 +0,0 @@
|
|
1
|
-
describe "if command" do
|
2
|
-
|
3
|
-
let(:context) { clean_context }
|
4
|
-
let(:then_block) { CommandBlock.new [Poner.new(verde)] }
|
5
|
-
let(:else_block) { CommandBlock.new [Poner.new(rojo)] }
|
6
|
-
|
7
|
-
describe "if-then" do
|
8
|
-
|
9
|
-
it "evaluates the 'then' command block if the condition is true" do
|
10
|
-
if_cmd = IfCmd.new true_value, then_block
|
11
|
-
if_cmd.evaluate context
|
12
|
-
expect(context.head.are_there_balls?(verde)).to be true
|
13
|
-
end
|
14
|
-
|
15
|
-
it "does not evaluate the 'then' command block if the condition is false" do
|
16
|
-
if_cmd = IfCmd.new false_value, then_block
|
17
|
-
if_cmd.evaluate context
|
18
|
-
expect(context.head.are_there_balls?(verde)).to be false
|
19
|
-
end
|
20
|
-
|
21
|
-
it "raises a type error if the condition is not boolean" do
|
22
|
-
[42.to_gbs_num, norte, verde].each do |value|
|
23
|
-
if_cmd = IfCmd.new value, then_block
|
24
|
-
expect { if_cmd.evaluate context }
|
25
|
-
.to raise_error(GobstonesTypeError, /is not a boolean/)
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
29
|
-
|
30
|
-
end
|
31
|
-
|
32
|
-
describe "if-then-else" do
|
33
|
-
|
34
|
-
it "evaluates the 'then' block and it does not evaluate the 'else' block" do
|
35
|
-
if_cmd = IfElseCmd.new true_value, then_block, else_block
|
36
|
-
if_cmd.evaluate context
|
37
|
-
expect(context.head.are_there_balls?(verde)).to be true
|
38
|
-
expect(context.head.are_there_balls?(rojo)).to be false
|
39
|
-
end
|
40
|
-
|
41
|
-
it "does not evaluate the 'then' block and it evaluates the 'else' block" do
|
42
|
-
if_cmd = IfElseCmd.new false_value, then_block, else_block
|
43
|
-
if_cmd.evaluate context
|
44
|
-
expect(context.head.are_there_balls?(verde)).to be false
|
45
|
-
expect(context.head.are_there_balls?(rojo)).to be true
|
46
|
-
end
|
47
|
-
|
48
|
-
end
|
49
|
-
|
50
|
-
end
|
@@ -1,16 +0,0 @@
|
|
1
|
-
describe IrAlOrigen do
|
2
|
-
|
3
|
-
let(:context) { clean_context }
|
4
|
-
|
5
|
-
it "puts the head in 0, 0 when evaluating" do
|
6
|
-
context.head.move_north
|
7
|
-
context.head.move_east
|
8
|
-
ir_al_origen = IrAlOrigen.new
|
9
|
-
|
10
|
-
ir_al_origen.evaluate context
|
11
|
-
|
12
|
-
expect(context.head.x_pos).to eq(0)
|
13
|
-
expect(context.head.y_pos).to eq(0)
|
14
|
-
end
|
15
|
-
|
16
|
-
end
|
@@ -1,36 +0,0 @@
|
|
1
|
-
describe Mover do
|
2
|
-
|
3
|
-
let(:context) { clean_context }
|
4
|
-
|
5
|
-
it "moves the head to the specified direction when evaluating" do
|
6
|
-
Mover.new(norte).evaluate(context)
|
7
|
-
|
8
|
-
expect(context.head.x_pos).to eq(0)
|
9
|
-
expect(context.head.y_pos).to eq(1)
|
10
|
-
end
|
11
|
-
|
12
|
-
it "undoes the given movement" do
|
13
|
-
cmd = Mover.new norte
|
14
|
-
|
15
|
-
cmd.evaluate context
|
16
|
-
cmd.undo context
|
17
|
-
|
18
|
-
expect(context.head.x_pos).to eq(0)
|
19
|
-
expect(context.head.y_pos).to eq(0)
|
20
|
-
end
|
21
|
-
|
22
|
-
it "returns the opposite command" do
|
23
|
-
expect(Mover.new(norte).opposite).to eq(Mover.new(sur))
|
24
|
-
end
|
25
|
-
|
26
|
-
it "fails if the argument is not a direction" do
|
27
|
-
expect { Mover.new(verde).evaluate(context) }.
|
28
|
-
to raise_error(GobstonesTypeError, /is not a direction/)
|
29
|
-
end
|
30
|
-
|
31
|
-
it "fails when the resulting position is out of board" do
|
32
|
-
expect { Mover.new(sur).evaluate(context) }.
|
33
|
-
to raise_error(OutOfBoardError)
|
34
|
-
end
|
35
|
-
|
36
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
describe Poner do
|
2
|
-
|
3
|
-
let(:context) { clean_context }
|
4
|
-
|
5
|
-
it "puts a ball of the given color in the current cell when evaluating" do
|
6
|
-
Poner.new(verde).evaluate(context)
|
7
|
-
|
8
|
-
expect(context.head.number_of_balls(verde)).to eq(1)
|
9
|
-
end
|
10
|
-
|
11
|
-
it "undoes the command" do
|
12
|
-
context.head.put verde
|
13
|
-
|
14
|
-
Poner.new(verde).undo context
|
15
|
-
|
16
|
-
expect(context.head.number_of_balls(verde)).to eq(0)
|
17
|
-
end
|
18
|
-
|
19
|
-
it "returns the opposite command" do
|
20
|
-
expect(Poner.new(verde).opposite).to eq(Sacar.new(verde))
|
21
|
-
end
|
22
|
-
|
23
|
-
it "fails if the argument is not a color" do
|
24
|
-
expect { Poner.new(norte).evaluate(context) }.
|
25
|
-
to raise_error(GobstonesTypeError, /is not a color/)
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
@@ -1,60 +0,0 @@
|
|
1
|
-
describe RepeatWithCmd do
|
2
|
-
|
3
|
-
let(:context) { clean_context }
|
4
|
-
let(:var_name) { 'var'.to_var_name }
|
5
|
-
|
6
|
-
it "iterates over numbers when evaluating" do
|
7
|
-
command_block = CommandBlock.new [Poner.new(rojo)]
|
8
|
-
repeat_with = RepeatWithCmd.new var_name, 1.to_gbs_num, 10.to_gbs_num, command_block
|
9
|
-
|
10
|
-
repeat_with.evaluate context
|
11
|
-
|
12
|
-
expect(context.head.number_of_balls(rojo)).to eq(10)
|
13
|
-
end
|
14
|
-
|
15
|
-
it "raises an error if the range values have not the same type" do
|
16
|
-
repeat_with = RepeatWithCmd.new var_name, 1.to_gbs_num, este, empty_body
|
17
|
-
|
18
|
-
expect { repeat_with.evaluate context }
|
19
|
-
.to raise_error(GobstonesTypeError, /types don't match in range values/)
|
20
|
-
end
|
21
|
-
|
22
|
-
it "raises an error if the index variable is previously defined" do
|
23
|
-
repeat_with = RepeatWithCmd.new var_name, 1.to_gbs_num, 5.to_gbs_num, empty_body
|
24
|
-
|
25
|
-
context.set var_name, 42.to_gbs_num
|
26
|
-
|
27
|
-
expect { repeat_with.evaluate context }
|
28
|
-
.to raise_error(GobstonesRuntimeError, /index variable can't be used because it's already defined/)
|
29
|
-
end
|
30
|
-
|
31
|
-
it "removes the index variable assignment after execution" do
|
32
|
-
repeat_with = RepeatWithCmd.new var_name, azul, verde, empty_body
|
33
|
-
|
34
|
-
repeat_with.evaluate context
|
35
|
-
|
36
|
-
expect(context.has_variable_named?('var')).to be false
|
37
|
-
end
|
38
|
-
|
39
|
-
it "allows to use the index variable inside the command block" do
|
40
|
-
cmd_block = CommandBlock.new [Poner.new(var_name)]
|
41
|
-
repeat_with = RepeatWithCmd.new var_name, azul, verde, cmd_block
|
42
|
-
|
43
|
-
repeat_with.evaluate context
|
44
|
-
|
45
|
-
expect(context.head.are_there_balls?(azul)).to be true
|
46
|
-
expect(context.head.are_there_balls?(negro)).to be true
|
47
|
-
expect(context.head.are_there_balls?(rojo)).to be true
|
48
|
-
expect(context.head.are_there_balls?(verde)).to be true
|
49
|
-
end
|
50
|
-
|
51
|
-
it "does no iterations if the from is higher than the to" do
|
52
|
-
cmd_block = CommandBlock.new [Poner.new(verde)]
|
53
|
-
repeat_with = RepeatWithCmd.new var_name, 8.to_gbs_num, 4.to_gbs_num, cmd_block
|
54
|
-
|
55
|
-
repeat_with.evaluate context
|
56
|
-
|
57
|
-
expect(context.head.are_there_balls?(verde)).to be false
|
58
|
-
end
|
59
|
-
|
60
|
-
end
|
@@ -1,35 +0,0 @@
|
|
1
|
-
describe Sacar do
|
2
|
-
|
3
|
-
let(:context) { clean_context }
|
4
|
-
|
5
|
-
it "take off balls from the board when evaluating" do
|
6
|
-
3.times { context.head.put rojo }
|
7
|
-
|
8
|
-
Sacar.new(rojo).evaluate context
|
9
|
-
|
10
|
-
expect(context.head.number_of_balls(rojo)).to eq(2)
|
11
|
-
end
|
12
|
-
|
13
|
-
it "undoes a command" do
|
14
|
-
Sacar.new(rojo).undo context
|
15
|
-
|
16
|
-
expect(context.head.number_of_balls(rojo)).to eq(1)
|
17
|
-
end
|
18
|
-
|
19
|
-
it "returns the opposite command" do
|
20
|
-
opposite_command = Sacar.new(rojo).opposite
|
21
|
-
|
22
|
-
expect(opposite_command).to eq(Poner.new(rojo))
|
23
|
-
end
|
24
|
-
|
25
|
-
it "fails if there are no balls in the board" do
|
26
|
-
expect { Sacar.new(rojo).evaluate(context) }.
|
27
|
-
to raise_error(EmptyCellError)
|
28
|
-
end
|
29
|
-
|
30
|
-
it "fails if the argument is not a color" do
|
31
|
-
expect { Sacar.new(true_value).evaluate(context) }.
|
32
|
-
to raise_error(GobstonesTypeError, /is not a color/)
|
33
|
-
end
|
34
|
-
|
35
|
-
end
|
@@ -1,14 +0,0 @@
|
|
1
|
-
describe VaciarTablero do
|
2
|
-
|
3
|
-
let(:context) { clean_context }
|
4
|
-
|
5
|
-
it "empties the board in the context" do
|
6
|
-
context.head.put rojo
|
7
|
-
vaciar_tablero = VaciarTablero.new
|
8
|
-
|
9
|
-
vaciar_tablero.evaluate context
|
10
|
-
|
11
|
-
expect(context.board.empty?).to be true
|
12
|
-
end
|
13
|
-
|
14
|
-
end
|
@@ -1,43 +0,0 @@
|
|
1
|
-
describe WhileCmd do
|
2
|
-
|
3
|
-
let(:context) { clean_context }
|
4
|
-
let(:while_block) { CommandBlock.new [Poner.new(verde)] }
|
5
|
-
|
6
|
-
def condition(times)
|
7
|
-
double('while condition').tap do |condition|
|
8
|
-
expected_values = [true_value] * times + [false_value]
|
9
|
-
allow(condition).to receive(:evaluate).and_return(*expected_values)
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
it "evaluates the command block until the condition is not satisfied" do
|
14
|
-
while_cmd = WhileCmd.new condition(3), while_block
|
15
|
-
|
16
|
-
while_cmd.evaluate context
|
17
|
-
|
18
|
-
expect(context.head.number_of_balls(verde)).to eq(3)
|
19
|
-
end
|
20
|
-
|
21
|
-
it "does not evaluate the command block if the condition is false" do
|
22
|
-
while_cmd = WhileCmd.new condition(0), while_block
|
23
|
-
|
24
|
-
while_cmd.evaluate context
|
25
|
-
|
26
|
-
expect(context.head.are_there_balls?(verde)).to be false
|
27
|
-
end
|
28
|
-
|
29
|
-
it "fails by type error if the condition is not boolean" do
|
30
|
-
while_cmd = WhileCmd.new sur, while_block
|
31
|
-
|
32
|
-
expect { while_cmd.evaluate context }
|
33
|
-
.to raise_error(GobstonesTypeError, /is not a boolean/)
|
34
|
-
end
|
35
|
-
|
36
|
-
it "fails by stack overflow if the condition is always true" do
|
37
|
-
while_cmd = WhileCmd.new true_value, while_block
|
38
|
-
|
39
|
-
expect { while_cmd.evaluate context }
|
40
|
-
.to raise_error(GobstonesRuntimeError, /stack overflow/)
|
41
|
-
end
|
42
|
-
|
43
|
-
end
|