sumcol 0.2.1

A command-line tool to sum a column of numbers.
Documentation
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
use assert_cmd::Command;
use predicates::prelude::*;
use std::io::Write;

type TestResult = Result<(), Box<dyn std::error::Error>>;

#[test]
fn help_works() -> TestResult {
    let mut cmd = Command::cargo_bin("sumcol")?;
    cmd.arg("-h")
        .assert()
        .success()
        .stdout(predicate::str::contains("Usage: sumcol"));
    Ok(())
}

#[test]
fn no_args_sum() -> TestResult {
    let mut cmd = Command::cargo_bin("sumcol")?;
    let input = r"
    1
    2
    3
    ";
    cmd.write_stdin(input)
        .assert()
        .success()
        .stdout(predicate::str::contains("6"));
    Ok(())
}

#[test]
fn simple_column_sum() -> TestResult {
    let mut cmd = Command::cargo_bin("sumcol")?;
    let input = r"
    hello 2 foo
    hello 2 foo
    hello 2 foo
    ";
    cmd.write_stdin(input)
        .args(["-f2"])
        // .env("RUST_LOG", "debug")
        .assert()
        .success()
        .stdout(predicate::str::contains("6"));
    Ok(())
}

#[test]
fn sum_empty_input() -> TestResult {
    let mut cmd = Command::cargo_bin("sumcol")?;
    cmd.write_stdin("")
        .assert()
        .success()
        .stdout(predicate::str::contains("0"));
    Ok(())
}

#[test]
fn sum_field_zero() -> TestResult {
    let mut cmd = Command::cargo_bin("sumcol")?;
    let input = "1\n2\n3\n";
    cmd.write_stdin(input)
        .args(["-f=0"])
        .assert()
        .success()
        .stdout(predicate::str::contains("6"));
    Ok(())
}

#[test]
fn sum_field_out_of_range() -> TestResult {
    let mut cmd = Command::cargo_bin("sumcol")?;
    let input = "1 2 3\n4 5 6\n";
    cmd.write_stdin(input)
        .args(["-f=99"])
        .assert()
        .success()
        .stdout(predicate::str::contains("0"))
        .stderr(predicate::str::contains(
            "Field index out of range, skipping",
        ));
    Ok(())
}

#[test]
fn sum_negative_integers() -> TestResult {
    let mut cmd = Command::cargo_bin("sumcol")?;
    cmd.write_stdin("3\n-1\n-2\n")
        .assert()
        .success()
        .stdout(predicate::str::contains("0"));
    Ok(())
}

#[test]
fn sum_negative_floats() -> TestResult {
    let mut cmd = Command::cargo_bin("sumcol")?;
    cmd.write_stdin("1.5\n-0.5\n-0.5\n")
        .assert()
        .success()
        .stdout(predicate::str::contains("0.5"));
    Ok(())
}

#[test]
fn sum_negative_mixed() -> TestResult {
    let mut cmd = Command::cargo_bin("sumcol")?;
    cmd.write_stdin("3\n-1.5\n")
        .assert()
        .success()
        .stdout(predicate::str::contains("1.5"));
    Ok(())
}

#[test]
fn sum_comma_numbers() -> TestResult {
    let mut cmd = Command::cargo_bin("sumcol")?;
    cmd.write_stdin("1,000\n2,000\n3,000\n")
        .assert()
        .success()
        .stdout(predicate::str::contains("6000"))
        .stderr(predicate::str::contains("Stripped commas from value"));
    Ok(())
}

#[test]
fn sum_invalid_0x_prefix() -> TestResult {
    let mut cmd = Command::cargo_bin("sumcol")?;
    cmd.write_stdin("0xGG\n")
        .assert()
        .success()
        .stdout(predicate::str::contains("0"))
        .stderr(predicate::str::contains(
            "Failed to parse as hex, treating as 0",
        ));
    Ok(())
}

#[test]
fn sum_field_out_of_range_no_double_warn() -> TestResult {
    let mut cmd = Command::cargo_bin("sumcol")?;
    cmd.write_stdin("1 2 3\n")
        .args(["-f=99"])
        .assert()
        .success()
        .stderr(predicate::str::contains(
            "Field index out of range, skipping",
        ))
        .stderr(
            predicate::str::contains("Failed to parse (use --radix=hex if hex), treating as 0")
                .not(),
        );
    Ok(())
}

#[test]
fn sum_large_integer_warns() -> TestResult {
    let mut cmd = Command::cargo_bin("sumcol")?;
    // i128::MAX is 170141183460469231731687303715884105727, so one digit more overflows
    cmd.write_stdin("999999999999999999999999999999999999999999\n")
        .assert()
        .success()
        .stderr(predicate::str::contains(
            "Value too large for integer, using float (may lose precision)",
        ));
    Ok(())
}

#[test]
fn sum_radix_decimal_rejects_0x_prefix() -> TestResult {
    // With --radix=decimal, a 0x-prefixed value must not be auto-detected as hex.
    let mut cmd = Command::cargo_bin("sumcol")?;
    cmd.write_stdin("5\n0xFF\n")
        .args(["--radix=decimal"])
        .assert()
        .success()
        .stdout(predicate::str::contains("5"))
        .stderr(predicate::str::contains("Failed to parse"));
    Ok(())
}

#[test]
fn sum_hex_flag_no_float_fallback() -> TestResult {
    // With -x, values that look like floats must not silently fall back to float parsing.
    let mut cmd = Command::cargo_bin("sumcol")?;
    cmd.write_stdin("1.5\n2.5\n")
        .args(["--radix=hex"])
        .assert()
        .success()
        .stdout(predicate::str::contains("0"))
        .stderr(predicate::str::contains(
            "Failed to parse as hex, treating as 0",
        ));
    Ok(())
}

#[test]
fn sum_implicit_hex() -> TestResult {
    let mut cmd = Command::cargo_bin("sumcol")?;
    let input = r"
    hello 2 foo
    hello 0xa foo
    hello 0xB foo
    ";
    cmd.write_stdin(input)
        .args(["-f2"])
        .assert()
        .success()
        .stdout(predicate::str::contains("23"));
    Ok(())
}

#[test]
fn sum_explicit_hex() -> TestResult {
    let mut cmd = Command::cargo_bin("sumcol")?;
    let input = r"
    hello 2 foo
    hello a foo
    hello 0xa foo
    hello 0xB foo
    ";
    cmd.write_stdin(input)
        .args(["-f2", "--radix=hex"])
        .assert()
        .success()
        .stdout(predicate::str::contains("0x21")); // 33 in decimal
    Ok(())
}

#[test]
fn sum_delimiter() -> TestResult {
    let mut cmd = Command::cargo_bin("sumcol")?;
    let input = r"
    hello:2:foo
    hello:2:foo
    hello:2:foo
    ";
    cmd.write_stdin(input)
        .args(["-f=2", "-d:"])
        .assert()
        .success()
        .stdout(predicate::str::contains("6")); // 33 in decimal
    Ok(())
}

#[test]
fn sum_first_delimiter() -> TestResult {
    let mut cmd = Command::cargo_bin("sumcol")?;
    let input = r"
    :2:foo
    :2:foo
    :2:foo
    ";
    cmd.write_stdin(input)
        .args(["-f=2", "-d:"])
        .assert()
        .success()
        .stdout(predicate::str::contains("6")); // 33 in decimal
    Ok(())
}

#[test]
fn sum_mixed_column() -> TestResult {
    let mut cmd = Command::cargo_bin("sumcol")?;
    let input = r"
    hello 2 foo
    hello OOPS foo
    hello 2 foo
    ";
    cmd.write_stdin(input)
        .args(["-f=2"])
        .assert()
        .success()
        .stdout(predicate::str::contains("4"))
        .stderr(predicate::str::contains(
            "Failed to parse (use --radix=hex if hex), treating as 0",
        ));
    Ok(())
}

#[test]
fn sum_mixed_column_looks_like_number() -> TestResult {
    let mut cmd = Command::cargo_bin("sumcol")?;
    let input = r"
    hello 2 foo
    hello a foo
    hello 2 foo
    ";
    cmd.write_stdin(input)
        .args(["-f=2"])
        .assert()
        .success()
        .stdout(predicate::str::contains("4"))
        .stderr(predicate::str::contains(
            "Failed to parse (use --radix=hex if hex), treating as 0",
        ));
    Ok(())
}

#[test]
fn sum_float() -> TestResult {
    let mut cmd = Command::cargo_bin("sumcol")?;
    let input = r"
    hello 2 blah
    hello 1.0 foo
    hello 2.2 oo
    blah 3e0 mumble
    ";
    cmd.write_stdin(input)
        .args(["-f=2"])
        .assert()
        .success()
        .stdout(predicate::str::contains("8.2"));

    // With -x, 3e0 will be parsed as 0x3e0 (decimal 992)
    let mut cmd = Command::cargo_bin("sumcol")?;
    cmd.write_stdin(input)
        .args(["-f=2", "--radix=hex"])
        .assert()
        .success()
        .stdout(predicate::str::contains("0x3E2")) // 2 + 0x3e0=992 = 994; 1.0 and 2.2 fail hex
        .stderr(predicate::str::contains(
            "Failed to parse as hex, treating as 0",
        ));
    Ok(())
}

#[test]
fn sum_float_0xhex() -> TestResult {
    let mut cmd = Command::cargo_bin("sumcol")?;
    let input = r"
    hello 2 blah
    hello 0xA blah
    hello 1.0 foo
    hello 2.2 oo
    ";
    cmd.write_stdin(input)
        .args(["-f=2"])
        .assert()
        .success()
        .stdout(predicate::str::contains("15.2"));
    Ok(())
}

#[test]
fn sum_float_hex_flag() -> TestResult {
    let mut cmd = Command::cargo_bin("sumcol")?;
    let input = r"
    hello 2 blah
    hello A blah
    hello 1.0 foo
    hello 2.2 oo
    ";
    // Without the -x flag, the A in the second line will be ignored.
    cmd.write_stdin(input)
        .args(["-f=2"])
        .assert()
        .success()
        .stdout(predicate::str::contains("5.2"));

    // With -x, A=10 and 2 are parsed as hex; 1.0 and 2.2 fail hex parsing and warn.
    let mut cmd = Command::cargo_bin("sumcol")?;
    cmd.write_stdin(input)
        .args(["-f=2", "--radix=hex"])
        .assert()
        .success()
        .stdout(predicate::str::contains("0xC"))
        .stderr(predicate::str::contains(
            "Failed to parse as hex, treating as 0",
        ));
    Ok(())
}

#[test]
fn sum_nonexistent_file() -> TestResult {
    let mut cmd = Command::cargo_bin("sumcol")?;
    cmd.arg("/nonexistent/path/to/file.txt").assert().failure();
    Ok(())
}

#[test]
fn sum_one_file() -> TestResult {
    let mut file = tempfile::NamedTempFile::new()?;
    writeln!(file, "1\n2\n3")?;
    let mut cmd = Command::cargo_bin("sumcol")?;
    cmd.arg(file.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("6"));
    Ok(())
}

#[test]
fn sum_two_files() -> TestResult {
    let mut file1 = tempfile::NamedTempFile::new()?;
    writeln!(file1, "1\n2\n3")?;
    let mut file2 = tempfile::NamedTempFile::new()?;
    writeln!(file2, "4\n5\n6")?;
    let mut cmd = Command::cargo_bin("sumcol")?;
    cmd.args([file1.path(), file2.path()])
        .assert()
        .success()
        .stdout(predicate::str::contains("21"));
    Ok(())
}

#[test]
fn sum_verbose_flag() -> TestResult {
    let mut cmd = Command::cargo_bin("sumcol")?;
    let input = r"
    hello 2 blah
    hello OOPS blah
    hello 1.0 foo
    hello 2.2 oo
    ";

    // Without the -x flag, the A in the second line will be ignored.
    cmd.write_stdin(input)
        .args(["-f=2", "-v"])
        .assert()
        .success()
        .stdout(predicate::str::contains(
            r#"n=Integer(2) sum=Integer(2) radix=Decimal raw_str="2""#,
        ))
        .stdout(predicate::str::contains(
            r#"n=Integer(0) sum=Integer(2) radix=Decimal raw_str="OOPS" err="Failed to parse (use --radix=hex if hex), treating as 0""#,
        ))
        .stdout(predicate::str::contains(
            r#"n=Float(2.2) sum=Float(5.2) radix=Decimal raw_str="2.2""#
        ))
        .stdout(predicate::str::contains(
            r#"=="#))
        .stdout(predicate::str::contains(
            r#"5.2"#
        ));
    Ok(())
}