(info) This AI is still in early stages of development.

Test API - possible future direction

Rough ideas for future development...

 

Goals

Extend the abilities of unit tests:

  • Allow additional setup / teardown to be defined at unit test level
  • Allow a more narrative style of test script

Setup / Teardown

Setup and teardown is already available at module-level - see Test.module() and Module Lifecycle Object for details.

Adding these features at unit test level should be relatively easy, the test script would look like this:

Test("test name", Test.ANY( ), function() {
	setup(function() {
		// stuff to do before this unit starts
	});
 
	teardown(function() {
		// stuff to do after this unit finishes
	});
});

Narrative test scripts

BDD promotes an approach like this:

Story: Returns go to stock

In order to keep track of stock
As a store owner
I want to add items back to stock when they're returned

Scenario 1: Refunded items should be returned to stock
Given a customer previously bought a black sweater from me
And I currently have three black sweaters left in stock
When he returns the sweater for a refund
Then I should have four black sweaters in stock

Using something like Cucumber, that can actually be parsed and used to perform the tests. However, such notation is really aimed at commercial projects where there's a project manager or customer who wants to read and write tests in plain English. I doubt anyone writing unit tests for Warzone scripts will be such a person! (although I can see other interesting uses for Cucumber-like stuff in WZ, eg. to define campaign missions...)

For unit testing, I'm thinking something more along the lines of spectIt (based on RSpec). The above "script" would become:

Test("Returns go to stock", Test.ANY(), function() {
 
	// In order to keep track of stock
	// As a store owner
	// I want to add items back to stock when they're returned
	
	var stock, item;
 
	setup(function() {
		// imagine we have a stock class, and we make an instance of it...
		stock = new Stock("BlackSweater", "BlackSweater", "BlackSweater", "BlackSweater");
		// stock = ["BlackSweater", "BlackSweater", "BlackSweater", "BlackSweater"]
		// stock.remove(item) = removes item of specific name from stock
		// stock.add(item) = adds item of specific name to stock
		// stock.count(item) = counts number of item in stock
	});
 
	teardown(function() {
		stock = null;
		item = null;
	});
 
	it("should be returned to stock when refunded", function() {
		// Given a customer previously bought a black sweater from me
		item = stock.remove("BlackSweater");
		// And I currently have three black sweaters left in stock
		asssert(stock).should(lookLike, ["BlackSweater", "BlackSweater", "BlackSweater"]);
		// When he returns the sweater for a refund
		stock.add(item);
		// Then I should have four black sweaters in stock
		assert(stock.count("BlackSweater")).should(equal, 4);
	}
 
});