Add tests for left-hand side of assignment being an identifier.

Relates to #106
This commit is contained in:
Sebastian Schmidt 2022-07-04 16:50:39 +03:00
parent 563d82a525
commit 65fadfd164
2 changed files with 39 additions and 1 deletions

View File

@ -444,7 +444,7 @@ pub(crate) fn tokenize(string: &str) -> EvalexprResult<Vec<Token>> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::token::{char_to_partial_token, tokenize}; use crate::token::{char_to_partial_token, tokenize, Token};
use std::fmt::Write; use std::fmt::Write;
#[test] #[test]
@ -474,4 +474,17 @@ mod tests {
assert_eq!(token_string, result_string); assert_eq!(token_string, result_string);
} }
#[test]
fn assignment_lhs_is_identifier() {
let tokens = tokenize("a = 1").unwrap();
assert_eq!(
tokens.as_slice(),
[
Token::Identifier("a".to_string()),
Token::Assign,
Token::Int(1)
]
);
}
} }

View File

@ -2083,3 +2083,28 @@ fn test_try_from() {
); );
assert_eq!(EmptyType::try_from(value.clone()), Ok(())); assert_eq!(EmptyType::try_from(value.clone()), Ok(()));
} }
#[test]
fn assignment_lhs_is_identifier() {
let tree = build_operator_tree("a = 1").unwrap();
let operators: Vec<_> = tree.iter().map(|node| node.operator().clone()).collect();
let mut context = HashMapContext::new();
tree.eval_empty_with_context_mut(&mut context).unwrap();
assert_eq!(context.get_value("a"), Some(&Value::Int(1)));
assert!(
matches!(
operators.as_slice(),
[
Operator::Assign,
Operator::VariableIdentifier { identifier: value },
Operator::Const {
value: Value::Int(1)
}
] if value == "a"
),
"actual: {:#?}",
operators
);
}