Make sure all tests run with all features.

This commit is contained in:
Stephen Chung 2020-03-10 19:48:47 +08:00
parent cc772c6e2a
commit e22aaca5c1
14 changed files with 71 additions and 38 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
target/ target/
Cargo.lock Cargo.lock
.vscode/ .vscode/
.cargo/

View File

@ -43,8 +43,8 @@ Optional features
| `debug_msgs` | Print debug messages to stdout (using `println!`) related to function registrations and function calls. | | `debug_msgs` | Print debug messages to stdout (using `println!`) related to function registrations and function calls. |
| `no_stdlib` | Exclude the standard library of utility functions in the build, and only include the minimum necessary functionalities. | | `no_stdlib` | Exclude the standard library of utility functions in the build, and only include the minimum necessary functionalities. |
| `unchecked` | Exclude arithmetic checking in the standard library. Beware that a bad script may panic the entire system! | | `unchecked` | Exclude arithmetic checking in the standard library. Beware that a bad script may panic the entire system! |
| `no_index` | Disable arrays and indexing features | | `no_index` | Disable arrays and indexing features if you don't need them. |
| `no_float` | Disable floating-point numbers and math | | `no_float` | Disable floating-point numbers and math if you don't need them. |
Related Related
------- -------
@ -190,14 +190,14 @@ Values and types
The following primitive types are supported natively: The following primitive types are supported natively:
| Category | Types | | Category | Types |
| ------------------------------ | -------------------------------------- | | --------------------------------------------------------------- | -------------------------------------- |
| Integer | `i32`, `u32`, `i64` _(default)_, `u64` | | Integer | `i32`, `u32`, `i64` _(default)_, `u64` |
| Floating-point | `f32`, `f64` _(default)_ | | Floating-point (disabled with [`no_float`](#optional-features)) | `f32`, `f64` _(default)_ |
| Character | `char` | | Character | `char` |
| Boolean | `bool` | | Boolean | `bool` |
| Array | `rhai::Array` | | Array (disabled with [`no_index`](#optional-features)) | `rhai::Array` |
| Dynamic (i.e. can be anything) | `rhai::Dynamic` | | Dynamic (i.e. can be anything) | `rhai::Dynamic` |
Value conversions Value conversions
----------------- -----------------

View File

@ -274,6 +274,8 @@ impl<'e> Engine<'e> {
/// ///
/// ```rust /// ```rust
/// # fn main() -> Result<(), rhai::EvalAltResult> { /// # fn main() -> Result<(), rhai::EvalAltResult> {
/// # #[cfg(not(feature = "no_stdlib"))]
/// # {
/// use rhai::Engine; /// use rhai::Engine;
/// ///
/// let mut engine = Engine::new(); /// let mut engine = Engine::new();
@ -283,6 +285,7 @@ impl<'e> Engine<'e> {
/// let result: i64 = engine.call_fn("add", &ast, (String::from("abc"), 123_i64))?; /// let result: i64 = engine.call_fn("add", &ast, (String::from("abc"), 123_i64))?;
/// ///
/// assert_eq!(result, 126); /// assert_eq!(result, 126);
/// # }
/// # Ok(()) /// # Ok(())
/// # } /// # }
/// ``` /// ```

View File

@ -479,7 +479,7 @@ impl Engine<'_> {
reg_func1!(self, "debug", print_debug, String, i8, u8, i16, u16); reg_func1!(self, "debug", print_debug, String, i8, u8, i16, u16);
reg_func1!(self, "debug", print_debug, String, i32, i64, u32, u64); reg_func1!(self, "debug", print_debug, String, i32, i64, u32, u64);
reg_func1!(self, "debug", print_debug, String, bool, char); reg_func1!(self, "debug", print_debug, String, bool, char, String, ());
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
{ {
@ -490,7 +490,7 @@ impl Engine<'_> {
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
{ {
reg_func1!(self, "print", print_debug, String, Array); reg_func1!(self, "print", print_debug, String, Array);
reg_func1!(self, "debug", print_debug, String, String, Array, ()); reg_func1!(self, "debug", print_debug, String, Array);
// Register array iterator // Register array iterator
self.register_iterator::<Array, _>(|a| { self.register_iterator::<Array, _>(|a| {

View File

@ -61,7 +61,7 @@ fn optimize_stmt(stmt: Stmt, changed: &mut bool) -> Stmt {
// Remove all raw expression statements that evaluate to constants // Remove all raw expression statements that evaluate to constants
// except for the very last statement // except for the very last statement
result.retain(|stmt| match stmt { result.retain(|stmt| match stmt {
Stmt::Expr(expr) if expr.is_constant() => false, Stmt::Expr(expr) if expr.is_constant() || expr.is_identifier() => false,
_ => true, _ => true,
}); });

View File

@ -1,3 +1,4 @@
#![cfg(not(feature = "no_index"))]
use rhai::{Engine, EvalAltResult, RegisterFn}; use rhai::{Engine, EvalAltResult, RegisterFn};
#[test] #[test]

View File

@ -6,11 +6,15 @@ fn test_chars() -> Result<(), EvalAltResult> {
assert_eq!(engine.eval::<char>("'y'")?, 'y'); assert_eq!(engine.eval::<char>("'y'")?, 'y');
assert_eq!(engine.eval::<char>("'\\u2764'")?, '❤'); assert_eq!(engine.eval::<char>("'\\u2764'")?, '❤');
assert_eq!(engine.eval::<char>(r#"let x="hello"; x[2]"#)?, 'l');
assert_eq!( #[cfg(not(feature = "no_index"))]
engine.eval::<String>(r#"let x="hello"; x[2]='$'; x"#)?, {
"he$lo".to_string() assert_eq!(engine.eval::<char>(r#"let x="hello"; x[2]"#)?, 'l');
); assert_eq!(
engine.eval::<String>(r#"let x="hello"; x[2]='$'; x"#)?,
"he$lo".to_string()
);
}
assert!(engine.eval::<char>("'\\uhello'").is_err()); assert!(engine.eval::<char>("'\\uhello'").is_err());
assert!(engine.eval::<char>("''").is_err()); assert!(engine.eval::<char>("''").is_err());

View File

@ -1,3 +1,4 @@
#![cfg(not(feature = "no_stdlib"))]
use rhai::{Engine, EvalAltResult}; use rhai::{Engine, EvalAltResult};
#[test] #[test]

View File

@ -1,3 +1,4 @@
#![cfg(not(feature = "no_float"))]
use rhai::{Engine, EvalAltResult, RegisterFn}; use rhai::{Engine, EvalAltResult, RegisterFn};
#[test] #[test]

View File

@ -1,3 +1,4 @@
#![cfg(not(feature = "no_index"))]
use rhai::{Engine, EvalAltResult}; use rhai::{Engine, EvalAltResult};
#[test] #[test]

View File

@ -1,6 +1,7 @@
use rhai::{Engine, EvalAltResult, RegisterFn}; use rhai::{Engine, EvalAltResult, RegisterFn};
#[test] #[test]
#[cfg(not(feature = "no_stdlib"))]
fn test_mismatched_op() { fn test_mismatched_op() {
let mut engine = Engine::new(); let mut engine = Engine::new();
@ -26,17 +27,13 @@ fn test_mismatched_op_custom_type() {
} }
let mut engine = Engine::new(); let mut engine = Engine::new();
engine.register_type::<TestStruct>(); engine.register_type_with_name::<TestStruct>("TestStruct");
engine.register_fn("new_ts", TestStruct::new); engine.register_fn("new_ts", TestStruct::new);
let r = engine.eval::<i64>("60 + new_ts()"); let r = engine.eval::<i64>("60 + new_ts()");
match r { match r {
Err(EvalAltResult::ErrorFunctionNotFound(err, _)) Err(EvalAltResult::ErrorFunctionNotFound(err, _)) if err == "+ (i64, TestStruct)" => (),
if err == "+ (i64, mismatched_op::test_mismatched_op_custom_type::TestStruct)" =>
{
()
}
_ => panic!(), _ => panic!(),
} }
} }

View File

@ -6,11 +6,15 @@ fn test_power_of() -> Result<(), EvalAltResult> {
assert_eq!(engine.eval::<i64>("2 ~ 3")?, 8); assert_eq!(engine.eval::<i64>("2 ~ 3")?, 8);
assert_eq!(engine.eval::<i64>("(-2 ~ 3)")?, -8); assert_eq!(engine.eval::<i64>("(-2 ~ 3)")?, -8);
assert_eq!(engine.eval::<f64>("2.2 ~ 3.3")?, 13.489468760533386_f64);
assert_eq!(engine.eval::<f64>("2.0~-2.0")?, 0.25_f64); #[cfg(not(feature = "no_float"))]
assert_eq!(engine.eval::<f64>("(-2.0~-2.0)")?, 0.25_f64); {
assert_eq!(engine.eval::<f64>("(-2.0~-2)")?, 0.25_f64); assert_eq!(engine.eval::<f64>("2.2 ~ 3.3")?, 13.489468760533386_f64);
assert_eq!(engine.eval::<i64>("4~3")?, 64); assert_eq!(engine.eval::<f64>("2.0~-2.0")?, 0.25_f64);
assert_eq!(engine.eval::<f64>("(-2.0~-2.0)")?, 0.25_f64);
assert_eq!(engine.eval::<f64>("(-2.0~-2)")?, 0.25_f64);
assert_eq!(engine.eval::<i64>("4~3")?, 64);
}
Ok(()) Ok(())
} }
@ -21,14 +25,18 @@ fn test_power_of_equals() -> Result<(), EvalAltResult> {
assert_eq!(engine.eval::<i64>("let x = 2; x ~= 3; x")?, 8); assert_eq!(engine.eval::<i64>("let x = 2; x ~= 3; x")?, 8);
assert_eq!(engine.eval::<i64>("let x = -2; x ~= 3; x")?, -8); assert_eq!(engine.eval::<i64>("let x = -2; x ~= 3; x")?, -8);
assert_eq!(
engine.eval::<f64>("let x = 2.2; x ~= 3.3; x")?, #[cfg(not(feature = "no_float"))]
13.489468760533386_f64 {
); assert_eq!(
assert_eq!(engine.eval::<f64>("let x = 2.0; x ~= -2.0; x")?, 0.25_f64); engine.eval::<f64>("let x = 2.2; x ~= 3.3; x")?,
assert_eq!(engine.eval::<f64>("let x = -2.0; x ~= -2.0; x")?, 0.25_f64); 13.489468760533386_f64
assert_eq!(engine.eval::<f64>("let x = -2.0; x ~= -2; x")?, 0.25_f64); );
assert_eq!(engine.eval::<i64>("let x =4; x ~= 3; x")?, 64); assert_eq!(engine.eval::<f64>("let x = 2.0; x ~= -2.0; x")?, 0.25_f64);
assert_eq!(engine.eval::<f64>("let x = -2.0; x ~= -2.0; x")?, 0.25_f64);
assert_eq!(engine.eval::<f64>("let x = -2.0; x ~= -2; x")?, 0.25_f64);
assert_eq!(engine.eval::<i64>("let x =4; x ~= 3; x")?, 64);
}
Ok(()) Ok(())
} }

View File

@ -12,10 +12,20 @@ fn test_string() -> Result<(), EvalAltResult> {
engine.eval::<String>(r#""Test string: \x58""#)?, engine.eval::<String>(r#""Test string: \x58""#)?,
"Test string: X".to_string() "Test string: X".to_string()
); );
assert_eq!( assert_eq!(
engine.eval::<String>(r#""foo" + "bar""#)?, engine.eval::<String>(r#""foo" + "bar""#)?,
"foobar".to_string() "foobar".to_string()
); );
#[cfg(not(feature = "no_stdlib"))]
assert_eq!(
engine.eval::<String>(r#""foo" + 123"#)?,
"foo123".to_string()
);
#[cfg(not(feature = "no_float"))]
#[cfg(not(feature = "no_stdlib"))]
assert_eq!( assert_eq!(
engine.eval::<String>(r#""foo" + 123.4556"#)?, engine.eval::<String>(r#""foo" + 123.4556"#)?,
"foo123.4556".to_string() "foo123.4556".to_string()

View File

@ -5,11 +5,17 @@ fn test_type_of() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<String>("type_of(60 + 5)")?, "i64"); assert_eq!(engine.eval::<String>("type_of(60 + 5)")?, "i64");
#[cfg(not(feature = "no_float"))]
assert_eq!(engine.eval::<String>("type_of(1.0 + 2.0)")?, "f64"); assert_eq!(engine.eval::<String>("type_of(1.0 + 2.0)")?, "f64");
#[cfg(not(feature = "no_index"))]
#[cfg(not(feature = "no_float"))]
assert_eq!( assert_eq!(
engine.eval::<String>(r#"type_of([1.0, 2, "hello"])"#)?, engine.eval::<String>(r#"type_of([1.0, 2, "hello"])"#)?,
"array" "array"
); );
assert_eq!(engine.eval::<String>(r#"type_of("hello")"#)?, "string"); assert_eq!(engine.eval::<String>(r#"type_of("hello")"#)?, "string");
assert_eq!(engine.eval::<String>("let x = 123; x.type_of()")?, "i64"); assert_eq!(engine.eval::<String>("let x = 123; x.type_of()")?, "i64");