/t/carped.t

http://github.com/chorny/test-warn · Perl · 71 lines · 55 code · 11 blank · 5 comment · 2 complexity · c7b9f7739f6c78619e7dbeb8d3be4857 MD5 · raw file

  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Carp;
  5. use Test::Builder::Tester tests => 6;
  6. Test::Builder::Tester::color 'on';
  7. use Test::Warn;
  8. sub foo {
  9. warn "Warning 1";
  10. carp "Carping 2";
  11. carp "Carping 3";
  12. warn "Warning 4";
  13. }
  14. #use File::Spec;
  15. #my $tcarped = File::Spec->catfile('t','carped.t');
  16. #$tcarped =~ s/\\/\//g if $^O eq 'MSWin32';
  17. #also will not work on VMS
  18. my $tcarped = $0; #'t/carped.t';
  19. test_out "ok 1";
  20. warnings_like {foo()} [map {qr/$_/} (1 .. 4)];
  21. test_test "Warnings and Carpings mixed, asked only for like warnings";
  22. test_out "not ok 1";
  23. my @test_diag = (
  24. "found warning: Warning 1 at $tcarped line 13.",
  25. "found carped warning: Carping 2 at $tcarped line 14".($Carp::VERSION gt "1.24"?".":""),
  26. "found carped warning: Carping 3 at $tcarped line 15".($Carp::VERSION gt "1.24"?".":""),
  27. "found warning: Warning 4 at $tcarped line 16.",
  28. "expected to find carped warning: (?-xism:1)",
  29. "expected to find carped warning: (?-xism:2)",
  30. "expected to find carped warning: (?-xism:3)",
  31. "expected to find carped warning: (?-xism:4)",
  32. );
  33. if (qr/x/ =~ /\(\?\^/){ s/-xism/^/ for @test_diag }
  34. test_fail +2;
  35. test_diag @test_diag;
  36. warnings_like {foo()} [{carped => [map {qr/$_/} (1 .. 4)]}];
  37. test_test "Warnings and Carpings mixed, asked only for like carpings";
  38. test_out "ok 1";
  39. warnings_like {foo()} [qr/1/, {carped => [qr/2/, qr/3/]}, qr/4/];
  40. test_test "Warnings and Carpings mixed, asked for the right likes";
  41. my @msg = ("Warning 1", "Carping 2", "Carping 3", "Warning 4");
  42. test_out "ok 1";
  43. warnings_are {foo()} \@msg;
  44. test_test "Warnings and Carpings mixed, asked only for warnings";
  45. test_out "not ok 1";
  46. test_fail +10;
  47. test_diag
  48. "found warning: Warning 1 at $tcarped line 13.",
  49. "found carped warning: Carping 2 at $tcarped line 14".($Carp::VERSION gt "1.24"?".":""),
  50. "found carped warning: Carping 3 at $tcarped line 15".($Carp::VERSION gt "1.24"?".":""),
  51. "found warning: Warning 4 at $tcarped line 16.",
  52. "expected to find carped warning: Warning 1",
  53. "expected to find carped warning: Carping 2",
  54. "expected to find carped warning: Carping 3",
  55. "expected to find carped warning: Warning 4";
  56. warnings_are {foo()} {carped => \@msg};
  57. test_test "Warnings and Carpings mixed, asked only for carpings";
  58. test_out "ok 1";
  59. warnings_are {foo()} [$msg[0], {carped => [@msg[1..2]]}, $msg[3]];
  60. test_test "Warnings and Carpings mixed, asked for the right ones";