spec/lang/commands/procedure_spec.rb in gobstones-0.0.1.1 vs spec/lang/commands/procedure_spec.rb in gobstones-0.0.2

- old
+ new

@@ -1,67 +1,63 @@ describe Procedure do - let(:program_context) { ProgramExecutionContext.for Program.new([], nil) } - let(:empty_args) { VarTuple.new [] } - let(:empty_body) { CmdBlock.empty } + let(:context) { clean_context } - it "should execute its body and leave state in the program context" do - poner_cmd = Poner.new Rojo.new - body = CmdBlock.new [poner_cmd] - procedure = Procedure.new 'MyProcedure', empty_args, body - procedure.evaluate program_context - expect(program_context.head.are_there_balls?(Rojo.new)).to be_true + it "executes its body and leaves state in the program context" do + poner_cmd = Poner.new rojo + body = CommandBlock.new [poner_cmd] + procedure = Procedure.new 'MyProcedure', no_arguments, body + procedure.evaluate context + expect(context.head.are_there_balls?(rojo)).to be true end - it "should execute in a new clean context, without having variables defined in another contexts" do - var_name = VarName.new 'var' - program_context.set var_name, Verde.new + it "fails getting a variable which is in the outer context" do + var_name = 'var'.to_var_name + context.set var_name, verde poner_cmd = Poner.new var_name - body = CmdBlock.new [poner_cmd] - procedure = Procedure.new 'MyProcedure', empty_args, body - expect { procedure.evaluate program_context } - .to raise_error(UndefinedVariableError) + body = CommandBlock.new [poner_cmd] + procedure = Procedure.new 'MyProcedure', no_arguments, body + expect { procedure.evaluate context }.to raise_error(UndefinedVariableError) end - it "should set arguments in the new context and they can be used" do - a_color = VarName.new 'a_color' - a_direction = VarName.new 'a_direction' + it "sets arguments in the new context so they can be used" do + a_color = 'a_color'.to_var_name + a_direction = 'a_direction'.to_var_name args = VarTuple.new [a_color, a_direction] mover_cmd = Mover.new a_direction poner_cmd = Poner.new a_color - body = CmdBlock.new [mover_cmd, poner_cmd] + body = CommandBlock.new [mover_cmd, poner_cmd] procedure = Procedure.new 'MyProc', args, body - procedure.evaluate program_context, [Negro.new, Norte.new] + procedure.evaluate context, [negro, norte] - expect(program_context.head.are_there_balls?(Negro.new)).to be_true - expect(program_context.head.y_pos).to eq(1) + expect(context.head.are_there_balls?(negro)).to be true + expect(context.head.y_pos).to eq(1) end - it "should not set arguments as var names in outer context" do - a_direction = VarName.new 'a_direction' + it "does not set arguments as var names in outer context" do + a_direction = 'a_direction'.to_var_name args = VarTuple.new [a_direction] procedure = Procedure.new 'MyProc', args, empty_body - procedure.evaluate program_context, [Oeste.new] + procedure.evaluate context, [oeste] - expect(program_context.has_variable_named?('a_direction')).to be_false + expect(context.has_variable_named?('a_direction')).to be false end - it "should fail if it is executed with more arguments than expected" do - procedure = Procedure.new 'MyProcedure', empty_args, empty_body + it "fails if it is executed with more arguments than expected" do + procedure = Procedure.new 'MyProcedure', no_arguments, empty_body error_message = "Wrong number of arguments in procedure 'MyProcedure': expected 0, got 1" - expect { procedure.evaluate program_context, [Norte.new] } + expect { procedure.evaluate context, [norte] } .to raise_error(WrongArgumentsError, error_message) end - it "should fail if it is executed with less arguments than expected" do - arg1, arg2 = VarName.new('arg1'), VarName.new('arg2') - args = VarTuple.new [arg1, arg2] + it "fails if it is executed with less arguments than expected" do + args = VarTuple.new ['arg1'.to_var_name, 'arg2'.to_var_name] procedure = Procedure.new 'MyProcedure2', args, empty_body error_message = "Wrong number of arguments in procedure 'MyProcedure2': expected 2, got 1" - expect { procedure.evaluate program_context, [Verde.new] } + expect { procedure.evaluate context, [verde] } .to raise_error(WrongArgumentsError, error_message) end -end \ No newline at end of file +end