package Wurstunit import NoWurst import Integer import Real import Boolean import Annotations // Wurst unit testing library /** Functions annotated with @test can be executed as unit tests. */ @annotation public function test() @compiletimenative public function println(string msg) DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, msg) @compiletimenative public function testFail(string msg) @compiletimenative public function testSuccess() /** * Asserts that two ints are equal. If they are not, an error is thrown with a message. */ public function int.assertEquals(int expected) if this != expected testFail("Expected <" + expected.toString() + ">, Actual <" + this.toString() + ">") /** * Asserts that the passed int is greater than the parameter. If they are not, an error is thrown with a message. */ public function int.assertGreaterThan(int less) if this <= less testFail("Expected <" + this.toString() + "> to be greater than <" + less.toString() + ">") /** * Asserts that the passed int is greater than the parameter. If they are not, an error is thrown with a message. */ public function int.assertGreaterThanOrEqual(int less) if this < less testFail("Expected <" + this.toString() + "> to be greater than or equal to <" + less.toString() + ">") /** * Asserts that the passed int is greater than the parameter. If they are not, an error is thrown with a message. */ public function int.assertLessThan(int greater) if this >= greater testFail("Expected <" + this.toString() + "> to be less than <" + greater.toString() + ">") /** * Asserts that the passed int is greater than the parameter. If they are not, an error is thrown with a message. */ public function int.assertLessThanOrEqual(int greater) if this > greater testFail("Expected <" + this.toString() + "> to be less than or equal to <" + greater.toString() + ">") /** * Asserts that two strings are equal. If they are not, an error is thrown with a message. */ public function string.assertEquals(string expected) if this != expected testFail("Expected <" + expected + ">, Actual <" + this + ">") /** * Asserts that two reals are equal. If they are not, an error is thrown with a message. */ public function real.assertEquals(real expected) if this != expected testFail("Expected <" + expected.toString() + ">, Actual <" + this.toString() + ">") /** * Asserts that the passed real is greater than the parameter. If they are not, an error is thrown with a message. */ public function real.assertGreaterThan(real less) if this <= less testFail("Expected <" + this.toString() + "> to be greater than <" + less.toString() + ">") /** * Asserts that the passed real is greater than the parameter. If they are not, an error is thrown with a message. */ public function real.assertGreaterThanOrEqual(real less) if this < less testFail("Expected <" + this.toString() + "> to be greater than or equal to <" + less.toString() + ">") /** * Asserts that the passed real is greater than the parameter. If they are not, an error is thrown with a message. */ public function real.assertLessThan(real greater) if this >= greater testFail("Expected <" + this.toString() + "> to be less than <" + greater.toString() + ">") /** * Asserts that the passed real is greater than the parameter. If they are not, an error is thrown with a message. */ public function real.assertLessThanOrEqual(real greater) if this > greater testFail("Expected <" + this.toString() + "> to be less than or equal to <" + greater.toString() + ">") /** * Asserts that two reals are equal to within a positive delta. If they are not, an error is thrown * with a message. */ public function real.assertEquals(real expected, real delta) if (this - expected > delta) or (expected - this > delta) testFail("Expected <" + expected.toString() + ">, Actual <" + this.toString() + " with delta " + delta.toString() + ">") /** * Asserts that two booleans are equal. If they are not, an error is thrown with a message. */ public function boolean.assertEquals(boolean expected) if this != expected testFail("Expected <" + expected.toString() + ">, Actual <" + this.toString() + ">") /** Asserts that this booleans is true. If it is not, an error is thrown with a message. */ public function boolean.assertTrue() this.assertEquals(true) /** Asserts that this booleans is false. If it is not, an error is thrown with a message. */ public function boolean.assertFalse() this.assertEquals(false) /** Asserts that this booleans is true. If it is not, an error is thrown. */ public function assertTrue(boolean b) if not b testFail("Assertion failed") /** Asserts that this booleans is true. If it is not, an error is thrown with the supplied message. */ public function assertTrue(boolean b, string msg) if not b testFail("Assertion failed: " + msg) /** Asserts that the reference t is not null */ public function assertNotNull(T t) if t == null testFail("Assertion failed: Reference was null.")