> But it's quite possible that argument could be readonly while not a > string, a simple example is a return value of a function: > > % perl -le 'a(b(), "b"); sub a {($_[0], $_[1]) = ($_[1], $_[0]);}; \ > sub b { 5 }' > Modification of a read-only value attempted at -e line 1.
I think you need to revisit that example :) nevertheless, the by-reference nature of $@ was getting in the way of a few mod_perl tests that I didn't notice before, so I needed to use intermediate variables anyway :) > I'd rather see t_is_equal and t_cmp have identical API, if it's not too > much of a trouble to you. ok, the attached patch reflects that. > btw, you don't need to craft \n in t_debug: > just pass an array and it'll add them for you: done --Geoff
Index: Changes =================================================================== RCS file: /home/cvs/httpd-test/perl-framework/Apache-Test/Changes,v retrieving revision 1.137 diff -u -r1.137 Changes --- Changes 28 May 2004 02:07:35 -0000 1.137 +++ Changes 9 Jun 2004 19:22:53 -0000 @@ -8,6 +8,12 @@ =item 1.12-dev +switch the order of arguments in t_cmp() so that it matches +Test::More::is() and other Test::More functions. the new call is + t_cmp($received, $expected, $comment); +support for $expected as the first argument marked as deprecated +and scheduled for removal with Apache-Test 1.15. [Geoffrey Young] + add skip_reason() to Apache::Test, which provides a mechanism for user-specified skip messages [Geoffrey Young] Index: lib/Apache/TestUtil.pm =================================================================== RCS file: /home/cvs/httpd-test/perl-framework/Apache-Test/lib/Apache/TestUtil.pm,v retrieving revision 1.39 diff -u -r1.39 TestUtil.pm --- lib/Apache/TestUtil.pm 2 Jun 2004 02:13:23 -0000 1.39 +++ lib/Apache/TestUtil.pm 9 Jun 2004 19:22:55 -0000 @@ -59,6 +59,16 @@ my ($a, $b) = @_; return 0 unless @_ == 2; + # this was added in Apache::Test::VERSION 1.12 - remove deprecated + # logic in 1.15. + if (UNIVERSAL::isa($a, 'Regexp')) { + my @warning = ("WARNING!!! t_is_equal() argument order has changed.", + "use of a regular expression as the first argument", + "is deprecated. support will be removed soon."); + t_debug(@warning); + ($a, $b) = ($b, $a); + } + if (defined $a && defined $b) { my $ref_a = ref $a; my $ref_b = ref $b; @@ -78,8 +88,8 @@ t_is_equal($a->{$key}, $b->{$key}) || return 0; } } - elsif ($ref_a eq 'Regexp') { - return $b =~ $a; + elsif ($ref_b eq 'Regexp') { + return $a =~ $b; } else { # try to compare the references @@ -97,13 +107,25 @@ sub t_cmp ($$;$) { Carp::carp(join(":", (caller)[1..2]) . - ' usage: $res = t_cmp($expected, $received, [$comment])') + ' usage: $res = t_cmp($received, $expected, [$comment])') if @_ < 2 || @_ > 3; + my ($received, $expected) = @_; + + # this was added in Apache::Test::VERSION 1.12 - remove deprecated + # logic in 1.15. + if (UNIVERSAL::isa($_[0], 'Regexp')) { + my @warning = ("WARNING!!! t_cmp() argument order has changed.", + "use of a regular expression as the first argument", + "is deprecated. support will be removed soon."); + t_debug(@warning); + ($received, $expected) = ($expected, $received); + } + t_debug("testing : " . pop) if @_ == 3; - t_debug("expected: " . struct_as_string(0, $_[0])); - t_debug("received: " . struct_as_string(0, $_[1])); - return t_is_equal($_[0], $_[1]); + t_debug("received: " . struct_as_string(0, $received)); + t_debug("expected: " . struct_as_string(0, $expected)); + return t_is_equal($received, $expected); } # Essentially t_cmp, but on Win32, first converts pathnames @@ -405,7 +427,7 @@ =item t_cmp() - t_cmp($expected, $received, $comment); + t_cmp($received, $expected, $comment); t_cmp() prints the values of I<$comment>, I<$expected> and I<$received>. e.g.: @@ -443,9 +465,9 @@ "hash of array of hashes"); You can also compare the second argument against the first as a -regex. Use the C<qr//> function in the first argument. For example: +regex. Use the C<qr//> function in the second argument. For example: - t_cmp(qr/^abc/, "abcd", "regex compare"); + t_cmp("abcd", qr/^abc/, "regex compare"); will do: @@ -584,10 +606,10 @@ hashes, arrays, scalars, undefs or a combination of any of these. See t_cmp() for an example. -If C<$a> is a regex reference, the regex comparison C<$b =~ $a> is +If C<$b> is a regex reference, the regex comparison C<$a =~ $b> is performed. For example: - t_is_equal(qr{^Apache}, $server_version); + t_is_equal($server_version, qr{^Apache}); If comparing non-scalars make sure to pass the references to the datastructures.