rhai/tests/switch.rs

355 lines
8.8 KiB
Rust
Raw Normal View History

2021-04-16 07:28:36 +02:00
use rhai::{Engine, EvalAltResult, ParseErrorType, Scope, INT};
2020-11-13 11:32:18 +01:00
#[test]
fn test_switch() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
let mut scope = Scope::new();
scope.push("x", 42 as INT);
2021-12-15 05:06:17 +01:00
assert_eq!(
engine.eval::<char>("switch 2 { 1 => (), 2 => 'a', 42 => true }")?,
'a'
);
engine.run("switch 3 { 1 => (), 2 => 'a', 42 => true }")?;
2021-12-15 05:06:17 +01:00
assert_eq!(
engine.eval::<INT>("switch 3 { 1 => (), 2 => 'a', 42 => true, _ => 123 }")?,
123
);
assert_eq!(
engine.eval_with_scope::<INT>(
&mut scope,
"switch 2 { 1 => (), 2 if x < 40 => 'a', 42 => true, _ => 123 }"
)?,
123
);
assert_eq!(
engine.eval_with_scope::<char>(
&mut scope,
"switch 2 { 1 => (), 2 if x > 40 => 'a', 42 => true, _ => 123 }"
)?,
'a'
);
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' }")?;
2022-12-30 18:07:39 +01:00
2022-07-04 11:42:24 +02:00
assert_eq!(
engine.eval_with_scope::<INT>(
&mut scope,
"switch x { 1 | 2 | 3 | 5..50 | 'x' | true => 123, 'z' => 'a' }"
)?,
123
);
2021-03-05 03:33:48 +01:00
assert_eq!(
2022-10-16 06:07:47 +02:00
engine.eval_with_scope::<INT>(&mut scope, "switch x { 424242 => 123, _ => 42 }")?,
2021-03-05 03:33:48 +01:00
42
);
2020-11-13 11:32:18 +01:00
assert_eq!(
engine.eval_with_scope::<INT>(
&mut scope,
"switch x { 1 => 123, 42 => { x / 2 }, _ => 999 }"
)?,
21
);
#[cfg(not(feature = "no_index"))]
assert_eq!(
engine.eval_with_scope::<INT>(
&mut scope,
2021-04-20 06:01:35 +02:00
"
2020-11-13 11:32:18 +01:00
let y = [1, 2, 3];
switch y {
42 => 1,
true => 2,
[1, 2, 3] => 3,
_ => 9
}
"
)?,
3
);
#[cfg(not(feature = "no_object"))]
assert_eq!(
engine.eval_with_scope::<INT>(
&mut scope,
2021-04-20 06:01:35 +02:00
"
2020-11-13 11:32:18 +01:00
let y = #{a:1, b:true, c:'x'};
switch y {
42 => 1,
true => 2,
#{b:true, c:'x', a:1} => 3,
_ => 9
}
"
)?,
3
);
2022-07-18 07:40:41 +02:00
assert_eq!(
engine.eval_with_scope::<INT>(&mut scope, "switch 42 { 42 => 123, 42 => 999 }")?,
123
);
assert_eq!(
engine.eval_with_scope::<INT>(&mut scope, "switch x { 42 => 123, 42 => 999 }")?,
123
);
2020-11-13 11:32:18 +01:00
Ok(())
}
2020-11-14 02:38:16 +01:00
2021-04-16 07:28:36 +02:00
#[test]
fn test_switch_errors() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
assert!(matches!(
engine
2021-04-16 07:28:36 +02:00
.compile("switch x { _ => 123, 1 => 42 }")
2023-04-10 17:23:59 +02:00
.unwrap_err()
.err_type(),
2021-04-16 07:28:36 +02:00
ParseErrorType::WrongSwitchDefaultCase
));
Ok(())
}
2021-04-16 06:04:33 +02:00
#[test]
fn test_switch_condition() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
let mut scope = Scope::new();
scope.push("x", 42 as INT);
assert_eq!(
engine.eval_with_scope::<INT>(
&mut scope,
2021-04-20 06:01:35 +02:00
"
2021-04-16 06:04:33 +02:00
switch x / 2 {
21 if x > 40 => 1,
0 if x < 100 => 2,
1 => 3,
_ => 9
}
"
)?,
1
);
assert_eq!(
engine.eval_with_scope::<INT>(
&mut scope,
2021-04-20 06:01:35 +02:00
"
2021-04-16 06:04:33 +02:00
switch x / 2 {
21 if x < 40 => 1,
0 if x < 100 => 2,
1 => 3,
_ => 9
}
"
)?,
9
);
2022-07-18 07:40:41 +02:00
assert_eq!(
engine.eval_with_scope::<INT>(
&mut scope,
"
switch x {
42 if x < 40 => 1,
42 if x > 40 => 7,
0 if x < 100 => 2,
1 => 3,
42 if x == 10 => 10,
_ => 9
}
"
)?,
7
);
2021-04-16 07:28:36 +02:00
assert!(matches!(
engine
2021-04-16 07:28:36 +02:00
.compile("switch x { 1 => 123, _ if true => 42 }")
2023-04-10 17:23:59 +02:00
.unwrap_err()
.err_type(),
2021-04-16 07:28:36 +02:00
ParseErrorType::WrongSwitchCaseCondition
));
2021-04-16 06:04:33 +02:00
Ok(())
}
2020-11-14 02:38:16 +01:00
#[cfg(not(feature = "no_index"))]
2020-11-14 11:30:26 +01:00
#[cfg(not(feature = "no_object"))]
2020-11-14 02:38:16 +01:00
mod test_switch_enum {
use super::*;
use rhai::Array;
#[derive(Debug, Clone)]
2021-07-04 10:40:15 +02:00
#[allow(dead_code)]
2020-11-14 02:38:16 +01:00
enum MyEnum {
Foo,
2020-11-14 11:30:26 +01:00
Bar(INT),
2020-11-14 02:38:16 +01:00
Baz(String, bool),
}
impl MyEnum {
fn get_enum_data(&mut self) -> Array {
match self {
Self::Foo => vec!["Foo".into()] as Array,
Self::Bar(num) => vec!["Bar".into(), (*num).into()] as Array,
Self::Baz(name, option) => {
vec!["Baz".into(), name.clone().into(), (*option).into()] as Array
}
}
}
}
#[test]
fn test_switch_enum() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
2020-11-14 11:30:26 +01:00
engine
.register_type_with_name::<MyEnum>("MyEnum")
.register_get("get_data", MyEnum::get_enum_data);
2020-11-14 02:38:16 +01:00
let mut scope = Scope::new();
scope.push("x", MyEnum::Baz("hello".to_string(), true));
assert_eq!(
engine.eval_with_scope::<INT>(
&mut scope,
r#"
switch x.get_data {
["Foo"] => 1,
["Bar", 42] => 2,
["Bar", 123] => 3,
["Baz", "hello", false] => 4,
["Baz", "hello", true] => 5,
_ => 9
}
"#
)?,
5
);
Ok(())
}
}
2021-12-15 05:06:17 +01:00
#[test]
fn test_switch_ranges() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
let mut scope = Scope::new();
scope.push("x", 42 as INT);
assert_eq!(
engine.eval_with_scope::<char>(
&mut scope,
"switch x { 10..20 => (), 20..=42 => 'a', 25..45 => 'z', 30..100 => true }"
)?,
'a'
);
assert_eq!(
engine.eval_with_scope::<char>(
&mut scope,
"switch x { 10..20 => (), 20..=42 if x < 40 => 'a', 25..45 => 'z', 30..100 => true }"
)?,
'z'
);
assert_eq!(
engine.eval_with_scope::<char>(
&mut scope,
"switch x { 42 => 'x', 10..20 => (), 20..=42 => 'a', 25..45 => 'z', 30..100 => true, 'w' => true }"
)?,
'x'
);
assert!(matches!(
engine.compile(
"switch x { 10..20 => (), 20..=42 => 'a', 25..45 => 'z', 42 => 'x', 30..100 => true }"
2023-04-10 17:23:59 +02:00
).unwrap_err().err_type(),
2021-12-15 05:06:17 +01:00
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 }"
2023-04-10 17:23:59 +02:00
).unwrap_err().err_type(),
ParseErrorType::WrongSwitchIntegerCase
));
2021-12-15 05:06:17 +01:00
assert_eq!(
engine.eval_with_scope::<char>(
&mut scope,
"
switch 5 {
'a' => true,
0..10 if x+2==1+2 => print(40+2),
_ => 'x'
}
"
)?,
'x'
);
2022-07-18 02:54:10 +02:00
assert_eq!(
engine.eval_with_scope::<INT>(
&mut scope,
"
switch 5 {
'a' => true,
0..10 => 123,
2..12 => 'z',
_ => 'x'
}
"
)?,
123
);
assert_eq!(
engine.eval_with_scope::<INT>(
&mut scope,
"
switch 5 {
'a' => true,
4 | 5 | 6 => 42,
0..10 => 123,
2..12 => 'z',
_ => 'x'
}
"
)?,
42
);
assert_eq!(
engine.eval_with_scope::<char>(
&mut scope,
"
switch 5 {
'a' => true,
2..12 => 'z',
0..10 if x+2==1+2 => print(40+2),
_ => 'x'
}
"
)?,
'z'
);
2021-12-15 05:06:17 +01:00
assert_eq!(
engine.eval_with_scope::<char>(
&mut scope,
"
switch 5 {
'a' => true,
0..10 if x+2==1+2 => print(40+2),
2..12 => 'z',
_ => 'x'
}
"
)?,
'z'
);
Ok(())
}