completely rewrote test suite for top level src files and all builtin files
This commit is contained in:
@@ -226,3 +226,266 @@ pub fn get_arr_op_opts(opts: Vec<Opt>) -> ShResult<ArrOpOpts> {
|
||||
}
|
||||
Ok(arr_op_opts)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::collections::VecDeque;
|
||||
use crate::state::{self, read_vars, write_vars, VarFlags, VarKind};
|
||||
use crate::testutil::{TestGuard, test_input};
|
||||
|
||||
fn set_arr(name: &str, elems: &[&str]) {
|
||||
let arr = VecDeque::from_iter(elems.iter().map(|s| s.to_string()));
|
||||
write_vars(|v| v.set_var(name, VarKind::Arr(arr), VarFlags::NONE)).unwrap();
|
||||
}
|
||||
|
||||
fn get_arr(name: &str) -> Vec<String> {
|
||||
read_vars(|v| v.get_arr_elems(name)).unwrap()
|
||||
}
|
||||
|
||||
// ===================== push =====================
|
||||
|
||||
#[test]
|
||||
fn push_to_existing_array() {
|
||||
let _guard = TestGuard::new();
|
||||
set_arr("arr", &["a", "b"]);
|
||||
|
||||
test_input("push arr c").unwrap();
|
||||
assert_eq!(get_arr("arr"), vec!["a", "b", "c"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn push_creates_array() {
|
||||
let _guard = TestGuard::new();
|
||||
|
||||
test_input("push newarr hello").unwrap();
|
||||
assert_eq!(get_arr("newarr"), vec!["hello"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn push_multiple_values() {
|
||||
let _guard = TestGuard::new();
|
||||
set_arr("arr", &["a"]);
|
||||
|
||||
test_input("push arr b c d").unwrap();
|
||||
assert_eq!(get_arr("arr"), vec!["a", "b", "c", "d"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn push_no_array_name() {
|
||||
let _guard = TestGuard::new();
|
||||
let result = test_input("push");
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
// ===================== fpush =====================
|
||||
|
||||
#[test]
|
||||
fn fpush_to_existing_array() {
|
||||
let _guard = TestGuard::new();
|
||||
set_arr("arr", &["b", "c"]);
|
||||
|
||||
test_input("fpush arr a").unwrap();
|
||||
assert_eq!(get_arr("arr"), vec!["a", "b", "c"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fpush_multiple_values() {
|
||||
let _guard = TestGuard::new();
|
||||
set_arr("arr", &["c"]);
|
||||
|
||||
test_input("fpush arr a b").unwrap();
|
||||
// Each value is pushed to the front in order: c -> a,c -> b,a,c
|
||||
assert_eq!(get_arr("arr"), vec!["b", "a", "c"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fpush_creates_array() {
|
||||
let _guard = TestGuard::new();
|
||||
|
||||
test_input("fpush newarr x").unwrap();
|
||||
assert_eq!(get_arr("newarr"), vec!["x"]);
|
||||
}
|
||||
|
||||
// ===================== pop =====================
|
||||
|
||||
#[test]
|
||||
fn pop_removes_last() {
|
||||
let guard = TestGuard::new();
|
||||
set_arr("arr", &["a", "b", "c"]);
|
||||
|
||||
test_input("pop arr").unwrap();
|
||||
let out = guard.read_output();
|
||||
assert_eq!(out, "c\n");
|
||||
assert_eq!(get_arr("arr"), vec!["a", "b"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pop_with_count() {
|
||||
let guard = TestGuard::new();
|
||||
set_arr("arr", &["a", "b", "c", "d"]);
|
||||
|
||||
test_input("pop -c 2 arr").unwrap();
|
||||
let out = guard.read_output();
|
||||
assert_eq!(out, "d\nc\n");
|
||||
assert_eq!(get_arr("arr"), vec!["a", "b"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pop_into_variable() {
|
||||
let _guard = TestGuard::new();
|
||||
set_arr("arr", &["x", "y", "z"]);
|
||||
|
||||
test_input("pop -v result arr").unwrap();
|
||||
let val = read_vars(|v| v.get_var("result"));
|
||||
assert_eq!(val, "z");
|
||||
assert_eq!(get_arr("arr"), vec!["x", "y"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pop_empty_array_fails() {
|
||||
let _guard = TestGuard::new();
|
||||
set_arr("arr", &[]);
|
||||
|
||||
test_input("pop arr").unwrap();
|
||||
assert_eq!(state::get_status(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pop_nonexistent_array() {
|
||||
let _guard = TestGuard::new();
|
||||
|
||||
test_input("pop nosucharray").unwrap();
|
||||
assert_eq!(state::get_status(), 1);
|
||||
}
|
||||
|
||||
// ===================== fpop =====================
|
||||
|
||||
#[test]
|
||||
fn fpop_removes_first() {
|
||||
let guard = TestGuard::new();
|
||||
set_arr("arr", &["a", "b", "c"]);
|
||||
|
||||
test_input("fpop arr").unwrap();
|
||||
let out = guard.read_output();
|
||||
assert_eq!(out, "a\n");
|
||||
assert_eq!(get_arr("arr"), vec!["b", "c"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fpop_with_count() {
|
||||
let guard = TestGuard::new();
|
||||
set_arr("arr", &["a", "b", "c", "d"]);
|
||||
|
||||
test_input("fpop -c 2 arr").unwrap();
|
||||
let out = guard.read_output();
|
||||
assert_eq!(out, "a\nb\n");
|
||||
assert_eq!(get_arr("arr"), vec!["c", "d"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fpop_into_variable() {
|
||||
let _guard = TestGuard::new();
|
||||
set_arr("arr", &["first", "second"]);
|
||||
|
||||
test_input("fpop -v result arr").unwrap();
|
||||
let val = read_vars(|v| v.get_var("result"));
|
||||
assert_eq!(val, "first");
|
||||
assert_eq!(get_arr("arr"), vec!["second"]);
|
||||
}
|
||||
|
||||
// ===================== rotate =====================
|
||||
|
||||
#[test]
|
||||
fn rotate_left_default() {
|
||||
let _guard = TestGuard::new();
|
||||
set_arr("arr", &["a", "b", "c", "d"]);
|
||||
|
||||
test_input("rotate arr").unwrap();
|
||||
assert_eq!(get_arr("arr"), vec!["b", "c", "d", "a"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rotate_left_with_count() {
|
||||
let _guard = TestGuard::new();
|
||||
set_arr("arr", &["a", "b", "c", "d"]);
|
||||
|
||||
test_input("rotate -c 2 arr").unwrap();
|
||||
assert_eq!(get_arr("arr"), vec!["c", "d", "a", "b"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rotate_right() {
|
||||
let _guard = TestGuard::new();
|
||||
set_arr("arr", &["a", "b", "c", "d"]);
|
||||
|
||||
test_input("rotate -r arr").unwrap();
|
||||
assert_eq!(get_arr("arr"), vec!["d", "a", "b", "c"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rotate_right_with_count() {
|
||||
let _guard = TestGuard::new();
|
||||
set_arr("arr", &["a", "b", "c", "d"]);
|
||||
|
||||
test_input("rotate -r -c 2 arr").unwrap();
|
||||
assert_eq!(get_arr("arr"), vec!["c", "d", "a", "b"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rotate_count_exceeds_len() {
|
||||
let _guard = TestGuard::new();
|
||||
set_arr("arr", &["a", "b"]);
|
||||
|
||||
// count clamped to arr.len(), so rotate by 2 on len=2 is a no-op
|
||||
test_input("rotate -c 5 arr").unwrap();
|
||||
assert_eq!(get_arr("arr"), vec!["a", "b"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rotate_single_element() {
|
||||
let _guard = TestGuard::new();
|
||||
set_arr("arr", &["only"]);
|
||||
|
||||
test_input("rotate arr").unwrap();
|
||||
assert_eq!(get_arr("arr"), vec!["only"]);
|
||||
}
|
||||
|
||||
// ===================== combined ops =====================
|
||||
|
||||
#[test]
|
||||
fn push_then_pop_roundtrip() {
|
||||
let guard = TestGuard::new();
|
||||
set_arr("arr", &["a"]);
|
||||
|
||||
test_input("push arr b").unwrap();
|
||||
test_input("pop arr").unwrap();
|
||||
let out = guard.read_output();
|
||||
assert_eq!(out, "b\n");
|
||||
assert_eq!(get_arr("arr"), vec!["a"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fpush_then_fpop_roundtrip() {
|
||||
let guard = TestGuard::new();
|
||||
set_arr("arr", &["a"]);
|
||||
|
||||
test_input("fpush arr z").unwrap();
|
||||
test_input("fpop arr").unwrap();
|
||||
let out = guard.read_output();
|
||||
assert_eq!(out, "z\n");
|
||||
assert_eq!(get_arr("arr"), vec!["a"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pop_until_empty() {
|
||||
let _guard = TestGuard::new();
|
||||
set_arr("arr", &["x", "y"]);
|
||||
|
||||
test_input("pop arr").unwrap();
|
||||
assert_eq!(state::get_status(), 0);
|
||||
test_input("pop arr").unwrap();
|
||||
assert_eq!(state::get_status(), 0);
|
||||
test_input("pop arr").unwrap();
|
||||
assert_eq!(state::get_status(), 1);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user