2021-09-02 08:25:22 +00:00
|
|
|
use nu_parser::ParseError;
|
2021-08-30 18:36:07 +00:00
|
|
|
use nu_parser::*;
|
2021-09-02 18:21:37 +00:00
|
|
|
use nu_protocol::{
|
2021-09-03 02:15:01 +00:00
|
|
|
ast::{Expr, Expression, Pipeline, Statement},
|
2021-09-02 18:21:37 +00:00
|
|
|
engine::{EngineState, StateWorkingSet},
|
|
|
|
Signature, SyntaxShape,
|
|
|
|
};
|
2021-08-30 18:36:07 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn parse_int() {
|
2021-09-02 08:25:22 +00:00
|
|
|
let engine_state = EngineState::new();
|
|
|
|
let mut working_set = StateWorkingSet::new(&engine_state);
|
2021-08-30 18:36:07 +00:00
|
|
|
|
2021-09-02 18:21:37 +00:00
|
|
|
let (block, err) = parse_source(&mut working_set, b"3", true);
|
2021-08-30 18:36:07 +00:00
|
|
|
|
|
|
|
assert!(err.is_none());
|
|
|
|
assert!(block.len() == 1);
|
2021-09-03 02:15:01 +00:00
|
|
|
match &block[0] {
|
2021-09-03 07:35:29 +00:00
|
|
|
Statement::Pipeline(Pipeline { expressions }) => {
|
2021-09-03 02:15:01 +00:00
|
|
|
assert!(expressions.len() == 1);
|
|
|
|
assert!(matches!(
|
|
|
|
expressions[0],
|
|
|
|
Expression {
|
|
|
|
expr: Expr::Int(3),
|
|
|
|
..
|
|
|
|
}
|
|
|
|
))
|
|
|
|
}
|
|
|
|
_ => panic!("No match"),
|
|
|
|
}
|
2021-08-30 18:36:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn parse_call() {
|
2021-09-02 08:25:22 +00:00
|
|
|
let engine_state = EngineState::new();
|
|
|
|
let mut working_set = StateWorkingSet::new(&engine_state);
|
2021-08-30 18:36:07 +00:00
|
|
|
|
|
|
|
let sig = Signature::build("foo").named("--jazz", SyntaxShape::Int, "jazz!!", Some('j'));
|
2021-09-02 18:21:37 +00:00
|
|
|
working_set.add_decl(sig.predeclare());
|
2021-08-30 18:36:07 +00:00
|
|
|
|
2021-09-02 18:21:37 +00:00
|
|
|
let (block, err) = parse_source(&mut working_set, b"foo", true);
|
2021-08-30 18:36:07 +00:00
|
|
|
|
|
|
|
assert!(err.is_none());
|
|
|
|
assert!(block.len() == 1);
|
|
|
|
|
|
|
|
match &block[0] {
|
2021-09-03 02:15:01 +00:00
|
|
|
Statement::Pipeline(Pipeline { expressions }) => {
|
|
|
|
assert_eq!(expressions.len(), 1);
|
|
|
|
|
|
|
|
if let Expression {
|
|
|
|
expr: Expr::Call(call),
|
|
|
|
..
|
|
|
|
} = &expressions[0]
|
|
|
|
{
|
|
|
|
assert_eq!(call.decl_id, 0);
|
|
|
|
}
|
2021-08-30 18:36:07 +00:00
|
|
|
}
|
|
|
|
_ => panic!("not a call"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn parse_call_missing_flag_arg() {
|
2021-09-02 08:25:22 +00:00
|
|
|
let engine_state = EngineState::new();
|
|
|
|
let mut working_set = StateWorkingSet::new(&engine_state);
|
2021-08-30 18:36:07 +00:00
|
|
|
|
|
|
|
let sig = Signature::build("foo").named("--jazz", SyntaxShape::Int, "jazz!!", Some('j'));
|
2021-09-02 18:21:37 +00:00
|
|
|
working_set.add_decl(sig.predeclare());
|
2021-08-30 18:36:07 +00:00
|
|
|
|
2021-09-02 18:21:37 +00:00
|
|
|
let (_, err) = parse_source(&mut working_set, b"foo --jazz", true);
|
2021-08-30 18:36:07 +00:00
|
|
|
assert!(matches!(err, Some(ParseError::MissingFlagParam(..))));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn parse_call_missing_short_flag_arg() {
|
2021-09-02 08:25:22 +00:00
|
|
|
let engine_state = EngineState::new();
|
|
|
|
let mut working_set = StateWorkingSet::new(&engine_state);
|
2021-08-30 18:36:07 +00:00
|
|
|
|
|
|
|
let sig = Signature::build("foo").named("--jazz", SyntaxShape::Int, "jazz!!", Some('j'));
|
2021-09-02 18:21:37 +00:00
|
|
|
working_set.add_decl(sig.predeclare());
|
2021-08-30 18:36:07 +00:00
|
|
|
|
2021-09-02 18:21:37 +00:00
|
|
|
let (_, err) = parse_source(&mut working_set, b"foo -j", true);
|
2021-08-30 18:36:07 +00:00
|
|
|
assert!(matches!(err, Some(ParseError::MissingFlagParam(..))));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn parse_call_too_many_shortflag_args() {
|
2021-09-02 08:25:22 +00:00
|
|
|
let engine_state = EngineState::new();
|
|
|
|
let mut working_set = StateWorkingSet::new(&engine_state);
|
2021-08-30 18:36:07 +00:00
|
|
|
|
|
|
|
let sig = Signature::build("foo")
|
|
|
|
.named("--jazz", SyntaxShape::Int, "jazz!!", Some('j'))
|
|
|
|
.named("--math", SyntaxShape::Int, "math!!", Some('m'));
|
2021-09-02 18:21:37 +00:00
|
|
|
working_set.add_decl(sig.predeclare());
|
|
|
|
let (_, err) = parse_source(&mut working_set, b"foo -mj", true);
|
2021-08-30 18:36:07 +00:00
|
|
|
assert!(matches!(
|
|
|
|
err,
|
|
|
|
Some(ParseError::ShortFlagBatchCantTakeArg(..))
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn parse_call_unknown_shorthand() {
|
2021-09-02 08:25:22 +00:00
|
|
|
let engine_state = EngineState::new();
|
|
|
|
let mut working_set = StateWorkingSet::new(&engine_state);
|
2021-08-30 18:36:07 +00:00
|
|
|
|
|
|
|
let sig = Signature::build("foo").switch("--jazz", "jazz!!", Some('j'));
|
2021-09-02 18:21:37 +00:00
|
|
|
working_set.add_decl(sig.predeclare());
|
|
|
|
let (_, err) = parse_source(&mut working_set, b"foo -mj", true);
|
2021-08-30 18:36:07 +00:00
|
|
|
assert!(matches!(err, Some(ParseError::UnknownFlag(..))));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn parse_call_extra_positional() {
|
2021-09-02 08:25:22 +00:00
|
|
|
let engine_state = EngineState::new();
|
|
|
|
let mut working_set = StateWorkingSet::new(&engine_state);
|
2021-08-30 18:36:07 +00:00
|
|
|
|
|
|
|
let sig = Signature::build("foo").switch("--jazz", "jazz!!", Some('j'));
|
2021-09-02 18:21:37 +00:00
|
|
|
working_set.add_decl(sig.predeclare());
|
|
|
|
let (_, err) = parse_source(&mut working_set, b"foo -j 100", true);
|
2021-08-30 18:36:07 +00:00
|
|
|
assert!(matches!(err, Some(ParseError::ExtraPositional(..))));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn parse_call_missing_req_positional() {
|
2021-09-02 08:25:22 +00:00
|
|
|
let engine_state = EngineState::new();
|
|
|
|
let mut working_set = StateWorkingSet::new(&engine_state);
|
2021-08-30 18:36:07 +00:00
|
|
|
|
|
|
|
let sig = Signature::build("foo").required("jazz", SyntaxShape::Int, "jazz!!");
|
2021-09-02 18:21:37 +00:00
|
|
|
working_set.add_decl(sig.predeclare());
|
|
|
|
let (_, err) = parse_source(&mut working_set, b"foo", true);
|
2021-08-30 18:36:07 +00:00
|
|
|
assert!(matches!(err, Some(ParseError::MissingPositional(..))));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn parse_call_missing_req_flag() {
|
2021-09-02 08:25:22 +00:00
|
|
|
let engine_state = EngineState::new();
|
|
|
|
let mut working_set = StateWorkingSet::new(&engine_state);
|
2021-08-30 18:36:07 +00:00
|
|
|
|
|
|
|
let sig = Signature::build("foo").required_named("--jazz", SyntaxShape::Int, "jazz!!", None);
|
2021-09-02 18:21:37 +00:00
|
|
|
working_set.add_decl(sig.predeclare());
|
|
|
|
let (_, err) = parse_source(&mut working_set, b"foo", true);
|
2021-08-30 18:36:07 +00:00
|
|
|
assert!(matches!(err, Some(ParseError::MissingRequiredFlag(..))));
|
|
|
|
}
|
2021-09-04 22:25:31 +00:00
|
|
|
|
|
|
|
mod range {
|
|
|
|
use super::*;
|
|
|
|
use nu_protocol::ast::{RangeInclusion, RangeOperator};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_inclusive_range() {
|
|
|
|
let engine_state = EngineState::new();
|
|
|
|
let mut working_set = StateWorkingSet::new(&engine_state);
|
|
|
|
|
|
|
|
let (block, err) = parse_source(&mut working_set, b"0..10", true);
|
|
|
|
|
|
|
|
assert!(err.is_none());
|
|
|
|
assert!(block.len() == 1);
|
|
|
|
match &block[0] {
|
|
|
|
Statement::Pipeline(Pipeline { expressions }) => {
|
|
|
|
assert!(expressions.len() == 1);
|
|
|
|
assert!(matches!(
|
|
|
|
expressions[0],
|
|
|
|
Expression {
|
|
|
|
expr: Expr::Range(
|
|
|
|
Some(_),
|
|
|
|
Some(_),
|
|
|
|
RangeOperator {
|
|
|
|
inclusion: RangeInclusion::Inclusive,
|
|
|
|
..
|
|
|
|
}
|
|
|
|
),
|
|
|
|
..
|
|
|
|
}
|
|
|
|
))
|
|
|
|
}
|
|
|
|
_ => panic!("No match"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_exclusive_range() {
|
|
|
|
let engine_state = EngineState::new();
|
|
|
|
let mut working_set = StateWorkingSet::new(&engine_state);
|
|
|
|
|
|
|
|
let (block, err) = parse_source(&mut working_set, b"0..<10", true);
|
|
|
|
|
|
|
|
assert!(err.is_none());
|
|
|
|
assert!(block.len() == 1);
|
|
|
|
match &block[0] {
|
|
|
|
Statement::Pipeline(Pipeline { expressions }) => {
|
|
|
|
assert!(expressions.len() == 1);
|
|
|
|
assert!(matches!(
|
|
|
|
expressions[0],
|
|
|
|
Expression {
|
|
|
|
expr: Expr::Range(
|
|
|
|
Some(_),
|
|
|
|
Some(_),
|
|
|
|
RangeOperator {
|
|
|
|
inclusion: RangeInclusion::RightExclusive,
|
|
|
|
..
|
|
|
|
}
|
|
|
|
),
|
|
|
|
..
|
|
|
|
}
|
|
|
|
))
|
|
|
|
}
|
|
|
|
_ => panic!("No match"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_subexpression_range() {
|
|
|
|
let engine_state = EngineState::new();
|
|
|
|
let mut working_set = StateWorkingSet::new(&engine_state);
|
|
|
|
|
|
|
|
let (block, err) = parse_source(&mut working_set, b"(3 - 3)..<(8 + 2)", true);
|
|
|
|
|
|
|
|
assert!(err.is_none());
|
|
|
|
assert!(block.len() == 1);
|
|
|
|
match &block[0] {
|
|
|
|
Statement::Pipeline(Pipeline { expressions }) => {
|
|
|
|
assert!(expressions.len() == 1);
|
|
|
|
assert!(matches!(
|
|
|
|
expressions[0],
|
|
|
|
Expression {
|
|
|
|
expr: Expr::Range(
|
|
|
|
Some(_),
|
|
|
|
Some(_),
|
|
|
|
RangeOperator {
|
|
|
|
inclusion: RangeInclusion::RightExclusive,
|
|
|
|
..
|
|
|
|
}
|
|
|
|
),
|
|
|
|
..
|
|
|
|
}
|
|
|
|
))
|
|
|
|
}
|
|
|
|
_ => panic!("No match"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_variable_range() {
|
|
|
|
let engine_state = EngineState::new();
|
|
|
|
let mut working_set = StateWorkingSet::new(&engine_state);
|
|
|
|
|
|
|
|
let (block, err) = parse_source(&mut working_set, b"let a = 2; $a..10", true);
|
|
|
|
|
|
|
|
assert!(err.is_none());
|
|
|
|
assert!(block.len() == 2);
|
|
|
|
match &block[1] {
|
|
|
|
Statement::Pipeline(Pipeline { expressions }) => {
|
|
|
|
assert!(expressions.len() == 1);
|
|
|
|
assert!(matches!(
|
|
|
|
expressions[0],
|
|
|
|
Expression {
|
|
|
|
expr: Expr::Range(
|
|
|
|
Some(_),
|
|
|
|
Some(_),
|
|
|
|
RangeOperator {
|
|
|
|
inclusion: RangeInclusion::Inclusive,
|
|
|
|
..
|
|
|
|
}
|
|
|
|
),
|
|
|
|
..
|
|
|
|
}
|
|
|
|
))
|
|
|
|
}
|
|
|
|
_ => panic!("No match"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_subexpression_variable_range() {
|
|
|
|
let engine_state = EngineState::new();
|
|
|
|
let mut working_set = StateWorkingSet::new(&engine_state);
|
|
|
|
|
|
|
|
let (block, err) = parse_source(&mut working_set, b"let a = 2; $a..<($a + 10)", true);
|
|
|
|
|
|
|
|
assert!(err.is_none());
|
|
|
|
assert!(block.len() == 2);
|
|
|
|
match &block[1] {
|
|
|
|
Statement::Pipeline(Pipeline { expressions }) => {
|
|
|
|
assert!(expressions.len() == 1);
|
|
|
|
assert!(matches!(
|
|
|
|
expressions[0],
|
|
|
|
Expression {
|
|
|
|
expr: Expr::Range(
|
|
|
|
Some(_),
|
|
|
|
Some(_),
|
|
|
|
RangeOperator {
|
|
|
|
inclusion: RangeInclusion::RightExclusive,
|
|
|
|
..
|
|
|
|
}
|
|
|
|
),
|
|
|
|
..
|
|
|
|
}
|
|
|
|
))
|
|
|
|
}
|
|
|
|
_ => panic!("No match"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_left_unbounded_range() {
|
|
|
|
let engine_state = EngineState::new();
|
|
|
|
let mut working_set = StateWorkingSet::new(&engine_state);
|
|
|
|
|
|
|
|
let (block, err) = parse_source(&mut working_set, b"..10", true);
|
|
|
|
|
|
|
|
assert!(err.is_none());
|
|
|
|
assert!(block.len() == 1);
|
|
|
|
match &block[0] {
|
|
|
|
Statement::Pipeline(Pipeline { expressions }) => {
|
|
|
|
assert!(expressions.len() == 1);
|
|
|
|
assert!(matches!(
|
|
|
|
expressions[0],
|
|
|
|
Expression {
|
|
|
|
expr: Expr::Range(
|
|
|
|
None,
|
|
|
|
Some(_),
|
|
|
|
RangeOperator {
|
|
|
|
inclusion: RangeInclusion::Inclusive,
|
|
|
|
..
|
|
|
|
}
|
|
|
|
),
|
|
|
|
..
|
|
|
|
}
|
|
|
|
))
|
|
|
|
}
|
|
|
|
_ => panic!("No match"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_right_unbounded_range() {
|
|
|
|
let engine_state = EngineState::new();
|
|
|
|
let mut working_set = StateWorkingSet::new(&engine_state);
|
|
|
|
|
|
|
|
let (block, err) = parse_source(&mut working_set, b"0..", true);
|
|
|
|
|
|
|
|
assert!(err.is_none());
|
|
|
|
assert!(block.len() == 1);
|
|
|
|
match &block[0] {
|
|
|
|
Statement::Pipeline(Pipeline { expressions }) => {
|
|
|
|
assert!(expressions.len() == 1);
|
|
|
|
assert!(matches!(
|
|
|
|
expressions[0],
|
|
|
|
Expression {
|
|
|
|
expr: Expr::Range(
|
|
|
|
Some(_),
|
|
|
|
None,
|
|
|
|
RangeOperator {
|
|
|
|
inclusion: RangeInclusion::Inclusive,
|
|
|
|
..
|
|
|
|
}
|
|
|
|
),
|
|
|
|
..
|
|
|
|
}
|
|
|
|
))
|
|
|
|
}
|
|
|
|
_ => panic!("No match"),
|
|
|
|
}
|
|
|
|
}
|
2021-09-05 17:33:53 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_negative_range() {
|
|
|
|
let engine_state = EngineState::new();
|
|
|
|
let mut working_set = StateWorkingSet::new(&engine_state);
|
|
|
|
|
|
|
|
let (block, err) = parse_source(&mut working_set, b"-10..-3", true);
|
|
|
|
|
|
|
|
assert!(err.is_none());
|
|
|
|
assert!(block.len() == 1);
|
|
|
|
match &block[0] {
|
|
|
|
Statement::Pipeline(Pipeline { expressions }) => {
|
|
|
|
assert!(expressions.len() == 1);
|
|
|
|
assert!(matches!(
|
|
|
|
expressions[0],
|
|
|
|
Expression {
|
|
|
|
expr: Expr::Range(
|
|
|
|
Some(_),
|
|
|
|
Some(_),
|
|
|
|
RangeOperator {
|
|
|
|
inclusion: RangeInclusion::Inclusive,
|
|
|
|
..
|
|
|
|
}
|
|
|
|
),
|
|
|
|
..
|
|
|
|
}
|
|
|
|
))
|
|
|
|
}
|
|
|
|
_ => panic!("No match"),
|
|
|
|
}
|
|
|
|
}
|
2021-09-04 22:25:31 +00:00
|
|
|
}
|