nushell/crates/nu-parser/tests/test_lite_parser.rs

243 lines
6.2 KiB
Rust
Raw Normal View History

2021-09-02 03:29:43 +02:00
use nu_parser::{lex, lite_parse, LiteBlock, ParseError};
use nu_protocol::Span;
2021-08-30 20:36:07 +02:00
fn lite_parse_helper(input: &[u8]) -> Result<LiteBlock, ParseError> {
2021-11-21 19:13:09 +01:00
let (output, err) = lex(input, 0, &[], &[], false);
2021-08-30 20:36:07 +02:00
if let Some(err) = err {
return Err(err);
}
let (output, err) = lite_parse(&output);
if let Some(err) = err {
return Err(err);
}
Ok(output)
}
#[test]
fn comment_before() -> Result<(), ParseError> {
// Code:
// # this is a comment
// def foo bar
2021-08-30 20:36:07 +02:00
let input = b"# this is a comment\ndef foo bar";
let lite_block = lite_parse_helper(input)?;
2021-09-01 22:05:37 +02:00
assert_eq!(lite_block.block.len(), 1);
2021-08-30 20:36:07 +02:00
assert_eq!(lite_block.block[0].commands.len(), 1);
assert_eq!(lite_block.block[0].commands[0].comments.len(), 1);
2021-09-01 22:05:37 +02:00
assert_eq!(lite_block.block[0].commands[0].parts.len(), 3);
assert_eq!(
lite_block.block[0].commands[0].comments[0],
Span { start: 0, end: 19 }
);
2021-08-30 20:36:07 +02:00
Ok(())
}
#[test]
fn comment_beside() -> Result<(), ParseError> {
// Code:
// def foo bar # this is a comment
2021-08-30 20:36:07 +02:00
let input = b"def foo bar # this is a comment";
let lite_block = lite_parse_helper(input)?;
assert_eq!(lite_block.block.len(), 1);
assert_eq!(lite_block.block[0].commands.len(), 1);
assert_eq!(lite_block.block[0].commands[0].comments.len(), 1);
assert_eq!(lite_block.block[0].commands[0].parts.len(), 3);
2021-09-01 22:05:37 +02:00
assert_eq!(
lite_block.block[0].commands[0].comments[0],
Span { start: 12, end: 31 }
);
2021-08-30 20:36:07 +02:00
Ok(())
}
#[test]
fn comments_stack() -> Result<(), ParseError> {
// Code:
// # this is a comment
// # another comment
// # def foo bar
2021-08-30 20:36:07 +02:00
let input = b"# this is a comment\n# another comment\ndef foo bar ";
let lite_block = lite_parse_helper(input)?;
2021-09-01 22:05:37 +02:00
assert_eq!(lite_block.block.len(), 1);
assert_eq!(lite_block.block[0].commands[0].comments.len(), 2);
assert_eq!(lite_block.block[0].commands[0].parts.len(), 3);
2021-09-01 22:05:37 +02:00
assert_eq!(
lite_block.block[0].commands[0].comments[0],
Span { start: 0, end: 19 }
);
2021-09-01 22:05:37 +02:00
assert_eq!(
lite_block.block[0].commands[0].comments[1],
Span { start: 20, end: 37 }
);
2021-08-30 20:36:07 +02:00
Ok(())
}
#[test]
fn separated_comments_dont_stack() -> Result<(), ParseError> {
// Code:
// # this is a comment
//
// # another comment
// # def foo bar
2021-08-30 20:36:07 +02:00
let input = b"# this is a comment\n\n# another comment\ndef foo bar ";
let lite_block = lite_parse_helper(input)?;
2021-09-01 22:05:37 +02:00
assert_eq!(lite_block.block.len(), 1);
assert_eq!(lite_block.block[0].commands[0].comments.len(), 1);
2021-09-01 22:05:37 +02:00
assert_eq!(lite_block.block[0].commands[0].parts.len(), 3);
2021-08-30 20:36:07 +02:00
assert_eq!(
lite_block.block[0].commands[0].comments[0],
Span { start: 21, end: 38 }
2021-08-30 20:36:07 +02:00
);
Ok(())
}
#[test]
fn multiple_pipelines() -> Result<(), ParseError> {
// Code:
2021-08-30 20:36:07 +02:00
// # A comment
// let a = ( 3 + (
// 4 +
// 5 ))
// let b = 1 # comment
let input = b"# comment \n let a = ( 3 + (\n 4 + \n 5 )) \n let b = 1 # comment";
let lite_block = lite_parse_helper(input)?;
2021-09-01 22:05:37 +02:00
assert_eq!(lite_block.block.len(), 2);
assert_eq!(lite_block.block[0].commands[0].comments.len(), 1);
2021-09-01 22:05:37 +02:00
assert_eq!(lite_block.block[0].commands[0].parts.len(), 4);
2021-08-30 20:36:07 +02:00
assert_eq!(
lite_block.block[0].commands[0].comments[0],
Span { start: 0, end: 10 }
2021-08-30 20:36:07 +02:00
);
2021-09-01 22:05:37 +02:00
assert_eq!(lite_block.block[1].commands[0].comments.len(), 1);
assert_eq!(lite_block.block[1].commands[0].parts.len(), 4);
2021-08-30 20:36:07 +02:00
assert_eq!(
2021-09-01 22:05:37 +02:00
lite_block.block[1].commands[0].comments[0],
2021-08-30 20:36:07 +02:00
Span { start: 52, end: 61 }
);
Ok(())
}
#[test]
fn multiple_commands() -> Result<(), ParseError> {
// Pipes add commands to the lite parser
// Code:
2021-08-30 20:36:07 +02:00
// let a = ls | where name == 1
// let b = 1 # comment
let input = b"let a = ls | where name == 1 \n let b = 1 # comment";
let lite_block = lite_parse_helper(input)?;
assert_eq!(lite_block.block.len(), 2);
assert_eq!(lite_block.block[0].commands.len(), 2);
assert_eq!(lite_block.block[1].commands.len(), 1);
assert_eq!(
lite_block.block[1].commands[0].comments[0],
Span { start: 41, end: 50 }
);
Ok(())
}
#[test]
fn multiple_commands_with_comment() -> Result<(), ParseError> {
// Pipes add commands to the lite parser
// The comments are attached to the commands next to them
// Code:
// let a = ls | where name == 1 # comment
// let b = 1 # comment
//let a = ls | where name == 1 # comment \n let b = 1 # comment
let input = b"let a = ls | where name == 1 # comment\n let b = 1 # comment";
let lite_block = lite_parse_helper(input)?;
assert_eq!(lite_block.block.len(), 2);
assert_eq!(lite_block.block[0].commands.len(), 2);
assert_eq!(lite_block.block[1].commands.len(), 1);
assert_eq!(
lite_block.block[0].commands[1].comments[0],
Span { start: 29, end: 38 }
);
Ok(())
}
2021-09-01 22:05:37 +02:00
#[test]
fn multiple_commands_with_pipes() -> Result<(), ParseError> {
// The comments inside () get encapsulated in the whole item
// Code:
// # comment 1
// # comment 2
// let a = ( ls
// | where name =~ some # another comment
// | each { |file| rm file.name } # final comment
// )
// # comment A
// let b = 0;
let input = b"# comment 1
# comment 2
let a = ( ls
| where name =~ some # another comment
| each { |file| rm file.name }) # final comment
# comment A
let b = 0
";
let lite_block = lite_parse_helper(input)?;
assert_eq!(lite_block.block.len(), 2);
assert_eq!(lite_block.block[0].commands[0].comments.len(), 3);
assert_eq!(lite_block.block[0].commands[0].parts.len(), 4);
assert_eq!(
lite_block.block[0].commands[0].parts[3],
Span {
start: 32,
end: 107
}
);
assert_eq!(
lite_block.block[0].commands[0].comments[2],
Span {
start: 108,
end: 123
}
);
assert_eq!(lite_block.block[1].commands[0].comments.len(), 1);
assert_eq!(lite_block.block[1].commands[0].parts.len(), 4);
assert_eq!(
lite_block.block[1].commands[0].comments[0],
Span {
start: 124,
end: 135
}
);
Ok(())
}