From 8102ebfac9c81a7040ff5ab137f366624a223387 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 7 Mar 2012 00:10:54 -0600 Subject: add basic test --- dist.ini | 4 +++ t/basic.t | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 t/basic.t diff --git a/dist.ini b/dist.ini index 6607f94..f136b27 100644 --- a/dist.ini +++ b/dist.ini @@ -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', <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; -- cgit v1.2.3