def test_getvariable(self): interpreter, outcome = interpret( 'uiNamespace setVariable ["_x", 2]; uiNamespace getVariable "_x"') self.assertEqual(N(2), outcome) interpreter, outcome = interpret('uiNamespace getVariable ["_x", 2]') self.assertEqual(N(2), outcome)
def test_resize_array(self): interpreter = interpret('_x = [1,2]; _x resize 4')[0] self.assertEqual(Array([N(1), N(2), Nothing(), Nothing()]), interpreter['_x']) interpreter = interpret('_x = [1,2,3,4]; _x resize 2')[0] self.assertEqual(Array([N(1), N(2)]), interpreter['_x'])
def test_set(self): test = '_x = [1, 2]; _x set [0, 2];' interpreter, _ = interpret(test) self.assertEqual(Array([N(2), N(2)]), interpreter['_x']) test = '_x = [1, 2]; _x set [2, 3];' interpreter, _ = interpret(test) self.assertEqual(Array([N(1), N(2), N(3)]), interpreter['_x'])
def test_then(self): interpreter, outcome = interpret('_x = 1; if (true) then {_x = 2}') self.assertEqual(N(2), outcome) self.assertEqual(N(2), interpreter['_x']) interpreter, outcome = interpret('_x = 1; if (false) then {_x = 2}') self.assertEqual(Nothing(), outcome) self.assertEqual(N(1), interpreter['_x'])
def test_then_array(self): interpreter, outcome = interpret('if (true) then [{_x = 2}, {_x = 3}]') self.assertEqual(N(2), outcome) self.assertEqual(N(2), interpreter['_x']) interpreter, outcome = interpret( 'if (false) then [{_x = 2}, {_x = 3}]') self.assertEqual(N(3), outcome) self.assertEqual(N(3), interpreter['_x'])
def test_then_else(self): interpreter, outcome = interpret( 'if (true) then {_x = 2} else {_x = 3}') self.assertEqual(N(2), outcome) self.assertEqual(N(2), interpreter['_x']) interpreter, outcome = interpret( 'if (false) then {_x = 2} else {_x = 3}') self.assertEqual(N(3), outcome) self.assertEqual(N(3), interpreter['_x'])
def test_setvariable(self): interpreter, outcome = interpret( 'missionNamespace setVariable ["_x", 2];') self.assertEqual(N(2), interpreter.namespace('missionNamespace')['_x']) interpreter, outcome = interpret('uiNamespace setVariable ["_x", 2];') self.assertEqual(N(2), interpreter.namespace('uiNamespace')['_x']) self.assertTrue('_x' not in interpreter.namespace('missionNamespace')) self.assertEqual(Nothing(), interpreter['_x'])
def test_select(self): _, outcome = interpret('[1, 2] select 0') self.assertEqual(N(1), outcome) # alternative using floats _, outcome = interpret('[1, 2] select 0.5') self.assertEqual(N(1), outcome) _, outcome = interpret('[1, 2] select 0.6') self.assertEqual(N(2), outcome) # alternative using booleans _, outcome = interpret('[1, 2] select true') self.assertEqual(N(2), outcome) _, outcome = interpret('[1, 2] select false') self.assertEqual(N(1), outcome) # alternative using [start, count] _, outcome = interpret('[1, 2, 3] select [1, 2]') self.assertEqual(Array([N(2), N(3)]), outcome) _, outcome = interpret('[1, 2, 3] select [1, 10]') self.assertEqual(Array([N(2), N(3)]), outcome) with self.assertRaises(SQFParserError): _, outcome = interpret('[1, 2, 3] select [4, 10]') with self.assertRaises(SQFParserError): _, outcome = interpret('[1, 2, 3] select 10')
def test_publicVariableOther(self): sim = Simulation() id0 = sim.add_client(Client(sim)) id1 = sim.add_client(Client(sim)) # to server sim.clients[id0].execute('x = 2; publicVariableServer "x";') self.assertEqual(N(2), sim.server._interpreter['x']) self.assertEqual(Nothing(), sim.clients[id1]._interpreter['x']) # to client but not the server sim.clients[id0].execute('x = 3; 1 publicVariableClient "x";') self.assertEqual(N(2), sim.server._interpreter['x']) self.assertEqual(N(3), sim.clients[id1]._interpreter['x'])
def test_broadcast(self): sim = Simulation() id = sim.add_client(Client(sim)) sim.clients[id].execute( '"x" addPublicVariableEventHandler {y = _this select 1};') # broadcast at assignment and the PublicVariableEventHandler (PVEH) sim.server.execute('x = 123; publicVariable "x";') self.assertEqual(N(123), sim._clients[id]._interpreter['x']) self.assertEqual(N(123), sim._clients[id]._interpreter['y']) # broadcast to a JIP client updates the var but does not trigger the PVEH id = sim.add_client(Client(sim)) sim.clients[id].execute( '"x" addPublicVariableEventHandler {y = _this select 1};') self.assertEqual(N(123), sim._clients[id]._interpreter['x']) self.assertEqual(Nothing(), sim._clients[id]._interpreter['y'])
def test_forspec_exit_with_bool(self): test = ''' a = 0; b = true; for [{_i = 0}, {_i < 10 && b}, {_i = _i + 1}] do { a = a + 1; if (a >= 7) then {b = false} } ''' interpreter, outcome = interpret(test) self.assertEqual(Boolean(False), outcome) self.assertEqual(N(7), interpreter['a'])
def test_reference(self): # tests that changing _x affects _y when _y = _x. interpreter, _ = interpret('_x = [1, 2]; _y = _x; _x set [0, 2];') self.assertEqual(Array([N(2), N(2)]), interpreter['_x']) self.assertEqual(Array([N(2), N(2)]), interpreter['_y']) interpreter, _ = interpret('_x = [1, 2]; _y = _x; reverse _x;') self.assertEqual(Array([N(2), N(1)]), interpreter['_y'])
def test_pushBackUnique(self): interpreter, outcome = interpret('_x = [1]; _x pushBackUnique 2') self.assertEqual(Array([N(1), N(2)]), interpreter['_x']) self.assertEqual(N(1), outcome) interpreter, outcome = interpret('_x = [1, 2]; _x pushBackUnique 2') self.assertEqual(Array([N(1), N(2)]), interpreter['_x']) self.assertEqual(N(-1), outcome)
def test_with_comments(self): # _x=2;/* the two # the three # the four # */ # _x=3' s = Statement([ Statement([ V('_x'), Keyword('='), N(2)], ending=';'), Statement([ Statement([Comment('/* the two \n the three\n the four\n */'), EndOfLine('\n'), V('_x')]), Keyword('='), N(3) ]) ]) s.set_position((1,1)) self.assertEqual(Keyword('='), s[1][1]) self.assertEqual((5, 3), s[1][1].position)
def test_forvar_edges(self): # see comments on https://community.bistudio.com/wiki/for_var # start = end => runs once test = 'y = -10; for "_i" from 0 to 0 do {y = _i;};' interpreter, _ = interpret(test) self.assertEqual(N(0), interpreter['y']) # start < end => never runs interpreter, _ = interpret( 'y = -10; for "_i" from 0 to -1 do {y = _i;};') self.assertEqual(N(-10), interpreter['y']) # do not overwrite globals interpreter, _ = interpret('for "x" from 0 to 0 do {};') self.assertEqual(Nothing(), interpreter['x']) # nested test = '_array = []; for "_i" from 0 to 1 do {for "_i" from 0 to 1 do {_array pushBack _i;}; _array pushBack _i;};' interpreter, _ = interpret(test) self.assertEqual(Array([N(0), N(1), N(0), N(0), N(1), N(1)]), interpreter['_array'])
def test_one_scope(self): interpreter, outcome = interpret('_x = 1;') self.assertEqual(N(1), interpreter['_x']) interpreter, outcome = interpret('_x = 1; if true then {_x}') self.assertEqual(N(1), outcome) interpreter, outcome = interpret( '_x = 1; if (true) then {private "_x"; _x}') self.assertEqual(Nothing(), outcome) interpreter, outcome = interpret( '_x = 1; if (true) then {private "_x"; _x = 2}') self.assertEqual(N(2), outcome) self.assertEqual(N(1), interpreter['_x']) # without private, set it to the outermost scope interpreter, outcome = interpret('_x = 1; if (true) then {_x = 2}') self.assertEqual(N(2), outcome) self.assertEqual(N(2), interpreter['_x'])
def test_global(self): interpreter, _ = interpret('x = 1; if (true) then {x = 2;}') self.assertEqual(N(2), interpreter['x'])
def test_function(self): scope, outcome = interpret( '_max = {(_this select 0) max (_this select 1)};' '_maxValue = [3,5] call _max;') self.assertEqual(N(5), scope['_maxValue'])
def test_assign(self): interpreter, outcome = interpret('x = 2; _x = 1;') self.assertEqual(N(1), interpreter['_x']) self.assertEqual(N(2), interpreter['x'])
def test_foreach(self): test = 'y = 0; {y = y + _x + _foreachindex} forEach [1,2]' interpreter, _ = interpret(test) self.assertEqual(N(1 + 0 + 2 + 1), interpreter['y'])
def test_for_var_step(self): test = 'y = []; for "_i" from 1 to 10 step 2 do {y pushBack _i;};' interpreter, outcome = interpret(test) self.assertEqual(Array([N(1), N(3), N(5), N(7), N(9)]), interpreter['y'])
def test_for_var(self): test = 'y = []; for "_i" from 1 to 10 do {y pushBack _i;};' interpreter, outcome = interpret(test) self.assertEqual(Array([N(i) for i in range(1, 11)]), interpreter['y'])
def test_reverse(self): interpreter, outcome = interpret('_x = [1, 2]; reverse _x') self.assertEqual(Nothing(), outcome) self.assertEqual(Array([N(2), N(1)]), interpreter['_x'])
def test_subtract(self): test = '_x = [1, 2, 3, 2, 4]; _y = [2, 3]; _z = _x - _y' _, outcome = interpret(test) self.assertEqual(Array([N(1), N(4)]), outcome)
def test_find(self): _, outcome = interpret('"Hello world!" find "world!"') self.assertEqual(N(6), outcome)
def test_find(self): _, outcome = interpret('[1, 2] find 2') self.assertEqual(N(1), outcome) _, outcome = interpret('[1, 2] find 3') self.assertEqual(N(-1), outcome)
def test_to_array_string(self): outcome = interpret('toArray("AaŒ")')[1] self.assertEqual(Array([N(65), N(97), N(338)]), outcome) outcome = interpret('toString([65,97,338])')[1] self.assertEqual(String('"AaŒ"'), outcome)
def test_while(self): interpreter, outcome = interpret( '_x = 0; while {_x != 10} do {_x = _x + 1};') self.assertEqual(N(10), interpreter['_x'])
def test_ignore_comment(self): interpreter, outcome = interpret('_x = 2; // the two\n_y = 3;') self.assertEqual(N(2), interpreter['_x']) self.assertEqual(N(3), interpreter['_y'])
def test_forspec(self): interpreter, outcome = interpret( '_y = 0; for [{_x = 1},{_x <= 10},{_x = _x + 1}] do {_y = _y + 2}') self.assertEqual(N(20), interpreter['_y']) self.assertEqual(N(11), interpreter['_x']) self.assertEqual(N(20), outcome)