Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmp-ok calls .Str on the comparison operator if the test fails, causing a warning. #5348

Closed
p6rt opened this issue May 29, 2016 · 10 comments
Closed
Labels

Comments

@p6rt
Copy link

p6rt commented May 29, 2016

Migrated from rt.perl.org#128284 (status was 'resolved')

Searchable as RT128284$

@p6rt
Copy link
Author

p6rt commented Nov 15, 2015

From magnolia.k@me.com

A warning message is displayed when the test using the 'cmp-ok' failed.

$ cat cmp.t
use v6;
use Test;

cmp-ok(1, '==', 2);

done-testing;

$ perl6 cmp.t
not ok 1 -

# Failed test at cmp.t line 4
# expected​: '2'
Sub+{<anon|140414387279056>}+{Precedence} object coerced to string (please use .gist or .perl to do that) in block at lib/Test.pm​:199
# matcher​: 'infix​:<==>'
# got​: '1'
1..1
# Looks like you failed 1 test of 1

In cmp-ok of Test.pm, as pointed out in the warning message, it seems to be because trying to display the code reference.

multi sub cmp-ok(Mu $got, $op, Mu $expected, $desc = '') is export {

...

  if $op ~~ Callable ?? $op !! try EVAL "&infix​:<$op>" -> $matcher {
  $ok = proclaim(?$matcher($got,$expected), $desc);
  if !$ok {
  diag "expected​: '{$expected // $expected.^name}'";
  diag " matcher​: '$matcher'";

@p6rt
Copy link
Author

p6rt commented May 29, 2016

From @zoffixznet

<Zoffix> m​: use Test; cmp-ok 5, &[<], 3;
<camelia> rakudo-moar beb3c9​: OUTPUT«not ok 1 - ␤␤# Failed test at /tmp/sQf9Np82kR line 1␤# expected​: '3'␤Sub+{<anon|52917568>} object coerced to string (please use .gist or .perl to do that) in block at /home/camelia/rakudo-m-inst-1/share/perl6/sources/C712FE6969F786C9380D643DF17E8…»

@p6rt
Copy link
Author

p6rt commented May 29, 2016

From @smls

Neither Roast nor p6doc currently promise support for passing anything other than a string to the $op parameter of &cmp-ok.

S24 also only shows the Str case explicitly, but vaguely alludes to "passing a custom operator", which I suppose refers to passing a Callable object to $op like you're tying to do​:

  The cmp-ok() function compares two values with the given operator
  and passes the test if the comparison yields a True value. For
  ease of use, operators may be passed as strings, such as '==' or
  '~~'. Note that you can also pass a custom operator, for special
  types of comparisons.
 
  <https://design.perl6.org/S24.html#line_172>

I'm changing the title of this ticket to make it clear that this is a speculated feature that has not yet been implemented.

@p6rt
Copy link
Author

p6rt commented May 29, 2016

From @smls

Eh I was wrong, it *is* implemented and works fine if the test passes - it's just when the test fails, that it tries to coerce $op to a string for the diagnostics message​:

  cmp-ok 5, &[<], 6; # works fine
  cmp-ok 6, &[<], 5; # warning about not being able to stringify Sub+{<anon|48380016>}

As the warning says, the code that prints the diagnostics message should probably check if $op is Callable, and if so use .name instead of .Str to stringify it.

(Changing the ticket title again.)

@p6rt
Copy link
Author

p6rt commented May 29, 2016

The RT System itself - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented May 29, 2016

From @zoffixznet

PR to fix this has been submitted​: rakudo/rakudo#777

@p6rt
Copy link
Author

p6rt commented May 29, 2016

From @zoffixznet

Merged in commit rakudo/rakudo@4a7eaa0

@p6rt
Copy link
Author

p6rt commented May 29, 2016

@zoffixznet - Status changed from 'open' to 'resolved'

@p6rt p6rt closed this as completed May 29, 2016
@p6rt
Copy link
Author

p6rt commented May 29, 2016

From @zoffixznet

This is fixed in rakudo/rakudo@4a7eaa0
Also reported in https://rt-archive.perl.org/perl6/Ticket/Display.html?id=128284

Can be closed.

@p6rt
Copy link
Author

p6rt commented May 29, 2016

The RT System itself - Status changed from 'new' to 'open'

@p6rt p6rt added the Bug label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant