From 1f0962eb08b91ffebcf4329a4c4614fd443d16e5 Mon Sep 17 00:00:00 2001 From: Philip Peterson Date: Sun, 19 Jul 2020 00:12:56 -0700 Subject: [PATCH] Add some tests for `parse_arg` (#2220) * add some tests for parse * Format * fix warnings --- crates/nu-parser/src/parse.rs | 71 +++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/crates/nu-parser/src/parse.rs b/crates/nu-parser/src/parse.rs index a746b662d9..aedb288c68 100644 --- a/crates/nu-parser/src/parse.rs +++ b/crates/nu-parser/src/parse.rs @@ -734,6 +734,77 @@ fn parse_arg( } } +#[cfg(test)] +mod test { + use super::*; + + #[derive(Clone, Debug)] + struct MockRegistry {} + + impl MockRegistry { + fn new() -> Self { + MockRegistry {} + } + } + + impl SignatureRegistry for MockRegistry { + fn has(&self, _name: &str) -> bool { + false + } + + fn get(&self, _name: &str) -> Option { + None + } + + fn clone_box(&self) -> Box { + Box::new(self.clone()) + } + } + + #[test] + fn parse_integer() -> Result<(), ParseError> { + let raw = "32".to_string(); + let input = raw.clone().spanned(Span::new(0, raw.len())); + let registry = MockRegistry::new(); + let result = parse_arg(SyntaxShape::Int, ®istry, &input); + assert_eq!(result.1, None); + assert_eq!(result.0.expr, Expression::integer(32)); + Ok(()) + } + + #[test] + fn parse_number() -> Result<(), ParseError> { + let raw = "-32.2".to_string(); + let input = raw.clone().spanned(Span::new(0, raw.len())); + let registry = MockRegistry::new(); + let result = parse_arg(SyntaxShape::Number, ®istry, &input); + assert_eq!(result.1, None); + assert_eq!(result.0.expr, Expression::decimal(-32.2)); + + let raw = "32.2".to_string(); + let input = raw.clone().spanned(Span::new(0, raw.len())); + let registry = MockRegistry::new(); + let result = parse_arg(SyntaxShape::Number, ®istry, &input); + assert_eq!(result.1, None); + assert_eq!(result.0.expr, Expression::decimal(32.2)); + + let raw = "-34".to_string(); + let input = raw.clone().spanned(Span::new(0, raw.len())); + let registry = MockRegistry::new(); + let result = parse_arg(SyntaxShape::Number, ®istry, &input); + assert_eq!(result.1, None); + assert_eq!(result.0.expr, Expression::integer(-34)); + + let raw = "34".to_string(); + let input = raw.clone().spanned(Span::new(0, raw.len())); + let registry = MockRegistry::new(); + let result = parse_arg(SyntaxShape::Number, ®istry, &input); + assert_eq!(result.1, None); + assert_eq!(result.0.expr, Expression::integer(34)); + Ok(()) + } +} + /// Match the available flags in a signature with what the user provided. This will check both long-form flags (--full) and shorthand flags (-f) /// This also allows users to provide a group of shorthand flags (-af) that correspond to multiple shorthand flags at once. fn get_flags_from_flag(