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
use super::operate; use crate::prelude::*; use inflector::cases::screamingsnakecase::to_screaming_snake_case; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, Value}; pub struct SubCommand; impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "str screaming-snake-case" } fn signature(&self) -> Signature { Signature::build("str screaming-snake-case").rest( SyntaxShape::ColumnPath, "optionally convert text to SCREAMING_SNAKE_CASE by column paths", ) } fn usage(&self) -> &str { "converts a string to SCREAMING_SNAKE_CASE" } fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> { operate(args, &to_screaming_snake_case) } fn examples(&self) -> Vec<Example> { vec![Example { description: "convert a string to SCREAMING_SNAKE_CASE", example: "echo 'NuShell' | str screaming-snake-case", result: Some(vec![Value::from("NU_SHELL")]), }] } } #[cfg(test)] mod tests { use super::ShellError; use super::{to_screaming_snake_case, SubCommand}; use crate::commands::strings::str_::case::action; use nu_source::Tag; use nu_test_support::value::string; #[test] fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; test_examples(SubCommand {}) } #[test] fn snake_case_from_kebab() { let word = string("this-is-the-first-case"); let expected = string("THIS_IS_THE_FIRST_CASE"); let actual = action(&word, Tag::unknown(), &to_screaming_snake_case).unwrap(); assert_eq!(actual, expected); } #[test] fn snake_case_from_snake() { let word = string("this_is_the_second_case"); let expected = string("THIS_IS_THE_SECOND_CASE"); let actual = action(&word, Tag::unknown(), &to_screaming_snake_case).unwrap(); assert_eq!(actual, expected); } }