[utest commit] r9 - in trunk: . src src/tests src/tests/iterations src/tests/requests src/tests/utest src/utest ...

0 views
Skip to first unread message

codesite...@google.com

unread,
Aug 30, 2008, 8:04:04 AM8/30/08
to utes...@googlegroups.com
Author: franco.ponticelli
Date: Sat Aug 30 05:02:20 2008
New Revision: 9

Added:
trunk/src/Test.hx
trunk/src/tests/
trunk/src/tests/MiscTest.hx
trunk/src/tests/iterations/
trunk/src/tests/iterations/Iteration1.hx
trunk/src/tests/iterations/Iteration2.hx
trunk/src/tests/iterations/TestCaseClass.hx
trunk/src/tests/iterations/TestClass.hx
trunk/src/tests/requests/
trunk/src/tests/requests/RequestTest.hx
trunk/src/tests/utest/
trunk/src/utest/
trunk/src/utest/Assert.hx
trunk/src/utest/Assertation.hx
trunk/src/utest/Runner.hx
trunk/src/utest/TestFixture.hx
trunk/src/utest/TestHandler.hx
trunk/src/utest/TestResult.hx
trunk/src/utest/ui/
trunk/src/utest/ui/common/
trunk/src/utest/ui/common/TestClass.hx
trunk/src/utest/ui/common/TestPackage.hx
trunk/src/utest/ui/text/
trunk/src/utest/ui/text/TraceReport.hx
Removed:
trunk/Test.hx
trunk/utest/

Log:


Added: trunk/src/Test.hx
==============================================================================
--- (empty file)
+++ trunk/src/Test.hx Sat Aug 30 05:02:20 2008
@@ -0,0 +1,13 @@
+import utest.Runner;
+import utest.ui.text.TraceReport;
+
+class Test {
+ public static function main() {
+// haxe.Firebug.redirectTraces();
+
+ var runner = new Runner();
+ runner.addCase(new tests.requests.RequestTest());
+ var report = new TraceReport(runner);
+ runner.run();
+ }
+}
\ No newline at end of file

Added: trunk/src/tests/MiscTest.hx
==============================================================================
--- (empty file)
+++ trunk/src/tests/MiscTest.hx Sat Aug 30 05:02:20 2008
@@ -0,0 +1,65 @@
+enum EmptyEnum {
+}
+
+class TestMisc {
+ public function new(){}
+
+ #if !(flash8 || flash7 || flash6 || js)
+ public function testZero(){
+ var s = "x\001\000y";
+ Assert.equals(4,s.length);
+ Assert.equals(1,s.charCodeAt(1));
+ Assert.equals(0,s.charCodeAt(2));
+ Assert.equals("y",s.charAt(3));
+ }
+ #end
+
+ public function testEmptyEnum(){
+ try {
+ Assert.equals(null,Type.resolveEnum("EmptyEnum"));
+ }catch( e : Dynamic )
+ Assert.isTrue(true);
+ }
+
+ public function testForInt(){
+ var sb = new StringBuf();
+ for( i in 0...5 ){
+ sb.add( Std.string(i) );
+ }
+ Assert.equals("01234",sb.toString());
+ }
+
+ public function testForIntBreak(){
+ var sb = new StringBuf();
+ for( i in 0...5 ){
+ sb.add( Std.string(i) );
+ if( i == 2 )
+ break;
+ }
+ Assert.equals("012",sb.toString());
+ }
+
+ public function testForIntContinue(){
+ var sb = new StringBuf();
+ for( i in 0...5 ){
+ sb.add( Std.string(i) );
+ if( i == 2 )
+ continue;
+ }
+ Assert.equals("01234",sb.toString());
+ }
+
+ public function testForIntIter(){
+ var sb = new StringBuf();
+ var ii = new IntIter(0,5);
+
+ for( i in ii ){
+ sb.add( Std.string(i) );
+ if( i == 2 ){
+ i++;
+ continue;
+ }
+ }
+ Assert.equals("01234",sb.toString());
+ }
+}

Added: trunk/src/tests/iterations/Iteration1.hx
==============================================================================
--- (empty file)
+++ trunk/src/tests/iterations/Iteration1.hx Sat Aug 30 05:02:20 2008
@@ -0,0 +1,514 @@
+package tests.iterations;
+
+import utest.Assert;
+import utest.TestFixture;
+import utest.TestHandler;
+
+class Iteration1 {
+ static inline var TIMEOUT = 50;
+ static inline var DELAY = 5;
+ function new();
+
+ // #1
+ public function testTestClass() {
+ var subject = new TestClass();
+ subject.test();
+ trace(subject.tested ? "OK #1" : "FAIL");
+ }
+
+ // #2
+ public function testHandlerExecute() {
+ var fixture = new TestFixture(new TestClass(), "test");
+ var handler = new TestHandler(fixture);
+ handler.execute();
+ trace(fixture.target.tested ? "OK #2" : "FAIL");
+ }
+
+ // #3
+ public function testHandlerProtectedContext() {
+ var fixture = new TestFixture(new TestClass(), "throwError");
+ var handler = new TestHandler(fixture);
+ try {
+ handler.execute();
+ trace("OK #3");
+ } catch(e : Dynamic) {
+ trace("FAIL");
+ }
+ }
+
+ // #4
+ public function assertTrueSuccess() {
+ Assert.results = new List();
+ Assert.isTrue(true, null);
+ var r = Assert.results.pop();
+ Assert.results = null;
+ switch(r) {
+ case Success(p):
+ trace(p != null ? "OK #4" : "FAIL");
+ default:
+ trace("FAIL");
+ }
+ }
+
+ // #5
+ public function assertTrueFailureNoMessage() {
+ Assert.results = new List();
+ Assert.isTrue(false); // null message
+ var r = Assert.results.pop();
+ Assert.results = null;
+ switch(r) {
+ case Failure(m, p):
+ trace(p != null ? "OK #5" : "FAIL");
+ default:
+ trace("FAIL");
+ }
+ }
+
+ // #6
+ public function assertTrueFailureMessage() {
+ Assert.results = new List();
+ Assert.isTrue(false, "FAIL");
+ var r = Assert.results.pop();
+ Assert.results = null;
+ switch(r) {
+ case Failure(m, p):
+ trace((m == "FAIL" && p != null) ? "OK #6" : "FAIL");
+ default:
+ trace("FAIL");
+ }
+ }
+
+ // #7
+ public function testHandlerHasResults() {
+ var fixture = new TestFixture(new TestClass(), "assertTrue");
+ var handler = new TestHandler(fixture);
+ handler.execute();
+
+ trace(handler.fixture != null && handler.results.length == 1 ? "OK
#7" : "FAIL");
+ }
+
+ // #8
+ public function testHandlerHasError() {
+ var fixture = new TestFixture(new TestClass(), "throwError");
+ var handler = new TestHandler(fixture);
+ handler.execute();
+
+ trace(handler.fixture != null && handler.results.length == 1 ? "OK
#8" : "FAIL");
+ switch(handler.results.pop()) {
+ case Error(e):
+ trace(e == "error" ? "OK #9" : "FAIL");
+ default:
+ trace("FAIL");
+ }
+ }
+
+ // #9
+ public function testHandlerSetup() {
+ var fixture = new TestFixture(new TestClass(), "test", "setup");
+ var handler = new TestHandler(fixture);
+ handler.execute();
+ trace(fixture.target.donesetup ? "OK #10" : "FAIL");
+ }
+
+ // #10
+ public function testHandlerSetupError() {
+ var fixture = new TestFixture(new TestClass(), "test", "throwError"); //
setup now throws an exception
+ var handler = new TestHandler(fixture);
+ handler.execute();
+ switch(handler.results.pop()) {
+ case SetupError(e):
+ trace(e == "error" ? "OK #11" : "FAIL");
+ default:
+ trace("FAIL");
+ }
+ }
+
+ // #11
+ public function testHandlerCompleteSync() {
+ var fixture = new TestFixture(new TestClass(), "test");
+ var handler = new TestHandler(fixture);
+ var flag = false;
+ handler.onTested = function(h) {
+ flag = true;
+ }
+ handler.execute();
+ trace(flag ? "OK #12" : "FAIL");
+ }
+
+ // #12
+ public function testHandlerSetTimeout() {
+ var fixture = new TestFixture(new TestClass(), "test");
+ var handler = new TestHandler(fixture);
+
+ trace(handler.expireson == null ? "OK #13" : "FAIL");
+ var before = haxe.Timer.stamp();
+ handler.setTimeout(1);
+ var after = haxe.Timer.stamp();
+ trace(handler.expireson >= before ? "OK #14" : "FAIL");
+ trace(handler.expireson <= (after+1) ? "OK #15" : "FAIL");
+ }
+
+ // #13
+ public function testHandlerTimeout() {
+ var fixture = new TestFixture(new TestClass(), "test");
+ var handler = new TestHandler(fixture);
+ var flag1 = false;
+ var flag2 = false;
+ handler.addAsync(function() {
+ // do nothing
+ }, TIMEOUT);
+ handler.onTimeout = function(h) {
+ trace(flag1 ? "OK #15" : "FAIL");
+ flag2 = true;
+ }
+ handler.onTested = function(h) {
+ trace("FAIL");
+ }
+ trace(flag1 ? "FAIL" : "OK #16");
+ flag1 = true;
+ handler.execute();
+#if (flash || js)
+ haxe.Timer.delay(function() trace(flag2 ? "OK #17" : "FAIL"), TIMEOUT*2);
+#else
+ trace(flag2 ? "OK #17" : "FAIL");
+#end
+ }
+
+ // #14
+ public function testAsync() {
+ var fixture = new TestFixture(new TestClass(), "test");
+ var handler = new TestHandler(fixture);
+ var flag = false;
+ var async = handler.addAsync(function() {
+ trace(flag ? "FAIL" : "OK #18");
+ flag = true;
+ }, TIMEOUT*2);
+ handler.onTimeout = function(h) {
+ trace("TIMEOUT");
+ }
+ handler.onTested = function(h) {
+ trace(flag ? "OK #19" : "FAIL");
+ }
+#if (flash || js)
+ haxe.Timer.delay(function() async(), DELAY);
+#else
+ async();
+#end
+ handler.execute();
+ }
+
+ // #15
+ public function testEvent() {
+ var fixture = new TestFixture(new TestClass(), "test");
+ var handler = new TestHandler(fixture);
+ var value = "";
+ var expected = "haxe";
+ var async = handler.addEvent(function(s) {
+ trace(s == expected ? "OK #20" : "FAIL");
+ value = s;
+ }, TIMEOUT*2);
+ handler.onTimeout = function(h) {
+ trace("TIMEOUT");
+ }
+ handler.onTested = function(h) {
+ trace(value == expected ? "OK #21" : "FAIL");
+ }
+#if (flash || js)
+ haxe.Timer.delay(function() async(expected), DELAY);
+#else
+ async(expected);
+#end
+ handler.execute();
+ }
+
+ // #16
+ public function testMultipleAsync() {
+ var fixture = new TestFixture(new TestClass(), "test");
+ var handler = new TestHandler(fixture);
+ var value = "";
+ var async1 = handler.addAsync(function() {
+ value += "1";
+ }, TIMEOUT*4);
+ var async2 = handler.addAsync(function() {
+ value += "2";
+ }, TIMEOUT*2);
+ handler.onTimeout = function(h) {
+ trace("TIMEOUT");
+ }
+ handler.onTested = function(h) {
+ trace(value == "12" ? "OK #22" : "FAIL");
+ }
+#if (flash || js)
+ haxe.Timer.delay(function() async2(), TIMEOUT*2);
+ async1();
+#else
+ async1();
+ async2();
+#end
+ handler.execute();
+ }
+
+ // #17
+ public function testAsyncRunTwice() {
+ var fixture = new TestFixture(new TestClass(), "test");
+ var handler = new TestHandler(fixture);
+ var value = 0;
+ var async = handler.addAsync(function() {
+ value++;
+ }, TIMEOUT*2);
+ handler.onTimeout = function(h) {
+ trace("TIMEOUT");
+ }
+ handler.onTested = function(h) {
+ trace(value == 1 ? "OK #23" : "FAIL");
+ }
+ async();
+ async();
+ handler.execute();
+ }
+
+ // #18
+ public function testAsyncRunTwiceGeneratesError() {
+ var fixture = new TestFixture(new TestClass(), "test");
+ var handler = new TestHandler(fixture);
+ var value = 0;
+ var async = handler.addAsync(function() { }, TIMEOUT*2);
+ handler.onTimeout = function(h) {
+ trace("TIMEOUT");
+ }
+ handler.onTested = function(h) {
+ switch(handler.results.pop()) {
+ case AsyncError(_):
+ trace("OK #24");
+ default:
+ trace("FAIL");
+ }
+ }
+ async();
+ async();
+ handler.execute();
+ }
+
+ // #19 I totally forgot about that
+ public function testTimeoutGeneratesError() {
+ var fixture = new TestFixture(new TestClass(), "test");
+ var handler = new TestHandler(fixture);
+ var value = 0;
+ var async = handler.addAsync(function() { }, TIMEOUT);
+ handler.onTimeout = function(h) {
+ switch(handler.results.pop()) {
+ case TimeoutError(i):
+ trace(i == 1 ? "OK #25" : "FAIL");
+ default:
+ trace("FAIL");
+ }
+ }
+ handler.execute();
+ }
+
+ // #20
+ public function testAsyncThrowException() {
+ var fixture = new TestFixture(new TestClass(), "test");
+ var handler = new TestHandler(fixture);
+ var async = handler.addAsync(function() throw "error", TIMEOUT);
+ handler.onTested = function(h) {
+ switch(handler.results.pop()) {
+ case AsyncError(_):
+ trace("OK #26");
+ default:
+ trace("FAIL");
+ }
+ }
+#if (flash || js)
+ haxe.Timer.delay(function() async(), DELAY);
+#else
+ async();
+#end
+ handler.execute();
+ }
+
+ // #21
+ public function testEmptyTestGeneratesWarning() {
+ var fixture = new TestFixture(new TestClass(), "test");
+ var handler = new TestHandler(fixture);
+ handler.execute();
+ switch(handler.results.pop()) {
+ case Warning(_):
+ trace("OK #27");
+ default:
+ trace("FAIL");
+ }
+ }
+
+ // #22
+ public function testOnComplete() {
+ var fixture = new TestFixture(new TestClass(), "test");
+ var handler = new TestHandler(fixture);
+ handler.onComplete = function(h) {
+ trace("OK #28");
+ }
+ handler.execute();
+ }
+
+ // #23
+ public function testOnCompleteSequence() {
+ var fixture = new TestFixture(new TestClass(), "test");
+ var handler = new TestHandler(fixture);
+ var value = "";
+ handler.onTested = function(h) {
+ value += "1";
+ }
+ handler.onTimeout = function(h) {
+ value += "2";
+ }
+ handler.onComplete = function(h) {
+ trace(value == "1" ? "OK #29" : "FAIL");
+ }
+ handler.execute();
+ }
+
+ // #24
+ public function testOnCompleteSequenceAsyncOk() {
+ var fixture = new TestFixture(new TestClass(), "test");
+ var handler = new TestHandler(fixture);
+ var async = handler.addAsync(function() {}, TIMEOUT);
+ var value = "";
+ handler.onTested = function(h) {
+ value += "1";
+ }
+ handler.onTimeout = function(h) {
+ value += "2";
+ }
+ handler.onComplete = function(h) {
+ trace(value == "1" ? "OK #30" : "FAIL");
+ }
+#if (flash || js)
+ haxe.Timer.delay(function() async(), DELAY);
+#else
+ async();
+#end
+ handler.execute();
+ }
+
+ // #25
+ public function testOnCompleteSequenceAsyncTimeout() {
+ var fixture = new TestFixture(new TestClass(), "test");
+ var handler = new TestHandler(fixture);
+ handler.addAsync(function() {}, DELAY);
+ var value = "";
+ handler.onTested = function(h) {
+ value += "1";
+ }
+ handler.onTimeout = function(h) {
+ value += "2";
+ }
+ handler.onComplete = function(h) {
+ trace(value == "2" ? "OK #31" : "FAIL");
+ }
+ handler.execute();
+ }
+
+ // #26
+ public function testTeardown() {
+ var fixture = new TestFixture(new TestClass(), "test", null, "teardown");
+ var handler = new TestHandler(fixture);
+ handler.onTested = function(h) {
+ trace(fixture.target.doneteardown ? "FAIL" : "OK #32");
+ }
+ handler.onTimeout = function(h) {
+ trace("FAIL");
+ }
+ handler.onComplete = function(h) {
+ trace(fixture.target.doneteardown ? "OK #33" : "FAIL");
+ }
+ handler.execute();
+ }
+
+ // #27
+ public function testTeardownAsync() {
+ var fixture = new TestFixture(new TestClass(), "test", null, "teardown");
+ var handler = new TestHandler(fixture);
+ var async = handler.addAsync(function(){}, TIMEOUT);
+ handler.onTested = function(h) {
+ trace(fixture.target.doneteardown ? "FAIL" : "OK #34");
+ }
+ handler.onTimeout = function(h) {
+ trace("FAIL");
+ }
+ handler.onComplete = function(h) {
+ trace(fixture.target.doneteardown ? "OK #35" : "FAIL");
+ }
+#if (flash || js)
+ haxe.Timer.delay(function() async(), DELAY);
+#else
+ async();
+#end
+ handler.execute();
+ }
+
+ // #28
+ public function testExecutionTime() {
+ var fixture = new TestFixture(new TestClass(), "test");
+ var handler = new TestHandler(fixture);
+ var start = haxe.Timer.stamp();
+ handler.execute();
+ var time = Std.int(1000*(haxe.Timer.stamp() - start));
+ trace((!Math.isNaN(handler.executionTime))? "OK #36" : "FAIL");
+ trace(handler.executionTime >= 0 ? "OK #37" : "FAIL");
+ trace(handler.executionTime <= time ? "OK #38" : "FAIL");
+ }
+
+ // #29
+ public function testExecutionTimeAsync() {
+ var fixture = new TestFixture(new TestClass(), "test");
+ var handler = new TestHandler(fixture);
+ var async = handler.addAsync(function(){}, TIMEOUT);
+ var start = haxe.Timer.stamp();
+ handler.onComplete = function(h) {
+ var time = Std.int(1000*(haxe.Timer.stamp() - start));
+ trace((!Math.isNaN(handler.executionTime))? "OK #39" : "FAIL");
+ trace(handler.executionTime >= 0 ? "OK #40" : "FAIL");
+ trace(handler.executionTime <= time ? "OK #41" : "FAIL");
+ }
+
+#if (flash || js)
+ haxe.Timer.delay(function() async(), DELAY);
+#else
+ async();
+#end
+ handler.execute();
+ }
+
+ public static function main() {
+ var t = new Iteration1();
+ t.testTestClass();
+ t.testHandlerExecute();
+ t.testHandlerProtectedContext();
+ t.assertTrueSuccess();
+ t.assertTrueFailureNoMessage();
+ t.assertTrueFailureMessage();
+ t.testHandlerHasResults();
+ t.testHandlerHasError();
+ t.testHandlerSetup();
+ t.testHandlerSetupError();
+ t.testHandlerCompleteSync();
+ t.testHandlerSetTimeout();
+ t.testHandlerTimeout();
+ t.testAsync();
+ t.testEvent();
+ t.testMultipleAsync();
+ t.testAsyncRunTwice();
+ t.testAsyncRunTwiceGeneratesError();
+ t.testTimeoutGeneratesError();
+ t.testAsyncThrowException();
+ t.testEmptyTestGeneratesWarning();
+ t.testOnComplete();
+ t.testOnCompleteSequence();
+ t.testOnCompleteSequenceAsyncOk();
+ t.testOnCompleteSequenceAsyncTimeout();
+ t.testTeardown();
+ t.testTeardownAsync();
+ t.testExecutionTime();
+ t.testExecutionTimeAsync();
+ }
+}
\ No newline at end of file

Added: trunk/src/tests/iterations/Iteration2.hx
==============================================================================
--- (empty file)
+++ trunk/src/tests/iterations/Iteration2.hx Sat Aug 30 05:02:20 2008
@@ -0,0 +1,126 @@
+package tests.iterations;
+
+import utest.Assert;
+import utest.Runner;
+import utest.TestFixture;
+
+class Iteration2 {
+ public function new();
+
+ // #1
+ public function testRunnerRun() {
+ var r = new Runner();
+ r.fixtures.add(new TestFixture(new TestClass(), "assertTrue"));
+ r.run();
+ trace(r.results.length == 1 ? "OK @1" : "FAIL");
+ }
+
+ // #2
+ public function testAssertCreateAsync() {
+ var r = new Runner();
+ r.fixtures.add(new TestFixture(new TestClass(), "assertAsync"));
+ r.onComplete = function(r){
+ trace(r.results.length == 1 ? "OK @2" : "FAIL");
+ }
+ r.run();
+ }
+
+ // #3
+ public function testRunnerSequenceOnAsync() {
+ var r = new Runner();
+ var test = new TestSequenceClass();
+ r.fixtures.add(new TestFixture(test, "test1"));
+ r.fixtures.add(new TestFixture(test, "test2"));
+ r.fixtures.add(new TestFixture(test, "test3"));
+ r.onComplete = function(r){
+ trace(test.seq == "123" ? "OK @3" : "FAIL");
+ }
+ r.run();
+ }
+
+ // #4
+ public function testRunnerAddCase() {
+ var r = new Runner();
+ r.addCase(new TestCaseClass());
+ trace(r.fixtures.length == 1 ? "OK @4" : "FAIL");
+ var fix = r.fixtures.pop();
+ trace(fix.method == "testOne" ? "OK @5" : "FAIL");
+ trace(fix.setup == null ? "OK @6" : "FAIL");
+ trace(fix.teardown == "teardown" ? "OK @7" : "FAIL");
+ }
+
+ // #5
+ public function testRunnerAddCaseCustomFun() {
+ var r = new Runner();
+ r.addCase(new TestCaseClass(), "_setup", "_teardown");
+ var fix = r.fixtures.pop();
+ trace(fix.setup == "_setup" ? "OK @8" : "FAIL");
+ trace(fix.teardown == null ? "OK @9" : "FAIL");
+ }
+
+ // #6
+ public function testRunnerAddCaseCustomPrefix() {
+ var r = new Runner();
+ r.addCase(new TestCaseClass(), "_setup", "teardown", "Test");
+ trace(r.fixtures.length == 1 ? "OK @10" : "FAIL ");
+ var fix = r.fixtures.pop();
+ trace(fix.method == "TestTwo" ? "OK @11" : "FAIL");
+ trace(fix.setup == "_setup" ? "OK @12" : "FAIL");
+ trace(fix.teardown == "teardown" ? "OK @13" : "FAIL");
+ }
+
+ // #7
+ public function testRunnerAddCaseCustomPattern() {
+ var r = new Runner();
+ r.addCase(new TestCaseClass(), null, null, ~/test/i);
+ trace(r.fixtures.length == 3 ? "OK @14" : "FAIL");
+ }
+
+
+ public static function main() {
+ var r = new Iteration2();
+
+ r.testRunnerRun();
+ r.testAssertCreateAsync();
+ r.testRunnerSequenceOnAsync();
+ r.testRunnerAddCase();
+ r.testRunnerAddCaseCustomFun();
+ r.testRunnerAddCaseCustomPrefix();
+#if (flash9 || !flash)
+ r.testRunnerAddCaseCustomPattern();
+#end
+ }
+}
+
+class TestSequenceClass {
+ public var seq : String;
+ public function new() {
+ seq = "";
+ }
+
+ public function test1() {
+ var me = this;
+ var async = Assert.createAsync(function() me.seq += "1");
+#if (flash || js)
+ haxe.Timer.delay(async, 100);
+#else
+ async();
+#end
+ }
+
+ public function test2() {
+ var me = this;
+ var async = Assert.createAsync(function() me.seq += "2");
+#if (flash || js)
+ haxe.Timer.delay(async, 50);
+#else
+ async();
+#end
+ }
+
+ public function test3() {
+ var me = this;
+ var async = Assert.createAsync(function() me.seq += "3");
+ async();
+ }
+}
\ No newline at end of file

Added: trunk/src/tests/iterations/TestCaseClass.hx
==============================================================================
--- (empty file)
+++ trunk/src/tests/iterations/TestCaseClass.hx Sat Aug 30 05:02:20 2008
@@ -0,0 +1,12 @@
+package tests.iterations;
+
+class TestCaseClass {
+ public function new();
+
+ public function _setup() {}
+ public function teardown() {}
+
+ public function testOne() {}
+ public function TestTwo() {}
+ public function _testThree() {}
+}
\ No newline at end of file

Added: trunk/src/tests/iterations/TestClass.hx
==============================================================================
--- (empty file)
+++ trunk/src/tests/iterations/TestClass.hx Sat Aug 30 05:02:20 2008
@@ -0,0 +1,44 @@
+package tests.iterations;
+
+import utest.Assert;
+
+class TestClass {
+ public var tested : Bool;
+ public var donesetup : Bool;
+ public var doneteardown : Bool;
+
+ public function new() {
+ tested = false;
+ doneteardown = false;
+ donesetup = false;
+ }
+
+ public function setup() {
+ donesetup = true;
+ }
+
+ public function teardown() {
+ doneteardown = true;
+ }
+
+ public function test() {
+ tested = true;
+ }
+
+ public function throwError() {
+ throw "error";
+ }
+
+ public function assertTrue() {
+ Assert.isTrue(true);
+ }
+
+ public function assertAsync() {
+ var async = Assert.createAsync(function() Assert.isTrue(true));
+#if (flash || js)
+ haxe.Timer.delay(async, 50);
+#else
+ async();
+#end
+ }
+}
\ No newline at end of file

Added: trunk/src/tests/requests/RequestTest.hx
==============================================================================
--- (empty file)
+++ trunk/src/tests/requests/RequestTest.hx Sat Aug 30 05:02:20 2008
@@ -0,0 +1,57 @@
+package tests.requests;
+
+import utest.Assert;
+import utest.Runner;
+
+#if php
+import php.Web;
+#elseif neko
+import neko.Web;
+#elseif flash
+import flash.external.ExternalInterface;
+#end
+
+class RequestTest {
+ static inline var TIMEOUT = 500;
+
+ public function new();
+
+ public function testWorkingHttp() {
+ var requestor = new haxe.Http( getUrl("real.html") );
+ requestor.onData = Assert.createEvent(onData, TIMEOUT);
+ requestor.request(false);
+ }
+
+ public function testFailingHttp() {
+ var requestor = new haxe.Http( getUrl("fake.html") );
+ requestor.onError = Assert.createEvent(onExpectedError, TIMEOUT);
+ requestor.request(false);
+ }
+
+ function onData(msg:String) {
+ Assert.isTrue(StringTools.startsWith(msg.toLowerCase(), "<html"));
+ }
+
+ public function onExpectedError(msg:String) {
+ Assert.notNull(msg);
+ }
+
+ static function getUrl(path : String) {
+#if (php || neko)
+ var uri = Web.getURI();
+ uri = uri.substr(0, uri.lastIndexOf("/")+1);
+ return "http://"+Web.getClientHeader("HOST")+uri+"../files/"+(path ==
null? '': path);
+#elseif js
+ var uri : String = Std.string(js.Lib.window.location);
+ uri = uri.substr(0, uri.lastIndexOf("/")+1);
+ return uri+"files/"+(path == null? '': path);
+#elseif (flash9 || flash8)
+ var uri : String =
ExternalInterface.call("window.location.href.toString");
+ uri = uri.substr(0, uri.lastIndexOf("/")+1);
+ return uri+"files/"+(path == null? '': path);
+#elseif flash
+// TODO: find a way to autodetect this
+ return "http://localhost/utest/files/"+path;
+#end
+ }
+}

Added: trunk/src/utest/Assert.hx
==============================================================================
--- (empty file)
+++ trunk/src/utest/Assert.hx Sat Aug 30 05:02:20 2008
@@ -0,0 +1,62 @@
+package utest;
+
+import utest.Assertation;
+import haxe.PosInfos;
+
+class Assert {
+ public static var results : List<Assertation>;
+ public static function isTrue(cond : Bool, msg = "true expected", ?pos :
PosInfos) {
+ if(results == null) throw "Assert.results is not currently bound to any
assert context";
+ if(cond)
+ results.add(Success(pos));
+ else
+ results.add(Failure(msg, pos));
+ }
+
+ public static function isFalse(value : Bool, msg = "expected false but
was true", ?pos : PosInfos) {
+ isTrue(value == false, msg, pos);
+ }
+
+ public static function isNull(value : Dynamic, msg = "expected null but
was not null", ?pos : PosInfos) {
+ isTrue(value == null, msg, pos);
+ }
+
+ public static function notNull(value : Dynamic, msg = "expected not null
but was null", ?pos : PosInfos) {
+ isTrue(value != null, msg, pos);
+ }
+
+ public static function is(value : Dynamic, type : Dynamic, ?msg :
String , ?pos : PosInfos) {
+ if(msg == null) msg = "expected type " + Std.string(type) + " but was "
+ Type.typeof(value);
+ isTrue(Std.is(value, type), msg, pos);
+ }
+
+ public static function equals(expected : Dynamic, value : Dynamic, ?msg :
String , ?pos : PosInfos) {
+ if(msg == null) msg = "expected " + expected + " but was " + value;
+ isTrue(expected == value, msg, pos);
+ }
+
+ public static function floatEquals(expected : Float, value :
Float, ?msg : String , ?pos : PosInfos) {
+ if(msg == null) msg = "expected " + expected + " but was " + value;
+ isTrue(Math.abs(value-expected) < 1e-5, msg, pos);
+ }
+
+ public static function raises(method:Void -> Void,
type:Class<Dynamic>, ?msg : String , ?pos : PosInfos) {
+ try {
+ method();
+ fail("exception of type " + type + " not raised", pos);
+ } catch (ex : Dynamic) {
+ isTrue(Std.is(ex, type), "expected throw of type " + type + " but
was " + ex, pos);
+ }
+ }
+
+ public static function fail(msg = "failure expected", ?pos : PosInfos) {
+ isTrue(false, msg, pos);
+ }
+
+ public static dynamic function createAsync(f : Void->Void, ?timeout :
Int) {
+ return function(){};
+ }
+ public static dynamic function createEvent<EventArg>(f :
EventArg->Void, ?timeout : Int) {
+ return function(e){};
+ }
+}
\ No newline at end of file

Added: trunk/src/utest/Assertation.hx
==============================================================================
--- (empty file)
+++ trunk/src/utest/Assertation.hx Sat Aug 30 05:02:20 2008
@@ -0,0 +1,14 @@
+package utest;
+
+import haxe.PosInfos;
+
+enum Assertation {
+ Success(pos : PosInfos);
+ Failure(msg : String, pos : PosInfos);
+ Error(e : Dynamic);
+ SetupError(e : Dynamic);
+ TeardownError(e : Dynamic);
+ TimeoutError(missedAsyncs : Int);
+ AsyncError(e : Dynamic);
+ Warning(msg : String);
+}
\ No newline at end of file

Added: trunk/src/utest/Runner.hx
==============================================================================
--- (empty file)
+++ trunk/src/utest/Runner.hx Sat Aug 30 05:02:20 2008
@@ -0,0 +1,70 @@
+package utest;
+
+
+class Runner {
+ public var fixtures(default, null) : List<TestFixture<Dynamic>>;
+ public function new() {
+ fixtures = new List();
+ }
+
+ public function addCase(test : Dynamic, setup = "setup", teardown
= "teardown", prefix = "test", ?pattern : EReg) {
+ if(!Reflect.isObject(test)) throw "can't add a null object as a test
case";
+ if(!isMethod(test, setup))
+ setup = null;
+ if(!isMethod(test, teardown))
+ teardown = null;
+ var fields = Type.getInstanceFields(Type.getClass(test));
+ if(pattern == null) {
+ for(field in fields) {
+ if(!StringTools.startsWith(field, prefix)) continue;
+ if(!isMethod(test, field)) continue;
+ fixtures.add(new TestFixture(test, field, setup, teardown));
+ }
+ } else {
+ for(field in fields) {
+ if(!pattern.match(field)) continue;
+ if(!isMethod(test, field)) continue;
+ fixtures.add(new TestFixture(test, field, setup, teardown));
+ }
+ }
+ }
+
+ function isMethod(test : Dynamic, name : String) {
+ try {
+ return Reflect.isFunction(Reflect.field(test, name));
+ } catch(e : Dynamic) {
+ return false;
+ }
+ }
+
+ var testsToRun : Int;
+ public function run() {
+ counter = 0;
+ testsToRun = fixtures.length;
+ onStart(this);
+ runNext();
+ }
+
+ function runNext() {
+ if(fixtures.length > 0)
+ runFixture(fixtures.pop());
+ else
+ onComplete(this);
+ }
+
+ public dynamic function onProgress(runner : Runner, result : TestResult,
done : Int, totals : Int);
+ public dynamic function onStart(r : Runner);
+ public dynamic function onComplete(r : Runner);
+
+ var counter : Int;
+ function runFixture(fixture : TestFixture<Dynamic>) {
+ var handler = new TestHandler(fixture);
+ handler.onComplete = testComplete;
+ handler.execute();
+ }
+
+ function testComplete(h : TestHandler<Dynamic>) {
+ onProgress(this, TestResult.ofHandler(h), fixtures.length+1, testsToRun);
+ runNext();
+ }
+}
\ No newline at end of file

Added: trunk/src/utest/TestFixture.hx
==============================================================================
--- (empty file)
+++ trunk/src/utest/TestFixture.hx Sat Aug 30 05:02:20 2008
@@ -0,0 +1,31 @@
+package utest;
+
+class TestFixture<T> {
+ public var target(default, null) : T;
+ public var method(default, null) : String;
+ public var setup(default, null) : String;
+ public var teardown(default, null) : String;
+ public function new(target : T, method : String, ?setup :
String, ?teardown : String) {
+ this.target = target;
+ /*
+ if(!Reflect.isObject(target)) throw "target argument is not an object";
+
+
+ checkMethod(method, "method");
+
+ if(setup != null)
+ checkMethod(setup, "setup");
+ if(teardown != null)
+ checkMethod(teardown, "teardown");
+*/
+ this.method = method;
+ this.setup = setup;
+ this.teardown = teardown;
+ }
+
+ function checkMethod(name : String, arg : String) {
+ var field = Reflect.field(target, name);
+ if(field == null) throw arg + " function " + name + " is
not a field of target";
+ if(!Reflect.isFunction(field)) throw arg + " function " + name + " is
not a function";
+ }
+}
\ No newline at end of file

Added: trunk/src/utest/TestHandler.hx
==============================================================================
--- (empty file)
+++ trunk/src/utest/TestHandler.hx Sat Aug 30 05:02:20 2008
@@ -0,0 +1,140 @@
+package utest;
+
+import utest.Assertation;
+
+class TestHandler<T> {
+ private static inline var POLLING_TIME = 10;
+ public var results(default, null) : List<Assertation>;
+ public var fixture(default, null) : TestFixture<T>;
+ public var executionTime(default, null) : Int;
+ var asyncStack : List<Dynamic>;
+ public function new(fixture : TestFixture<T>) {
+ if(fixture == null) throw "fixture argument is null";
+ this.fixture = fixture;
+ results = new List();
+ asyncStack = new List();
+ executionTime = -1;
+ }
+
+ var startTime : Null<Float>;
+ public function execute() {
+ try {
+ executeMethod(fixture.setup);
+ try {
+ startTime = haxe.Timer.stamp();
+ executeMethod(fixture.method);
+ } catch(e : Dynamic) {
+ results.add(Error(e));
+ }
+ } catch(e : Dynamic) {
+ results.add(SetupError(e));
+ }
+ checkTested();
+ }
+
+ function checkTested() {
+#if (flash || js)
+ if(expireson == null || asyncStack.length == 0) {
+ tested();
+ } else if(haxe.Timer.stamp() > expireson) {
+ timeout();
+ } else {
+ haxe.Timer.delay(checkTested, POLLING_TIME);
+ }
+#else
+ if(asyncStack.length == 0)
+ tested();
+ else
+ timeout();
+#end
+ }
+
+ public var expireson(default, null) : Null<Float>;
+ public function setTimeout(timeout : Int) {
+ var newexpire = haxe.Timer.stamp() + timeout/1000;
+ expireson = (expireson == null) ? newexpire : (newexpire > expireson ?
newexpire : expireson);
+ }
+
+ function bindHandler() {
+ Assert.results = this.results;
+ Assert.createAsync = this.addAsync;
+ Assert.createEvent = this.addEvent;
+ }
+
+ function unbindHandler() {
+ Assert.results = null;
+ Assert.createAsync = function(f, ?t){ return function(){}};
+ Assert.createEvent = function(f, ?t){ return function(e){}};
+ }
+
+ public function addAsync(f : Void->Void, timeout = 250) {
+ asyncStack.add(f);
+ var handler = this;
+ setTimeout(timeout);
+ return function() {
+ if(!handler.asyncStack.remove(f)) {
+ handler.results.add(AsyncError("method already executed"));
+ return;
+ }
+ try {
+ handler.bindHandler();
+ f();
+ } catch(e : Dynamic) {
+ handler.results.add(AsyncError(e));
+ }
+ };
+ }
+
+ public function addEvent<EventArg>(f : EventArg->Void, timeout = 250) {
+ asyncStack.add(f);
+ var handler = this;
+ setTimeout(timeout);
+ return function(e : EventArg) {
+ if(!handler.asyncStack.remove(f)) {
+ handler.results.add(AsyncError("event already executed"));
+ return;
+ }
+ try {
+ handler.bindHandler();
+ f(e);
+ } catch(e : Dynamic) {
+ handler.results.add(AsyncError(e));
+ }
+ };
+ }
+
+ function executeMethod(name : String) {
+ if(name == null) return;
+ bindHandler();
+ Reflect.callMethod(fixture.target, Reflect.field(fixture.target, name),
[]);
+ }
+
+ function tested() {
+ if(startTime != null)
+ executionTime = Std.int((haxe.Timer.stamp() - startTime)*1000);
+ if(results.length == 0)
+ results.add(Warning("no assertions"));
+ onTested(this);
+ completed();
+ }
+
+ function timeout() {
+ results.add(TimeoutError(asyncStack.length));
+ onTimeout(this);
+ completed();
+ }
+
+ function completed() {
+ try {
+ executeMethod(fixture.teardown);
+ } catch(e : Dynamic) {
+ results.add(TeardownError(e));
+ }
+ unbindHandler();
+ onComplete(this);
+ }
+
+ public dynamic function onTested(handler : TestHandler<T>);
+ public dynamic function onTimeout(handler : TestHandler<T>);
+ public dynamic function onComplete(handler : TestHandler<T>);
+}
\ No newline at end of file

Added: trunk/src/utest/TestResult.hx
==============================================================================
--- (empty file)
+++ trunk/src/utest/TestResult.hx Sat Aug 30 05:02:20 2008
@@ -0,0 +1,91 @@
+package utest;
+
+import utest.Assertation;
+
+class TestResult {
+ public var pack : String;
+ public var cls : String;
+ public var method : String;
+ public var setup : String;
+ public var teardown : String;
+ public var executionTime : Int;
+ public var assertations : List<Assertation>;
+/*
+ public function hasSetup() {
+ return setup != null;
+ }
+
+ public function hasTeardown() {
+ return setup != null;
+ }
+
+ public function count() {
+ return assertations.length;
+ }
+
+ public function successes() {
+ return countAssertions(Success(null));
+ }
+
+ public function failures() {
+ return countAssertions(Failure(null, null));
+ }
+
+ public function errors() {
+ return countAssertions(Error(null));
+ }
+
+ public function setupErrors() {
+ return countAssertions(SetupError(null));
+ }
+
+ public function teardownErrors() {
+ return countAssertions(TeardownError(null));
+ }
+
+ public function timeoutErrors() {
+ return countAssertions(TimeoutError(-1));
+ }
+
+ public function asyncErrors() {
+ return countAssertions(AsyncError(null));
+ }
+
+ public function warnings() {
+ return countAssertions(Warning(null));
+ }
+
+ public function allErrors() {
+ return errors() + setupErrors() + teardownErrors() + timeoutErrors() +
asyncErrors();
+ }
+
+ public function isOk() {
+ return assertations.length == successes();
+ }
+
+ function countAssertions(type : Assertation) {
+ var index = Type.enumIndex(type);
+ var v = 0;
+ for(assertation in assertations) {
+ if(index == Type.enumIndex(assertation))
+ v++;
+ }
+ return v;
+ }
+*/
+
+ public function new();
+
+ public static function ofHandler(handler : TestHandler<Dynamic>) {
+ var r = new TestResult();
+ var path =
Type.getClassName(Type.getClass(handler.fixture.target)).split('.');
+ r.cls = path.pop();
+ r.pack = path.join('.');
+ r.method = handler.fixture.method;
+ r.setup = handler.fixture.setup;
+ r.teardown = handler.fixture.teardown;
+ r.executionTime = handler.executionTime;
+ r.assertations = handler.results;
+ return r;
+ }
+}
\ No newline at end of file

Added: trunk/src/utest/ui/common/TestClass.hx
==============================================================================

Added: trunk/src/utest/ui/common/TestPackage.hx
==============================================================================

Added: trunk/src/utest/ui/text/TraceReport.hx
==============================================================================
--- (empty file)
+++ trunk/src/utest/ui/text/TraceReport.hx Sat Aug 30 05:02:20 2008
@@ -0,0 +1,87 @@
+package utest.ui.text;
+
+import haxe.PosInfos;
+
+import utest.Runner;
+import utest.TestResult;
+
+#if js
+import js.Dom;
+#end
+
+class TraceReport {
+ public function new(runner : Runner) {
+ runner.onStart = start;
+ runner.onComplete = complete;
+ runner.onProgress = progress;
+ }
+
+ var counter : Int;
+
+ static inline var STATUS_OK = 0;
+ static inline var STATUS_WARNING = 1;
+ static inline var STATUS_FAIL = 2;
+ static inline var STATUS_ERROR = 3;
+ function printResult(result : TestResult) {
+ counter++;
+ var status = STATUS_OK;
+ var buff = '';
+ var messages = '';
+ for(assertation in result.assertations) {
+ switch(assertation) {
+ case Success(pos):
+ buff += '.';
+ case Failure(msg, pos):
+ if(status < STATUS_FAIL) status = STATUS_FAIL;
+ buff += 'F';
+ messages += " line: " + pos.lineNumber + ", " + msg;
+ case Error(e):
+ if(status < STATUS_ERROR) status = STATUS_ERROR;
+ buff += 'E';
+ messages += " " + Std.string(e);
+ case SetupError(e):
+ if(status < STATUS_ERROR) status = STATUS_ERROR;
+ buff += 'S';
+ messages += " " + Std.string(e);
+ case TeardownError(e):
+ if(status < STATUS_ERROR) status = STATUS_ERROR;
+ buff += 'T';
+ messages += " " + Std.string(e);
+ case TimeoutError(missedAsyncs):
+ if(status < STATUS_ERROR) status = STATUS_ERROR;
+ buff += 'O';
+ messages += " " + "missed async calls: " + missedAsyncs;
+ case AsyncError(e):
+ if(status < STATUS_ERROR) status = STATUS_ERROR;
+ buff += 'A';
+ messages += " " + Std.string(e);
+ case Warning(msg):
+ if(status < STATUS_WARNING) status = STATUS_WARNING;
+ buff += 'W';
+ messages += " " + msg;
+ }
+ }
+ var r = switch(status) {
+ case STATUS_OK : "OK";
+ case STATUS_WARNING : "WARNING";
+ case STATUS_FAIL : "FAIL";
+ case STATUS_ERROR : "ERROR";
+ };
+ trace(counter +". "+result.cls + "." + result.method + ": "+r + " " +
buff);
+ if(messages != '') {
+ trace(messages);
+ }
+ }
+
+ function start(r : Runner) {
+ counter = 0;
+ }
+
+ function complete(r : Runner) {
+ trace("[DONE]");
+ }
+
+ function progress(r : Runner, result : TestResult, done : Int, totals :
Int) {
+ printResult(result);
+ }
+}
\ No newline at end of file

Reply all
Reply to author
Forward
0 new messages