Skip to content

Commit

Permalink
Add some unittests checking parenting functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
rtpg committed Feb 16, 2024
1 parent 704f3dd commit 5cc207e
Showing 1 changed file with 73 additions and 1 deletion.
74 changes: 73 additions & 1 deletion crates/nu-protocol/src/engine/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,4 +449,76 @@ impl Default for Stack {
}

#[cfg(test)]
mod test {}
mod test {
use std::sync::Arc;

use crate::{Span, Value};

use super::Stack;

const ZERO_SPAN: Span = Span { start: 0, end: 0 };
fn string_value(s: &str) -> Value {
return Value::String {

Check failure on line 461 in crates/nu-protocol/src/engine/stack.rs

View workflow job for this annotation

GitHub Actions / fmt-clippy (ubuntu-20.04, default)

unneeded `return` statement

Check failure on line 461 in crates/nu-protocol/src/engine/stack.rs

View workflow job for this annotation

GitHub Actions / fmt-clippy (ubuntu-20.04, extra)

unneeded `return` statement
val: s.to_string(),
internal_span: ZERO_SPAN,
};
}

#[test]
fn test_children_see_inner_values() {
let mut original = Stack::new();
original.add_var(0, string_value("hello"));

let cloned = Stack::with_parent(Arc::new(original));
assert_eq!(cloned.get_var(0, ZERO_SPAN), Ok(string_value("hello")));
}

#[test]
fn test_children_dont_see_deleted_values() {
let mut original = Stack::new();
original.add_var(0, string_value("hello"));

let mut cloned = Stack::with_parent(Arc::new(original));
cloned.remove_var(0);

assert_eq!(
cloned.get_var(0, ZERO_SPAN),
Err(crate::ShellError::VariableNotFoundAtRuntime { span: ZERO_SPAN })
);
}

#[test]
fn test_children_changes_override_parent() {
let mut original = Stack::new();
original.add_var(0, string_value("hello"));

let mut cloned = Stack::with_parent(Arc::new(original));
cloned.add_var(0, string_value("there"));
assert_eq!(cloned.get_var(0, ZERO_SPAN), Ok(string_value("there")));

cloned.remove_var(0);
// the underlying value shouldn't magically re-appear
assert_eq!(
cloned.get_var(0, ZERO_SPAN),
Err(crate::ShellError::VariableNotFoundAtRuntime { span: ZERO_SPAN })
);
}
#[test]
fn test_children_changes_persist_in_offspring() {
let mut original = Stack::new();
original.add_var(0, string_value("hello"));

let mut cloned = Stack::with_parent(Arc::new(original));
cloned.add_var(1, string_value("there"));

cloned.remove_var(0);
let cloned = Stack::with_parent(Arc::new(cloned));

assert_eq!(
cloned.get_var(0, ZERO_SPAN),
Err(crate::ShellError::VariableNotFoundAtRuntime { span: ZERO_SPAN })
);

assert_eq!(cloned.get_var(1, ZERO_SPAN), Ok(string_value("there")));
}
}

0 comments on commit 5cc207e

Please sign in to comment.