Skip to content

Commit

Permalink
Add tests and fix #9
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobdeichert committed Jul 19, 2019
1 parent 4804e5b commit 266d4cb
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ Valid lang codes: js, javascript

~~~js
const { name } = process.env;
console.log(`Hello, ${name}!`)
console.log(`Hello, ${name}!`);
~~~


Expand Down
98 changes: 97 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use pulldown_cmark::{
Event::{End, InlineHtml, Start, Text},
Event::{Code, End, InlineHtml, Start, Text},
Options, Parser, Tag,
};

Expand Down Expand Up @@ -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);
}
_ => (),
};
}
Expand Down Expand Up @@ -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 <port>
> Serve the app on the `port`
~~~bash
echo "Serving on port $port"
~~~
## node <name>
> 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"
);
}
}

0 comments on commit 266d4cb

Please sign in to comment.