core: evaluate left/right part of comparison after split on the comparison operator in ${if:xxx} (closes #1627)

To force evaluation of the expression before doing the comparison (less safe),
the "${eval_cond:xxx}" can be used.

With the old behavior we had:

    >> ${if:a==b}
    == [0]
    >> ${if:${raw:a==b}}
    == [0]
    >> ${if:${eval_cond:${raw:a==b}}}
    == [0]

And with the new behavior, we have:

    >> ${if:a==b}
    == [0]
    >> ${if:${raw:a==b}}
    == [1]
    >> ${if:${eval_cond:${raw:a==b}}}
    == [0]
This commit is contained in:
Sébastien Helleu 2021-04-04 14:44:18 +02:00
parent 1aefb0a546
commit 3bf585ba04
3 changed files with 37 additions and 6 deletions

View File

@ -27,6 +27,7 @@ New features::
Bug fixes::
* core: evaluate left/right part of comparison after split on the comparison operator in ${if:xxx} (issue #1627)
* core: prevent switching to start of visited buffers when jumping to next (issue #1591, issue #1592)
* core: recreate buflist and fset bars on /reload when WeeChat is started without configuration files (issue #1618)
* buflist: fix comparison of hotlists in option buflist.look.sort (issue #1621)

View File

@ -666,7 +666,7 @@ char *
eval_string_if (const char *text, struct t_eval_context *eval_context)
{
const char *pos, *pos2;
char *value, *condition, *tmp, *tmp2;
char *value, *condition, *tmp;
int rc;
value = NULL;
@ -679,13 +679,10 @@ eval_string_if (const char *text, struct t_eval_context *eval_context)
strndup (text, pos - text) : strdup (text);
if (!condition)
return strdup ("");
tmp = eval_replace_vars (condition, eval_context);
tmp2 = eval_expression_condition ((tmp) ? tmp : "", eval_context);
rc = eval_is_true (tmp2);
tmp = eval_expression_condition (condition, eval_context);
rc = eval_is_true (tmp);
if (tmp)
free (tmp);
if (tmp2)
free (tmp2);
if (rc)
{
/*

View File

@ -86,6 +86,9 @@ TEST(CoreEval, EvalCondition)
NULL, NULL);
CHECK(extra_vars);
hashtable_set (extra_vars, "test", "value");
hashtable_set (extra_vars, "var_abc", "abc");
hashtable_set (extra_vars, "var_cond_true", "a==a");
hashtable_set (extra_vars, "var_cond_false", "a==b");
options = hashtable_new (32,
WEECHAT_HASHTABLE_STRING,
@ -175,6 +178,8 @@ TEST(CoreEval, EvalCondition)
WEE_CHECK_EVAL("0", "${if:${buffer.number}==2?yes:no} == yes");
WEE_CHECK_EVAL("0", "yes == ${if:${buffer.number}==2?yes:no}");
WEE_CHECK_EVAL("0", "${if:\\$==A?yes:}");
WEE_CHECK_EVAL("0", "${var_does_not_exist}");
WEE_CHECK_EVAL("0", "${var_abc} == def");
/* conditions evaluated as true */
WEE_CHECK_EVAL("1", "1");
@ -262,6 +267,14 @@ TEST(CoreEval, EvalCondition)
WEE_CHECK_EVAL("1", "${if:${buffer.number}==1?yes:no} == yes");
WEE_CHECK_EVAL("1", "yes == ${if:${buffer.number}==1?yes:no}");
WEE_CHECK_EVAL("1", "${if:\\$==\\$?yes:}");
WEE_CHECK_EVAL("1", "${var_abc}");
WEE_CHECK_EVAL("1", "${var_abc} == abc");
WEE_CHECK_EVAL("1", "${var_cond_true}");
WEE_CHECK_EVAL("0", "${var_cond_true} == zzz");
WEE_CHECK_EVAL("1", "${var_cond_true} != zzz");
WEE_CHECK_EVAL("1", "${var_cond_false}");
WEE_CHECK_EVAL("0", "${var_cond_false} == zzz");
WEE_CHECK_EVAL("1", "${var_cond_false} != zzz");
/* evaluation of extra_vars */
hashtable_set (options, "extra", "eval");
@ -455,6 +468,9 @@ TEST(CoreEval, EvalExpression)
NULL, NULL);
CHECK(extra_vars);
hashtable_set (extra_vars, "test", "value");
hashtable_set (extra_vars, "var_abc", "abc");
hashtable_set (extra_vars, "var_cond_true", "a==a");
hashtable_set (extra_vars, "var_cond_false", "a==b");
options = hashtable_new (32,
WEECHAT_HASHTABLE_STRING,
@ -486,6 +502,12 @@ TEST(CoreEval, EvalExpression)
WEE_CHECK_EVAL("0", "${eval_cond:}");
WEE_CHECK_EVAL("0", "${eval_cond:${buffer.number} == 2}");
WEE_CHECK_EVAL("1", "${eval_cond:${buffer.number} == 1}");
WEE_CHECK_EVAL("1", "${eval_cond:${var_abc}}");
WEE_CHECK_EVAL("1", "${eval_cond:${var_abc} == abc}");
WEE_CHECK_EVAL("0", "${eval_cond:${var_abc} == def}");
WEE_CHECK_EVAL("1", "${eval_cond:${var_cond_true}}");
WEE_CHECK_EVAL("1", "${eval_cond:${var_cond_true}}");
WEE_CHECK_EVAL("0", "${eval_cond:${var_cond_false}}");
/* test value from extra_vars */
WEE_CHECK_EVAL("value", "${test}");
@ -702,13 +724,24 @@ TEST(CoreEval, EvalExpression)
WEE_CHECK_EVAL("yes-no", "${if:5>2?${if:1>7?yes-yes:yes-no}:${if:9>4?no-yes:no-no}}");
WEE_CHECK_EVAL("no-yes", "${if:1>7?${if:6>3?yes-yes:yes-no}:${if:9>4?no-yes:no-no}}");
WEE_CHECK_EVAL("no-no", "${if:1>7?${if:1>7?yes-yes:yes-no}:${if:1>7?no-yes:no-no}}");
WEE_CHECK_EVAL("0", "${if:}");
WEE_CHECK_EVAL("0", "${if:0}");
WEE_CHECK_EVAL("1", "${if:1}");
WEE_CHECK_EVAL("1", "${if:abc}");
WEE_CHECK_EVAL("0", "${if:abc!=abc}");
WEE_CHECK_EVAL("1", "${if:abc==abc}");
WEE_CHECK_EVAL("1", "${if:${if:abc==abc}}");
WEE_CHECK_EVAL("0", "${if:${rev:${if:42==42?hello:bye}}==eyb}");
WEE_CHECK_EVAL("1", "${if:${rev:${if:42==42?hello:bye}}==olleh}");
WEE_CHECK_EVAL("1", "${if:${var_abc}}");
WEE_CHECK_EVAL("1", "${if:${var_abc} == abc}");
WEE_CHECK_EVAL("0", "${if:${var_abc} == def}");
WEE_CHECK_EVAL("1", "${if:${var_cond_true}}");
WEE_CHECK_EVAL("0", "${if:${var_cond_true} == zzz}");
WEE_CHECK_EVAL("1", "${if:${var_cond_true} != zzz}");
WEE_CHECK_EVAL("1", "${if:${var_cond_false}}");
WEE_CHECK_EVAL("0", "${if:${var_cond_false} == zzz}");
WEE_CHECK_EVAL("1", "${if:${var_cond_false} != zzz}");
/* test calc */
WEE_CHECK_EVAL("0", "${calc:}");