]> granicus.if.org Git - php/commitdiff
assert() user message
authorLars Strojny <lstrojny@php.net>
Fri, 31 Aug 2012 17:04:53 +0000 (19:04 +0200)
committerLars Strojny <lstrojny@php.net>
Fri, 31 Aug 2012 17:04:53 +0000 (19:04 +0200)
Added 2nd, optional, param to assert. When passed in it will be added
to the printed warnings and passed as a 4th param to a callback. PR 150
by Lonny Kapelushnik

NEWS
ext/standard/assert.c
ext/standard/tests/assert/assert04.phpt
ext/standard/tests/assert/assert_basic6.phpt [new file with mode: 0644]
ext/standard/tests/assert/assert_error1.phpt
ext/standard/tests/assert/assert_error2.phpt [new file with mode: 0644]
ext/standard/tests/assert/assert_error3.phpt [new file with mode: 0644]
ext/standard/tests/assert/assert_error4.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index 97cb3d0fad3f8e5d681c20a50e1b076e03374feb..d88aa18783e605bf454e024d106c6dfcdbe8439e 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -25,6 +25,8 @@ PHP                                                                        NEWS
     handler). (Lonny Kapelushnik)
   . Fixed bug #40459 (Stat and Dir stream wrapper methods do not call 
     constructor). (Stas)
+  . Added optional second argument for assert() to specify custom message. Patch
+    by Lonny Kapelushnik (lonny@lonnylot.com). (Lars)
 
 - CURL:
   . Fixed bug #62912 (CURLINFO_PRIMARY_* AND CURLINFO_LOCAL_* not exposed).
index a2c50d5cb67721b71e8f6d1a86575be360172b07..0ff3f9b7ef124f19b570c4740e6707bc9bd0770e 100644 (file)
@@ -136,20 +136,20 @@ PHP_MINFO_FUNCTION(assert) /* {{{ */
 }
 /* }}} */
 
-/* {{{ proto int assert(string|bool assertion)
+/* {{{ proto int assert(string|bool assertion[, string description])
    Checks if assertion is false */
 PHP_FUNCTION(assert)
 {
        zval **assertion;
-       int val;
+       int val, description_len = 0;
        char *myeval = NULL;
-       char *compiled_string_description;
+       char *compiled_string_description, *description;
 
        if (! ASSERTG(active)) {
                RETURN_TRUE;
        }
 
-       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &assertion) == FAILURE) {
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z|s", &assertion, &description, &description_len) == FAILURE) {
                return;
        }
 
@@ -167,7 +167,11 @@ PHP_FUNCTION(assert)
                compiled_string_description = zend_make_compiled_string_description("assert code" TSRMLS_CC);
                if (zend_eval_stringl(myeval, Z_STRLEN_PP(assertion), &retval, compiled_string_description TSRMLS_CC) == FAILURE) {
                        efree(compiled_string_description);
-                       php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "Failure evaluating code: %s%s", PHP_EOL, myeval);
+                       if (description_len == 0) {
+                               php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "Failure evaluating code: %s%s", PHP_EOL, myeval);
+                       } else {
+                               php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "Failure evaluating code: %s%s:\"%s\"", PHP_EOL, description, myeval);
+                       }
                        if (ASSERTG(bail)) {
                                zend_bailout();
                        }
@@ -196,7 +200,7 @@ PHP_FUNCTION(assert)
        }
 
        if (ASSERTG(callback)) {
-               zval *args[3];
+               zval **args = safe_emalloc(description_len == 0 ? 3 : 4, sizeof(zval **), 0);
                zval *retval;
                int i;
                uint lineno = zend_get_executed_lineno(TSRMLS_C);
@@ -214,19 +218,38 @@ PHP_FUNCTION(assert)
                ZVAL_FALSE(retval);
 
                /* XXX do we want to check for error here? */
-               call_user_function(CG(function_table), NULL, ASSERTG(callback), retval, 3, args TSRMLS_CC);
+               if (description_len == 0) {
+                       call_user_function(CG(function_table), NULL, ASSERTG(callback), retval, 3, args TSRMLS_CC);
+                       for (i = 0; i <= 2; i++) {
+                               zval_ptr_dtor(&(args[i]));
+                       }
+               } else {
+                       MAKE_STD_ZVAL(args[3]);
+                       ZVAL_STRINGL(args[3], SAFE_STRING(description), description_len, 1);
 
-               for (i = 0; i <= 2; i++) {
-                       zval_ptr_dtor(&(args[i]));
+                       call_user_function(CG(function_table), NULL, ASSERTG(callback), retval, 4, args TSRMLS_CC);
+                       for (i = 0; i <= 3; i++) {
+                               zval_ptr_dtor(&(args[i]));
+                       }
                }
+
+               efree(args);
                zval_ptr_dtor(&retval);
        }
 
        if (ASSERTG(warning)) {
-               if (myeval) {
-                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Assertion \"%s\" failed", myeval);
+               if (description_len == 0) {
+                       if (myeval) {
+                               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Assertion \"%s\" failed", myeval);
+                       } else {
+                               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Assertion failed");
+                       }
                } else {
-                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Assertion failed");
+                       if (myeval) {
+                               php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s: \"%s\" failed", description, myeval);
+                       } else {
+                               php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s failed", description);
+                       }
                }
        }
 
@@ -321,3 +344,4 @@ PHP_FUNCTION(assert_options)
  * vim600: sw=4 ts=4 fdm=marker
  * vim<600: sw=4 ts=4
  */
+
index 0da23186e64327019a2c3ea148f8ca52664d665a..bffadcb97c6e744fbc0f13dc5a7dffcd99ff4a8a 100644 (file)
@@ -15,7 +15,7 @@ assert(1);
  
 /* Wrong parameter count in assert */
 assert_options(ASSERT_ACTIVE, 1);
-assert(2,3);
+assert(2, "failure", 3);
  
 /* Wrong parameter count in assert_options */
 assert_options(ASSERT_ACTIVE, 0, 2);
@@ -36,7 +36,7 @@ echo "not reached\n";
  
 ?>
 --EXPECTF--
-Warning: assert() expects exactly 1 parameter, 2 given in %s on line %d
+Warning: assert() expects at most 2 parameters, 3 given in %s on line %d
 
 Warning: assert_options() expects at most 2 parameters, 3 given in %s on line %d
 
@@ -45,3 +45,4 @@ Warning: assert_options() expects parameter 1 to be long, %unicode_string_option
 Warning: assert(): Assertion failed in %s on line %d
 
 Warning: assert(): Assertion failed in %s on line %d
+
diff --git a/ext/standard/tests/assert/assert_basic6.phpt b/ext/standard/tests/assert/assert_basic6.phpt
new file mode 100644 (file)
index 0000000..2a73713
--- /dev/null
@@ -0,0 +1,26 @@
+--TEST--
+assert() - basic - Test that bailout works
+--INI--
+assert.active = 1
+assert.warning = 1
+assert.callback = f1
+assert.quiet_eval = 1
+assert.bail = 0
+--FILE--
+<?php
+function f1($message) 
+{
+       echo "f1 called\n";
+}
+
+//bail out on error
+var_dump($rao = assert_options(ASSERT_BAIL, 1));
+$sa = "0 != 0";
+var_dump($r2 = assert($sa, "0 is 0"));
+echo "If this is printed BAIL hasn't worked";
+--EXPECTF--
+int(0)
+f1 called
+
+Warning: assert(): 0 is 0: "0 != 0" failed in %s on line 10
+
index 6211f1c918b1629377b5c20e2e1d738864e90716..657b411f67e2da6fb696aba3602bb694e7c2f3d6 100644 (file)
@@ -20,19 +20,19 @@ function handler($errno, $errstr) {
 
 //Wrong number of parameters for assert_options()
 assert_options(ASSERT_WARNING, 1);
-var_dump($rao=assert_options(ASSERT_CALLBACK,"f1",1));
+var_dump($rao = assert_options(ASSERT_CALLBACK, "f1", 1));
 
 
 //Unknown option for assert_options()
-var_dump($rao=assert_options("F1","f1"));
+var_dump($rao=assert_options("F1", "f1"));
 
 //Wrong number of parameters for  assert()
 $sa="0 != 0";
-var_dump($r2=assert($sa,1));
+var_dump($r2 = assert($sa, "message", 1));
 
 
 //Catch recoverable error with handler
-var_dump($rc=assert('aa=sd+as+safsafasfaçsafçsafç'));
+var_dump($rc = assert('aa=sd+as+safsafasfaçsafçsafç'));
 --EXPECTF--
 Warning: assert_options() expects at most 2 parameters, 3 given in %s on line %d
 NULL
@@ -40,5 +40,6 @@ NULL
 Warning: assert_options() expects parameter 1 to be long, string given in %s on line %d
 NULL
 
-Warning: assert() expects exactly 1 parameter, 2 given in %s on line %d
+Warning: assert() expects at most 2 parameters, 3 given in %s on line %d
 NULL
+
diff --git a/ext/standard/tests/assert/assert_error2.phpt b/ext/standard/tests/assert/assert_error2.phpt
new file mode 100644 (file)
index 0000000..da7c3d9
--- /dev/null
@@ -0,0 +1,30 @@
+--TEST--
+assert() - basic - Test that bailout works
+--INI--
+assert.active = 1
+assert.warning = 1
+assert.callback = f1
+assert.quiet_eval = 1
+assert.bail = 0
+error_reporting = -1
+display_errors = 1
+--FILE--
+<?php
+function f1($script, $line, $message, $user_message) 
+{
+       echo "f1 called\n";
+}
+
+//bail out on error
+var_dump($rao = assert_options(ASSERT_BAIL, 1));
+$sa = "0 != 0";
+var_dump($r2 = assert($sa));
+echo "If this is printed BAIL hasn't worked";
+--EXPECTF--
+int(0)
+
+Warning: Missing argument 4 for f1() in %s on line 2
+f1 called
+
+Warning: assert(): Assertion "0 != 0" failed in %s on line 10
+
diff --git a/ext/standard/tests/assert/assert_error3.phpt b/ext/standard/tests/assert/assert_error3.phpt
new file mode 100644 (file)
index 0000000..54b91ed
--- /dev/null
@@ -0,0 +1,21 @@
+--TEST--
+assert() - basic - Test recoverable error
+--INI--
+assert.active = 1
+assert.warning = 1
+assert.callback = f1
+assert.quiet_eval = 0
+assert.bail = 0
+error_reporting = -1
+display_errors = 1
+--FILE--
+<?php
+$sa = "0 $ 0";
+var_dump($r2 = assert($sa));
+--EXPECTF--
+
+Parse error: syntax error, unexpected '$' in %s(3) : assert code on line 1
+
+Catchable fatal error: assert(): Failure evaluating code: 
+0 $ 0 in %s on line 3
+
diff --git a/ext/standard/tests/assert/assert_error4.phpt b/ext/standard/tests/assert/assert_error4.phpt
new file mode 100644 (file)
index 0000000..264cc8f
--- /dev/null
@@ -0,0 +1,21 @@
+--TEST--
+assert() - basic - Test recoverable error
+--INI--
+assert.active = 1
+assert.warning = 1
+assert.callback = f1
+assert.quiet_eval = 0
+assert.bail = 0
+error_reporting = -1
+display_errors = 1
+--FILE--
+<?php
+$sa = "0 $ 0";
+var_dump($r2 = assert($sa, "Describing what was asserted"));
+--EXPECTF--
+
+Parse error: syntax error, unexpected '$' in %s(3) : assert code on line 1
+
+Catchable fatal error: assert(): Failure evaluating code: 
+Describing what was asserted:"0 $ 0" in %s on line 3
+