diff options
author | Jesse Luehrs <doy@tozt.net> | 2012-03-07 00:10:54 -0600 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2012-03-07 00:10:54 -0600 |
commit | 8102ebfac9c81a7040ff5ab137f366624a223387 (patch) | |
tree | 0c816905f326aeaeff37086738ab4a7cc4ae0d36 | |
parent | eb1534ec0924b8ff68e370249af41d14077411ab (diff) | |
download | app-ttyrec-8102ebfac9c81a7040ff5ab137f366624a223387.tar.gz app-ttyrec-8102ebfac9c81a7040ff5ab137f366624a223387.zip |
add basic test
-rw-r--r-- | dist.ini | 4 | ||||
-rw-r--r-- | t/basic.t | 87 |
2 files changed, 91 insertions, 0 deletions
@@ -8,3 +8,7 @@ dist = App-Ttyrec repository = github [AutoPrereqs] + +[Prereqs / TestRecommends] +Term::TtyRec::Plus = 0 +IO::Pty::Easy = 0 diff --git a/t/basic.t b/t/basic.t new file mode 100644 index 0000000..977ccc5 --- /dev/null +++ b/t/basic.t @@ -0,0 +1,87 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More; + +use File::Temp 'tempdir'; +use IO::Select; +use Test::Requires 'Term::TtyRec::Plus'; +use Test::Requires 'IO::Pty::Easy'; + +alarm 60; + +my $dir = tempdir(CLEANUP => 1); + +my $pty = IO::Pty::Easy->new; +$pty->spawn($^X, (map {; '-I', $_ } @INC), '-e', <<SCRIPT); +use strict; +use warnings; +use App::Ttyrec; +chdir '$dir'; +App::Ttyrec->new->run(\$^X, '-ple', q[last if /^\$/]); +SCRIPT + +my $crlf = qr/\x0d\x0a/; + +my @frames; +my @times; +{ + $pty->write("foo\n"); + my $frame = full_read($pty); + like($frame, qr/^foo${crlf}foo${crlf}$/m); + push @frames, $frame; + push @times, time; +} +{ + $pty->write("bar\nbaz\n"); + my $frame = full_read($pty); + like($frame, qr/^bar${crlf}(?:bar${crlf}baz${crlf}|baz${crlf}bar${crlf})baz${crlf}$/m); + push @frames, $frame; + push @times, time; +} +{ + $pty->write("\n"); + my $frame = full_read($pty); + like($frame, qr/^${crlf}$/m); + push @frames, $frame; + push @times, time; +} + +my $file = File::Spec->catfile($dir, 'ttyrecord'); +die "couldn't find ttyrecord file" unless -e $file; + +my $ttyrec = Term::TtyRec::Plus->new( + infile => $file, +); + +my $current_frame_idx = 0; +my $current_frame_data = ''; +while (my $frame = $ttyrec->next_frame) { + $current_frame_data .= $frame->{data}; + next if length($current_frame_data) < length($frames[$current_frame_idx]); + is($current_frame_data, $frames[$current_frame_idx]); + cmp_ok(abs($times[$current_frame_idx] - $frame->{orig_timestamp}), '<', 2); + $current_frame_idx++; + $current_frame_data = ''; +} +fail if length($current_frame_data); +fail if $current_frame_idx != 3; + +sub full_read { + my ($pty) = @_; + + my $select = IO::Select->new($pty); + return if $select->has_exception(0.1); + + my $ret; + while ($select->can_read(1)) { + my $new = $pty->read; + last unless defined($new) && length($new); + $ret .= $new; + return $ret if $select->has_exception(0.1); + } + + return $ret; +} + +done_testing; |