forked from uutils/coreutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_unexpand.rs
269 lines (236 loc) · 5.97 KB
/
test_unexpand.rs
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
// 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 contenta
use crate::common::util::TestScenario;
#[test]
fn test_invalid_arg() {
new_ucmd!().arg("--definitely-invalid").fails().code_is(1);
}
#[test]
fn unexpand_init_0() {
new_ucmd!()
.args(&["-t4"])
.pipe_in(" 1\n 2\n 3\n 4\n")
.run()
.stdout_is(" 1\n 2\n 3\n\t4\n");
}
#[test]
fn unexpand_init_1() {
new_ucmd!()
.args(&["-t4"])
.pipe_in(" 5\n 6\n 7\n 8\n")
.run()
.stdout_is("\t 5\n\t 6\n\t 7\n\t\t8\n");
}
#[test]
fn unexpand_init_list_0() {
new_ucmd!()
.args(&["-t2,4"])
.pipe_in(" 1\n 2\n 3\n 4\n")
.run()
.stdout_is(" 1\n\t2\n\t 3\n\t\t4\n");
}
#[test]
fn unexpand_init_list_1() {
// Once the list is exhausted, spaces are not converted anymore
new_ucmd!()
.args(&["-t2,4"])
.pipe_in(" 5\n 6\n 7\n 8\n")
.run()
.stdout_is("\t\t 5\n\t\t 6\n\t\t 7\n\t\t 8\n");
}
#[test]
fn unexpand_flag_a_0() {
new_ucmd!()
.args(&["--"])
.pipe_in("e E\nf F\ng G\nh H\n")
.run()
.stdout_is("e E\nf F\ng G\nh H\n");
}
#[test]
fn unexpand_flag_a_1() {
new_ucmd!()
.args(&["-a"])
.pipe_in("e E\nf F\ng G\nh H\n")
.run()
.stdout_is("e E\nf F\ng\tG\nh\t H\n");
}
#[test]
fn unexpand_flag_a_2() {
new_ucmd!()
.args(&["-t8"])
.pipe_in("e E\nf F\ng G\nh H\n")
.run()
.stdout_is("e E\nf F\ng\tG\nh\t H\n");
}
#[test]
fn unexpand_first_only_0() {
new_ucmd!()
.args(&["-t3"])
.pipe_in(" A B")
.run()
.stdout_is("\t\t A\t B");
}
#[test]
fn unexpand_first_only_1() {
new_ucmd!()
.args(&["-t3", "--first-only"])
.pipe_in(" A B")
.run()
.stdout_is("\t\t A B");
}
#[test]
fn unexpand_trailing_space_0() {
// evil
// Individual spaces before fields starting with non blanks should not be
// converted, unless they are at the beginning of the line.
new_ucmd!()
.args(&["-t4"])
.pipe_in("123 \t1\n123 1\n123 \n123 ")
.run()
.stdout_is("123\t\t1\n123 1\n123 \n123 ");
}
#[test]
fn unexpand_trailing_space_1() {
// super evil
new_ucmd!()
.args(&["-t1"])
.pipe_in(" abc d e f g ")
.run()
.stdout_is("\tabc d e\t\tf\t\tg ");
}
#[test]
fn unexpand_spaces_follow_tabs_0() {
// The two first spaces can be included into the first tab.
new_ucmd!()
.pipe_in(" \t\t A")
.run()
.stdout_is("\t\t A");
}
#[test]
fn unexpand_spaces_follow_tabs_1() {
// evil
// Explanation of what is going on here:
// 'a' -> 'a' // first tabstop (1)
// ' \t' -> '\t' // second tabstop (4)
// ' ' -> '\t' // third tabstop (5)
// ' B \t' -> ' B \t' // after the list is exhausted, nothing must change
new_ucmd!()
.args(&["-t1,4,5"])
.pipe_in("a \t B \t")
.run()
.stdout_is("a\t\t B \t");
}
#[test]
fn unexpand_spaces_after_fields() {
new_ucmd!()
.args(&["-a"])
.pipe_in(" \t A B C D A\t\n")
.run()
.stdout_is("\t\tA B C D\t\t A\t\n");
}
#[test]
fn unexpand_read_from_file() {
new_ucmd!()
.arg("with_spaces.txt")
.arg("-t4")
.run()
.success();
}
#[test]
fn unexpand_read_from_two_file() {
new_ucmd!()
.arg("with_spaces.txt")
.arg("with_spaces.txt")
.arg("-t4")
.run()
.success();
}
#[test]
fn test_tabs_shortcut() {
new_ucmd!()
.arg("-3")
.pipe_in(" a b")
.run()
.stdout_is("\ta b");
}
#[test]
fn test_tabs_shortcut_combined_with_all_arg() {
fn run_cmd(all_arg: &str) {
new_ucmd!()
.args(&[all_arg, "-3"])
.pipe_in("a b c")
.run()
.stdout_is("a\tb\tc");
}
let all_args = vec!["-a", "--all"];
for arg in all_args {
run_cmd(arg);
}
}
#[test]
fn test_comma_separated_tabs_shortcut() {
new_ucmd!()
.args(&["-a", "-3,9"])
.pipe_in("a b c")
.run()
.stdout_is("a\tb\tc");
}
#[test]
fn test_tabs_cannot_be_zero() {
new_ucmd!()
.arg("--tabs=0")
.fails()
.stderr_contains("tab size cannot be 0");
}
#[test]
fn test_tabs_must_be_ascending() {
new_ucmd!()
.arg("--tabs=1,1")
.fails()
.stderr_contains("tab sizes must be ascending");
}
#[test]
fn test_tabs_with_invalid_chars() {
new_ucmd!()
.arg("--tabs=x")
.fails()
.stderr_contains("tab size contains invalid character(s): 'x'");
new_ucmd!()
.arg("--tabs=1x2")
.fails()
.stderr_contains("tab size contains invalid character(s): 'x2'");
}
#[test]
fn test_tabs_shortcut_with_too_large_size() {
let arg = format!("-{}", u128::MAX);
let expected_error = "tab stop value is too large";
new_ucmd!().arg(arg).fails().stderr_contains(expected_error);
}
#[test]
fn test_is_directory() {
let (at, mut ucmd) = at_and_ucmd!();
let dir_name = "dir";
at.mkdir(dir_name);
ucmd.arg(dir_name)
.fails()
.stderr_contains(format!("unexpand: {dir_name}: Is a directory"));
}
#[test]
fn test_multiple_files() {
let (at, mut ucmd) = at_and_ucmd!();
at.write("file", "content");
at.write("file1", "a b");
ucmd.args(&["file", "file1"])
.succeeds()
.stdout_is("contenta b");
}
#[test]
fn test_one_nonexisting_file() {
new_ucmd!()
.arg("asdf.txt")
.fails()
.stderr_contains("asdf.txt: No such file or directory");
}