Skip to content

Instantly share code, notes, and snippets.

@thekid
Created July 22, 2011 20:41
Show Gist options
  • Save thekid/1100376 to your computer and use it in GitHub Desktop.
Save thekid/1100376 to your computer and use it in GitHub Desktop.
XP Framework: Patch for Issue #40
diff --git a/core/src/main/php/unittest/AssertionFailedError.class.php b/core/src/main/php/unittest/AssertionFailedError.class.php
index befaabd..eff84f7 100644
--- a/core/src/main/php/unittest/AssertionFailedError.class.php
+++ b/core/src/main/php/unittest/AssertionFailedError.class.php
@@ -27,6 +27,17 @@
$this->actual= $actual;
$this->expect= $expect;
}
+
+ /**
+ * Creates a string representation of a given value.
+ *
+ * @param var value
+ * @param string type NULL if type name should be not included.
+ * @return string
+ */
+ protected function stringOf($value, $type) {
+ return (NULL === $type ? '' : $type.':').xp::stringOf($value);
+ }
/**
* Return compound message of this exception.
@@ -34,12 +45,19 @@
* @return string
*/
public function compoundMessage() {
+ $te= xp::typeOf($this->expect);
+ $ta= xp::typeOf($this->actual);
+ if ($this->expect instanceof Generic && $this->actual instanceof Generic) {
+ $include= FALSE;
+ } else {
+ $include= $te !== $ta;
+ }
return sprintf(
- "%s (%s) { expected: [%s:%s] but was: [%s:%s] }\n",
+ "%s { expected [%s] but was [%s] (using %s) }\n",
$this->getClassName(),
- $this->message,
- xp::typeOf($this->expect), xp::stringOf($this->expect),
- xp::typeOf($this->actual), xp::stringOf($this->actual)
+ $this->stringOf($this->expect, $include ? $te : NULL),
+ $this->stringOf($this->actual, $include ? $ta : NULL),
+ $this->message
);
}
diff --git a/core/src/main/php/unittest/TestCase.class.php b/core/src/main/php/unittest/TestCase.class.php
index 889061f..b1c717e 100644
--- a/core/src/main/php/unittest/TestCase.class.php
+++ b/core/src/main/php/unittest/TestCase.class.php
@@ -54,9 +54,9 @@
*
* @deprecated
* @param var var
- * @param string error default 'notarray'
+ * @param string error default 'is_array'
*/
- public function assertArray($var, $error= 'notarray') {
+ public function assertArray($var, $error= 'is_array') {
if (!is_array($var) && !is('lang.types.ArrayList', $var)) {
$this->fail($error, xp::typeOf($var), 'array');
}
@@ -67,9 +67,9 @@
*
* @deprecated
* @param var var
- * @param string error default 'notobject'
+ * @param string error default 'is_object'
*/
- public function assertObject($var, $error= 'notobject') {
+ public function assertObject($var, $error= 'is_object') {
if (!is_object($var)) {
$this->fail($error, xp::typeOf($var), 'object');
}
@@ -80,10 +80,10 @@
*
* @deprecated
* @param var var
- * @param string error default 'notempty'
+ * @param string error default 'empty'
* @see php://empty
*/
- public function assertEmpty($var, $error= 'notempty') {
+ public function assertEmpty($var, $error= 'empty') {
if (!empty($var)) {
$this->fail($error, $var, '<empty>');
}
@@ -94,10 +94,10 @@
*
* @deprecated
* @param var var
- * @param string error default 'empty'
+ * @param string error default '!empty'
* @see php://empty
*/
- public function assertNotEmpty($var, $error= 'empty') {
+ public function assertNotEmpty($var, $error= '!empty') {
if (empty($var)) {
$this->fail($error, $var, '<not empty>');
}
@@ -109,9 +109,9 @@
* @deprecated Use assertInstanceOf() instead
* @param lang.Generic var
* @param string name
- * @param string error default 'notequal'
+ * @param string error default 'typeof'
*/
- public function assertClass($var, $name, $error= 'notequal') {
+ public function assertClass($var, $name, $error= 'typeof') {
if (!($var instanceof Generic)) {
$this->fail($error, $var, $name);
}
@@ -126,9 +126,9 @@
* @deprecated Use assertInstanceOf() instead
* @param lang.Generic var
* @param string name
- * @param string error default 'notsubclass'
+ * @param string error default 'instanceof'
*/
- public function assertSubclass($var, $name, $error= 'notsubclass') {
+ public function assertSubclass($var, $name, $error= 'instanceof') {
if (!($var instanceof Generic)) {
$this->fail($error, $var, $name);
}
@@ -165,7 +165,7 @@
* @param var actual
* @param string error default 'notequal'
*/
- public function assertEquals($expected, $actual, $error= 'notequal') {
+ public function assertEquals($expected, $actual, $error= 'equals') {
if (!$this->_compare($expected, $actual)) {
$this->fail($error, $actual, $expected);
}
@@ -178,7 +178,7 @@
* @param var actual
* @param string error default 'equal'
*/
- public function assertNotEquals($expected, $actual, $error= 'equal') {
+ public function assertNotEquals($expected, $actual, $error= '!equals') {
if ($this->_compare($expected, $actual)) {
$this->fail($error, $actual, $expected);
}
@@ -188,9 +188,9 @@
* Assert that a value is true
*
* @param var var
- * @param string error default 'nottrue'
+ * @param string error default '==='
*/
- public function assertTrue($var, $error= 'nottrue') {
+ public function assertTrue($var, $error= '===') {
if (TRUE !== $var) {
$this->fail($error, $var, TRUE);
}
@@ -200,9 +200,9 @@
* Assert that a value is false
*
* @param var var
- * @param string error default 'notfalse'
+ * @param string error default '==='
*/
- public function assertFalse($var, $error= 'notfalse') {
+ public function assertFalse($var, $error= '===') {
if (FALSE !== $var) {
$this->fail($error, $var, FALSE);
}
@@ -212,9 +212,9 @@
* Assert that a value's type is null
*
* @param var var
- * @param string error default 'notnull'
+ * @param string error default '==='
*/
- public function assertNull($var, $error= 'notnull') {
+ public function assertNull($var, $error= '===') {
if (NULL !== $var) {
$this->fail($error, $var, NULL);
}
@@ -225,9 +225,9 @@
*
* @param var type either a type name or a lang.Type instance
* @param var var
- * @param string error default 'notaninstance'
+ * @param string error default 'instanceof'
*/
- public function assertInstanceOf($type, $var, $error= 'notaninstance') {
+ public function assertInstanceOf($type, $var, $error= 'instanceof') {
if (!($type instanceof Type)) {
$type= Type::forName($type);
}
diff --git a/core/src/test/php/net/xp_framework/unittest/tests/AssertionMessagesTest.class.php b/core/src/test/php/net/xp_framework/unittest/tests/AssertionMessagesTest.class.php
index fee9ead..ae6857b 100644
--- a/core/src/test/php/net/xp_framework/unittest/tests/AssertionMessagesTest.class.php
+++ b/core/src/test/php/net/xp_framework/unittest/tests/AssertionMessagesTest.class.php
@@ -25,7 +25,7 @@
*/
protected function assertMessageEquals($expected, $error) {
$this->assertEquals(
- "unittest.AssertionFailedError ".$expected."\n",
+ "unittest.AssertionFailedError { ".$expected." }\n",
$error->compoundMessage()
);
}
@@ -37,8 +37,8 @@
#[@test]
public function differentIntegerPrimitives() {
$this->assertMessageEquals(
- '(==) { expected: [integer:2] but was: [integer:1] }',
- new AssertionFailedError('==', 1, 2)
+ 'expected [2] but was [1] (using equals)',
+ new AssertionFailedError('equals', 1, 2)
);
}
@@ -49,8 +49,8 @@
#[@test]
public function differentPrimitives() {
$this->assertMessageEquals(
- '(==) { expected: [integer:2] but was: [double:2] }',
- new AssertionFailedError('==', 2.0, 2)
+ 'expected [integer:2] but was [double:2] (using equals)',
+ new AssertionFailedError('equals', 2.0, 2)
);
}
@@ -61,8 +61,8 @@
#[@test]
public function differentStrings() {
$this->assertMessageEquals(
- '(equals) { expected: [lang.types.String:] but was: [lang.types.String:abc] }',
- new AssertionFailedError('equals', new String('abc'), new String(''))
+ 'expected [abc] but was [] (using equals)',
+ new AssertionFailedError('equals', new String(''), new String('abc'))
);
}
@@ -73,7 +73,7 @@
#[@test]
public function stringAndStringPrimitive() {
$this->assertMessageEquals(
- '(equals) { expected: [lang.types.String:] but was: [string:""] }',
+ 'expected [lang.types.String:] but was [string:""] (using equals)',
new AssertionFailedError('equals', '', new String(''))
);
}
@@ -85,7 +85,7 @@
#[@test]
public function differentTypes() {
$this->assertMessageEquals(
- '(equals) { expected: [lang.types.String:] but was: [net.xp_framework.unittest.tests.AssertionMessagesTest:net.xp_framework.unittest.tests.AssertionMessagesTest<differentTypes>] }',
+ 'expected [] but was [net.xp_framework.unittest.tests.AssertionMessagesTest<differentTypes>] (using equals)',
new AssertionFailedError('equals', $this, new String(''))
);
}
@thekid
Copy link
Author

thekid commented Jul 22, 2011

...also hides types when both are instanceof lang.Generic or they are the same

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment