From c8123fe6adb0fc2095131a09df1bb5a82dd3c2a3 Mon Sep 17 00:00:00 2001 From: Steven Schubiger Date: Sun, 18 Aug 2019 21:15:24 +0200 Subject: Add new test files --- t/conf/attr_clear.t | 31 +++++++++++++++++++++ t/conf/color.t | 33 ++++++++++++++++++++++ t/conf/fail.t | 59 ++++++++++++++++++++++++++++++++++++++++ t/conf/parse/fail.t | 63 ++++++++++++++++++++++++++++++++++++++++++ t/conf/parse/success.t | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++ t/conf/use.t | 57 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 317 insertions(+) create mode 100755 t/conf/attr_clear.t create mode 100755 t/conf/color.t create mode 100755 t/conf/fail.t create mode 100755 t/conf/parse/fail.t create mode 100755 t/conf/parse/success.t create mode 100755 t/conf/use.t diff --git a/t/conf/attr_clear.t b/t/conf/attr_clear.t new file mode 100755 index 0000000..7bbe5f9 --- /dev/null +++ b/t/conf/attr_clear.t @@ -0,0 +1,31 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use lib qw(lib); + +use Colorize::Common qw(:defaults $write_to_tmpfile); +use File::Temp qw(tmpnam); +use Test::More; + +my $tests = 1; + +plan tests => $tests; + +SKIP: { + my $program = tmpnam(); + my $conf_file = tmpnam(); + + skip 'compiling failed (attr clear)', $tests unless system(qq($compiler -DTEST -DCONF_FILE_TEST=\"$conf_file\" -o $program $source)) == 0; + + my $infile = $write_to_tmpfile->('foo'); + + open(my $fh, '>', $conf_file) or die "Cannot open `$conf_file' for writing: $!\n"; + print {$fh} "attr=blink\n"; + close($fh); + + is(qx($program default --attr=bold $infile), "\e[1;39mfoo\e[0m", 'discard conf attr string'); + + unlink $program; + unlink $conf_file; +} diff --git a/t/conf/color.t b/t/conf/color.t new file mode 100755 index 0000000..3adfd5b --- /dev/null +++ b/t/conf/color.t @@ -0,0 +1,33 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use lib qw(lib); + +use Colorize::Common qw(:defaults $write_to_tmpfile); +use File::Temp qw(tmpnam); +use Test::More; + +my $tests = 3; + +plan tests => $tests; + +SKIP: { + my $program = tmpnam(); + my $conf_file = tmpnam(); + + skip 'compiling failed (color)', $tests unless system(qq($compiler -DTEST -DCONF_FILE_TEST=\"$conf_file\" -o $program $source)) == 0; + + my $infile = $write_to_tmpfile->('foo'); + + open(my $fh, '>', $conf_file) or die "Cannot open `$conf_file' for writing: $!\n"; + print {$fh} "color=green\n"; + close($fh); + + is(qx($program $infile), "\e[32mfoo\e[0m", 'color from config'); + is(qx(printf %s "foo" | $program none/none), 'foo', 'read from stdin'); + is(qx($program none/none $infile), 'foo', 'read from file'); + + unlink $program; + unlink $conf_file; +} diff --git a/t/conf/fail.t b/t/conf/fail.t new file mode 100755 index 0000000..d3b8852 --- /dev/null +++ b/t/conf/fail.t @@ -0,0 +1,59 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use lib qw(lib); +use constant true => 1; +use constant false => 0; + +use Colorize::Common qw(:defaults $write_to_tmpfile); +use File::Temp qw(tmpnam); +use IPC::Open3 qw(open3); +use Symbol qw(gensym); +use Test::More; + +my $tests = 7; + +my $run_program_fail = sub +{ + my ($program, $message, $infile) = @_; + + my $err = gensym; + + my $pid = open3(gensym, gensym, $err, $program, qw(default), $infile); + waitpid($pid, 0); + + my $output = do { local $/; <$err> }; + + return ($? >> 8 == 1 && $output =~ /$message/) ? true : false; +}; + +plan tests => $tests; + +SKIP: { + my $program = tmpnam(); + my $conf_file = tmpnam(); + + skip 'compiling failed (config failure exit)', $tests unless system(qq($compiler -DTEST -DCONF_FILE_TEST=\"$conf_file\" -o $program $source)) == 0; + + my $infile = $write_to_tmpfile->(''); + + my @set = ( + [ 'attr=:', 'attr conf option must be provided a string' ], + [ 'attr=bold:underscore', 'attr conf option must have strings separated by ,' ], + [ 'attr=b0ld', 'attr conf option attribute \'b0ld\' is not valid' ], + [ 'attr=b0ld,underscore', 'attr conf option attribute \'b0ld\' is not valid' ], # handle comma + [ 'attr=bold,bold', 'attr conf option has attribute \'bold\' twice or more' ], + [ 'exclude-random=random', 'exclude-random conf option must be provided a plain color' ], + [ 'omit-color-empty=unsure', 'omit-color-empty conf option is not valid' ], + ); + foreach my $set (@set) { + open(my $fh, '>', $conf_file) or die "Cannot open `$conf_file' for writing: $!\n"; + print {$fh} $set->[0], "\n"; + close($fh); + ok($run_program_fail->($program, $set->[1], $infile), $set->[1]); + } + + unlink $program; + unlink $conf_file; +} diff --git a/t/conf/parse/fail.t b/t/conf/parse/fail.t new file mode 100755 index 0000000..673153b --- /dev/null +++ b/t/conf/parse/fail.t @@ -0,0 +1,63 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use lib qw(lib); +use constant true => 1; +use constant false => 0; + +use Colorize::Common qw(:defaults $write_to_tmpfile); +use File::Temp qw(tmpnam); +use IPC::Open3 qw(open3); +use Symbol qw(gensym); +use Test::More; + +my $tests = 8; + +my $run_program_fail = sub +{ + my ($program, $message, $infile) = @_; + + my $err = gensym; + + my $pid = open3(gensym, gensym, $err, $program, qw(default), $infile); + waitpid($pid, 0); + + my $output = do { local $/; <$err> }; + + return ($? >> 8 == 1 && $output =~ /$message/) ? true : false; +}; + +plan tests => $tests; + +SKIP: { + my $program = tmpnam(); + my $conf_file = tmpnam(); + + skip 'compiling failed (config parse failure)', $tests unless system(qq($compiler -DTEST -DCONF_FILE_TEST=\"$conf_file\" -o $program $source)) == 0; + + my $infile = $write_to_tmpfile->(''); + + my $chars_exceed = 'x' x 256; + + my @set = ( + [ '[attr=bold', 'option \'\[attr\' cannot be made of non-option characters' ], + [ 'attr1=bold', 'option \'attr1\' not recognized' ], + [ 'color1=magenta', 'option \'color1\' not recognized' ], + [ 'exclude-random1=black', 'option \'exclude-random1\' not recognized' ], + [ 'omit-color-empty1=yes', 'option \'omit-color-empty1\' not recognized' ], + [ 'attr', 'option \'attr\' not followed by =' ], + [ 'attr bold', 'option \'attr\' not followed by =' ], + [ "color=$chars_exceed", 'line exceeds maximum of' ], + ); + + foreach my $set (@set) { + open(my $fh, '>', $conf_file) or die "Cannot open `$conf_file' for writing: $!\n"; + print {$fh} $set->[0], "\n"; + close($fh); + ok($run_program_fail->($program, $set->[1], $infile), $set->[1]); + } + + unlink $program; + unlink $conf_file; +} diff --git a/t/conf/parse/success.t b/t/conf/parse/success.t new file mode 100755 index 0000000..fe2d46d --- /dev/null +++ b/t/conf/parse/success.t @@ -0,0 +1,74 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use lib qw(lib); +use constant true => 1; +use constant false => 0; + +use Colorize::Common qw(:defaults $write_to_tmpfile); +use File::Temp qw(tmpnam); +use IPC::Open3 qw(open3); +use Symbol qw(gensym); +use Test::More; + +my $tests = 21; + +my $conf = <<'EOT'; +# comment + # comment + # comment +attr=bold +attr =bold +attr= bold +attr =bold +attr= bold +attr = bold + color=green +color=green + color=green +color=green +exclude-random=black +omit-color-empty=yes +attr=bold # comment +attr=bold # comment +attr= +color= +exclude-random= +omit-color-empty= +EOT + +my $run_program_succeed = sub +{ + my ($program, $infile) = @_; + + my $err = gensym; + + my $pid = open3(gensym, gensym, $err, $program, qw(default), $infile); + waitpid($pid, 0); + + my $output = do { local $/; <$err> }; + + return ($? >> 8 == 0 && $output eq '') ? true : false; +}; + +plan tests => $tests; + +SKIP: { + my $program = tmpnam(); + my $conf_file = tmpnam(); + + skip 'compiling failed (config parse success)', $tests unless system(qq($compiler -DTEST -DCONF_FILE_TEST=\"$conf_file\" -o $program $source)) == 0; + + my $infile = $write_to_tmpfile->(''); + + foreach my $line (split /\n/, $conf) { + open(my $fh, '>', $conf_file) or die "Cannot open `$conf_file' for writing: $!\n"; + print {$fh} $line, "\n"; + close($fh); + ok($run_program_succeed->($program, $infile), $line); + } + + unlink $program; + unlink $conf_file; +} diff --git a/t/conf/use.t b/t/conf/use.t new file mode 100755 index 0000000..0a695df --- /dev/null +++ b/t/conf/use.t @@ -0,0 +1,57 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use lib qw(lib); + +use Colorize::Common qw(:defaults $write_to_tmpfile); +use File::Temp qw(tmpnam); +use Test::More; + +my $tests = 21; + +my $conf = <<'EOT'; +attr=underscore +color=yellow # tested also in color.t +omit-color-empty=yes +EOT + +plan tests => $tests; + +SKIP: { + my $program = tmpnam(); + my $conf_file = tmpnam(); + + skip 'compiling failed (use config)', $tests unless system(qq($compiler -DTEST -DCONF_FILE_TEST=\"$conf_file\" -o $program $source)) == 0; + + my $infile1 = $write_to_tmpfile->(<<'EOT'); +foo + +bar + +baz +EOT + open(my $fh, '>', $conf_file) or die "Cannot open `$conf_file' for writing: $!\n"; + print {$fh} $conf; + close($fh); + + is(qx($program $infile1), <<"EOT", 'use config'); +\e[4;33mfoo\e[0m + +\e[4;33mbar\e[0m + +\e[4;33mbaz\e[0m +EOT + my $infile2 = $write_to_tmpfile->('foo'); + + open($fh, '>', $conf_file) or die "Cannot open `$conf_file' for writing: $!\n"; + print {$fh} "exclude-random=black\n"; + close($fh); + + for (my $i = 1; $i <= 20; $i++) { + like(qx($program random $infile2), qr/^\e\[3[1-7]mfoo\e\[0m$/, 'use exclude-random'); + } + + unlink $program; + unlink $conf_file; +} -- cgit v1.2.3