Skip to content

Instantly share code, notes, and snippets.

@jamesu
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesu/4966d4689689d058f38a to your computer and use it in GitHub Desktop.
Save jamesu/4966d4689689d058f38a to your computer and use it in GitHub Desktop.
Tests some basic code patching features in TorqueScript
enableWinConsole(true);
setLogMode(2);
$fuck = "ERRR";
echo("TEST EXPRESSION" @ (1 + 2) @ $fuck SPC "SPACE");
for (%i=0; %i<10; %i++)
{
echo("loop" SPC %i);
}
echo("After LOOP");
for (%i=0; %i<10; %i++)
{
echo("next loop" SPC %i);
if (%i > 0)
break;
}
echo("After LOOP 2");
for (%i=0; %i<10; %i++)
{
echo("alternate loop " @ %i);
continue;
echo("next loop" SPC %i);
if (%i > 0)
break;
}
echo("After LOOP 3");
for (%i=0; %i<10; %i++)
{
echo("inner loop " @ %i);
for (%x=0; %x<20; %x++)
{
echo("inner inner loop " @ %i SPC %x);
continue;
}
continue;
}
echo("After LOOP 4");
$foo = new SimGroup(MyObj);
MyObj.dump();
for (%i=0; %i<10; %i++)
{
%a = new SimObject();
%a.counter = %i;
MyObj.add(%a);
}
echo("Test itr 1");
foreach(%b in MyObj)
{
echo(%b.counter);
continue;
echo("print " @ %b.getId());
}
echo("Test itr 2");
%c = 0;
foreach(%b in MyObj)
{
%c++;
echo(%b.counter);
if (%c > 5)
break;
}
echo("Done");
$test = "woo";
if ($test $= "woo")
{
echo("Test conditional 1");
}
else
{
echo("Test conditional 1 failed");
}
echo("--");
if ($test $= "woo")
{
echo("Test conditional 2");
}
else if ($test $= "boo")
{
echo("Test conditional 2 failed");
}
echo("--");
if ($test $= "boo")
{
echo("Test conditional 3 failed");
}
else if ($test $= "woo")
{
echo("Test conditional 3");
}
echo("--");
// conditional exprs
%value1 = 1;
$value2 = 2;
$value = $value2 == 2 ? "right" : "wrong";
echo("Conditional result 1 == " @ $value);
$value = $value1 == 2 ? "wrong!" : "right!";
echo("Conditional result 2 == " @ $value);
$value = $value1 > 1 ? "wrong" : "right";
echo("Conditional result 3 == " @ $value);
$value = $value2 > 1 ? "right!" : "wrong!";
echo("Conditional result 4 == " @ $value);
echo("--");
// conditional blocks
if ($test $= "eee")
{
echo("Test conditional 4 failed");
}
else if ($test $= "aaa")
{
echo("Test conditional 4 failed");
}
else
{
echo("Test conditional 4");
}
echo("--");
echo("FUNCTION TESTS FOLLOW");
function testFunc(%a, %b, %c)
{
echo("testFunc called..." SPC %a SPC %b SPC %c);
}
testFunc(1,2,3);
function testFunc2(%a, %b, %c)
{
echo("testFunc2 called..." SPC %a SPC %b SPC %c);
return;
echo("testFunc2 echo should not be seen");
}
testFunc2(1,2,3);
function TestNS::testFunc(%a, %b, %c)
{
echo("TestNS::testFunc called..." SPC %a SPC %b SPC %c);
}
TestNS::testFunc(1,2,3);
function testEmptyFunc(%a, %b, %c)
{
}
testEmptyFunc();
echo("Done");
package testPackage
{
function testFunc(%a, %b, %c)
{
echo("testPackage testFunc called..." SPC %a SPC %b SPC %c);
}
};
function callFuncFromFunc()
{
testFunc(1,2,3);
}
// Should print "testFunc called..."
callFuncFromFunc();
activatePackage(testPackage);
// Should print "testPackage testFunc called..."
callFuncFromFunc();
testFunc(p1, p2, p3);
function dummyReturn()
{
echo("Dummy func should not be called");
return 123;
}
// Test error handler (object already exists)
echo("--");
new SimObject(MyObj)
{
foo = dummyReturn();
err = 456;
};
echo("Should only see this message");
quit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment