From: Gustav Eek Date: Sun, 19 Feb 2023 16:26:29 +0000 (+0100) Subject: Src. Split input function and provide tests X-Git-Tag: v.0.4~5 X-Git-Url: https://git.g-eek.se/?a=commitdiff_plain;h=f04c89d4735e2d0a37c9c81e6eb14510f5a3ef1d;p=ranknauto.git Src. Split input function and provide tests * Refactor a split of the *input* function into *winnow* * Trim and remove empty items * Provice two tests for input cleaning * Clean up among *use* headers --- diff --git a/src/main.rs b/src/main.rs index 56721ab..255524b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,22 +2,34 @@ //! //! SPDX-License-Identifier: GPL-3.0-or-later -// use std::env; // Replaced by clap::Parser stuff use std::io; // bring flush() into scope with `use std::io::Write` use std::io::prelude::*; // Bring `std::io::BufRead` in scope -use statrs::distribution::{Exp, LogNormal, ContinuousCDF}; // Continuous needed for pdf -// use statrs::statistics::Distribution; -// use statrs::prec; +// Statrs, `don't use statrs::prec` and +// `statrs::statistics::Distribution` from examples +use statrs::distribution::{ + Exp, LogNormal, ContinuousCDF}; // Continuous needed for pdf + +// CLI Parser, replaces need for use `std::env` use clap::Parser; const DEBUG: bool = false; +fn winnow(list: &mut Vec) { + for l in &mut *list { + *l = l.trim().to_owned(); + } + list.retain(|x| x != ""); // only keep nonempty + // Search bullets +} fn input() -> Vec { - // Consider datastructure some list of strings let stdin = io::stdin(); - let v = stdin.lock().lines().map(|x| x.unwrap()).collect(); - v + let mut list: Vec = + stdin.lock().lines() + .map(|x| x.unwrap()) + .collect(); + winnow(&mut list); + list } fn output(prio: &Vec, ranked: &Vec) { @@ -152,3 +164,35 @@ fn main() { output(&prio, &ranked); } + +#[test] + +fn ordinary_input() { + + let mut arg: Vec = Vec::new(); + let mut res: Vec = Vec::new(); + for s in vec!["", "", "Hej ☕ glade", "", "Amatör", "", ""] { + arg.push(String::from(s)); + } + for s in vec!["Hej ☕ glade", "Amatör"] { + res.push(String::from(s)); + } + winnow(&mut arg); + assert_eq!(arg, res); +} + +#[test] + +fn whitespace() { + + let mut arg: Vec = + "\n\nHej du\t\n glade\nta en\n\n" // Convert &str to String + .split("\n").map(|x| x.to_owned()) // with `.to_owned()` or + .collect(); // `.to_string()` + let res: Vec = + "Hej du\nglade\nta en" + .split("\n").map(|x| x.to_owned()).collect(); + winnow(&mut arg); + assert_eq!(arg, res); + +}