ruchy 4.1.2

A systems scripting language that transpiles to idiomatic Rust with extreme quality engineering
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
//! Integration tests for `stdlib::dataframe` module
//!
//! Target: 0% → 100% coverage for stdlib/dataframe.rs (178 lines)
//! Protocol: EXTREME TDD - External integration tests provide llvm-cov coverage
//!
//! Root Cause: #[cfg(test)] unit tests exist but aren't tracked by coverage.
//! Solution: Integration tests in tests/ directory ARE tracked by llvm-cov.
//!
//! **Note**: This module is feature-gated - only available with `--features dataframe`

#![cfg(feature = "dataframe")]

use ruchy::stdlib::dataframe;
use std::fs;
use tempfile::TempDir;

// ============================================================================
// from_columns() TESTS
// ============================================================================

#[test]
fn test_dataframe_from_columns_basic() {
    let df = dataframe::from_columns(vec![("age", vec![25, 30, 35])])
        .expect("operation should succeed in test");
    assert_eq!(df.height(), 3, "Should have 3 rows");
    assert_eq!(df.width(), 1, "Should have 1 column");
}

#[test]
fn test_dataframe_from_columns_multiple() {
    let df = dataframe::from_columns(vec![("age", vec![25, 30]), ("score", vec![95, 87])])
        .expect("operation should succeed in test");
    assert_eq!(df.height(), 2, "Should have 2 rows");
    assert_eq!(df.width(), 2, "Should have 2 columns");
}

#[test]
fn test_dataframe_from_columns_empty() {
    let df = dataframe::from_columns(vec![]).expect("operation should succeed in test");
    assert_eq!(df.height(), 0, "Empty dataframe should have 0 rows");
    assert_eq!(df.width(), 0, "Empty dataframe should have 0 columns");
}

#[test]
fn test_dataframe_from_columns_mismatched_lengths() {
    let result = dataframe::from_columns(vec![("age", vec![25, 30]), ("score", vec![95])]);
    assert!(result.is_err(), "Mismatched lengths should return error");
    let err = result.unwrap_err();
    assert!(err.contains("length"), "Error should mention 'length'");
    assert!(err.contains("score"), "Error should mention 'score' column");
}

#[test]
fn test_dataframe_from_columns_single_column_various_lengths() {
    // Single column with 0 elements
    let df =
        dataframe::from_columns(vec![("empty", vec![])]).expect("operation should succeed in test");
    assert_eq!(df.height(), 0, "Empty column should have 0 rows");
    assert_eq!(df.width(), 1, "Should have 1 column");

    // Single column with 1 element
    let df = dataframe::from_columns(vec![("single", vec![42])])
        .expect("operation should succeed in test");
    assert_eq!(df.height(), 1, "Should have 1 row");
    assert_eq!(df.width(), 1, "Should have 1 column");

    // Single column with many elements
    let df = dataframe::from_columns(vec![("many", vec![1, 2, 3, 4, 5])])
        .expect("operation should succeed in test");
    assert_eq!(df.height(), 5, "Should have 5 rows");
    assert_eq!(df.width(), 1, "Should have 1 column");
}

// ============================================================================
// CSV I/O TESTS
// ============================================================================

#[test]
fn test_dataframe_read_csv_nonexistent() {
    let result = dataframe::read_csv("/nonexistent/path/file.csv");
    assert!(
        result.is_err(),
        "Reading nonexistent file should return error"
    );
}

#[test]
fn test_dataframe_write_csv_basic() {
    let temp_dir = TempDir::new().expect("operation should succeed in test");
    let csv_path = temp_dir.path().join("test.csv");
    let csv_path_str = csv_path.to_str().expect("operation should succeed in test");

    let mut df = dataframe::from_columns(vec![("age", vec![25, 30])])
        .expect("operation should succeed in test");
    dataframe::write_csv(&mut df, csv_path_str).expect("operation should succeed in test");

    // Verify file exists
    assert!(csv_path.exists(), "CSV file should be created");

    // Verify content can be read back
    let contents = fs::read_to_string(&csv_path).expect("operation should succeed in test");
    assert!(contents.contains("age"), "CSV should contain column name");
}

#[test]
fn test_dataframe_write_csv_multiple_columns() {
    let temp_dir = TempDir::new().expect("operation should succeed in test");
    let csv_path = temp_dir.path().join("multi.csv");
    let csv_path_str = csv_path.to_str().expect("operation should succeed in test");

    let mut df = dataframe::from_columns(vec![("age", vec![25, 30]), ("score", vec![95, 87])])
        .expect("operation should succeed in test");
    dataframe::write_csv(&mut df, csv_path_str).expect("operation should succeed in test");

    // Verify content
    let contents = fs::read_to_string(&csv_path).expect("operation should succeed in test");
    assert!(contents.contains("age"), "CSV should contain 'age' column");
    assert!(
        contents.contains("score"),
        "CSV should contain 'score' column"
    );
}

#[test]
fn test_dataframe_read_write_csv_roundtrip() {
    let temp_dir = TempDir::new().expect("operation should succeed in test");
    let csv_path = temp_dir.path().join("roundtrip.csv");
    let csv_path_str = csv_path.to_str().expect("operation should succeed in test");

    // Write
    let mut df1 = dataframe::from_columns(vec![("age", vec![25, 30, 35])])
        .expect("operation should succeed in test");
    dataframe::write_csv(&mut df1, csv_path_str).expect("operation should succeed in test");

    // Read back
    let df2 = dataframe::read_csv(csv_path_str).expect("operation should succeed in test");
    assert_eq!(df2.height(), 3, "Roundtrip should preserve row count");
    assert_eq!(df2.width(), 1, "Roundtrip should preserve column count");
}

// ============================================================================
// select() TESTS
// ============================================================================

#[test]
fn test_dataframe_select_single_column() {
    let df = dataframe::from_columns(vec![("age", vec![25, 30]), ("score", vec![95, 87])])
        .expect("operation should succeed in test");
    let subset = dataframe::select(&df, &["age"]).expect("operation should succeed in test");
    assert_eq!(subset.width(), 1, "Should select 1 column");
    assert_eq!(subset.height(), 2, "Should preserve row count");
}

#[test]
fn test_dataframe_select_multiple_columns() {
    let df = dataframe::from_columns(vec![
        ("age", vec![25, 30]),
        ("score", vec![95, 87]),
        ("grade", vec![1, 2]),
    ])
    .expect("operation should succeed in test");
    let subset =
        dataframe::select(&df, &["age", "score"]).expect("operation should succeed in test");
    assert_eq!(subset.width(), 2, "Should select 2 columns");
    assert_eq!(subset.height(), 2, "Should preserve row count");
}

#[test]
fn test_dataframe_select_nonexistent_column() {
    let df = dataframe::from_columns(vec![("age", vec![25, 30])])
        .expect("operation should succeed in test");
    let result = dataframe::select(&df, &["nonexistent"]);
    assert!(
        result.is_err(),
        "Selecting nonexistent column should return error"
    );
}

// ============================================================================
// head() TESTS
// ============================================================================

#[test]
fn test_dataframe_head_basic() {
    let df = dataframe::from_columns(vec![("age", vec![25, 30, 35, 40, 45])])
        .expect("operation should succeed in test");
    let top3 = dataframe::head(&df, 3).expect("operation should succeed in test");
    assert_eq!(top3.height(), 3, "head(3) should return 3 rows");
}

#[test]
fn test_dataframe_head_more_than_length() {
    let df = dataframe::from_columns(vec![("age", vec![25, 30])])
        .expect("operation should succeed in test");
    let top10 = dataframe::head(&df, 10).expect("operation should succeed in test");
    assert_eq!(top10.height(), 2, "head(10) should return all 2 rows");
}

#[test]
fn test_dataframe_head_zero() {
    let df = dataframe::from_columns(vec![("age", vec![25, 30, 35])])
        .expect("operation should succeed in test");
    let top0 = dataframe::head(&df, 0).expect("operation should succeed in test");
    assert_eq!(top0.height(), 0, "head(0) should return 0 rows");
}

// ============================================================================
// tail() TESTS
// ============================================================================

#[test]
fn test_dataframe_tail_basic() {
    let df = dataframe::from_columns(vec![("age", vec![25, 30, 35, 40, 45])])
        .expect("operation should succeed in test");
    let bottom3 = dataframe::tail(&df, 3).expect("operation should succeed in test");
    assert_eq!(bottom3.height(), 3, "tail(3) should return 3 rows");
}

#[test]
fn test_dataframe_tail_more_than_length() {
    let df = dataframe::from_columns(vec![("age", vec![25, 30])])
        .expect("operation should succeed in test");
    let bottom10 = dataframe::tail(&df, 10).expect("operation should succeed in test");
    assert_eq!(bottom10.height(), 2, "tail(10) should return all 2 rows");
}

#[test]
fn test_dataframe_tail_zero() {
    let df = dataframe::from_columns(vec![("age", vec![25, 30, 35])])
        .expect("operation should succeed in test");
    let bottom0 = dataframe::tail(&df, 0).expect("operation should succeed in test");
    assert_eq!(bottom0.height(), 0, "tail(0) should return 0 rows");
}

// ============================================================================
// shape() TESTS
// ============================================================================

#[test]
fn test_dataframe_shape_basic() {
    let df = dataframe::from_columns(vec![("age", vec![25, 30, 35])])
        .expect("operation should succeed in test");
    let (rows, cols) = dataframe::shape(&df).expect("operation should succeed in test");
    assert_eq!(rows, 3, "Should have 3 rows");
    assert_eq!(cols, 1, "Should have 1 column");
}

#[test]
fn test_dataframe_shape_multiple_columns() {
    let df = dataframe::from_columns(vec![("age", vec![25, 30]), ("score", vec![95, 87])])
        .expect("operation should succeed in test");
    let (rows, cols) = dataframe::shape(&df).expect("operation should succeed in test");
    assert_eq!(rows, 2, "Should have 2 rows");
    assert_eq!(cols, 2, "Should have 2 columns");
}

#[test]
fn test_dataframe_shape_empty() {
    let df = dataframe::from_columns(vec![]).expect("operation should succeed in test");
    let (rows, cols) = dataframe::shape(&df).expect("operation should succeed in test");
    assert_eq!(rows, 0, "Empty dataframe should have 0 rows");
    assert_eq!(cols, 0, "Empty dataframe should have 0 columns");
}

// ============================================================================
// columns() TESTS
// ============================================================================

#[test]
fn test_dataframe_columns_basic() {
    let df = dataframe::from_columns(vec![("age", vec![25, 30])])
        .expect("operation should succeed in test");
    let names = dataframe::columns(&df).expect("operation should succeed in test");
    assert_eq!(names.len(), 1, "Should have 1 column name");
    assert_eq!(names[0], "age", "Column name should be 'age'");
}

#[test]
fn test_dataframe_columns_multiple() {
    let df = dataframe::from_columns(vec![
        ("age", vec![25]),
        ("score", vec![95]),
        ("grade", vec![1]),
    ])
    .expect("operation should succeed in test");
    let names = dataframe::columns(&df).expect("operation should succeed in test");
    assert_eq!(names.len(), 3, "Should have 3 column names");
    assert!(names.contains(&"age".to_string()), "Should contain 'age'");
    assert!(
        names.contains(&"score".to_string()),
        "Should contain 'score'"
    );
    assert!(
        names.contains(&"grade".to_string()),
        "Should contain 'grade'"
    );
}

#[test]
fn test_dataframe_columns_empty() {
    let df = dataframe::from_columns(vec![]).expect("operation should succeed in test");
    let names = dataframe::columns(&df).expect("operation should succeed in test");
    assert_eq!(
        names.len(),
        0,
        "Empty dataframe should have no column names"
    );
}

// ============================================================================
// row_count() TESTS
// ============================================================================

#[test]
fn test_dataframe_row_count_basic() {
    let df = dataframe::from_columns(vec![("age", vec![25, 30, 35])])
        .expect("operation should succeed in test");
    let count = dataframe::row_count(&df).expect("operation should succeed in test");
    assert_eq!(count, 3, "Should have 3 rows");
}

#[test]
fn test_dataframe_row_count_empty() {
    let df =
        dataframe::from_columns(vec![("age", vec![])]).expect("operation should succeed in test");
    let count = dataframe::row_count(&df).expect("operation should succeed in test");
    assert_eq!(count, 0, "Empty column should have 0 rows");
}

#[test]
fn test_dataframe_row_count_multiple_columns() {
    let df = dataframe::from_columns(vec![("age", vec![25, 30]), ("score", vec![95, 87])])
        .expect("operation should succeed in test");
    let count = dataframe::row_count(&df).expect("operation should succeed in test");
    assert_eq!(count, 2, "Should have 2 rows");
}

// ============================================================================
// WORKFLOW TEST
// ============================================================================

#[test]
fn test_dataframe_complete_workflow() {
    // Complete workflow: create, select, head, tail, shape, columns, row_count
    let df = dataframe::from_columns(vec![
        ("age", vec![25, 30, 35, 40, 45]),
        ("score", vec![95, 87, 92, 88, 90]),
    ])
    .expect("operation should succeed in test");

    // Select
    let subset = dataframe::select(&df, &["age"]).expect("operation should succeed in test");
    assert_eq!(subset.width(), 1, "Select should return 1 column");

    // Head
    let top2 = dataframe::head(&df, 2).expect("operation should succeed in test");
    assert_eq!(top2.height(), 2, "Head should return 2 rows");

    // Tail
    let bottom2 = dataframe::tail(&df, 2).expect("operation should succeed in test");
    assert_eq!(bottom2.height(), 2, "Tail should return 2 rows");

    // Shape
    let (rows, cols) = dataframe::shape(&df).expect("operation should succeed in test");
    assert_eq!(rows, 5, "Shape should report 5 rows");
    assert_eq!(cols, 2, "Shape should report 2 columns");

    // Columns
    let names = dataframe::columns(&df).expect("operation should succeed in test");
    assert_eq!(names.len(), 2, "Should have 2 column names");

    // Row count
    let count = dataframe::row_count(&df).expect("operation should succeed in test");
    assert_eq!(count, 5, "Row count should be 5");
}

#[test]
fn test_dataframe_csv_workflow() {
    let temp_dir = TempDir::new().expect("operation should succeed in test");
    let csv_path = temp_dir.path().join("workflow.csv");
    let csv_path_str = csv_path.to_str().expect("operation should succeed in test");

    // Create dataframe
    let mut df =
        dataframe::from_columns(vec![("age", vec![25, 30, 35]), ("score", vec![95, 87, 92])])
            .expect("operation should succeed in test");

    // Check initial state
    let (rows, cols) = dataframe::shape(&df).expect("operation should succeed in test");
    assert_eq!(rows, 3, "Initial dataframe should have 3 rows");
    assert_eq!(cols, 2, "Initial dataframe should have 2 columns");

    // Write to CSV
    dataframe::write_csv(&mut df, csv_path_str).expect("operation should succeed in test");

    // Read back from CSV
    let df2 = dataframe::read_csv(csv_path_str).expect("operation should succeed in test");

    // Verify roundtrip
    let (rows2, cols2) = dataframe::shape(&df2).expect("operation should succeed in test");
    assert_eq!(rows2, 3, "Roundtrip should preserve 3 rows");
    assert_eq!(cols2, 2, "Roundtrip should preserve 2 columns");

    // Verify column names
    let names = dataframe::columns(&df2).expect("operation should succeed in test");
    assert!(
        names.contains(&"age".to_string()),
        "Should have 'age' column after roundtrip"
    );
    assert!(
        names.contains(&"score".to_string()),
        "Should have 'score' column after roundtrip"
    );
}