Skip to content

Commit

Permalink
refactor the tests (#25)
Browse files Browse the repository at this point in the history
* break the multi-line tests for readability

* write a `run_test` and use `&str` to simplify the tests

this commit also renames `nu` to `input` to test
    nufmt(input) == expected
  • Loading branch information
amtoine authored Jun 10, 2023
1 parent 113a2d2 commit 3454d7a
Showing 1 changed file with 49 additions and 23 deletions.
72 changes: 49 additions & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,57 +46,83 @@ pub fn format_string(input_string: &String, config: &Config) -> String {
mod test {
use super::*;

fn run_test(input: &str, expected: &str) {
assert_eq!(
expected.to_string(),
format_string(&input.to_string(), &Config::default())
);
}

#[test]
fn array_of_object() {
let nu = String::from(
"[
let input = "[
{
\"a\": 0
},
{},
{
\"a\": null
}
]",
);
let expected = String::from("[{\"a\":0},{},{\"a\":null}]\n");
assert_eq!(expected, format_string(&nu, &Config::default()));
]";
let expected = "[{\"a\":0},{},{\"a\":null}]\n";
run_test(input, expected);
}

#[test]
fn echoes_primitive() {
let nu = String::from("1.35");
let expected = String::from("1.35\n");
assert_eq!(expected, format_string(&nu, &Config::default()));
let input = "1.35";
let expected = "1.35\n";
run_test(input, expected);
}

#[test]
fn handle_escaped_strings() {
let nu = String::from(" \"hallo\\\"\"");
let expected = String::from("\"hallo\\\"\"\n");
assert_eq!(expected, format_string(&nu, &Config::default()));
let input = " \"hallo\\\"\"";
let expected = "\"hallo\\\"\"\n";
run_test(input, expected);
}

#[test]
fn ignore_comments() {
let nu = String::from(
"# beginning of script comment\n\n let one = 1\ndef my-func [\n param1:int # inline comment\n]{ print(param1) \n}\nmyfunc(one)\n\n\n\n\n\n# final comment\n\n\n");
let expected = String::from(
"# beginning of script comment\nlet one = 1\ndef my-func [\n param1:int # inline comment\n]{ print(param1) \n}\nmyfunc(one) \n# final comment\n");
assert_eq!(expected, format_string(&nu, &Config::default()));
let input = "# beginning of script comment
let one = 1
def my-func [
param1:int # inline comment
]{ print(param1)
}
myfunc(one)
# final comment
";
let expected = "# beginning of script comment
let one = 1
def my-func [
param1:int # inline comment
]{ print(param1)
}
myfunc(one)
# final comment\n";
run_test(input, expected);
}

#[test]
fn ignore_whitespace_in_string() {
let nu = String::from("\" hallo \"");
let expected = String::from("\" hallo \"\n");
assert_eq!(expected, format_string(&nu, &Config::default()));
let input = "\" hallo \"";
let expected = "\" hallo \"\n";
run_test(input, expected);
}

#[test]
fn remove_leading_whitespace() {
let nu = String::from(" 0");
let expected = String::from("0\n");
assert_eq!(expected, format_string(&nu, &Config::default()));
let input = " 0";
let expected = "0\n";
run_test(input, expected);
}
}

0 comments on commit 3454d7a

Please sign in to comment.