Skip to content

Commit

Permalink
Merge pull request #1848 from jqnatividad/100.dathere.com
Browse files Browse the repository at this point in the history
`tests`: add test_100 for 100.dathere.com & tests for lesson/exercise 0
  • Loading branch information
jqnatividad authored May 30, 2024
2 parents 71cd173 + f3eca85 commit 3e20a15
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tests/test_100.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// The tests in this file and the test_100 folder are for
// lessons and exercises in the online book "100 exercises with qsv".

// You may read lessons and try out the exercises at:
// https://100.dathere.com

// Warning: These tests may contain solutions to the exercises.

mod exercise_0;
62 changes: 62 additions & 0 deletions tests/test_100/exercise_0.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Lesson 0: Exploring qsv help messages and syntax
// https://100.dathere.com/lessons/0/notes.html

use std::process;

use crate::workdir::Workdir;

fn setup(name: &str, command_str: &str, args: Vec<&str>) -> (Workdir, process::Command) {
let wrk = Workdir::new(name);
wrk.create(
"fruits.csv",
vec![
svec!["fruit", "price"],
svec!["apple", "2.50"],
svec!["banana", "3.00"],
svec!["carrot", "1.50"],
],
);

let mut cmd = wrk.command(command_str);
cmd.args(args);

(wrk, cmd)
}

// https://100.dathere.com/lessons/0/notes.html#displaying-headers-of-a-csv
#[test]
fn fruits_headers() {
let name = "fruits_headers";
let command_str = "headers";
let args = vec!["fruits.csv"];
let (wrk, mut cmd) = setup(name, command_str, args);

let got: String = wrk.stdout(&mut cmd);
let expected = r#"1 fruit
2 price"#;
assert_eq!(got, expected);
}

// https://100.dathere.com/lessons/0/notes.html#exercise-0-total-rows
#[test]
fn fruits_count_total() {
let name = "fruits_count";
let command_str = "count";
let args = vec!["fruits.csv", "--no-headers"];
let (wrk, mut cmd) = setup(name, command_str, args);

let got: String = wrk.stdout(&mut cmd);
let expected = "4";
assert_eq!(got, expected);
}
#[test]
fn fruits_count() {
let name = "fruits_count";
let command_str = "count";
let args = vec!["fruits.csv"];
let (wrk, mut cmd) = setup(name, command_str, args);

let got: String = wrk.stdout(&mut cmd);
let expected = "3";
assert_eq!(got, expected);
}
1 change: 1 addition & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ macro_rules! rassert_eq {

mod workdir;

mod test_100;
#[cfg(feature = "apply")]
mod test_apply;
#[cfg(feature = "datapusher_plus")]
Expand Down

0 comments on commit 3e20a15

Please sign in to comment.