Game Development Community

dev|Pro Game Development Curriculum

Unit Tests for Torque Script

by BlueRaja · 07/20/2008 (10:10 am) · 4 comments

After learning a bit about TorqueScript, it seemed to me to be a very flexible scripting language with easy access to metadata - which is why I was so surprised to find out that nobody had written a unit testing framework for it yet.

So, I wrote one. It was written for TGB Pro, but will probably work on TGE Pro and may even be edited to work for those without the engine-source.

Please keep in mind that this was my first project using TorqueScript, so I probably did things in a very non-standard (or even incorrect) way - if anyone sees something that should be done a different way, let me know and I'll try to fix it.

To use the testing framework (aptly named TUnit), take the following steps:

1. Before you can use TUnit, you need to edit the engine to allow iteration through an object's methods. To do this, add the following code to simBase.cc and recompile (I placed it on line 1004, but it doesn't really matter):
ConsoleMethod(SimObject, getMethod, const char*, 3, 3, "(int index) - Gets the name of the method at the given index")
{
	S32 index = dAtoi(argv[2]);

	Namespace *nameSpace = object->getNamespace();
	Vector<Namespace::Entry *> entryList(__FILE__, __LINE__);

	if(nameSpace)
		nameSpace->getEntryList(&entryList);

	if(index<0 || index>=entryList.size())
		return "";
	return entryList[index]->mFunctionName;
}

ConsoleMethod(SimObject, getMethodCount, S32, 2, 2, "() - Returns the number of methods callable by this object")
{
	Namespace *nameSpace = object->getNamespace();
	Vector<Namespace::Entry *> entryList(__FILE__, __LINE__);

	if(nameSpace)
		nameSpace->getEntryList(&entryList);

	return entryList.size();
}
2. Download the TUnit source code (Github repository, mirror). Extract the files to your game/gameScripts folder.
3. Add the following line to startGame() in game/gameScripts/game.cs:
exec("./TUnit/TUnit.cs");

That's it! To begin writing tests, simply add a file to the Tests/ directory, and add this line somewhere in the file:
TUnit::registerClass(myClassName); //Replace myClassName with the name of your testing-class
Actual tests take this form:
function myClassName::testNameTest()
{
   //Test stuff here
}
Please note that All tests must end in the word test.
(For more info on writing tests, see the examples given in the Tests/ directory, as well as the methods available in Assert.cs).

Finally, to run the tests, simply run the game and type
TUnit::runTests(myClassName);
in the console. To run the tests for all classes, type
TUnit::runAllTests();

Here is an example screenshot:
www.blueraja.com/images/2008-07-20_1103.png

The next thing to do would probably be to write up a mocking framework. If I ever get not-lazy, perhaps I'll do that.

[Edit] I got not lazy.

- BlueRaja

About the author

Recent Blogs

• Mock-objects for TorqueScript

#1
07/20/2008 (1:03 pm)
v. interesting!
#2
07/20/2008 (6:35 pm)
Useful! Great addition.
#3
07/21/2008 (4:56 am)
If you have TGEA, there's a engine based framework in the engine/unit directory that's worth looking at. Pretty straightforward to expand upon and add your own unit tests.
#4
07/21/2008 (3:57 pm)
Congratulations! This looks like a great solution to the problem. I've pointed my old posts to here.