diff options
author | Steven Schubiger <stsc@refcnt.org> | 2019-08-18 21:15:24 +0200 |
---|---|---|
committer | Steven Schubiger <stsc@refcnt.org> | 2019-08-18 21:15:24 +0200 |
commit | c8123fe6adb0fc2095131a09df1bb5a82dd3c2a3 (patch) | |
tree | f722ee7b14443ac344d4bb16dabe613a41832201 /t/conf/parse | |
parent | 25afef6a972cc5761894da143924856eb19cb749 (diff) | |
download | colorize-c8123fe6adb0fc2095131a09df1bb5a82dd3c2a3.tar.gz colorize-c8123fe6adb0fc2095131a09df1bb5a82dd3c2a3.tar.bz2 |
Add new test files
Diffstat (limited to 't/conf/parse')
-rwxr-xr-x | t/conf/parse/fail.t | 63 | ||||
-rwxr-xr-x | t/conf/parse/success.t | 74 |
2 files changed, 137 insertions, 0 deletions
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; +} |