gitweb/lib - capture output directly to cache entry file
[git/jnareb-git.git] / t / t9504 / test_capture_interface.pl
blob26c93037d5618d426c6adcae192235405c54cd09
1 #!/usr/bin/perl
2 use lib (split(/:/, $ENV{GITPERLLIB}));
4 use warnings;
5 use strict;
6 use utf8;
8 use Test::More;
9 use File::Compare;
11 # test source version
12 use lib $ENV{GITWEBLIBDIR} || "$ENV{GIT_BUILD_DIR}/gitweb/lib";
14 # ....................................................................
16 use_ok('GitwebCache::Capture::Simple');
17 diag("Using lib '$INC[0]'");
18 diag("Testing '$INC{'GitwebCache/Capture/Simple.pm'}'");
20 # Test setting up capture
22 my $capture = new_ok('GitwebCache::Capture::Simple' => [], 'The $capture');
24 # Test capturing
26 sub capture_block (&) {
27 return $capture->capture(shift);
30 diag('Should not print anything except test results and diagnostic');
31 my $test_data = 'Capture this';
32 my $captured = capture_block {
33 print $test_data;
35 is($captured, $test_data, 'capture simple data');
37 binmode STDOUT, ':utf8';
38 $test_data = <<'EOF';
39 Zażółć gęsią jaźń
40 EOF
41 utf8::decode($test_data);
42 $captured = capture_block {
43 binmode STDOUT, ':utf8';
45 print $test_data;
47 utf8::decode($captured);
48 is($captured, $test_data, 'capture utf8 data');
50 $test_data = '|\x{fe}\x{ff}|\x{9F}|\000|'; # invalid utf-8
51 $captured = capture_block {
52 binmode STDOUT, ':raw';
54 print $test_data;
56 is($captured, $test_data, 'capture raw data');
58 # Test nested capturing
60 TODO: {
61 local $TODO = "not required for capturing gitweb output";
62 no warnings;
64 my $outer_capture = GitwebCache::Capture::Simple->new();
65 $captured = $outer_capture->capture(sub {
66 print "pre|";
67 my $captured = $capture->capture(sub {
68 print "INNER";
69 });
70 print lc($captured);
71 print "|post";
72 });
73 is($captured, "pre|inner|post", 'nested capture');
76 SKIP: {
77 skip "Capture::Tiny not available", 1
78 unless eval { require Capture::Tiny; };
80 $captured = Capture::Tiny::capture(sub {
81 my $inner = $capture->capture(sub {
82 print "INNER";
83 });
84 });
85 is($captured, '', "doesn't print while capturing");
88 # Test capturing to file
90 my $test_data = 'Capture this';
91 open my $fh, '>', 'expected' or die "Couldn't open file for writing: $!";
92 print {$fh} $test_data;
93 close $fh;
95 $capture->capture(sub { print $test_data; }, 'actual');
96 cmp_ok(compare('expected', 'actual'), '==', 0, 'capturing to file via filename');
98 open my $fh, '>', 'actual' or die "Couldn't open file for writing: $!";
99 $capture->capture(sub { print $test_data; }, $fh);
100 close $fh;
101 cmp_ok(compare('expected', 'actual'), '==', 0, 'capturing to file via filehandle');
104 done_testing();
106 # Local Variables:
107 # coding: utf-8
108 # End: