Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

assert() user message

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
  • Loading branch information...
commit e658a91b3d826ea4104b17f3a6123c1e9f3aee86 1 parent 66fad45
Lars Strojny lstrojny authored
2  NEWS
@@ -25,6 +25,8 @@ PHP NEWS
25 25 handler). (Lonny Kapelushnik)
26 26 . Fixed bug #40459 (Stat and Dir stream wrapper methods do not call
27 27 constructor). (Stas)
  28 + . Added optional second argument for assert() to specify custom message. Patch
  29 + by Lonny Kapelushnik (lonny@lonnylot.com). (Lars)
28 30
29 31 - CURL:
30 32 . Fixed bug #62912 (CURLINFO_PRIMARY_* AND CURLINFO_LOCAL_* not exposed).
48 ext/standard/assert.c
@@ -136,20 +136,20 @@ PHP_MINFO_FUNCTION(assert) /* {{{ */
136 136 }
137 137 /* }}} */
138 138
139   -/* {{{ proto int assert(string|bool assertion)
  139 +/* {{{ proto int assert(string|bool assertion[, string description])
140 140 Checks if assertion is false */
141 141 PHP_FUNCTION(assert)
142 142 {
143 143 zval **assertion;
144   - int val;
  144 + int val, description_len = 0;
145 145 char *myeval = NULL;
146   - char *compiled_string_description;
  146 + char *compiled_string_description, *description;
147 147
148 148 if (! ASSERTG(active)) {
149 149 RETURN_TRUE;
150 150 }
151 151
152   - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &assertion) == FAILURE) {
  152 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z|s", &assertion, &description, &description_len) == FAILURE) {
153 153 return;
154 154 }
155 155
@@ -167,7 +167,11 @@ PHP_FUNCTION(assert)
167 167 compiled_string_description = zend_make_compiled_string_description("assert code" TSRMLS_CC);
168 168 if (zend_eval_stringl(myeval, Z_STRLEN_PP(assertion), &retval, compiled_string_description TSRMLS_CC) == FAILURE) {
169 169 efree(compiled_string_description);
170   - php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "Failure evaluating code: %s%s", PHP_EOL, myeval);
  170 + if (description_len == 0) {
  171 + php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "Failure evaluating code: %s%s", PHP_EOL, myeval);
  172 + } else {
  173 + php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "Failure evaluating code: %s%s:\"%s\"", PHP_EOL, description, myeval);
  174 + }
171 175 if (ASSERTG(bail)) {
172 176 zend_bailout();
173 177 }
@@ -196,7 +200,7 @@ PHP_FUNCTION(assert)
196 200 }
197 201
198 202 if (ASSERTG(callback)) {
199   - zval *args[3];
  203 + zval **args = safe_emalloc(description_len == 0 ? 3 : 4, sizeof(zval **), 0);
200 204 zval *retval;
201 205 int i;
202 206 uint lineno = zend_get_executed_lineno(TSRMLS_C);
@@ -214,19 +218,38 @@ PHP_FUNCTION(assert)
214 218 ZVAL_FALSE(retval);
215 219
216 220 /* XXX do we want to check for error here? */
217   - call_user_function(CG(function_table), NULL, ASSERTG(callback), retval, 3, args TSRMLS_CC);
  221 + if (description_len == 0) {
  222 + call_user_function(CG(function_table), NULL, ASSERTG(callback), retval, 3, args TSRMLS_CC);
  223 + for (i = 0; i <= 2; i++) {
  224 + zval_ptr_dtor(&(args[i]));
  225 + }
  226 + } else {
  227 + MAKE_STD_ZVAL(args[3]);
  228 + ZVAL_STRINGL(args[3], SAFE_STRING(description), description_len, 1);
218 229
219   - for (i = 0; i <= 2; i++) {
220   - zval_ptr_dtor(&(args[i]));
  230 + call_user_function(CG(function_table), NULL, ASSERTG(callback), retval, 4, args TSRMLS_CC);
  231 + for (i = 0; i <= 3; i++) {
  232 + zval_ptr_dtor(&(args[i]));
  233 + }
221 234 }
  235 +
  236 + efree(args);
222 237 zval_ptr_dtor(&retval);
223 238 }
224 239
225 240 if (ASSERTG(warning)) {
226   - if (myeval) {
227   - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Assertion \"%s\" failed", myeval);
  241 + if (description_len == 0) {
  242 + if (myeval) {
  243 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Assertion \"%s\" failed", myeval);
  244 + } else {
  245 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Assertion failed");
  246 + }
228 247 } else {
229   - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Assertion failed");
  248 + if (myeval) {
  249 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s: \"%s\" failed", description, myeval);
  250 + } else {
  251 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s failed", description);
  252 + }
230 253 }
231 254 }
232 255
@@ -321,3 +344,4 @@ PHP_FUNCTION(assert_options)
321 344 * vim600: sw=4 ts=4 fdm=marker
322 345 * vim<600: sw=4 ts=4
323 346 */
  347 +
5 ext/standard/tests/assert/assert04.phpt
@@ -15,7 +15,7 @@ assert(1);
15 15
16 16 /* Wrong parameter count in assert */
17 17 assert_options(ASSERT_ACTIVE, 1);
18   -assert(2,3);
  18 +assert(2, "failure", 3);
19 19
20 20 /* Wrong parameter count in assert_options */
21 21 assert_options(ASSERT_ACTIVE, 0, 2);
@@ -36,7 +36,7 @@ echo "not reached\n";
36 36
37 37 ?>
38 38 --EXPECTF--
39   -Warning: assert() expects exactly 1 parameter, 2 given in %s on line %d
  39 +Warning: assert() expects at most 2 parameters, 3 given in %s on line %d
40 40
41 41 Warning: assert_options() expects at most 2 parameters, 3 given in %s on line %d
42 42
@@ -45,3 +45,4 @@ Warning: assert_options() expects parameter 1 to be long, %unicode_string_option
45 45 Warning: assert(): Assertion failed in %s on line %d
46 46
47 47 Warning: assert(): Assertion failed in %s on line %d
  48 +
26 ext/standard/tests/assert/assert_basic6.phpt
... ... @@ -0,0 +1,26 @@
  1 +--TEST--
  2 +assert() - basic - Test that bailout works
  3 +--INI--
  4 +assert.active = 1
  5 +assert.warning = 1
  6 +assert.callback = f1
  7 +assert.quiet_eval = 1
  8 +assert.bail = 0
  9 +--FILE--
  10 +<?php
  11 +function f1($message)
  12 +{
  13 + echo "f1 called\n";
  14 +}
  15 +
  16 +//bail out on error
  17 +var_dump($rao = assert_options(ASSERT_BAIL, 1));
  18 +$sa = "0 != 0";
  19 +var_dump($r2 = assert($sa, "0 is 0"));
  20 +echo "If this is printed BAIL hasn't worked";
  21 +--EXPECTF--
  22 +int(0)
  23 +f1 called
  24 +
  25 +Warning: assert(): 0 is 0: "0 != 0" failed in %s on line 10
  26 +
11 ext/standard/tests/assert/assert_error1.phpt
@@ -20,19 +20,19 @@ function handler($errno, $errstr) {
20 20
21 21 //Wrong number of parameters for assert_options()
22 22 assert_options(ASSERT_WARNING, 1);
23   -var_dump($rao=assert_options(ASSERT_CALLBACK,"f1",1));
  23 +var_dump($rao = assert_options(ASSERT_CALLBACK, "f1", 1));
24 24
25 25
26 26 //Unknown option for assert_options()
27   -var_dump($rao=assert_options("F1","f1"));
  27 +var_dump($rao=assert_options("F1", "f1"));
28 28
29 29 //Wrong number of parameters for assert()
30 30 $sa="0 != 0";
31   -var_dump($r2=assert($sa,1));
  31 +var_dump($r2 = assert($sa, "message", 1));
32 32
33 33
34 34 //Catch recoverable error with handler
35   -var_dump($rc=assert('aa=sd+as+safsafasfaçsafçsafç'));
  35 +var_dump($rc = assert('aa=sd+as+safsafasfaçsafçsafç'));
36 36 --EXPECTF--
37 37 Warning: assert_options() expects at most 2 parameters, 3 given in %s on line %d
38 38 NULL
@@ -40,5 +40,6 @@ NULL
40 40 Warning: assert_options() expects parameter 1 to be long, string given in %s on line %d
41 41 NULL
42 42
43   -Warning: assert() expects exactly 1 parameter, 2 given in %s on line %d
  43 +Warning: assert() expects at most 2 parameters, 3 given in %s on line %d
44 44 NULL
  45 +
30 ext/standard/tests/assert/assert_error2.phpt
... ... @@ -0,0 +1,30 @@
  1 +--TEST--
  2 +assert() - basic - Test that bailout works
  3 +--INI--
  4 +assert.active = 1
  5 +assert.warning = 1
  6 +assert.callback = f1
  7 +assert.quiet_eval = 1
  8 +assert.bail = 0
  9 +error_reporting = -1
  10 +display_errors = 1
  11 +--FILE--
  12 +<?php
  13 +function f1($script, $line, $message, $user_message)
  14 +{
  15 + echo "f1 called\n";
  16 +}
  17 +
  18 +//bail out on error
  19 +var_dump($rao = assert_options(ASSERT_BAIL, 1));
  20 +$sa = "0 != 0";
  21 +var_dump($r2 = assert($sa));
  22 +echo "If this is printed BAIL hasn't worked";
  23 +--EXPECTF--
  24 +int(0)
  25 +
  26 +Warning: Missing argument 4 for f1() in %s on line 2
  27 +f1 called
  28 +
  29 +Warning: assert(): Assertion "0 != 0" failed in %s on line 10
  30 +
21 ext/standard/tests/assert/assert_error3.phpt
... ... @@ -0,0 +1,21 @@
  1 +--TEST--
  2 +assert() - basic - Test recoverable error
  3 +--INI--
  4 +assert.active = 1
  5 +assert.warning = 1
  6 +assert.callback = f1
  7 +assert.quiet_eval = 0
  8 +assert.bail = 0
  9 +error_reporting = -1
  10 +display_errors = 1
  11 +--FILE--
  12 +<?php
  13 +$sa = "0 $ 0";
  14 +var_dump($r2 = assert($sa));
  15 +--EXPECTF--
  16 +
  17 +Parse error: syntax error, unexpected '$' in %s(3) : assert code on line 1
  18 +
  19 +Catchable fatal error: assert(): Failure evaluating code:
  20 +0 $ 0 in %s on line 3
  21 +
21 ext/standard/tests/assert/assert_error4.phpt
... ... @@ -0,0 +1,21 @@
  1 +--TEST--
  2 +assert() - basic - Test recoverable error
  3 +--INI--
  4 +assert.active = 1
  5 +assert.warning = 1
  6 +assert.callback = f1
  7 +assert.quiet_eval = 0
  8 +assert.bail = 0
  9 +error_reporting = -1
  10 +display_errors = 1
  11 +--FILE--
  12 +<?php
  13 +$sa = "0 $ 0";
  14 +var_dump($r2 = assert($sa, "Describing what was asserted"));
  15 +--EXPECTF--
  16 +
  17 +Parse error: syntax error, unexpected '$' in %s(3) : assert code on line 1
  18 +
  19 +Catchable fatal error: assert(): Failure evaluating code:
  20 +Describing what was asserted:"0 $ 0" in %s on line 3
  21 +

0 comments on commit e658a91

Please sign in to comment.
Something went wrong with that request. Please try again.