Implemented an abstraction for extracting flags from builtins
This commit is contained in:
@@ -17,7 +17,7 @@ fn simple_expansion() {
|
||||
let var_tk = tokens.pop().unwrap();
|
||||
|
||||
let var_span = var_tk.span.clone();
|
||||
let exp_tk = var_tk.expand(var_span, TkFlags::empty());
|
||||
let exp_tk = var_tk.expand(var_span, TkFlags::empty()).unwrap();
|
||||
write_vars(|v| v.vars_mut().clear());
|
||||
insta::assert_debug_snapshot!(exp_tk.get_words())
|
||||
}
|
||||
@@ -62,6 +62,16 @@ fn expand_multiple_aliases() {
|
||||
assert_eq!(result.as_str(),"echo foo; echo bar; echo biz")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alias_in_arg_position() {
|
||||
write_logic(|l| l.insert_alias("foo", "echo foo"));
|
||||
|
||||
let input = String::from("echo foo");
|
||||
|
||||
let result = expand_aliases(input.clone(), HashSet::new());
|
||||
assert_eq!(input,result)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn expand_recursive_alias() {
|
||||
write_logic(|l| l.insert_alias("foo", "echo foo"));
|
||||
@@ -74,9 +84,9 @@ fn expand_recursive_alias() {
|
||||
|
||||
#[test]
|
||||
fn test_infinite_recursive_alias() {
|
||||
write_logic(|l| l.insert_alias("foo", "foo"));
|
||||
write_logic(|l| l.insert_alias("foo", "foo bar"));
|
||||
|
||||
let input = String::from("foo");
|
||||
let result = expand_aliases(input, HashSet::new());
|
||||
assert_eq!(result.as_str(),"foo")
|
||||
assert_eq!(result.as_str(),"foo bar")
|
||||
}
|
||||
|
||||
37
src/tests/getopt.rs
Normal file
37
src/tests/getopt.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use getopt::get_opts_from_tokens;
|
||||
use parse::NdRule;
|
||||
use tests::get_nodes;
|
||||
|
||||
use super::super::*;
|
||||
|
||||
#[test]
|
||||
fn getopt_from_argv() {
|
||||
let node = get_nodes("echo -n -e foo", |node| matches!(node.class, NdRule::Command {..}))
|
||||
.pop()
|
||||
.unwrap();
|
||||
let NdRule::Command { assignments, argv } = node.class else {
|
||||
panic!()
|
||||
};
|
||||
|
||||
let (words,opts) = get_opts_from_tokens(argv);
|
||||
insta::assert_debug_snapshot!(words);
|
||||
insta::assert_debug_snapshot!(opts)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn getopt_simple() {
|
||||
let raw = "echo -n foo".split_whitespace().map(|s| s.to_string()).collect::<Vec<_>>();
|
||||
|
||||
let (words,opts) = get_opts(raw);
|
||||
insta::assert_debug_snapshot!(words);
|
||||
insta::assert_debug_snapshot!(opts);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn getopt_multiple_short() {
|
||||
let raw = "echo -nre foo".split_whitespace().map(|s| s.to_string()).collect::<Vec<_>>();
|
||||
|
||||
let (words,opts) = get_opts(raw);
|
||||
insta::assert_debug_snapshot!(words);
|
||||
insta::assert_debug_snapshot!(opts);
|
||||
}
|
||||
@@ -1,5 +1,32 @@
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::parse::{lex::{LexFlags, LexStream}, node_operation, Node, ParseStream};
|
||||
|
||||
pub mod lexer;
|
||||
pub mod parser;
|
||||
pub mod expand;
|
||||
pub mod term;
|
||||
pub mod error;
|
||||
pub mod getopt;
|
||||
|
||||
/// Unsafe to use outside of tests
|
||||
pub fn get_nodes<F1>(input: &str, filter: F1) -> Vec<Node>
|
||||
where
|
||||
F1: Fn(&Node) -> bool
|
||||
{
|
||||
let mut nodes = vec![];
|
||||
let tokens = LexStream::new(Rc::new(input.into()), LexFlags::empty())
|
||||
.map(|tk| tk.unwrap())
|
||||
.collect::<Vec<_>>();
|
||||
let mut parsed_nodes = ParseStream::new(tokens)
|
||||
.map(|nd| nd.unwrap())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
for node in parsed_nodes.iter_mut() {
|
||||
node_operation(node,
|
||||
&filter,
|
||||
&mut |node: &mut Node| nodes.push(node.clone())
|
||||
);
|
||||
}
|
||||
nodes
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use parse::{node_operation, NdRule, Node};
|
||||
|
||||
use super::super::*;
|
||||
|
||||
#[test]
|
||||
@@ -196,3 +198,22 @@ fn parse_cursed() {
|
||||
// 15,000 line snapshot file btw
|
||||
insta::assert_debug_snapshot!(nodes)
|
||||
}
|
||||
#[test]
|
||||
fn test_node_operation() {
|
||||
let input = String::from("echo hello world; echo foo bar");
|
||||
let mut check_nodes = vec![];
|
||||
let mut tokens: Vec<Tk> = LexStream::new(input.into(), LexFlags::empty())
|
||||
.map(|tk| tk.unwrap())
|
||||
.collect();
|
||||
|
||||
let nodes = ParseStream::new(tokens)
|
||||
.map(|nd| nd.unwrap());
|
||||
|
||||
for mut node in nodes {
|
||||
node_operation(&mut node,
|
||||
&|node: &Node| matches!(node.class, NdRule::Command {..}),
|
||||
&mut |node: &mut Node| check_nodes.push(node.clone()),
|
||||
);
|
||||
}
|
||||
insta::assert_debug_snapshot!(check_nodes)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
---
|
||||
source: src/tests/getopt.rs
|
||||
expression: opts
|
||||
---
|
||||
[
|
||||
Short(
|
||||
'n',
|
||||
),
|
||||
Short(
|
||||
'e',
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,26 @@
|
||||
---
|
||||
source: src/tests/getopt.rs
|
||||
expression: words
|
||||
---
|
||||
[
|
||||
Tk {
|
||||
class: Str,
|
||||
span: Span {
|
||||
range: 0..4,
|
||||
source: "echo -n -e foo",
|
||||
},
|
||||
flags: TkFlags(
|
||||
IS_CMD | BUILTIN,
|
||||
),
|
||||
},
|
||||
Tk {
|
||||
class: Str,
|
||||
span: Span {
|
||||
range: 11..14,
|
||||
source: "echo -n -e foo",
|
||||
},
|
||||
flags: TkFlags(
|
||||
0x0,
|
||||
),
|
||||
},
|
||||
]
|
||||
@@ -0,0 +1,15 @@
|
||||
---
|
||||
source: src/tests/getopt.rs
|
||||
expression: opts
|
||||
---
|
||||
[
|
||||
Short(
|
||||
'n',
|
||||
),
|
||||
Short(
|
||||
'r',
|
||||
),
|
||||
Short(
|
||||
'e',
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,8 @@
|
||||
---
|
||||
source: src/tests/getopt.rs
|
||||
expression: words
|
||||
---
|
||||
[
|
||||
"echo",
|
||||
"foo",
|
||||
]
|
||||
@@ -0,0 +1,9 @@
|
||||
---
|
||||
source: src/tests/getopt.rs
|
||||
expression: opts
|
||||
---
|
||||
[
|
||||
Short(
|
||||
'n',
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,8 @@
|
||||
---
|
||||
source: src/tests/getopt.rs
|
||||
expression: words
|
||||
---
|
||||
[
|
||||
"echo",
|
||||
"foo",
|
||||
]
|
||||
162
src/tests/snapshots/fern__tests__parser__node_operation.snap
Normal file
162
src/tests/snapshots/fern__tests__parser__node_operation.snap
Normal file
@@ -0,0 +1,162 @@
|
||||
---
|
||||
source: src/tests/parser.rs
|
||||
expression: check_nodes
|
||||
---
|
||||
[
|
||||
Node {
|
||||
class: Command {
|
||||
assignments: [],
|
||||
argv: [
|
||||
Tk {
|
||||
class: Str,
|
||||
span: Span {
|
||||
range: 0..4,
|
||||
source: "echo hello world; echo foo bar",
|
||||
},
|
||||
flags: TkFlags(
|
||||
IS_CMD | BUILTIN,
|
||||
),
|
||||
},
|
||||
Tk {
|
||||
class: Str,
|
||||
span: Span {
|
||||
range: 5..10,
|
||||
source: "echo hello world; echo foo bar",
|
||||
},
|
||||
flags: TkFlags(
|
||||
0x0,
|
||||
),
|
||||
},
|
||||
Tk {
|
||||
class: Str,
|
||||
span: Span {
|
||||
range: 11..16,
|
||||
source: "echo hello world; echo foo bar",
|
||||
},
|
||||
flags: TkFlags(
|
||||
0x0,
|
||||
),
|
||||
},
|
||||
],
|
||||
},
|
||||
flags: NdFlags(
|
||||
0x0,
|
||||
),
|
||||
redirs: [],
|
||||
tokens: [
|
||||
Tk {
|
||||
class: Str,
|
||||
span: Span {
|
||||
range: 0..4,
|
||||
source: "echo hello world; echo foo bar",
|
||||
},
|
||||
flags: TkFlags(
|
||||
IS_CMD | BUILTIN,
|
||||
),
|
||||
},
|
||||
Tk {
|
||||
class: Str,
|
||||
span: Span {
|
||||
range: 5..10,
|
||||
source: "echo hello world; echo foo bar",
|
||||
},
|
||||
flags: TkFlags(
|
||||
0x0,
|
||||
),
|
||||
},
|
||||
Tk {
|
||||
class: Str,
|
||||
span: Span {
|
||||
range: 11..16,
|
||||
source: "echo hello world; echo foo bar",
|
||||
},
|
||||
flags: TkFlags(
|
||||
0x0,
|
||||
),
|
||||
},
|
||||
Tk {
|
||||
class: Sep,
|
||||
span: Span {
|
||||
range: 16..18,
|
||||
source: "echo hello world; echo foo bar",
|
||||
},
|
||||
flags: TkFlags(
|
||||
0x0,
|
||||
),
|
||||
},
|
||||
],
|
||||
},
|
||||
Node {
|
||||
class: Command {
|
||||
assignments: [],
|
||||
argv: [
|
||||
Tk {
|
||||
class: Str,
|
||||
span: Span {
|
||||
range: 18..22,
|
||||
source: "echo hello world; echo foo bar",
|
||||
},
|
||||
flags: TkFlags(
|
||||
IS_CMD | BUILTIN,
|
||||
),
|
||||
},
|
||||
Tk {
|
||||
class: Str,
|
||||
span: Span {
|
||||
range: 23..26,
|
||||
source: "echo hello world; echo foo bar",
|
||||
},
|
||||
flags: TkFlags(
|
||||
0x0,
|
||||
),
|
||||
},
|
||||
Tk {
|
||||
class: Str,
|
||||
span: Span {
|
||||
range: 27..30,
|
||||
source: "echo hello world; echo foo bar",
|
||||
},
|
||||
flags: TkFlags(
|
||||
0x0,
|
||||
),
|
||||
},
|
||||
],
|
||||
},
|
||||
flags: NdFlags(
|
||||
0x0,
|
||||
),
|
||||
redirs: [],
|
||||
tokens: [
|
||||
Tk {
|
||||
class: Str,
|
||||
span: Span {
|
||||
range: 18..22,
|
||||
source: "echo hello world; echo foo bar",
|
||||
},
|
||||
flags: TkFlags(
|
||||
IS_CMD | BUILTIN,
|
||||
),
|
||||
},
|
||||
Tk {
|
||||
class: Str,
|
||||
span: Span {
|
||||
range: 23..26,
|
||||
source: "echo hello world; echo foo bar",
|
||||
},
|
||||
flags: TkFlags(
|
||||
0x0,
|
||||
),
|
||||
},
|
||||
Tk {
|
||||
class: Str,
|
||||
span: Span {
|
||||
range: 27..30,
|
||||
source: "echo hello world; echo foo bar",
|
||||
},
|
||||
flags: TkFlags(
|
||||
0x0,
|
||||
),
|
||||
},
|
||||
],
|
||||
},
|
||||
]
|
||||
Reference in New Issue
Block a user