|
From: Robert P. <in...@ro...> - 2008-10-06 21:03:03
|
I ported assertEqualsFloat() from AsUnit 3 back to 2.5. I also
corrected some docs regarding == vs. === that were out of date. I
tried attaching the patch file but here it is below as well.
Robert
+++ \asunit\framework\Assert.as
@@ -73,7 +73,7 @@
* Asserts that two objects are equal. If either of the
* objects provides an <code>equals()</code> method it will be used to
* determine equality. Otherwise, equality will be evaluated using the
- * strict equality operator (<code>===</code>).<br />
+ * equality operator (<code>==</code>).<br />
* <br />
* The method below tests the <code>getFullName()</code> method of the
* <code>Person</code> class to make sure that the formatting of its
@@ -114,6 +114,35 @@
}
/**
+ * Asserts that two numerical values are equal within a tolerance range.
+ * If they are not an AssertionFailedError is thrown with the given message.
+ */
+ static public function assertEqualsFloat():Void {
+ var message:String;
+ var expected:Number;
+ var actual:Number;
+ var tolerance:Number = 0;
+
+ if(arguments.length == 3) {
+ message = "";
+ expected = arguments[0];
+ actual = arguments[1];
+ tolerance = arguments[2];
+ }
+ else if(arguments.length == 4) {
+ message = arguments[0];
+ expected = arguments[1];
+ actual = arguments[2];
+ tolerance = arguments[3];
+ }
+ if (isNaN(tolerance)) tolerance = 0;
+ addTestResult(formatEqualsFloat(String(message), expected, actual,
tolerance),
+ "assertEqualsFloat",
+ (Math.abs(expected - actual) <= tolerance));
+ }
+
+
+ /**
* Asserts that a value is <code>null</code>.<br />
* <br />
* The method below tests to confirm that the parameter passed to the
@@ -221,7 +250,7 @@
/**
* Asserts that two variables are pointing to the same object in
- * memory.<br />
+ * memory. Uses the strict equality operator (<code>===</code>).<br />
* <br />
* The method below tests to confirm that the static
* <code>getInstance()</code> method of the <code>ServiceLocator</code>
@@ -358,10 +387,6 @@
throw new AssertionFailedError(msg);
}
-// private static function failNotEquals(message:String,
expected:Object, actual:Object):Void {
-// fail(format(message, expected, actual));
-// }
-
private static function format(message:String, expected:Object,
actual:Object):String {
var formatted:String = "";
if(message != null) {
@@ -370,6 +395,14 @@
return formatted + "expected:<" + expected + "> but was:<" + actual + ">";
}
+ private static function formatEqualsFloat(message:String,
expected:Object, actual:Object, tolerance:Number):String {
+ var formatted:String = "";
+ if(message != null) {
+ formatted = message + " ";
+ }
+ return formatted + "expected:<" + expected + "> but was:<" + actual
+ "> with tolerance:<"+tolerance+">";
+ }
+
public static function failError(msg:String):Void {
throw new IllegalOperationError(msg);
}
|