tva 0.3.3

Tab-separated Values Assistant
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
use assert_cmd::cargo::cargo_bin_cmd;
use predicates::prelude::*;
use std::path::PathBuf;

mod common;
use common::TvaCmd;

fn data_path(filename: &str) -> PathBuf {
    let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    path.push("tests/data/plot");
    path.push(filename);
    path
}

#[test]
fn test_plot_point_help() {
    let mut cmd = cargo_bin_cmd!("tva");
    cmd.args(["plot", "point", "--help"]);
    cmd.assert()
        .success()
        .stdout(predicate::str::contains(
            "Draw a scatter plot or line chart",
        ))
        .stdout(predicate::str::contains("-x, --x"))
        .stdout(predicate::str::contains("-y, --y"))
        .stdout(predicate::str::contains("--color"))
        .stdout(predicate::str::contains("-l, --line"));
}

#[test]
fn test_plot_point_basic() {
    let tva = TvaCmd::new();

    // Create a simple TSV file with numeric data
    let input = "x\ty\n1\t2\n2\t4\n3\t6\n4\t8\n";

    let (stdout, _stderr) = tva
        .args(&["plot", "point", "-x", "1", "-y", "2"])
        .stdin(input)
        .run();

    // The command should produce some output (terminal graphics)
    assert!(!stdout.is_empty());
}

#[test]
fn test_plot_point_with_column_names() {
    let tva = TvaCmd::new();

    let input = "x\ty\n1\t2\n2\t4\n3\t6\n";

    let (stdout, _stderr) = tva
        .args(&["plot", "point", "-x", "x", "-y", "y"])
        .stdin(input)
        .run();

    assert!(!stdout.is_empty());
}

#[test]
fn test_plot_point_line() {
    let tva = TvaCmd::new();

    let input = "x\ty\n1\t2\n2\t4\n3\t6\n";

    let (stdout, _stderr) = tva
        .args(&["plot", "point", "-l", "-x", "1", "-y", "2"])
        .stdin(input)
        .run();

    assert!(!stdout.is_empty());
}

#[test]
fn test_plot_point_with_color() {
    let tva = TvaCmd::new();

    let input = "x\ty\tgroup\n1\t2\tA\n2\t4\tA\n3\t6\tB\n4\t8\tB\n";

    let (stdout, _stderr) = tva
        .args(&["plot", "point", "-x", "1", "-y", "2", "--color", "3"])
        .stdin(input)
        .run();

    assert!(!stdout.is_empty());
}

#[test]
fn test_plot_point_ignore_errors() {
    let tva = TvaCmd::new();

    // Data with some non-numeric values
    let input = "x\ty\n1\t2\n2\tabc\n3\t6\n";

    let (stdout, _stderr) = tva
        .args(&["plot", "point", "-x", "1", "-y", "2", "--ignore"])
        .stdin(input)
        .run();

    assert!(!stdout.is_empty());
}

#[test]
fn test_plot_point_error_on_invalid_data() {
    let tva = TvaCmd::new();

    // Data with non-numeric values and no --ignore flag
    let input = "x\ty\n1\t2\n2\tabc\n3\t6\n";

    let (_stdout, stderr) = tva
        .args(&["plot", "point", "-x", "1", "-y", "2"])
        .stdin(input)
        .run_fail();

    assert!(stderr.contains("Cannot parse") || stderr.contains("abc"));
}

#[test]
fn test_plot_point_empty_data() {
    let tva = TvaCmd::new();

    // Empty data (only headers)
    let input = "x\ty\n";

    let (_stdout, stderr) = tva
        .args(&["plot", "point", "-x", "1", "-y", "2"])
        .stdin(input)
        .run_fail();

    assert!(stderr.contains("No valid data points"));
}

#[test]
fn test_plot_point_invalid_column() {
    let tva = TvaCmd::new();

    let input = "x\ty\n1\t2\n";

    let (_stdout, stderr) = tva
        .args(&["plot", "point", "-x", "nonexistent", "-y", "2"])
        .stdin(input)
        .run_fail();

    assert!(stderr.contains("Invalid column spec"));
}

#[test]
fn test_plot_point_zero_based_index() {
    let tva = TvaCmd::new();

    let input = "x\ty\n1\t2\n";

    let (_stdout, stderr) = tva
        .args(&["plot", "point", "-x", "0", "-y", "2"])
        .stdin(input)
        .run_fail();

    assert!(stderr.contains("field index must be >= 1"));
}

// Iris dataset tests
#[test]
fn test_plot_point_iris_basic() {
    let tva = TvaCmd::new();
    let iris_path = data_path("iris.tsv");

    let (stdout, _stderr) = tva
        .args(&[
            "plot",
            "point",
            "-x",
            "sepal_length",
            "-y",
            "sepal_width",
            iris_path.to_str().unwrap(),
        ])
        .run();

    assert!(!stdout.is_empty());
}

#[test]
fn test_plot_point_iris_with_color() {
    let tva = TvaCmd::new();
    let iris_path = data_path("iris.tsv");

    let (stdout, _stderr) = tva
        .args(&[
            "plot",
            "point",
            "-x",
            "petal_length",
            "-y",
            "petal_width",
            "--color",
            "label",
            iris_path.to_str().unwrap(),
        ])
        .run();

    assert!(!stdout.is_empty());
}

#[test]
fn test_plot_point_iris_by_index() {
    let tva = TvaCmd::new();
    let iris_path = data_path("iris.tsv");

    // Use column indices (1-based)
    let (stdout, _stderr) = tva
        .args(&[
            "plot",
            "point",
            "-x",
            "1", // sepal_length
            "-y",
            "3", // petal_length
            "--color",
            "5", // label
            iris_path.to_str().unwrap(),
        ])
        .run();

    assert!(!stdout.is_empty());
}

#[test]
fn test_plot_point_iris_with_size() {
    let tva = TvaCmd::new();
    let iris_path = data_path("iris.tsv");

    let (stdout, _stderr) = tva
        .args(&[
            "plot",
            "point",
            "-x",
            "sepal_length",
            "-y",
            "petal_length",
            "--color",
            "label",
            "--cols",
            "80",
            "--rows",
            "24",
            iris_path.to_str().unwrap(),
        ])
        .run();

    assert!(!stdout.is_empty());
}

// --path parameter tests (geom_path behavior)
#[test]
fn test_plot_point_path_basic() {
    let tva = TvaCmd::new();

    // Trajectory data - order matters
    let input = "x\ty\n0\t0\n1\t1\n0\t2\n-1\t1\n0\t0\n";

    let (stdout, _stderr) = tva
        .args(&["plot", "point", "--path", "-x", "1", "-y", "2"])
        .stdin(input)
        .run();

    assert!(!stdout.is_empty());
}

#[test]
fn test_plot_point_path_vs_line() {
    // Data where order matters: a zigzag pattern
    let input = "x\ty\n1\t1\n3\t2\n2\t3\n4\t4\n";

    // --path should preserve order
    let tva = TvaCmd::new();
    let (stdout_path, _stderr) = tva
        .args(&["plot", "point", "--path", "-x", "1", "-y", "2"])
        .stdin(input)
        .run();

    // --line should sort by x
    let tva = TvaCmd::new();
    let (stdout_line, _stderr) = tva
        .args(&["plot", "point", "--line", "-x", "1", "-y", "2"])
        .stdin(input)
        .run();

    // Both should produce output
    assert!(!stdout_path.is_empty());
    assert!(!stdout_line.is_empty());
}

#[test]
fn test_plot_point_path_and_line_mutual_exclusion() {
    let tva = TvaCmd::new();

    let input = "x\ty\n1\t2\n2\t4\n";

    let (_stdout, stderr) = tva
        .args(&["plot", "point", "--line", "--path", "-x", "1", "-y", "2"])
        .stdin(input)
        .run_fail();

    assert!(stderr.contains("Cannot use both"));
}

// Multi-Y column tests
#[test]
fn test_plot_point_multi_y_basic() {
    let tva = TvaCmd::new();

    // Data with multiple Y columns
    let input = "x\ty1\ty2\n0\t0\t0\n1\t2\t1\n2\t4\t2\n3\t6\t3\n";

    let (stdout, _stderr) = tva
        .args(&["plot", "point", "-x", "1", "-y", "2,3"])
        .stdin(input)
        .run();

    assert!(!stdout.is_empty());
    // Should show two series in legend
    assert!(stdout.contains("y1") || stdout.contains("y2"));
}

#[test]
fn test_plot_point_multi_y_with_names() {
    let tva = TvaCmd::new();
    let iris_path = data_path("iris.tsv");

    // Plot sepal_length vs both petal_length and petal_width
    let (stdout, _stderr) = tva
        .args(&[
            "plot",
            "point",
            "-x",
            "sepal_length",
            "-y",
            "petal_length,petal_width",
            iris_path.to_str().unwrap(),
        ])
        .run();

    assert!(!stdout.is_empty());
    // Should show both column names in legend or axis label
    assert!(stdout.contains("petal_length") || stdout.contains("petal_width"));
}

#[test]
fn test_plot_point_multi_y_with_color() {
    let tva = TvaCmd::new();
    let iris_path = data_path("iris.tsv");

    // Multiple Y columns with color grouping
    let (stdout, _stderr) = tva
        .args(&[
            "plot",
            "point",
            "-x",
            "sepal_length",
            "-y",
            "petal_length,petal_width",
            "--color",
            "label",
            "--cols",
            "100",
            "--rows",
            "30",
            iris_path.to_str().unwrap(),
        ])
        .run();

    assert!(!stdout.is_empty());
    // Should show axis label with series count
    assert!(stdout.contains("2 series"));
}

// Regression line tests
#[test]
fn test_plot_point_regression_basic() {
    let tva = TvaCmd::new();

    // Linear data: y = 2x
    let input = "x\ty\n0\t0\n1\t2\n2\t4\n3\t6\n4\t8\n";

    let (stdout, _stderr) = tva
        .args(&["plot", "point", "--regression", "-x", "1", "-y", "2"])
        .stdin(input)
        .run();

    assert!(!stdout.is_empty());
    // Chart renders successfully with regression line
    // Note: Legend only shows when there are multiple datasets
}

#[test]
fn test_plot_point_regression_with_color() {
    let tva = TvaCmd::new();
    let iris_path = data_path("iris.tsv");

    let (stdout, stderr) = tva
        .args(&[
            "plot",
            "point",
            "--regression",
            "-x",
            "sepal_length",
            "-y",
            "petal_length",
            "--color",
            "label",
            iris_path.to_str().unwrap(),
        ])
        .run();

    if stdout.is_empty() {
        eprintln!("stderr: {}", stderr);
    }
    assert!(!stdout.is_empty());
    // Should show axis labels
    assert!(stdout.contains("sepal_length"));
    assert!(stdout.contains("petal_length"));
}

#[test]
fn test_plot_point_regression_and_line_mutual_exclusion() {
    let tva = TvaCmd::new();

    let input = "x\ty\n1\t2\n2\t4\n";

    let (_stdout, stderr) = tva
        .args(&[
            "plot",
            "point",
            "--regression",
            "--line",
            "-x",
            "1",
            "-y",
            "2",
        ])
        .stdin(input)
        .run_fail();

    assert!(stderr.contains("Cannot use"));
}

#[test]
fn test_plot_point_regression_and_path_mutual_exclusion() {
    let tva = TvaCmd::new();

    let input = "x\ty\n1\t2\n2\t4\n";

    let (_stdout, stderr) = tva
        .args(&[
            "plot",
            "point",
            "--regression",
            "--path",
            "-x",
            "1",
            "-y",
            "2",
        ])
        .stdin(input)
        .run_fail();

    assert!(stderr.contains("Cannot use"));
}