📄 oneshot.pl
字号:
{ title "$TopType - From Filename to Buffer, Append($append)" ; my $lex = new LexFile(my $in_file) ; writeFile($in_file, $comp); my $in = new IO::File "<$in_file" ; my $output ; $output = $incumbent if $append ; ok &$Func($in, \$output, Append => $append, @opts), ' Uncompressed ok' ; is $keep_comp, $comp, " Input buffer not changed" ; is $output, $expected, " Uncompressed matches original"; } { title "$TopType - From stdin (via '-') to Buffer content, Append($append) " ; my $lex = new LexFile(my $in_file) ; writeFile($in_file, $comp); open(SAVEIN, "<&STDIN"); my $dummy = fileno SAVEIN ; ok open(STDIN, "<$in_file"), " redirect STDIN"; my $output ; $output = $incumbent if $append ; ok &$Func('-', \$output, Append => $append, @opts), ' Uncompressed ok' or diag $$Error ; open(STDIN, "<&SAVEIN"); is $keep_comp, $comp, " Input buffer not changed" ; is $output, $expected, " Uncompressed matches original"; } } { title "$TopType - From Handle to Buffer, InputLength" ; my $lex = new LexFile(my $in_file, my $out_file) ; my $out ; my $expected = $buffer ; my $appended = 'appended'; my $len_appended = length $appended; writeFile($in_file, $comp . $appended . $comp . $appended) ; my $in = new IO::File "<$in_file" ; ok &$Func($in, \$out, Transparent => 0, InputLength => length $comp, @opts), ' Uncompressed ok' ; is $out, $expected, " Uncompressed matches original"; my $buff; is $in->read($buff, $len_appended), $len_appended, " Length of Appended data ok"; is $buff, $appended, " Appended data ok"; $out = ''; ok &$Func($in, \$out, Transparent => 0, InputLength => length $comp, @opts), ' Uncompressed ok' ; is $out, $expected, " Uncompressed matches original"; $buff = ''; is $in->read($buff, $len_appended), $len_appended, " Length of Appended data ok"; is $buff, $appended, " Appended data ok"; } for my $stdin ('-', *STDIN) # , \*STDIN) { title "$TopType - From stdin (via $stdin) to Buffer content, InputLength" ; my $lex = new LexFile my $in_file ; my $expected = $buffer ; my $appended = 'appended'; my $len_appended = length $appended; writeFile($in_file, $comp . $appended ) ; open(SAVEIN, "<&STDIN"); my $dummy = fileno SAVEIN ; ok open(STDIN, "<$in_file"), " redirect STDIN"; my $output ; ok &$Func($stdin, \$output, Transparent => 0, InputLength => length $comp, @opts), ' Uncompressed ok' or diag $$Error ; my $buff ; is read(STDIN, $buff, $len_appended), $len_appended, " Length of Appended data ok"; is $output, $expected, " Uncompressed matches original"; is $buff, $appended, " Appended data ok"; open(STDIN, "<&SAVEIN"); } } foreach my $bit ($UncompressClass, 'IO::Uncompress::AnyUncompress', ) { # TODO -- Add Append mode tests my $Error = getErrorRef($bit); my $Func = getTopFuncRef($bit); my $TopType = getTopFuncName($bit); my $buffer = "abcde" ; my $keep_orig = $buffer; my $null = compressBuffer(getTopFuncName($UncompressClass), "") ; my $undef = compressBuffer(getTopFuncName($UncompressClass), undef) ; my $comp = compressBuffer(getTopFuncName($UncompressClass), $buffer) ; my $keep_comp = $comp; my @opts = (); @opts = (RawInflate => 1) if $bit eq 'IO::Uncompress::AnyUncompress'; my $incumbent = "incumbent data" ; my $lex = new LexFile(my $file1, my $file2) ; writeFile($file1, compressBuffer(getTopFuncName($UncompressClass),"data1")); writeFile($file2, compressBuffer(getTopFuncName($UncompressClass),"data2")); my $of = new IO::File "<$file1" ; ok $of, " Created output filehandle" ; #my @input = ($file2, \$undef, \$null, \$comp, $of) ; #my @expected = ('data2', '', '', 'abcde', 'data1'); my @input = ($file1, $file2); my @expected = ('data1', 'data2'); my @keep = @input ; { title "$TopType - From ArrayRef to Buffer" ; my $output ; ok &$Func(\@input, \$output, AutoClose => 0, @opts), ' UnCompressed ok' ; is $output, join('', @expected) } { title "$TopType - From ArrayRef to Filename" ; my $lex = new LexFile my $output; $of->open("<$file1") ; ok &$Func(\@input, $output, AutoClose => 0, @opts), ' UnCompressed ok' ; is readFile($output), join('', @expected) } { title "$TopType - From ArrayRef to Filehandle" ; my $lex = new LexFile my $output; my $fh = new IO::File ">$output" ; $of->open("<$file1") ; ok &$Func(\@input, $fh, AutoClose => 0, @opts), ' UnCompressed ok' ; $fh->close; is readFile($output), join('', @expected) } { title "$TopType - From Array Ref to Array Ref" ; my @output = (\'first') ; $of->open("<$file1") ; ok &$Func(\@input, \@output, AutoClose => 0, @opts), ' UnCompressed ok' ; is_deeply \@input, \@keep, " Input array not changed" ; is_deeply [map { defined $$_ ? $$_ : "" } @output], ['first', @expected], " Got Expected uncompressed data"; } } foreach my $bit ($UncompressClass, 'IO::Uncompress::AnyUncompress', ) { # TODO -- Add Append mode tests my $Error = getErrorRef($bit); my $Func = getTopFuncRef($bit); my $TopType = getTopFuncName($bit); my $tmpDir1 = 'tmpdir1'; my $tmpDir2 = 'tmpdir2'; my $lex = new LexDir($tmpDir1, $tmpDir2) ; mkdir $tmpDir1, 0777; mkdir $tmpDir2, 0777; my @opts = (); @opts = (RawInflate => 1) if $bit eq 'IO::Uncompress::AnyUncompress'; ok -d $tmpDir1, " Temp Directory $tmpDir1 exists"; #ok ! -d $tmpDir2, " Temp Directory $tmpDir2 does not exist"; my @files = map { "$tmpDir1/$_.tmp" } qw( a1 a2 a3) ; foreach (@files) { writeFile($_, compressBuffer(getTopFuncName($UncompressClass), "abc $_")) } my @expected = map { "abc $_" } @files ; my @outFiles = map { s/$tmpDir1/$tmpDir2/; $_ } @files ; { title "$TopType - From FileGlob to FileGlob" ; ok &$Func("<$tmpDir1/a*.tmp>" => "<$tmpDir2/a#1.tmp>", @opts), ' UnCompressed ok' or diag $$Error ; my @copy = @expected; for my $file (@outFiles) { is readFile($file), shift @copy, " got expected from $file" ; } is @copy, 0, " got all files"; } { title "$TopType - From FileGlob to Arrayref" ; my @output = (\'first'); ok &$Func("<$tmpDir1/a*.tmp>" => \@output, @opts), ' UnCompressed ok' or diag $$Error ; my @copy = ('first', @expected); for my $data (@output) { is $$data, shift @copy, " got expected data" ; } is @copy, 0, " got all files"; } { title "$TopType - From FileGlob to Buffer" ; my $output ; ok &$Func("<$tmpDir1/a*.tmp>" => \$output, @opts), ' UnCompressed ok' or diag $$Error ; is $output, join('', @expected), " got expected uncompressed data"; } { title "$TopType - From FileGlob to Filename" ; my $lex = new LexFile my $output ; ok ! -e $output, " $output does not exist" ; ok &$Func("<$tmpDir1/a*.tmp>" => $output, @opts), ' UnCompressed ok' or diag $$Error ; ok -e $output, " $output does exist" ; is readFile($output), join('', @expected), " got expected uncompressed data"; } { title "$TopType - From FileGlob to Filehandle" ; my $output = 'abc' ; my $lex = new LexFile $output ; my $fh = new IO::File ">$output" ; ok &$Func("<$tmpDir1/a*.tmp>" => $fh, AutoClose => 1, @opts), ' UnCompressed ok' or diag $$Error ; ok -e $output, " $output does exist" ; is readFile($output), join('', @expected), " got expected uncompressed data"; } } foreach my $TopType ($CompressClass # TODO -- add the inflate classes ) { my $Error = getErrorRef($TopType); my $Func = getTopFuncRef($TopType); my $Name = getTopFuncName($TopType); title "More write tests" ; my $lex = new LexFile(my $file1, my $file2, my $file3) ; writeFile($file1, "F1"); writeFile($file2, "F2"); writeFile($file3, "F3");# my @data = (# [ '[\"ab", \"cd"]', "abcd" ],## [ '[\"a", $fh1, \"bc"]', "aF1bc"],# ) ;### foreach my $data (@data)# {# my ($send, $get) = @$data ;## my $fh1 = new IO::File "< $file1" ;# my $fh2 = new IO::File "< $file2" ;# my $fh3 = new IO::File "< $file3" ;## title "$send";# my ($copy);# eval "\$copy = $send";# my $Answer ;# ok &$Func($copy, \$Answer), " $Name ok";## my $got = anyUncompress(\$Answer);# is $got, $get, " got expected output" ;# ok ! $$Error, " no error"# or diag "Error is $$Error";## } title "Array Input Error tests" ; my @data = ( [ '[]', "empty array reference"], [ '[[]]', "unknown input parameter"], [ '[[[]]]', "unknown input parameter"], [ '[[\"ab"], [\"cd"]]', "unknown input parameter"], [ '[\""]', "not a filename"], [ '[\undef]', "not a filename"], [ '[\"abcd"]', "not a filename"], [ '[\&xx]', "unknown input parameter"], [ '[$fh2]', "not a filename"], ) ; foreach my $data (@data) { my ($send, $get) = @$data ; my $fh1 = new IO::File "< $file1" ; my $fh2 = new IO::File "< $file2" ; my $fh3 = new IO::File "< $file3" ; title "$send"; my($copy); eval "\$copy = $send"; my $Answer ; my $a ; eval { $a = &$Func($copy, \$Answer) }; ok ! $a, " $Name fails"; is $$Error, $get, " got error message"; } @data = ( '[""]', '[undef]', ) ; foreach my $send (@data) { title "$send"; my($copy); eval "\$copy = $send"; my $Answer ; eval { &$Func($copy, \$Answer) } ; like $@, mkErr("^$TopFuncName: input filename is undef or null string"), " got error message"; } }}# TODO add more error cases1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -