2021-03-15 04:36:30 +01:00
|
|
|
use rhai::{Engine, EvalAltResult, INT};
|
2020-03-03 10:28:38 +01:00
|
|
|
|
|
|
|
#[test]
|
2020-04-21 17:25:12 +02:00
|
|
|
fn test_type_of() -> Result<(), Box<EvalAltResult>> {
|
2022-12-30 18:07:39 +01:00
|
|
|
#[allow(dead_code)]
|
2020-03-30 10:10:40 +02:00
|
|
|
#[derive(Clone)]
|
|
|
|
struct TestStruct {
|
|
|
|
x: INT,
|
|
|
|
}
|
|
|
|
|
2020-03-03 10:28:38 +01:00
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
2020-03-10 16:06:20 +01:00
|
|
|
#[cfg(not(feature = "only_i32"))]
|
2020-03-03 10:28:38 +01:00
|
|
|
assert_eq!(engine.eval::<String>("type_of(60 + 5)")?, "i64");
|
2020-03-10 12:48:47 +01:00
|
|
|
|
2020-03-10 16:06:20 +01:00
|
|
|
#[cfg(feature = "only_i32")]
|
|
|
|
assert_eq!(engine.eval::<String>("type_of(60 + 5)")?, "i32");
|
|
|
|
|
2020-03-10 12:48:47 +01:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
2020-11-01 08:48:48 +01:00
|
|
|
{
|
|
|
|
#[cfg(not(feature = "f32_float"))]
|
|
|
|
assert_eq!(engine.eval::<String>("type_of(1.0 + 2.0)")?, "f64");
|
|
|
|
|
|
|
|
#[cfg(feature = "f32_float")]
|
|
|
|
assert_eq!(engine.eval::<String>("type_of(1.0 + 2.0)")?, "f32");
|
|
|
|
}
|
2020-03-10 12:48:47 +01:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
2020-03-03 10:28:38 +01:00
|
|
|
assert_eq!(
|
2020-03-30 10:10:40 +02:00
|
|
|
engine.eval::<String>(r#"type_of([true, 2, "hello"])"#)?,
|
2020-03-03 10:28:38 +01:00
|
|
|
"array"
|
|
|
|
);
|
2020-03-10 12:48:47 +01:00
|
|
|
|
2020-03-30 11:40:26 +02:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<String>(r#"type_of(#{a:true, "":2, "z":"hello"})"#)?,
|
|
|
|
"map"
|
|
|
|
);
|
2020-03-30 10:10:40 +02:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
{
|
2020-03-30 10:18:33 +02:00
|
|
|
engine.register_type_with_name::<TestStruct>("Hello");
|
2020-03-30 10:10:40 +02:00
|
|
|
engine.register_fn("new_ts", || TestStruct { x: 1 });
|
|
|
|
|
|
|
|
assert_eq!(engine.eval::<String>("type_of(new_ts())")?, "Hello");
|
|
|
|
}
|
|
|
|
|
2020-03-03 10:28:38 +01:00
|
|
|
assert_eq!(engine.eval::<String>(r#"type_of("hello")"#)?, "string");
|
2020-03-10 16:06:20 +01:00
|
|
|
|
2021-03-01 15:44:56 +01:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
assert_eq!(engine.eval::<String>(r#""hello".type_of()"#)?, "string");
|
|
|
|
|
2020-03-10 16:06:20 +01:00
|
|
|
#[cfg(not(feature = "only_i32"))]
|
2020-03-20 12:27:02 +01:00
|
|
|
assert_eq!(engine.eval::<String>("let x = 123; type_of(x)")?, "i64");
|
2020-03-03 10:28:38 +01:00
|
|
|
|
2020-03-10 16:06:20 +01:00
|
|
|
#[cfg(feature = "only_i32")]
|
2020-03-20 12:27:02 +01:00
|
|
|
assert_eq!(engine.eval::<String>("let x = 123; type_of(x)")?, "i32");
|
2020-03-10 16:06:20 +01:00
|
|
|
|
2020-03-03 10:28:38 +01:00
|
|
|
Ok(())
|
|
|
|
}
|