From 04565cb84496a6b4556a44b281674b03adc76fa9 Mon Sep 17 00:00:00 2001 From: perro Date: Mon, 13 Feb 2023 16:54:49 -0800 Subject: [PATCH] Ejercicio 2 --- .gitmodules | 3 + exercises/ex02/Cargo.lock | 295 +++++++++++++++++++++ exercises/ex02/Cargo.toml | 13 + exercises/ex02/src/main.rs | 26 ++ exercises/ex02/tests/cli.rs | 47 ++++ exercises/ex02/tests/expected/hello1.n.txt | 1 + exercises/ex02/tests/expected/hello1.txt | 1 + exercises/ex02/tests/expected/hello2.n.txt | 1 + exercises/ex02/tests/expected/hello2.txt | 1 + solutions | 1 + 10 files changed, 389 insertions(+) create mode 100644 .gitmodules create mode 100644 exercises/ex02/Cargo.lock create mode 100644 exercises/ex02/Cargo.toml create mode 100644 exercises/ex02/src/main.rs create mode 100644 exercises/ex02/tests/cli.rs create mode 100644 exercises/ex02/tests/expected/hello1.n.txt create mode 100644 exercises/ex02/tests/expected/hello1.txt create mode 100644 exercises/ex02/tests/expected/hello2.n.txt create mode 100644 exercises/ex02/tests/expected/hello2.txt create mode 160000 solutions diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..2a0d6f3 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "solutions"] + path = solutions + url = git@github.com:kyclark/command-line-rust.git diff --git a/exercises/ex02/Cargo.lock b/exercises/ex02/Cargo.lock new file mode 100644 index 0000000..0068223 --- /dev/null +++ b/exercises/ex02/Cargo.lock @@ -0,0 +1,295 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "0.7.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac" +dependencies = [ + "memchr", +] + +[[package]] +name = "ansi_term" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" +dependencies = [ + "winapi", +] + +[[package]] +name = "assert_cmd" +version = "2.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9834fcc22e0874394a010230586367d4a3e9f11b560f469262678547e1d2575e" +dependencies = [ + "bstr", + "doc-comment", + "predicates", + "predicates-core", + "predicates-tree", + "wait-timeout", +] + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bstr" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7f0778972c64420fdedc63f09919c8a88bda7b25135357fd25a5d9f3257e832" +dependencies = [ + "memchr", + "once_cell", + "regex-automata", + "serde", +] + +[[package]] +name = "clap" +version = "2.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" +dependencies = [ + "ansi_term", + "atty", + "bitflags", + "strsim", + "textwrap", + "unicode-width", + "vec_map", +] + +[[package]] +name = "difflib" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8" + +[[package]] +name = "doc-comment" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" + +[[package]] +name = "echor" +version = "0.1.0" +dependencies = [ + "assert_cmd", + "clap", + "predicates", +] + +[[package]] +name = "either" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" + +[[package]] +name = "float-cmp" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98de4bbd547a563b716d8dfa9aad1cb19bfab00f4fa09a6a4ed21dbcf44ce9c4" +dependencies = [ + "num-traits", +] + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "itertools" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +dependencies = [ + "either", +] + +[[package]] +name = "libc" +version = "0.2.139" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" + +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + +[[package]] +name = "normalize-line-endings" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be" + +[[package]] +name = "num-traits" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f61fba1741ea2b3d6a1e3178721804bb716a68a6aeba1149b5d52e3d464ea66" + +[[package]] +name = "predicates" +version = "2.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59230a63c37f3e18569bdb90e4a89cbf5bf8b06fea0b84e65ea10cc4df47addd" +dependencies = [ + "difflib", + "float-cmp", + "itertools", + "normalize-line-endings", + "predicates-core", + "regex", +] + +[[package]] +name = "predicates-core" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72f883590242d3c6fc5bf50299011695fa6590c2c70eac95ee1bdb9a733ad1a2" + +[[package]] +name = "predicates-tree" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54ff541861505aabf6ea722d2131ee980b8276e10a1297b94e896dd8b621850d" +dependencies = [ + "predicates-core", + "termtree", +] + +[[package]] +name = "regex" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48aaa5748ba571fb95cd2c85c09f629215d3a6ece942baa100950af03a34f733" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" + +[[package]] +name = "regex-syntax" +version = "0.6.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" + +[[package]] +name = "serde" +version = "1.0.152" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb" + +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" + +[[package]] +name = "termtree" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95059e91184749cb66be6dc994f67f182b6d897cb3df74a5bf66b5e709295fd8" + +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + +[[package]] +name = "unicode-width" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" + +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + +[[package]] +name = "wait-timeout" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6" +dependencies = [ + "libc", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/exercises/ex02/Cargo.toml b/exercises/ex02/Cargo.toml new file mode 100644 index 0000000..fc2a8bb --- /dev/null +++ b/exercises/ex02/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "echor" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +clap = "2.33" + +[dev-dependencies] +assert_cmd = "2" +predicates = "2" diff --git a/exercises/ex02/src/main.rs b/exercises/ex02/src/main.rs new file mode 100644 index 0000000..95efc74 --- /dev/null +++ b/exercises/ex02/src/main.rs @@ -0,0 +1,26 @@ +use clap::{App, Arg}; + +fn main() { + let matches = App::new("echor") + .version("0.1.0") + .author("perro ") + .about("Rust echo") + .arg( + Arg::with_name("text") + .value_name("TEXTO") + .help("Texto de entrada") + .required(true) + .min_values(1), + ) + .arg( + Arg::with_name("omit_newline") + .short("n") + .help("No imprime nueva línea") + .takes_value(false), + ) + .get_matches(); + // OJO: unwrap es seguro de llamar porque 'text' está forzado en tener al menos un valor + let text = matches.values_of_lossy("text").unwrap(); + let ending = if matches.is_present("omit_newline") { "" } else { "\n"}; + print!("{}{}", text.join(" "), ending); +} diff --git a/exercises/ex02/tests/cli.rs b/exercises/ex02/tests/cli.rs new file mode 100644 index 0000000..6985392 --- /dev/null +++ b/exercises/ex02/tests/cli.rs @@ -0,0 +1,47 @@ +use std::fs; +use assert_cmd::Command; +use predicates::prelude::*; + +type TestResult = Result<(), Box>; + +fn run(args: &[&str], expected_file: &str) -> TestResult { + let expected = fs::read_to_string(format!("{}{}", "tests/expected/", expected_file))?; + Command::cargo_bin("echor")?.args(args).assert().success().stdout(expected); + Ok(()) +} + +#[test] +fn error_no_args() -> TestResult { + let mut cmd = Command::cargo_bin("echor")?; + cmd.assert() + .failure() + .stderr(predicate::str::contains("USAGE")); + Ok(()) +} + +#[test] +fn runs() -> TestResult { + let mut cmd = Command::cargo_bin("echor")?; + cmd.arg("Hola").assert().success(); + Ok(()) +} + +#[test] +fn hello1() -> TestResult { + run(&["Hello there"], "hello1.txt") +} + +#[test] +fn hello2() -> TestResult { + run(&["Hello", "there"], "hello2.txt") +} + +#[test] +fn hello1_no_newline() -> TestResult { + run(&["Hello there", "-n"], "hello1.n.txt") +} + +#[test] +fn hello2_no_newline() -> TestResult { + run(&["-n", "Hello", "there"], "hello2.n.txt") +} diff --git a/exercises/ex02/tests/expected/hello1.n.txt b/exercises/ex02/tests/expected/hello1.n.txt new file mode 100644 index 0000000..b1011d0 --- /dev/null +++ b/exercises/ex02/tests/expected/hello1.n.txt @@ -0,0 +1 @@ +Hello there \ No newline at end of file diff --git a/exercises/ex02/tests/expected/hello1.txt b/exercises/ex02/tests/expected/hello1.txt new file mode 100644 index 0000000..d6613f5 --- /dev/null +++ b/exercises/ex02/tests/expected/hello1.txt @@ -0,0 +1 @@ +Hello there diff --git a/exercises/ex02/tests/expected/hello2.n.txt b/exercises/ex02/tests/expected/hello2.n.txt new file mode 100644 index 0000000..466c153 --- /dev/null +++ b/exercises/ex02/tests/expected/hello2.n.txt @@ -0,0 +1 @@ +Hello there \ No newline at end of file diff --git a/exercises/ex02/tests/expected/hello2.txt b/exercises/ex02/tests/expected/hello2.txt new file mode 100644 index 0000000..d6613f5 --- /dev/null +++ b/exercises/ex02/tests/expected/hello2.txt @@ -0,0 +1 @@ +Hello there diff --git a/solutions b/solutions new file mode 160000 index 0000000..e977e5b --- /dev/null +++ b/solutions @@ -0,0 +1 @@ +Subproject commit e977e5b1412bb3cd20737465d61f0baca05536d3