Test::LongString - tests strings for equality, with more helpful failures


NAME

Test::LongString - tests strings for equality, with more helpful failures


SYNOPSIS

    use Test::More tests => 1;
    use Test::LongString;
    like_string( $html, qr/(perl|cpan)\.org/ );
    #     Failed test (html-test.t at line 12)
    #          got: "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Trans"...
    #       length: 58930
    #     doesn't match '(?-xism:(perl|cpan)\.org)'


DESCRIPTION

This module provides some drop-in replacements for the string comparison functions of the Test::More manpage, but which are more suitable when you test against long strings. If you've ever had to search for text in a multi-line string like an HTML document, or find specific items in binary data, this is the module for you.


FUNCTIONS

is_string( $string, $expected [, $label ] )

is_string() is equivalent to Test::More::is(), but with more helpful diagnostics in case of failure.

For example:

    is_string( $soliloquy, $juliet );
    #     Failed test (soliloquy.t at line 15)
    #          got: "To be, or not to be: that is the question:\x{0a}Whether"...
    #       length: 1490
    #     expected: "O Romeo, Romeo,\x{0a}wherefore art thou Romeo?\x{0a}Deny thy"...
    #       length: 154
    #     strings begin to differ at char 1

is_string_nows( $string, $expected [, $label ] )

Like is_string(), but removes whitepace (in the \s sense) from the arguments before comparing them.

like_string( $string, qr/regex/ [, $label ] )

unlike_string( $string, qr/regex/ [, $label ] )

like_string() and unlike_string() are replacements for Test::More:like() and unlike() that only print the beginning of the received string in the output. Unfortunately, they can't print out the position where the regex failed to match.

    like_string( $soliloquy, qr/Romeo|Juliet|Mercutio|Tybalt/ );
    #     Failed test (soliloquy.t at line 15)
    #          got: "To be, or not to be: that is the question:\x{0a}Whether"...
    #       length: 1490
    #     doesn't match '(?-xism:Romeo|Juliet|Mercutio|Tybalt)'

contains_string( $string, $substring [, $label ] )

contains_string() searches for $substring in $string. It's the same as like_string(), except that it's not a regular expression search.

    contains_string( $soliloquy, "Romeo" );
    #     Failed test (soliloquy.t at line 10)
    #         searched: "To be, or not to be: that is the question:\x{0a}Whether"...
    #   and can't find: "Romeo"

lacks_string( $string, $substring [, $label ] )

lacks_string() makes sure that $substring does NOT exist in $string. It's the same as like_string(), except that it's not a regular expression search.

    lacks_string( $soliloquy, "slings" );
    #     Failed test (soliloquy.t at line 10)
    #         searched: "To be, or not to be: that is the question:\x{0a}Whether"...
    #        and found: "slings"
    #      at position: 147


CONTROLLING OUTPUT

By default, only the first 50 characters of the compared strings are shown in the failure message. This value is in $Test::LongString::Max, and can be set at run-time.

You can also set it by specifying an argument to use:

    use Test::LongString max => 100;

When the compared strings begin to differ after a large prefix, Test::LongString will not print them from the beginning, but will start at the middle, more precisely at $Test::LongString::Context characters before the first difference. By default this value is 10 characters. If you want Test::LongString to always print the beginning of compared strings no matter where they differ, undefine $Test::LongString::Context.


AUTHOR

Written by Rafael Garcia-Suarez. Thanks to Mark Fowler (and to Joss Whedon) for the inspirational the Acme::Test::Buffy manpage. Thanks to Andy Lester for lots of patches.

This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself.


SEE ALSO

the Test::Builder manpage, the Test::Builder::Tester manpage, the Test::More manpage.

 Test::LongString - tests strings for equality, with more helpful failures