Re-implemented aliases

This commit is contained in:
2025-03-18 21:50:53 -04:00
parent 0d286ba006
commit 19dee4bcd6
12 changed files with 314 additions and 55 deletions

View File

@@ -1,6 +1,8 @@
use expand::unescape_str;
use std::collections::HashSet;
use expand::{expand_aliases, unescape_str};
use parse::lex::{Tk, TkFlags, TkRule};
use state::write_vars;
use state::{write_logic, write_vars};
use super::super::*;
#[test]
@@ -8,7 +10,7 @@ fn simple_expansion() {
let varsub = "$foo";
write_vars(|v| v.new_var("foo", "this is the value of the variable".into()));
let mut tokens: Vec<Tk> = LexStream::new(varsub, LexFlags::empty())
let mut tokens: Vec<Tk> = LexStream::new(Rc::new(varsub.to_string()), LexFlags::empty())
.map(|tk| tk.unwrap())
.filter(|tk| !matches!(tk.class, TkRule::EOI | TkRule::SOI))
.collect();
@@ -27,3 +29,54 @@ fn unescape_string() {
insta::assert_snapshot!(unescaped)
}
#[test]
fn expand_alias_simple() {
write_logic(|l| l.insert_alias("foo", "echo foo"));
let input = String::from("foo");
let result = expand_aliases(input, HashSet::new());
assert_eq!(result.as_str(),"echo foo")
}
#[test]
fn expand_alias_in_if() {
write_logic(|l| l.insert_alias("foo", "echo foo"));
let input = String::from("if foo; then echo bar; fi");
let result = expand_aliases(input, HashSet::new());
assert_eq!(result.as_str(),"if echo foo; then echo bar; fi")
}
#[test]
fn expand_multiple_aliases() {
write_logic(|l| l.insert_alias("foo", "echo foo"));
write_logic(|l| l.insert_alias("bar", "echo bar"));
write_logic(|l| l.insert_alias("biz", "echo biz"));
let input = String::from("foo; bar; biz");
let result = expand_aliases(input, HashSet::new());
assert_eq!(result.as_str(),"echo foo; echo bar; echo biz")
}
#[test]
fn expand_recursive_alias() {
write_logic(|l| l.insert_alias("foo", "echo foo"));
write_logic(|l| l.insert_alias("bar", "foo bar"));
let input = String::from("bar");
let result = expand_aliases(input, HashSet::new());
assert_eq!(result.as_str(),"echo foo bar")
}
#[test]
fn test_infinite_recursive_alias() {
write_logic(|l| l.insert_alias("foo", "foo"));
let input = String::from("foo");
let result = expand_aliases(input, HashSet::new());
assert_eq!(result.as_str(),"foo")
}

View File

@@ -2,35 +2,35 @@ use super::super::*;
#[test]
fn lex_simple() {
let input = "echo hello world";
let tokens: Vec<_> = LexStream::new(input, LexFlags::empty()).collect();
let tokens: Vec<_> = LexStream::new(Rc::new(input.to_string()), LexFlags::empty()).collect();
insta::assert_debug_snapshot!(tokens)
}
#[test]
fn lex_redir() {
let input = "echo foo > bar.txt";
let tokens: Vec<_> = LexStream::new(input, LexFlags::empty()).collect();
let tokens: Vec<_> = LexStream::new(Rc::new(input.to_string()), LexFlags::empty()).collect();
insta::assert_debug_snapshot!(tokens)
}
#[test]
fn lex_redir_fds() {
let input = "echo foo 1>&2";
let tokens: Vec<_> = LexStream::new(input, LexFlags::empty()).collect();
let tokens: Vec<_> = LexStream::new(Rc::new(input.to_string()), LexFlags::empty()).collect();
insta::assert_debug_snapshot!(tokens)
}
#[test]
fn lex_quote_str() {
let input = "echo \"foo bar\" biz baz";
let tokens: Vec<_> = LexStream::new(input, LexFlags::empty()).collect();
let tokens: Vec<_> = LexStream::new(Rc::new(input.to_string()), LexFlags::empty()).collect();
insta::assert_debug_snapshot!(tokens)
}
#[test]
fn lex_with_keywords() {
let input = "if true; then echo foo; fi";
let tokens: Vec<_> = LexStream::new(input, LexFlags::empty()).collect();
let tokens: Vec<_> = LexStream::new(Rc::new(input.to_string()), LexFlags::empty()).collect();
insta::assert_debug_snapshot!(tokens)
}
@@ -38,7 +38,7 @@ fn lex_with_keywords() {
#[test]
fn lex_multiline() {
let input = "echo hello world\necho foo bar\necho boo biz";
let tokens: Vec<_> = LexStream::new(input, LexFlags::empty()).collect();
let tokens: Vec<_> = LexStream::new(Rc::new(input.to_string()), LexFlags::empty()).collect();
insta::assert_debug_snapshot!(tokens)
}

View File

@@ -3,7 +3,7 @@ use super::super::*;
#[test]
fn parse_simple() {
let input = "echo hello world";
let tk_stream: Vec<_> = LexStream::new(input, LexFlags::empty())
let tk_stream: Vec<_> = LexStream::new(Rc::new(input.to_string()), LexFlags::empty())
.map(|tk| tk.unwrap())
.collect();
let nodes: Vec<_> = ParseStream::new(tk_stream).collect();
@@ -14,7 +14,7 @@ fn parse_simple() {
#[test]
fn parse_pipeline() {
let input = "echo foo | sed s/foo/bar";
let tk_stream: Vec<_> = LexStream::new(input, LexFlags::empty())
let tk_stream: Vec<_> = LexStream::new(Rc::new(input.to_string()), LexFlags::empty())
.map(|tk| tk.unwrap())
.collect();
let nodes: Vec<_> = ParseStream::new(tk_stream).collect();
@@ -25,7 +25,7 @@ fn parse_pipeline() {
#[test]
fn parse_conjunction() {
let input = "echo foo && echo bar";
let tk_stream: Vec<_> = LexStream::new(input, LexFlags::empty())
let tk_stream: Vec<_> = LexStream::new(Rc::new(input.to_string()), LexFlags::empty())
.map(|tk| tk.unwrap())
.collect();
let nodes: Vec<_> = ParseStream::new(tk_stream).collect();
@@ -36,7 +36,7 @@ fn parse_conjunction() {
#[test]
fn parse_conjunction_and_pipeline() {
let input = "echo foo | sed s/foo/bar/ && echo bar | sed s/bar/foo/ || echo foo bar | sed s/foo bar/bar foo/";
let tk_stream: Vec<_> = LexStream::new(input, LexFlags::empty())
let tk_stream: Vec<_> = LexStream::new(Rc::new(input.to_string()), LexFlags::empty())
.map(|tk| tk.unwrap())
.collect();
let nodes: Vec<_> = ParseStream::new(tk_stream).collect();
@@ -50,7 +50,7 @@ fn parse_multiline() {
echo hello world
echo foo bar
echo boo biz";
let tk_stream: Vec<_> = LexStream::new(input, LexFlags::empty())
let tk_stream: Vec<_> = LexStream::new(Rc::new(input.to_string()), LexFlags::empty())
.map(|tk| tk.unwrap())
.collect();
let nodes: Vec<_> = ParseStream::new(tk_stream).collect();
@@ -61,7 +61,7 @@ echo boo biz";
#[test]
fn parse_if_simple() {
let input = "if foo; then echo bar; fi";
let tk_stream: Vec<_> = LexStream::new(input, LexFlags::empty())
let tk_stream: Vec<_> = LexStream::new(Rc::new(input.to_string()), LexFlags::empty())
.map(|tk| tk.unwrap())
.collect();
let nodes: Vec<_> = ParseStream::new(tk_stream).collect();
@@ -71,7 +71,7 @@ fn parse_if_simple() {
#[test]
fn parse_if_with_elif() {
let input = "if foo; then echo bar; elif bar; then echo foo; fi";
let tk_stream: Vec<_> = LexStream::new(input, LexFlags::empty())
let tk_stream: Vec<_> = LexStream::new(Rc::new(input.to_string()), LexFlags::empty())
.map(|tk| tk.unwrap())
.collect();
let nodes: Vec<_> = ParseStream::new(tk_stream).collect();
@@ -81,7 +81,7 @@ fn parse_if_with_elif() {
#[test]
fn parse_if_multiple_elif() {
let input = "if foo; then echo bar; elif bar; then echo foo; elif biz; then echo baz; fi";
let tk_stream: Vec<_> = LexStream::new(input, LexFlags::empty())
let tk_stream: Vec<_> = LexStream::new(Rc::new(input.to_string()), LexFlags::empty())
.map(|tk| tk.unwrap())
.collect();
let nodes: Vec<_> = ParseStream::new(tk_stream).collect();
@@ -98,7 +98,7 @@ elif bar; then
elif biz; then
echo baz
fi";
let tk_stream: Vec<_> = LexStream::new(input, LexFlags::empty())
let tk_stream: Vec<_> = LexStream::new(Rc::new(input.to_string()), LexFlags::empty())
.map(|tk| tk.unwrap())
.collect();
let nodes: Vec<_> = ParseStream::new(tk_stream).collect();
@@ -108,7 +108,7 @@ fi";
#[test]
fn parse_loop_simple() {
let input = "while foo; do bar; done";
let tk_stream: Vec<_> = LexStream::new(input, LexFlags::empty())
let tk_stream: Vec<_> = LexStream::new(Rc::new(input.to_string()), LexFlags::empty())
.map(|tk| tk.unwrap())
.collect();
let nodes: Vec<_> = ParseStream::new(tk_stream).collect();
@@ -118,7 +118,7 @@ fn parse_loop_simple() {
#[test]
fn parse_loop_until() {
let input = "until foo; do bar; done";
let tk_stream: Vec<_> = LexStream::new(input, LexFlags::empty())
let tk_stream: Vec<_> = LexStream::new(Rc::new(input.to_string()), LexFlags::empty())
.map(|tk| tk.unwrap())
.collect();
let nodes: Vec<_> = ParseStream::new(tk_stream).collect();
@@ -131,7 +131,7 @@ fn parse_loop_multiline() {
until foo; do
bar
done";
let tk_stream: Vec<_> = LexStream::new(input, LexFlags::empty())
let tk_stream: Vec<_> = LexStream::new(Rc::new(input.to_string()), LexFlags::empty())
.map(|tk| tk.unwrap())
.collect();
let nodes: Vec<_> = ParseStream::new(tk_stream).collect();