gitweb/lib - Simple output capture by redirecting STDOUT
[git/jnareb-git.git] / t / t9504 / test_capture_interface.pl
blob47ab80459267be66d59cdd254855fa5eeee1060a
1 #!/usr/bin/perl
2 use lib (split(/:/, $ENV{GITPERLLIB}));
4 use warnings;
5 use strict;
6 use utf8;
8 use Test::More;
10 # test source version
11 use lib $ENV{GITWEBLIBDIR} || "$ENV{GIT_BUILD_DIR}/gitweb/lib";
13 # ....................................................................
15 use_ok('GitwebCache::Capture::Simple');
16 diag("Using lib '$INC[0]'");
17 diag("Testing '$INC{'GitwebCache/Capture/Simple.pm'}'");
19 # Test setting up capture
21 my $capture = new_ok('GitwebCache::Capture::Simple' => [], 'The $capture');
23 # Test capturing
25 sub capture_block (&) {
26 return $capture->capture(shift);
29 diag('Should not print anything except test results and diagnostic');
30 my $test_data = 'Capture this';
31 my $captured = capture_block {
32 print $test_data;
34 is($captured, $test_data, 'capture simple data');
36 binmode STDOUT, ':utf8';
37 $test_data = <<'EOF';
38 Zażółć gęsią jaźń
39 EOF
40 utf8::decode($test_data);
41 $captured = capture_block {
42 binmode STDOUT, ':utf8';
44 print $test_data;
46 utf8::decode($captured);
47 is($captured, $test_data, 'capture utf8 data');
49 $test_data = '|\x{fe}\x{ff}|\x{9F}|\000|'; # invalid utf-8
50 $captured = capture_block {
51 binmode STDOUT, ':raw';
53 print $test_data;
55 is($captured, $test_data, 'capture raw data');
57 # Test nested capturing
59 TODO: {
60 local $TODO = "not required for capturing gitweb output";
61 no warnings;
63 my $outer_capture = GitwebCache::Capture::Simple->new();
64 $captured = $outer_capture->capture(sub {
65 print "pre|";
66 my $captured = $capture->capture(sub {
67 print "INNER";
68 });
69 print lc($captured);
70 print "|post";
71 });
72 is($captured, "pre|inner|post", 'nested capture');
75 SKIP: {
76 skip "Capture::Tiny not available", 1
77 unless eval { require Capture::Tiny; };
79 $captured = Capture::Tiny::capture(sub {
80 my $inner = $capture->capture(sub {
81 print "INNER";
82 });
83 });
84 is($captured, '', "doesn't print while capturing");
87 done_testing();
89 # Local Variables:
90 # coding: utf-8
91 # End: