From 266d4cb14512c411e6723b1e2aa4da97eab507a6 Mon Sep 17 00:00:00 2001 From: Jake Deichert Date: Thu, 18 Jul 2019 23:44:19 -0400 Subject: [PATCH] Add tests and fix #9 --- README.md | 2 +- src/parser.rs | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 98 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1cc0528..7a43abe 100644 --- a/README.md +++ b/README.md @@ -181,7 +181,7 @@ Valid lang codes: js, javascript ~~~js const { name } = process.env; -console.log(`Hello, ${name}!`) +console.log(`Hello, ${name}!`); ~~~ diff --git a/src/parser.rs b/src/parser.rs index c533ea8..eecfbe6 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,5 +1,5 @@ use pulldown_cmark::{ - Event::{End, InlineHtml, Start, Text}, + Event::{Code, End, InlineHtml, Start, Text}, Options, Parser, Tag, }; @@ -112,6 +112,9 @@ pub fn build_command_structure(maskfile_contents: String) -> Command { InlineHtml(html) => { text += &html.to_string(); } + Code(inline_code) => { + text += &format!("`{}`", inline_code); + } _ => (), }; } @@ -215,3 +218,96 @@ fn parse_command_name_and_required_args( (name, required_args) } + +#[cfg(test)] +const TEST_MASKFILE: &str = r#" +# Document Title + +This is an example maskfile for the tests below. + +## serve + +> Serve the app on the `port` + +~~~bash +echo "Serving on port $port" +~~~ + +## node + +> An example node script + +Valid lang codes: js, javascript + +```js +const { name } = process.env; +console.log(`Hello, ${name}!`); +``` +"#; + +#[cfg(test)] +mod build_command_structure { + use super::*; + + #[test] + fn parses_serve_command_name() { + let tree = build_command_structure(TEST_MASKFILE.to_string()); + let serve_command = &tree.subcommands[0]; + assert_eq!(serve_command.name, "serve"); + } + + #[test] + fn parses_serve_command_description() { + let tree = build_command_structure(TEST_MASKFILE.to_string()); + let serve_command = &tree.subcommands[0]; + assert_eq!(serve_command.desc, "Serve the app on the `port`"); + } + + #[test] + fn parses_serve_required_positional_arguments() { + let tree = build_command_structure(TEST_MASKFILE.to_string()); + let serve_command = &tree.subcommands[0]; + assert_eq!(serve_command.required_args.len(), 1); + assert_eq!(serve_command.required_args[0].name, "port"); + } + + #[test] + fn adds_default_verbose_optional_flag() { + let tree = build_command_structure(TEST_MASKFILE.to_string()); + let serve_command = &tree.subcommands[0]; + assert_eq!(serve_command.option_flags.len(), 1); + assert_eq!(serve_command.option_flags[0].name, "verbose"); + assert_eq!( + serve_command.option_flags[0].desc, + "Sets the level of verbosity" + ); + assert_eq!(serve_command.option_flags[0].short, "v"); + assert_eq!(serve_command.option_flags[0].long, "verbose"); + assert_eq!(serve_command.option_flags[0].multiple, false); + assert_eq!(serve_command.option_flags[0].takes_value, false); + } + + #[test] + fn parses_serve_command_executor() { + let tree = build_command_structure(TEST_MASKFILE.to_string()); + let serve_command = &tree.subcommands[0]; + assert_eq!(serve_command.executor, "bash"); + } + + #[test] + fn parses_serve_command_source_with_tildes() { + let tree = build_command_structure(TEST_MASKFILE.to_string()); + let serve_command = &tree.subcommands[0]; + assert_eq!(serve_command.source, "echo \"Serving on port $port\"\n"); + } + + #[test] + fn parses_node_command_source_with_backticks() { + let tree = build_command_structure(TEST_MASKFILE.to_string()); + let node_command = &tree.subcommands[1]; + assert_eq!( + node_command.source, + "const { name } = process.env;\nconsole.log(`Hello, ${name}!`);\n" + ); + } +}