Приглашаем посетить
Пастернак (pasternak.niv.ru)

Section 18.1.  Testing Large Strings

Previous
Table of Contents
Next

18.1. Testing Large Strings

We showed in Chapter 17 that when a test fails, Test::More can show us what we expected and what we actually got.

#!/usr/bin/perl
use Test::More 'no_plan';
is( "Hello Perl", "Hello perl" );

When I run this program, Test::More shows me what went wrong.

$ perl test.pl
not ok 1
#     Failed test (test.pl at line 5)
#          got: 'Hello Perl'
#     expected: 'Hello perl'
1..1
# Looks like you failed 1 test of 1.

What if that string is really long? We don't want to see the whole string, which might be hundreds or thousands of characters long. We just want to see where they start to be different.

#!/usr/bin/perl

use Test::More 'no_plan';
use Test::LongString;

is_string(
        "The quick brown fox jumped over the lazy dog\n" x 10,

        "The quick brown fox jumped over the lazy dog\n" x 9 .
        "The quick brown fox jumped over the lazy camel",
        );

The error output doesn't have to show us the whole string to tell us where things went wrong. It shows us the relevant parts along with the string lengths. Although our example is a bit contrived, imagine doing this with a web page, configuration file, or some other huge chunk of data that we don't want cluttering our testing output.

not ok 1
#   Failed test in long_string.pl at line 6.
#          got: ..." the lazy dog\x{0a}"...
#       length: 450
#     expected: ..." the lazy camel"...
#       length: 451
#     strings begin to differ at char 447
1..1
# Looks like you failed 1 test of 1.


Previous
Table of Contents
Next