Perl, strings, floats, unit testing and regexps! -
ok, preface question potentially 'stupider' normal level of question - problem has been annoying me last few days i'll ask anyway. i'll give mock example of problem can hope generalize current problem.
#!/usr/bin/perl -w use strict; use test::more 'no_plan'; $fruit_string = 'apples cost $1.50'; ($fruit, $price) = $fruit_string =~ /(\w+)s cost \$(\d+\.\d+)/; # $price += 0; # uncomment great success ($price, 1.50, 'great success');
now when run message
# failed test 'great success' # got: '1.50' # expected: '1.5'
to make test work - either uncomment commented line, or use is ($price, '1.50', 'great success')
. both options not work me - i'm testing huge amount of nested data using test::deep , cmp_deeply. question is, how can extract double regexp use double - or if there better way altogether let me know - , feel free tell me take gardening or lol, learning perl hard.
you're using test::deep, can use num()
wrapper perform numerical rather stringwise comparison (it lets add in tolerance, comparing 2 inexact floating point values):
cmp_deeply( $result, { foo => 'foo', bar => 'blah', quantity => 3, price => num(1.5), }, 'result hash correct', );
for normal comparisons done separately, cmp_ok
work, num()
still available: cmp_deeply($value, num(1.5), 'test name')
still works.
Comments
Post a Comment