Skip to content

Commit

Permalink
Changed,removed tests with illegal identifiers.
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroishero committed Jun 5, 2023
1 parent 10005ec commit 53d4507
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 57 deletions.
4 changes: 3 additions & 1 deletion src/error/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ impl fmt::Display for EvalexprError {
write!(f, "This context does not allow disabling builtin functions")
},
IllegalEscapeSequence(string) => write!(f, "Illegal escape sequence: {}", string),
IllegalIdentifierSequence => write!(f, "Illegal Identifier Sequence"),
IllegalIdentifierSequence(string) => {
write!(f, "Illegal Identifier Sequence: {}", string)
},
CustomMessage(message) => write!(f, "Error: {}", message),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ pub enum EvalexprError {
BuiltinFunctionsCannotBeDisabled,

/// Error parsing identifier
IllegalIdentifierSequence,
IllegalIdentifierSequence(String),

/// A custom error explained by its message.
CustomMessage(String),
Expand Down
28 changes: 19 additions & 9 deletions src/token/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,12 +350,16 @@ fn partial_tokens_to_tokens(mut tokens: &[PartialToken]) -> EvalexprResult<Vec<T
cutoff = 1;
let starts_with_alphabet_or_underscore =
literal.starts_with(|x: char| x.is_alphabetic() || x == '_');
let contains_alphanumeric_only =
literal.chars().all(|x| x.is_alphanumeric() || x == '_');
// Alphanumeric || Underscore || Colon
let contains_only_valid_chars = literal
.chars()
.all(|x| x.is_alphanumeric() || x == '_' || x == ':');
let is_not_underscore = literal != "_";
let contains_alphabet = literal.contains(|x: char| x.is_alphabetic());
if starts_with_alphabet_or_underscore
&& contains_alphanumeric_only
if let Ok(boolean) = literal.parse::<bool>() {
Some(Token::Boolean(boolean))
} else if starts_with_alphabet_or_underscore
&& contains_only_valid_chars
&& is_not_underscore
&& contains_alphabet
{
Expand All @@ -364,8 +368,6 @@ fn partial_tokens_to_tokens(mut tokens: &[PartialToken]) -> EvalexprResult<Vec<T
Some(Token::Int(number))
} else if let Ok(number) = literal.parse::<FloatType>() {
Some(Token::Float(number))
} else if let Ok(boolean) = literal.parse::<bool>() {
Some(Token::Boolean(boolean))
} else {
// If there are two tokens following this one, check if the next one is
// a plus or a minus. If so, then attempt to parse all three tokens as a
Expand All @@ -382,10 +384,16 @@ fn partial_tokens_to_tokens(mut tokens: &[PartialToken]) -> EvalexprResult<Vec<T
cutoff = 3;
Some(Token::Float(number))
} else {
return Err(EvalexprError::IllegalIdentifierSequence);
return Err(EvalexprError::IllegalIdentifierSequence(
literal.to_string(),
));
}
},
_ => return Err(EvalexprError::IllegalIdentifierSequence),
_ => {
return Err(EvalexprError::IllegalIdentifierSequence(
literal.to_string(),
))
},
}
}
},
Expand Down Expand Up @@ -512,7 +520,9 @@ mod tests {
fn wrong_identifier_sequence() {
assert_eq!(
tokenize("1b + 1"),
Err(crate::EvalexprError::IllegalIdentifierSequence)
Err(crate::EvalexprError::IllegalIdentifierSequence(
String::from("1b")
))
);
assert_eq!(
tokenize("_b + 1"),
Expand Down
69 changes: 23 additions & 46 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ fn test_shortcut_functions() {
);
assert_eq!(
eval_string("3..3"),
Err(EvalexprError::VariableIdentifierNotFound("3..3".to_owned()))
Err(EvalexprError::IllegalIdentifierSequence("3..3".to_owned()))
);
assert_eq!(
eval_string_with_context("string", &context),
Expand All @@ -629,7 +629,7 @@ fn test_shortcut_functions() {
);
assert_eq!(
eval_string_with_context("3..3", &context),
Err(EvalexprError::VariableIdentifierNotFound("3..3".to_owned()))
Err(EvalexprError::IllegalIdentifierSequence("3..3".to_owned()))
);
assert_eq!(
eval_string_with_context_mut("string", &mut context),
Expand All @@ -643,7 +643,7 @@ fn test_shortcut_functions() {
);
assert_eq!(
eval_string_with_context_mut("3..3", &mut context),
Err(EvalexprError::VariableIdentifierNotFound("3..3".to_owned()))
Err(EvalexprError::IllegalIdentifierSequence("3..3".to_owned()))
);

assert_eq!(eval_float("3.3"), Ok(3.3));
Expand Down Expand Up @@ -689,7 +689,7 @@ fn test_shortcut_functions() {
);
assert_eq!(
eval_int("(,);."),
Err(EvalexprError::VariableIdentifierNotFound(".".to_owned()))
Err(EvalexprError::IllegalIdentifierSequence(".".to_owned()))
);
assert_eq!(eval_int_with_context("3", &context), Ok(3));
assert_eq!(
Expand All @@ -700,7 +700,7 @@ fn test_shortcut_functions() {
);
assert_eq!(
eval_int_with_context("(,);.", &context),
Err(EvalexprError::VariableIdentifierNotFound(".".to_owned()))
Err(EvalexprError::IllegalIdentifierSequence(".".to_owned()))
);
assert_eq!(eval_int_with_context_mut("3", &mut context), Ok(3));
assert_eq!(
Expand All @@ -711,7 +711,7 @@ fn test_shortcut_functions() {
);
assert_eq!(
eval_int_with_context_mut("(,);.", &mut context),
Err(EvalexprError::VariableIdentifierNotFound(".".to_owned()))
Err(EvalexprError::IllegalIdentifierSequence(".".to_owned()))
);

assert_eq!(eval_number("3"), Ok(3.0));
Expand Down Expand Up @@ -802,7 +802,7 @@ fn test_shortcut_functions() {
);
assert_eq!(
eval_tuple("3a3"),
Err(EvalexprError::VariableIdentifierNotFound("3a3".to_owned()))
Err(EvalexprError::IllegalIdentifierSequence("3a3".to_owned()))
);
assert_eq!(
eval_tuple_with_context("3,3", &context),
Expand All @@ -816,7 +816,7 @@ fn test_shortcut_functions() {
);
assert_eq!(
eval_tuple_with_context("3a3", &context),
Err(EvalexprError::VariableIdentifierNotFound("3a3".to_owned()))
Err(EvalexprError::IllegalIdentifierSequence("3a3".to_owned()))
);
assert_eq!(
eval_tuple_with_context_mut("3,3", &mut context),
Expand All @@ -830,7 +830,7 @@ fn test_shortcut_functions() {
);
assert_eq!(
eval_tuple_with_context_mut("3a3", &mut context),
Err(EvalexprError::VariableIdentifierNotFound("3a3".to_owned()))
Err(EvalexprError::IllegalIdentifierSequence("3a3".to_owned()))
);

assert_eq!(eval_empty(""), Ok(EMPTY_VALUE));
Expand Down Expand Up @@ -889,8 +889,8 @@ fn test_shortcut_functions() {
})
);
assert_eq!(
build_operator_tree("3..3").unwrap().eval_string(),
Err(EvalexprError::VariableIdentifierNotFound("3..3".to_owned()))
build_operator_tree("3..3"),
Err(EvalexprError::IllegalIdentifierSequence("3..3".to_owned()))
);
assert_eq!(
build_operator_tree("string")
Expand All @@ -907,10 +907,8 @@ fn test_shortcut_functions() {
})
);
assert_eq!(
build_operator_tree("3..3")
.unwrap()
.eval_string_with_context(&context),
Err(EvalexprError::VariableIdentifierNotFound("3..3".to_owned()))
build_operator_tree("3..3"),
Err(EvalexprError::IllegalIdentifierSequence("3..3".to_owned()))
);
assert_eq!(
build_operator_tree("string")
Expand All @@ -926,12 +924,6 @@ fn test_shortcut_functions() {
actual: Value::Float(3.3)
})
);
assert_eq!(
build_operator_tree("3..3")
.unwrap()
.eval_string_with_context_mut(&mut context),
Err(EvalexprError::VariableIdentifierNotFound("3..3".to_owned()))
);

assert_eq!(build_operator_tree("3.3").unwrap().eval_float(), Ok(3.3));
assert_eq!(
Expand Down Expand Up @@ -993,8 +985,8 @@ fn test_shortcut_functions() {
})
);
assert_eq!(
build_operator_tree("(,);.").unwrap().eval_int(),
Err(EvalexprError::VariableIdentifierNotFound(".".to_owned()))
build_operator_tree("(,);."),
Err(EvalexprError::IllegalIdentifierSequence(".".to_owned()))
);
assert_eq!(
build_operator_tree("3")
Expand All @@ -1011,10 +1003,8 @@ fn test_shortcut_functions() {
})
);
assert_eq!(
build_operator_tree("(,);.")
.unwrap()
.eval_int_with_context(&context),
Err(EvalexprError::VariableIdentifierNotFound(".".to_owned()))
build_operator_tree("(,);."),
Err(EvalexprError::IllegalIdentifierSequence(".".to_owned()))
);
assert_eq!(
build_operator_tree("3")
Expand All @@ -1030,12 +1020,6 @@ fn test_shortcut_functions() {
actual: Value::Float(3.3)
})
);
assert_eq!(
build_operator_tree("(,);.")
.unwrap()
.eval_int_with_context_mut(&mut context),
Err(EvalexprError::VariableIdentifierNotFound(".".to_owned()))
);

assert_eq!(build_operator_tree("3").unwrap().eval_number(), Ok(3.0));
assert_eq!(
Expand Down Expand Up @@ -1161,8 +1145,8 @@ fn test_shortcut_functions() {
})
);
assert_eq!(
build_operator_tree("3a3").unwrap().eval_tuple(),
Err(EvalexprError::VariableIdentifierNotFound("3a3".to_owned()))
build_operator_tree("3a3"),
Err(EvalexprError::IllegalIdentifierSequence("3a3".to_owned()))
);
assert_eq!(
build_operator_tree("3,3")
Expand All @@ -1178,12 +1162,7 @@ fn test_shortcut_functions() {
actual: Value::Int(33)
})
);
assert_eq!(
build_operator_tree("3a3")
.unwrap()
.eval_tuple_with_context(&context),
Err(EvalexprError::VariableIdentifierNotFound("3a3".to_owned()))
);

assert_eq!(
build_operator_tree("3,3")
.unwrap()
Expand All @@ -1199,10 +1178,8 @@ fn test_shortcut_functions() {
})
);
assert_eq!(
build_operator_tree("3a3")
.unwrap()
.eval_tuple_with_context_mut(&mut context),
Err(EvalexprError::VariableIdentifierNotFound("3a3".to_owned()))
build_operator_tree("3a3"),
Err(EvalexprError::IllegalIdentifierSequence("3a3".to_owned()))
);

assert_eq!(
Expand Down Expand Up @@ -2301,6 +2278,6 @@ fn test_hex() {
eval("0x"),
// The "VariableIdentifierNotFound" error is what evalexpr currently returns,
// but ideally it would return more specific errors for "illegal" literals.
Err(EvalexprError::VariableIdentifierNotFound("0x".into()))
Err(EvalexprError::IllegalIdentifierSequence("0x".into()))
);
}

0 comments on commit 53d4507

Please sign in to comment.