]> git.refcnt.org Git - colorize.git/blob - t/fail.t
Enhance message if attribute is invalid
[colorize.git] / t / fail.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use lib qw(lib);
6 use constant true => 1;
7 use constant false => 0;
8
9 use Colorize::Common qw(:defaults $write_to_tmpfile);
10 use File::Temp qw(tempdir tmpnam);
11 use IPC::Open3 qw(open3);
12 use Symbol qw(gensym);
13 use Test::More;
14
15 my $tests = 25;
16
17 my $run_program_fail = sub
18 {
19 my ($program, $args, $message) = @_;
20
21 my @args = split /\s+/, $args;
22
23 my $err = gensym;
24
25 my $pid = open3(gensym, gensym, $err, $program, @args);
26 waitpid($pid, 0);
27
28 my $output = do { local $/; <$err> };
29
30 return ($? >> 8 == 1 && $output =~ /$message/) ? true : false;
31 };
32
33 plan tests => $tests;
34
35 SKIP: {
36 my $program = tmpnam();
37 skip 'compiling failed (failure exit)', $tests unless system("$compiler -DTEST -o $program $source") == 0;
38
39 my $file = $write_to_tmpfile->('abc');
40 my $dir = tempdir(CLEANUP => true);
41
42 my @set = (
43 [ '--attr=:', 'must be provided a string' ],
44 [ '--attr=bold:underscore', 'must have strings separated by ,' ],
45 [ '--attr=b0ld', 'attribute \'b0ld\' is not valid' ],
46 [ '--attr=b0ld,underscore', 'attribute \'b0ld\' is not valid' ], # NULify comma
47 [ '--attr=bold,bold', 'has attribute \'bold\' twice or more' ],
48 [ '--exclude-random=random', 'must be provided a plain color' ],
49 [ '--clean --clean-all', 'mutually exclusive' ],
50 [ '--clean file1 file2', 'more than one file' ],
51 [ '--clean-all file1 file2', 'more than one file' ],
52 [ '- file', 'hyphen cannot be used as color string' ],
53 [ '-', 'hyphen must be preceded by color string' ],
54 [ "$file file", 'cannot be used as color string' ],
55 [ "$file", 'must be preceded by color string' ],
56 [ "$dir", 'is not a valid file type' ],
57 [ '/black', 'foreground color missing' ],
58 [ 'white/', 'background color missing' ],
59 [ 'white/black/yellow', 'one color pair allowed only' ],
60 [ 'y3llow', 'cannot be made of non-alphabetic characters' ],
61 [ 'yEllow', 'cannot be in mixed lower/upper case' ],
62 [ 'None', 'cannot be bold' ],
63 [ 'white/Black', 'cannot be bold' ],
64 [ 'random/none', 'cannot be combined with' ],
65 [ 'random/default', 'cannot be combined with' ],
66 [ 'none/random', 'cannot be combined with' ],
67 [ 'default/random', 'cannot be combined with' ],
68 );
69 foreach my $set (@set) {
70 ok($run_program_fail->($program, $set->[0], $set->[1]), $set->[1]);
71 }
72
73 unlink $program;
74 }