Skip to content

Commit

Permalink
Merge pull request #117 from ashes999/master
Browse files Browse the repository at this point in the history
Add Assert.throws
  • Loading branch information
mikestead committed Apr 13, 2016
2 parents 2f79d3c + bf5fa80 commit ef2e06e
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 55 deletions.
40 changes: 35 additions & 5 deletions src/massive/munit/Assert.hx
Expand Up @@ -4,12 +4,12 @@
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY MASSIVE INTERACTIVE ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
Expand Down Expand Up @@ -215,6 +215,36 @@ class Assert
assertionCount++;
if (expected == actual) fail("Value [" + actual +"] was the same as expected value [" + expected + "]", info);
}

/**
* Assert that an expectation was thrown. Can expect strings and non-strings.
*
* @Param expectedType the type of exception expected (eg. String, AssertionException)
* @param code a function which should throw an exception
* @return the exception that was thrown
* @throws AssertionException if no expectation is thrown
*/
public static function throws(expectedType:Dynamic, code:Dynamic, ?info:PosInfos):Dynamic
{
try
{
code();
fail("Expected exception wasn't thrown!", info);
return null; // needed to compile
}
catch (e:Dynamic)
{
if (Std.is(e, expectedType))
{
return e;
}
else
{
Assert.fail('Expected exception of type ${Type.getClassName(expectedType)} but got ${Type.getClassName(Type.getClass(e))}: ${e}');
return null; // needed to compile
}
}
}

/**
* Force an assertion failure.
Expand Down
32 changes: 16 additions & 16 deletions test/TestSuite.hx
@@ -1,20 +1,20 @@
import massive.munit.TestSuite;

import massive.munit.MUnitExceptionTest;
import massive.munit.AssertionExceptionTest;
import massive.munit.util.TimerTest;
import massive.munit.util.MathUtilTest;
import massive.munit.AssertTest;
import massive.munit.TestRunnerTest;
import massive.munit.TestResultTest;
import massive.munit.async.AsyncDelegateTest;
import massive.munit.async.AsyncFactoryTest;
import massive.munit.async.AsyncTimeoutExceptionTest;
import massive.munit.async.MissingAsyncDelegateExceptionTest;
import massive.munit.client.URLRequestTest;
import massive.munit.MUnitExceptionTest;
import massive.munit.TestClassHelperTest;
import massive.munit.TestResultTest;
import massive.munit.TestRunnerTest;
import massive.munit.async.AsyncFactoryTest;
import massive.munit.TestSuiteTest;
import massive.munit.client.URLRequestTest;
import massive.munit.UnhandledExceptionTest;
import massive.munit.util.MathUtilTest;
import massive.munit.util.TimerTest;
import massive.munit.TestClassHelperTest;

/**
* Auto generated Test Suite for MassiveUnit.
Expand All @@ -28,20 +28,20 @@ class TestSuite extends massive.munit.TestSuite
{
super();

add(massive.munit.MUnitExceptionTest);
add(massive.munit.AssertionExceptionTest);
add(massive.munit.util.TimerTest);
add(massive.munit.util.MathUtilTest);
add(massive.munit.AssertTest);
add(massive.munit.TestRunnerTest);
add(massive.munit.TestResultTest);
add(massive.munit.async.AsyncDelegateTest);
add(massive.munit.async.AsyncFactoryTest);
add(massive.munit.async.AsyncTimeoutExceptionTest);
add(massive.munit.async.MissingAsyncDelegateExceptionTest);
add(massive.munit.client.URLRequestTest);
add(massive.munit.MUnitExceptionTest);
add(massive.munit.TestClassHelperTest);
add(massive.munit.TestResultTest);
add(massive.munit.TestRunnerTest);
add(massive.munit.async.AsyncFactoryTest);
add(massive.munit.TestSuiteTest);
add(massive.munit.client.URLRequestTest);
add(massive.munit.UnhandledExceptionTest);
add(massive.munit.util.MathUtilTest);
add(massive.munit.util.TimerTest);
add(massive.munit.TestClassHelperTest);
}
}
74 changes: 69 additions & 5 deletions test/massive/munit/AssertTest.hx
Expand Up @@ -4,12 +4,12 @@
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY MASSIVE INTERACTIVE ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
Expand Down Expand Up @@ -479,6 +479,58 @@ class AssertTest
}
Assert.fail("Invalid assertion not captured");
}

@Test
public function testThrowsStringAndObject():Void
{
// Positive case: throws expected string
var expectedMessage:String = "Invalid operation!";
var actualMessage:String = Assert.throws(String, function()
{
throw expectedMessage;
});
Assert.areEqual(expectedMessage, actualMessage);

// Positive case: throws expected exception
var expectedError:CustomException = new CustomException('URL not reachable', 37);
var actualError:CustomException = Assert.throws(CustomException, function()
{
throw expectedError;
});
Assert.areEqual(expectedError.message, actualError.message);
Assert.areEqual(expectedError.code, actualError.code);

// Negative case: assertion raised if block doesn't throw
try
{
Assert.throws(String, function()
{
// Doesn't throw
});
}
catch (e:AssertionException)
{
Assert.isTrue(e.message.indexOf("wasn't thrown") > -1);
}
}

@Test
public function testThrowsFailsIfWrongExceptionTypeThrown():Void
{
try
{
Assert.throws(CustomException, function()
{
throw "String error!";
});
Assert.fail("Throwing the wrong exception type didn't fail");
}
catch (e:AssertionException)
{
Assert.isTrue(e.message.indexOf("Expected exception of type") > -1);
}
}

}

private enum DummyEnum
Expand All @@ -487,3 +539,15 @@ private enum DummyEnum
ValueB;
ValueC(param:String);
}

private class CustomException
{
public var message(default, default):String;
public var code(default, default):Int;

public function new(message:String, code:Int)
{
this.message = message;
this.code = code;
}
}
30 changes: 1 addition & 29 deletions test/test.hxml
@@ -1,31 +1,3 @@
# Flash 9+
-main TestMain
-cp test
-cp src
-lib mlib
-swf bin/test/core/as3_test.swf
-swf-version 9
-swf-header 800:600:60:FFFFFF
--next

#Flash 8
-main TestMain
-cp test
-cp src
-lib mlib
-swf-version 8
-swf bin/test/core/as2_test.swf
-swf-header 800:600:60:FFFFFF
--next

# JavaScript
-main TestMain
-cp test
-cp src
-lib mlib
-js bin/test/core/js_test.js
--next

# Neko
-main TestMain
-cp test
Expand All @@ -40,4 +12,4 @@
# -cp src
# -lib mlib
# -D HXCPP_M64
# -cpp bin/test/core/cpp_test
# -cpp bin/test/core/cpp_test

0 comments on commit ef2e06e

Please sign in to comment.