Forbid floating-point switch cases after range case.

This commit is contained in:
Stephen Chung
2023-02-13 08:58:59 +08:00
parent 10089c5cb0
commit 7da20dd090
3 changed files with 31 additions and 17 deletions

View File

@@ -10,9 +10,7 @@ fn test_switch() -> Result<(), Box<EvalAltResult>> {
engine.eval::<char>("switch 2 { 1 => (), 2 => 'a', 42 => true }")?,
'a'
);
engine
.run("switch 3 { 1 => (), 2 => 'a', 42 => true }")
.unwrap();
engine.run("switch 3 { 1 => (), 2 => 'a', 42 => true }")?;
assert_eq!(
engine.eval::<INT>("switch 3 { 1 => (), 2 => 'a', 42 => true, _ => 123 }")?,
123
@@ -31,15 +29,13 @@ fn test_switch() -> Result<(), Box<EvalAltResult>> {
)?,
'a'
);
assert!(engine
.eval_with_scope::<bool>(&mut scope, "switch x { 1 => (), 2 => 'a', 42 => true }")
.unwrap());
assert!(engine
.eval_with_scope::<bool>(&mut scope, "switch x { 1 => (), 2 => 'a', _ => true }")
.unwrap());
let _: () = engine
.eval_with_scope::<()>(&mut scope, "switch x { 1 => 123, 2 => 'a' }")
.unwrap();
assert!(
engine.eval_with_scope::<bool>(&mut scope, "switch x { 1 => (), 2 => 'a', 42 => true }")?
);
assert!(
engine.eval_with_scope::<bool>(&mut scope, "switch x { 1 => (), 2 => 'a', _ => true }")?
);
let _: () = engine.eval_with_scope::<()>(&mut scope, "switch x { 1 => 123, 2 => 'a' }")?;
assert_eq!(
engine.eval_with_scope::<INT>(
@@ -276,6 +272,13 @@ fn test_switch_ranges() -> Result<(), Box<EvalAltResult>> {
).expect_err("should error").err_type(),
ParseErrorType::WrongSwitchIntegerCase
));
#[cfg(not(feature = "no_float"))]
assert!(matches!(
engine.compile(
"switch x { 10..20 => (), 20..=42 => 'a', 25..45 => 'z', 42.0 => 'x', 30..100 => true }"
).expect_err("should error").err_type(),
ParseErrorType::WrongSwitchIntegerCase
));
assert_eq!(
engine.eval_with_scope::<char>(
&mut scope,