summaryrefslogtreecommitdiffstats
path: root/tests/by-util/test_tac.rs
blob: fa5f67f133f0e725dca229407e6a34565e578dda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// This file is part of the uutils coreutils package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
// spell-checker:ignore axxbxx bxxaxx axxx axxxx xxaxx xxax xxxxa axyz zyax zyxa
use crate::common::util::TestScenario;

#[test]
fn test_invalid_arg() {
    new_ucmd!().arg("--definitely-invalid").fails().code_is(1);
}

#[test]
// FIXME: See https://github.com/uutils/coreutils/issues/4204
#[cfg(not(windows))]
fn test_stdin_default() {
    new_ucmd!()
        .pipe_in("100\n200\n300\n400\n500")
        .run()
        .stdout_is("500400\n300\n200\n100\n");
}

#[test]
// FIXME: See https://github.com/uutils/coreutils/issues/4204
#[cfg(not(windows))]
fn test_stdin_non_newline_separator() {
    new_ucmd!()
        .args(&["-s", ":"])
        .pipe_in("100:200:300:400:500")
        .run()
        .stdout_is("500400:300:200:100:");
}

#[test]
// FIXME: See https://github.com/uutils/coreutils/issues/4204
#[cfg(not(windows))]
fn test_stdin_non_newline_separator_before() {
    new_ucmd!()
        .args(&["-b", "-s", ":"])
        .pipe_in("100:200:300:400:500")
        .run()
        .stdout_is(":500:400:300:200100");
}

#[test]
fn test_single_default() {
    new_ucmd!()
        .arg("prime_per_line.txt")
        .run()
        .stdout_is_fixture("prime_per_line.expected");
}

#[test]
fn test_single_non_newline_separator() {
    new_ucmd!()
        .args(&["-s", ":", "delimited_primes.txt"])
        .run()
        .stdout_is_fixture("delimited_primes.expected");
}

#[test]
fn test_single_non_newline_separator_before() {
    new_ucmd!()
        .args(&["-b", "-s", ":", "delimited_primes.txt"])
        .run()
        .stdout_is_fixture("delimited_primes_before.expected");
}

#[test]
fn test_invalid_input() {
    let scene = TestScenario::new(util_name!());
    let at = &scene.fixtures;

    scene
        .ucmd()
        .arg("b")
        .fails()
        .stderr_contains("failed to open 'b' for reading: No such file or directory");

    at.mkdir("a");
    scene
        .ucmd()
        .arg("a")
        .fails()
        .stderr_contains("a: read error: Invalid argument");
}

#[test]
#[cfg(not(windows))] // FIXME: https://github.com/uutils/coreutils/issues/4204
fn test_no_line_separators() {
    new_ucmd!().pipe_in("a").succeeds().stdout_is("a");
}

#[test]
// FIXME: See https://github.com/uutils/coreutils/issues/4204
#[cfg(not(windows))]
fn test_before_trailing_separator_no_leading_separator() {
    new_ucmd!()
        .arg("-b")
        .pipe_in("a\nb\n")
        .succeeds()
        .stdout_is("\n\nba");
}

#[test]
// FIXME: See https://github.com/uutils/coreutils/issues/4204
#[cfg(not(windows))]
fn test_before_trailing_separator_and_leading_separator() {
    new_ucmd!()
        .arg("-b")
        .pipe_in("\na\nb\n")
        .succeeds()
        .stdout_is("\n\nb\na");
}

#[test]
// FIXME: See https://github.com/uutils/coreutils/issues/4204
#[cfg(not(windows))]
fn test_before_leading_separator_no_trailing_separator() {
    new_ucmd!()
        .arg("-b")
        .pipe_in("\na\nb")
        .succeeds()
        .stdout_is("\nb\na");
}

#[test]
// FIXME: See https://github.com/uutils/coreutils/issues/4204
#[cfg(not(windows))]
fn test_before_no_separator() {
    new_ucmd!()
        .arg("-b")
        .pipe_in("ab")
        .succeeds()
        .stdout_is("ab");
}

#[test]
// FIXME: See https://github.com/uutils/coreutils/issues/4204
#[cfg(not(windows))]
fn test_before_empty_file() {
    new_ucmd!().arg("-b").pipe_in("").succeeds().stdout_is("");
}

#[test]
// FIXME: See https://github.com/uutils/coreutils/issues/4204
#[cfg(not(windows))]
fn test_multi_char_separator() {
    new_ucmd!()
        .args(&["-s", "xx"])
        .pipe_in("axxbxx")
        .succeeds()
        .stdout_is("bxxaxx");
}

#[test]
// FIXME: See https://github.com/uutils/coreutils/issues/4204
#[cfg(not(windows))]
fn test_multi_char_separator_overlap() {
    // The right-most pair of "x" characters in the input is treated as
    // the only line separator. That is, "axxx" is interpreted as having
    // one line comprising the string "ax" followed by the line
    // separator "xx".
    new_ucmd!()
        .args(&["-s", "xx"])
        .pipe_in("axxx")
        .succeeds()
        .stdout_is("axxx");

    // Each non-overlapping pair of "x" characters in the input is
    // treated as a line separator. That is, "axxxx" is interpreted as
    // having two lines:
    //
    // * the second line is the empty string "" followed by the line
    //   separator "xx",
    // * the first line is the string "a" followed by the line separator
    //   "xx".
    //
    // The lines are printed in reverse, resulting in "xx" followed by
    // "axx".
    new_ucmd!()
        .args(&["-s", "xx"])
        .pipe_in("axxxx")
        .succeeds()
        .stdout_is("xxaxx");
}

#[test]
// FIXME: See https://github.com/uutils/coreutils/issues/4204
#[cfg(not(windows))]
fn test_multi_char_separator_overlap_before() {
    // With the "-b" option, the line separator is assumed to be at the
    // beginning of the line. In this case, That is, "axxx" is
    // interpreted as having two lines:
    //
    // * the second line is the empty string "" preceded by the line
    //   separator "xx",
    // * the first line is the string "ax" preceded by no line
    //   separator, since there are no more characters preceding it.
    //
    // The lines are printed in reverse, resulting in "xx" followed by
    // "ax".
    new_ucmd!()
        .args(&["-b", "-s", "xx"])
        .pipe_in("axxx")
        .succeeds()
        .stdout_is("xxax");

    // With the "-b" option, the line separator is assumed to be at the
    // beginning of the line. Each non-overlapping pair of "x"
    // characters in the input is treated as a line separator. That is,
    // "axxxx" is interpreted as having three lines:
    //
    // * the third line is the empty string "" preceded by the line
    //   separator "xx" (the last two "x" characters in the input
    //   string),
    // * the second line is the empty string "" preceded by the line
    //   separator "xx" (the first two "x" characters in the input
    //   string),
    // * the first line is the string "a" preceded by no line separator,
    //   since there are no more characters preceding it.
    //
    // The lines are printed in reverse, resulting in "xx" followed by
    // "xx" followed by "a".
    new_ucmd!()
        .args(&["-b", "-s", "xx"])
        .pipe_in("axxxx")
        .succeeds()
        .stdout_is("xxxxa");
}

#[test]
// FIXME: See https://github.com/uutils/coreutils/issues/4204
#[cfg(not(windows))]
fn test_null_separator() {
    new_ucmd!()
        .args(&["-s", ""])
        .pipe_in("a\0b\0")
        .succeeds()
        .stdout_is("b\0a\0");
}

#[test]
// FIXME: See https://github.com/uutils/coreutils/issues/4204
#[cfg(not(windows))]
fn test_regex() {
    new_ucmd!()
        .args(&["-r", "-s", "[xyz]+"])
        .pipe_in("axyz")
        .succeeds()
        .no_stderr()
        .stdout_is("zyax");

    new_ucmd!()
        .args(&["-r", "-s", ":+"])
        .pipe_in("a:b::c:::d::::")
        .succeeds()
        .no_stderr()
        .stdout_is(":::d:::c::b:a:");

    new_ucmd!()
        .args(&["-r", "-s", r"[\+]+[-]+[\+]+"])
        //   line  0     1        2
        //        |--||-----||--------|
        .pipe_in("a+-+b++--++c+d-e+---+")
        .succeeds()
        .no_stderr()
        //   line       2        1    0
        //          |--------||-----||--|
        .stdout_is("c+d-e+---+b++--++a+-+");
}

#[test]
// FIXME: See https://github.com/uutils/coreutils/issues/4204
#[cfg(not(windows))]
fn test_regex_before() {
    new_ucmd!()
        .args(&["-b", "-r", "-s", "[xyz]+"])
        .pipe_in("axyz")
        .succeeds()
        .no_stderr()
        .stdout_is("zyxa");

    new_ucmd!()
        .args(&["-b", "-r", "-s", ":+"])
        .pipe_in(":a::b:::c::::d")
        .succeeds()
        .stdout_is(":d::::c:::b::a");

    // Because `tac` searches for matches of the regular expression from
    // right to left, the second to last line is
    //
    //     +--++b
    //
    // not
    //
    //     ++--++b
    //
    new_ucmd!()
        .args(&["-b", "-r", "-s", r"[\+]+[-]+[\+]+"])
        //   line   0     1       2
        //        |---||----||--------|
        .pipe_in("+-+a++--++b+---+c+d-e")
        .succeeds()
        .no_stderr()
        //   line       2        1    0
        //          |--------||----||---|
        .stdout_is("+---+c+d-e+--++b+-+a+");
}