Debian Codegraph @

tests/datamash-check-tabular.pl

raw
1 #!/usr/bin/env perl
2 =pod
3 Unit Tests for GNU Datamash - perform simple calculation on input data
4 Tests for 'transpose' and 'reverse' operation modes.
5
6
7 Copyright (C) 2013-2021 Assaf Gordon <[email protected]>
8 Copyright (C) 2022-2025 Timothy Rice <[email protected]>
9
10 This file is part of GNU Datamash.
11
12 GNU Datamash is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
16
17 GNU Datamash is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with GNU Datamash. If not, see <https://www.gnu.org/licenses/>.
24
25 Written by Assaf Gordon.
26 =cut
27 use strict;
28 use warnings;
29 use List::Util qw/max/;
30 use Data::Dumper;
31
32 # Until a better way comes along to auto-use Coreutils Perl modules
33 # as in the coreutils' autotools system.
34 use Coreutils;
35 use CuSkip;
36 use CuTmpdir qw(datamash);
37
38 (my $program_name = $0) =~ s|.*/||;
39 my $prog_bin = 'datamash';
40
41 my $prog = `$prog_bin ---print-progname`;
42
43 # Turn off localization of executable's output.
44 @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
45
46 my $in1=<<'EOF';
47 a x 1
48 b z 6
49 c x 7
50 EOF
51
52 # missing field on second line
53 my $in2=<<'EOF';
54 a x 1
55 b z
56 c x 7
57 EOF
58
59 # Same as in2, with whitespace delimiters
60 my $in2_ws=<<"EOF";
61 a x \t 1
62 b \t z
63 c x 7
64 EOF
65
66 # second line has 2 tab characters, thus 3 fields
67 # (the last field is empty).
68 # version 1.1.0 and before rejected such input.
69 my $in3=<<"EOF";
70 a x 1
71 b z\t
72 c x 7
73 EOF
74
75 # Same as in3, with whitespace delimiters
76 my $in3_ws=<<"EOF";
77 a x \t 1
78 b \t z \t
79 c\t\t\tx \t 7
80 EOF
81
82
83 # one line
84 my $in4=<<'EOF';
85 a x 1
86 EOF
87
88 # one field
89 my $in5=<<'EOF';
90 a
91 b
92 c
93 d
94 EOF
95
96 # one field, with bad input (fourth line has 0 fields)
97 my $in6=<<'EOF';
98 a
99 b
100 c
101
102 e
103 EOF
104
105 my @Tests =
106 (
107 ['c1', 'check', {IN_PIPE=>$in1}, {OUT=>"3 lines, 3 fields\n"}],
108
109 ['c2', 'check', {IN_PIPE=>$in4}, {OUT=>"1 line, 3 fields\n"}],
110 ['c3', 'check', {IN_PIPE=>$in5}, {OUT=>"4 lines, 1 field\n"}],
111 ['c4', 'check', {IN_PIPE=>$in3}, {OUT=>"3 lines, 3 fields\n"}],
112 ['c5', '-W check', {IN_PIPE=>$in3_ws}, {OUT=>"3 lines, 3 fields\n"}],
113
114 # Check bad input:
115 # The first four lines will be something like:
116 # 'line X has N fields:'
117 # ' [content of line X]'
118 # 'line Y has M fields:'
119 # ' [content of line Y]'
120 # The ERR_SUBSTR will remove these messages, as they are highly variable
121 # and dependant on the input. Then only the last line of error message
122 # is checked.
123 ['e1', 'check', {IN_PIPE=>$in2}, {EXIT=>1},
124 {ERR_SUBST => 's/^(li| ).*$//'},
125 {ERR => "\n\n\n\n$prog: check failed: line 2 has 2 fields " .
126 "(previous line had 3)\n"}],
127 ['e1ws', '-W check', {IN_PIPE=>$in2_ws}, {EXIT=>1},
128 {ERR_SUBST => 's/^(li| ).*$//'},
129 {ERR => "\n\n\n\n$prog: check failed: line 2 has 2 fields " .
130 "(previous line had 3)\n"}],
131
132 ['e2', 'check', {IN_PIPE=>$in6}, {EXIT=>1},
133 {ERR_SUBST => 's/^(li| ).*$//'},
134 {ERR => "\n\n\n\n$prog: check failed: line 4 has 0 fields " .
135 "(previous line had 1)\n"}],
136 ['e2ws', '-W check', {IN_PIPE=>$in6}, {EXIT=>1},
137 {ERR_SUBST => 's/^(li| ).*$//'},
138 {ERR => "\n\n\n\n$prog: check failed: line 4 has 0 fields " .
139 "(previous line had 1)\n"}],
140 );
141
142 my $save_temps = $ENV{SAVE_TEMPS};
143 my $verbose = $ENV{VERBOSE};
144
145 my $fail = run_tests ($program_name, $prog_bin, \@Tests, $save_temps, $verbose);
146 exit $fail;