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