unit testing again...

For AI and campaign script related discussions and questions
Post Reply
User avatar
aubergine
Professional
Professional
Posts: 3459
Joined: 10 Oct 2010, 00:58
Contact:

unit testing again...

Post by aubergine »

So, I'm finally getting round to finishing the unit testing for my Util API, and I've run in to a stumbling block -- maybe someone with more experience of unit testing can help?

I've got a function like this, within the API's sandbox:

Code: Select all

	var difficultyIndex = Math.max( 0, ([EASY, MEDIUM, HARD, INSANE]).indexOf(difficulty) );

	_global.addConst(
		"difficultyScale",
		function(/* easy, medium, hard, insane */) {
			return arguments[ Math.min( arguments.length-1, difficultyIndex ) ];
		}
	);
That adds an immutable 'difficultyScale()' function to the global object (docs).

I can easily test different parameter combinations, but I can't test them in relation to different 'difficultyIndex' values.

Any ideas on how to accomplish that?
"Dedicated to discovering Warzone artefacts, and sharing them freely for the benefit of the community."
-- https://warzone.atlassian.net/wiki/display/GO
User avatar
aubergine
Professional
Professional
Posts: 3459
Joined: 10 Oct 2010, 00:58
Contact:

Re: unit testing again...

Post by aubergine »

Found a way to deal with above scenario -- I added an accessor to the _global.difficultyScale() method, which allows the difficultyIndex to be overriden like so:

Code: Select all

// set
difficultyScale.level = INSANE;
// get
var foo = difficultyScale.level; // INSANE
"Dedicated to discovering Warzone artefacts, and sharing them freely for the benefit of the community."
-- https://warzone.atlassian.net/wiki/display/GO
Post Reply