Do not unnecessarily use raw strings.

This commit is contained in:
Stephen Chung
2021-04-20 12:01:35 +08:00
parent a186eb8d97
commit 0f66c67f82
29 changed files with 100 additions and 103 deletions

View File

@@ -56,7 +56,7 @@ fn test_arrays() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
convert_to_vec::<INT>(engine.eval(
r"
"
let x = [2, 9];
x.insert(-1, 1);
x.insert(999, 3);
@@ -76,7 +76,7 @@ fn test_arrays() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
convert_to_vec::<INT>(engine.eval(
r"
"
let x = [1, 2, 3];
x += [4, 5];
x
@@ -86,7 +86,7 @@ fn test_arrays() -> Result<(), Box<EvalAltResult>> {
);
assert_eq!(
convert_to_vec::<INT>(engine.eval(
r"
"
let x = [1, 2, 3];
let y = [4, 5];
x + y
@@ -136,7 +136,7 @@ fn test_array_with_structs() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine.eval::<INT>(
r"
"
let a = [new_ts()];
a[0].x = 100;
a[0].update();
@@ -158,7 +158,7 @@ fn test_arrays_map_reduce() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
convert_to_vec::<INT>(engine.eval(
r"
"
let x = [1, 2, 3];
x.filter(|v| v > 2)
"
@@ -168,7 +168,7 @@ fn test_arrays_map_reduce() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
convert_to_vec::<INT>(engine.eval(
r"
"
let x = [1, 2, 3];
x.filter(|v, i| v > i)
"
@@ -178,7 +178,7 @@ fn test_arrays_map_reduce() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
convert_to_vec::<INT>(engine.eval(
r"
"
let x = [1, 2, 3];
x.map(|v| v * 2)
"
@@ -188,7 +188,7 @@ fn test_arrays_map_reduce() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
convert_to_vec::<INT>(engine.eval(
r"
"
let x = [1, 2, 3];
x.map(|v, i| v * i)
"

View File

@@ -21,41 +21,38 @@ fn test_assignments_bad_lhs() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
assert_eq!(
*engine.compile(r"(x+y) = 42;").expect_err("should error").0,
*engine.compile("(x+y) = 42;").expect_err("should error").0,
ParseErrorType::AssignmentToInvalidLHS("".to_string())
);
assert_eq!(
*engine.compile(r"foo(x) = 42;").expect_err("should error").0,
*engine.compile("foo(x) = 42;").expect_err("should error").0,
ParseErrorType::AssignmentToInvalidLHS("".to_string())
);
assert_eq!(
*engine.compile(r"true = 42;").expect_err("should error").0,
*engine.compile("true = 42;").expect_err("should error").0,
ParseErrorType::AssignmentToConstant("".to_string())
);
assert_eq!(
*engine.compile(r"123 = 42;").expect_err("should error").0,
*engine.compile("123 = 42;").expect_err("should error").0,
ParseErrorType::AssignmentToConstant("".to_string())
);
#[cfg(not(feature = "no_object"))]
{
assert_eq!(
*engine.compile("x.foo() = 42;").expect_err("should error").0,
ParseErrorType::AssignmentToInvalidLHS("".to_string())
);
assert_eq!(
*engine
.compile(r"x.foo() = 42;")
.compile("x.foo().x.y = 42;")
.expect_err("should error")
.0,
ParseErrorType::AssignmentToInvalidLHS("".to_string())
);
assert_eq!(
*engine
.compile(r"x.foo().x.y = 42;")
.expect_err("should error")
.0,
ParseErrorType::AssignmentToInvalidLHS("".to_string())
);
assert_eq!(
*engine
.compile(r"x.y.z.foo() = 42;")
.compile("x.y.z.foo() = 42;")
.expect_err("should error")
.0,
ParseErrorType::AssignmentToInvalidLHS("".to_string())
@@ -63,7 +60,7 @@ fn test_assignments_bad_lhs() -> Result<(), Box<EvalAltResult>> {
#[cfg(not(feature = "no_index"))]
assert_eq!(
*engine
.compile(r"x.foo()[0] = 42;")
.compile("x.foo()[0] = 42;")
.expect_err("should error")
.0,
ParseErrorType::AssignmentToInvalidLHS("".to_string())
@@ -71,7 +68,7 @@ fn test_assignments_bad_lhs() -> Result<(), Box<EvalAltResult>> {
#[cfg(not(feature = "no_index"))]
assert_eq!(
*engine
.compile(r"x[y].z.foo() = 42;")
.compile("x[y].z.foo() = 42;")
.expect_err("should error")
.0,
ParseErrorType::AssignmentToInvalidLHS("".to_string())

View File

@@ -38,7 +38,7 @@ fn test_bool_op_short_circuit() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine.eval::<bool>(
r"
"
let x = true;
x || { throw; };
"
@@ -48,7 +48,7 @@ fn test_bool_op_short_circuit() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine.eval::<bool>(
r"
"
let x = false;
x && { throw; };
"
@@ -65,7 +65,7 @@ fn test_bool_op_no_short_circuit1() {
assert!(engine
.eval::<bool>(
r"
"
let x = true;
x | { throw; }
"
@@ -79,7 +79,7 @@ fn test_bool_op_no_short_circuit2() {
assert!(engine
.eval::<bool>(
r"
"
let x = false;
x & { throw; }
"

View File

@@ -10,7 +10,7 @@ fn test_call_fn() -> Result<(), Box<EvalAltResult>> {
scope.push("foo", 42 as INT);
let ast = engine.compile(
r"
"
fn hello(x, y) {
x + y
}

View File

@@ -7,7 +7,7 @@ fn test_chars() -> Result<(), Box<EvalAltResult>> {
assert_eq!(engine.eval::<char>("'y'")?, 'y');
assert_eq!(engine.eval::<char>(r"'\''")?, '\'');
assert_eq!(engine.eval::<char>(r#"'"'"#)?, '"');
assert_eq!(engine.eval::<char>("'\\u2764'")?, '❤');
assert_eq!(engine.eval::<char>(r"'\u2764'")?, '❤');
#[cfg(not(feature = "no_index"))]
{

View File

@@ -54,7 +54,7 @@ fn test_closures() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine.eval::<INT>(
r"
"
let foo = #{ x: 42 };
let f = || { this.x };
foo.call(f)

View File

@@ -33,7 +33,7 @@ fn test_comments_doc() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
let ast = engine.compile(
r"
"
/// Hello world
@@ -48,7 +48,7 @@ fn test_comments_doc() -> Result<(), Box<EvalAltResult>> {
assert!(engine
.compile(
r"
"
/// Hello world
let x = 42;
"
@@ -56,7 +56,7 @@ fn test_comments_doc() -> Result<(), Box<EvalAltResult>> {
.is_err());
engine.compile(
r"
"
///////////////
let x = 42;
@@ -66,7 +66,7 @@ fn test_comments_doc() -> Result<(), Box<EvalAltResult>> {
)?;
let ast = engine.compile(
r"
"
/** Hello world
** how are you?
**/
@@ -82,7 +82,7 @@ fn test_comments_doc() -> Result<(), Box<EvalAltResult>> {
assert!(engine
.compile(
r"
"
/** Hello world */
let x = 42;
"
@@ -92,7 +92,7 @@ fn test_comments_doc() -> Result<(), Box<EvalAltResult>> {
engine.enable_doc_comments(false);
engine.compile(
r"
"
/// Hello world!
let x = 42;

View File

@@ -59,7 +59,7 @@ fn test_constant_mut() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine.eval_with_scope::<INT>(
&mut scope,
r"
"
MY_NUMBER.update_value(42);
MY_NUMBER.value
",

View File

@@ -91,7 +91,7 @@ fn test_max_array_size() -> Result<(), Box<EvalAltResult>> {
assert!(matches!(
*engine
.eval::<Array>(
r"
"
let x = [1,2,3,4,5,6];
let y = [7,8,9,10,11,12];
x + y
@@ -105,7 +105,7 @@ fn test_max_array_size() -> Result<(), Box<EvalAltResult>> {
assert!(matches!(
*engine
.eval::<Array>(
r"
"
let x = [1,2,3,4,5,6];
x.pad(100, 42);
x
@@ -118,7 +118,7 @@ fn test_max_array_size() -> Result<(), Box<EvalAltResult>> {
assert!(matches!(
*engine
.eval::<Array>(
r"
"
let x = [1,2,3];
[x, x, x, x]
"
@@ -131,7 +131,7 @@ fn test_max_array_size() -> Result<(), Box<EvalAltResult>> {
assert!(matches!(
*engine
.eval::<Array>(
r"
"
let x = #{a:1, b:2, c:3};
[x, x, x, x]
"
@@ -143,7 +143,7 @@ fn test_max_array_size() -> Result<(), Box<EvalAltResult>> {
assert!(matches!(
*engine
.eval::<Array>(
r"
"
let x = [1];
let y = [x, x];
let z = [y, y];
@@ -159,7 +159,7 @@ fn test_max_array_size() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine
.eval::<Array>(
r"
"
let x = [1,2,3,4,5,6];
let y = [7,8,9,10,11,12];
x + y
@@ -172,7 +172,7 @@ fn test_max_array_size() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine
.eval::<Array>(
r"
"
let x = [1,2,3];
[x, x, x, x]
"
@@ -209,7 +209,7 @@ fn test_max_map_size() -> Result<(), Box<EvalAltResult>> {
assert!(matches!(
*engine
.eval::<Map>(
r"
"
let x = #{a:1,b:2,c:3,d:4,e:5,f:6};
let y = #{g:7,h:8,i:9,j:10,k:11,l:12};
x + y
@@ -222,7 +222,7 @@ fn test_max_map_size() -> Result<(), Box<EvalAltResult>> {
assert!(matches!(
*engine
.eval::<Map>(
r"
"
let x = #{a:1,b:2,c:3};
#{u:x, v:x, w:x, z:x}
"
@@ -235,7 +235,7 @@ fn test_max_map_size() -> Result<(), Box<EvalAltResult>> {
assert!(matches!(
*engine
.eval::<Map>(
r"
"
let x = [1, 2, 3];
#{u:x, v:x, w:x, z:x}
"
@@ -249,7 +249,7 @@ fn test_max_map_size() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine
.eval::<Map>(
r"
"
let x = #{a:1,b:2,c:3,d:4,e:5,f:6};
let y = #{g:7,h:8,i:9,j:10,k:11,l:12};
x + y
@@ -262,7 +262,7 @@ fn test_max_map_size() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine
.eval::<Map>(
r"
"
let x = #{a:1,b:2,c:3};
#{u:x, v:x, w:x, z:x}
"

View File

@@ -5,7 +5,7 @@ use rhai::{Engine, EvalAltResult, Module, INT};
fn test_for() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
let script = r"
let script = "
let sum1 = 0;
let sum2 = 0;
let inputs = [1, 2, 3, 4, 5];
@@ -29,7 +29,7 @@ fn test_for() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine.eval::<INT>(
r"
"
let sum = 0;
for x in range(1, 10, 2) { sum += x; }
sum
@@ -40,7 +40,7 @@ fn test_for() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine.eval::<INT>(
r"
"
let sum = 0;
for x in range(10, 1, 2) { sum += x; }
sum
@@ -51,7 +51,7 @@ fn test_for() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine.eval::<INT>(
r"
"
let sum = 0;
for x in range(1, 10, -2) { sum += x; }
sum
@@ -62,7 +62,7 @@ fn test_for() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine.eval::<INT>(
r"
"
let sum = 0;
for x in range(10, 1, -2) { sum += x; }
sum
@@ -80,7 +80,7 @@ fn test_for_overflow() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
#[cfg(not(feature = "only_i32"))]
let script = r"
let script = "
let sum = 0;
for x in range(9223372036854775807, 0, 9223372036854775807) {
@@ -90,7 +90,7 @@ fn test_for_overflow() -> Result<(), Box<EvalAltResult>> {
sum
";
#[cfg(feature = "only_i32")]
let script = r"
let script = "
let sum = 0;
for x in range(2147483647 , 0, 2147483647 ) {

View File

@@ -67,7 +67,7 @@ fn test_functions_namespaces() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine.eval::<INT>(
r"
"
const ANSWER = 42;
fn foo() { global::ANSWER }

View File

@@ -13,7 +13,7 @@ fn test_if() -> Result<(), Box<EvalAltResult>> {
);
assert_eq!(
engine.eval::<INT>(
r"
"
if false { 55 }
else if false { 33 }
else if false { 66 }
@@ -34,7 +34,7 @@ fn test_if_expr() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine.eval::<INT>(
r"
"
let x = 42;
let y = 1 + if x > 40 { 100 } else { 0 } / x;
y

View File

@@ -70,7 +70,7 @@ fn test_internal_fn_big() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine.eval::<INT>(
r"
"
fn math_me(a, b, c, d, e, f) {
a - b * c + d * e - f
}
@@ -89,7 +89,7 @@ fn test_internal_fn_overloading() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine.eval::<INT>(
r"
"
fn abc(x,y,z) { 2*x + 3*y + 4*z + 888 }
fn abc(x,y) { x + 2*y + 88 }
fn abc() { 42 }
@@ -104,7 +104,7 @@ fn test_internal_fn_overloading() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
*engine
.compile(
r"
"
fn abc(x) { x + 42 }
fn abc(x) { x - 42 }
"

View File

@@ -6,7 +6,7 @@ fn test_loop() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine.eval::<INT>(
r"
"
let x = 0;
let i = 0;

View File

@@ -72,7 +72,7 @@ b`: 1}; y["a\nb"]
);
assert_eq!(
engine.eval::<INT>(
r"
"
let x = #{a: 1, b: 2, c: 3};
let y = #{b: 42, d: 9};
x.mixin(y);
@@ -83,7 +83,7 @@ b`: 1}; y["a\nb"]
);
assert_eq!(
engine.eval::<INT>(
r"
"
let x = #{a: 1, b: 2, c: 3};
x += #{b: 42, d: 9};
x.len() + x.b
@@ -94,7 +94,7 @@ b`: 1}; y["a\nb"]
assert_eq!(
engine
.eval::<Map>(
r"
"
let x = #{a: 1, b: 2, c: 3};
let y = #{b: 42, d: 9};
x + y

View File

@@ -30,7 +30,7 @@ fn test_mismatched_op_custom_type() -> Result<(), Box<EvalAltResult>> {
.register_type_with_name::<TestStruct>("TestStruct")
.register_fn("new_ts", TestStruct::new);
assert!(matches!(*engine.eval::<bool>(r"
assert!(matches!(*engine.eval::<bool>("
let x = new_ts();
let y = new_ts();
x == y

View File

@@ -381,13 +381,13 @@ fn test_module_export() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
assert!(matches!(
engine.compile(r"let x = 10; { export x; }").expect_err("should error"),
engine.compile("let x = 10; { export x; }").expect_err("should error"),
ParseError(x, _) if *x == ParseErrorType::WrongExport
));
#[cfg(not(feature = "no_function"))]
assert!(matches!(
engine.compile(r"fn abc(x) { export x; }").expect_err("should error"),
engine.compile("fn abc(x) { export x; }").expect_err("should error"),
ParseError(x, _) if *x == ParseErrorType::WrongExport
));

View File

@@ -5,9 +5,9 @@ use rhai::{Engine, EvalAltResult, OptimizationLevel, INT};
#[test]
fn test_optimizer_run() -> Result<(), Box<EvalAltResult>> {
fn run_test(engine: &mut Engine) -> Result<(), Box<EvalAltResult>> {
assert_eq!(engine.eval::<INT>(r"if true { 42 } else { 123 }")?, 42);
assert_eq!(engine.eval::<INT>("if true { 42 } else { 123 }")?, 42);
assert_eq!(
engine.eval::<INT>(r"if 1 == 1 || 2 > 3 { 42 } else { 123 }")?,
engine.eval::<INT>("if 1 == 1 || 2 > 3 { 42 } else { 123 }")?,
42
);
assert_eq!(
@@ -34,14 +34,14 @@ fn test_optimizer_run() -> Result<(), Box<EvalAltResult>> {
engine.set_optimization_level(OptimizationLevel::Simple);
assert_eq!(
engine.eval::<INT>(r"if 1 == 1 || 2 > 3 { 42 } else { 123 }")?,
engine.eval::<INT>("if 1 == 1 || 2 > 3 { 42 } else { 123 }")?,
123
);
engine.set_optimization_level(OptimizationLevel::Full);
assert_eq!(
engine.eval::<INT>(r"if 1 == 1 || 2 > 3 { 42 } else { 123 }")?,
engine.eval::<INT>("if 1 == 1 || 2 > 3 { 42 } else { 123 }")?,
123
);

View File

@@ -49,7 +49,7 @@ fn test_side_effects_command() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine.eval_with_scope::<INT>(
&mut scope,
r"
"
// Drive the command object via the wrapper
Command.action(30);
Command.value

File diff suppressed because one or more lines are too long

View File

@@ -319,7 +319,7 @@ fn test_string_interpolated() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine.eval::<String>(
r"
"
let x = 40;
`hello ${x+2} worlds!`
"
@@ -339,7 +339,7 @@ fn test_string_interpolated() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine.eval::<String>(
r"
"
const x = 42;
`hello ${x} worlds!`
"
@@ -351,7 +351,7 @@ fn test_string_interpolated() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine.eval::<String>(
r"
"
const x = 42;
`${x} worlds!`
"
@@ -361,7 +361,7 @@ fn test_string_interpolated() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine.eval::<String>(
r"
"
const x = 42;
`hello ${x}`
"
@@ -371,7 +371,7 @@ fn test_string_interpolated() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine.eval::<String>(
r"
"
const x = 20;
`hello ${let y = x + 1; `${y * 2}`} worlds!`
"

View File

@@ -33,7 +33,7 @@ fn test_switch() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine.eval_with_scope::<INT>(
&mut scope,
r"
"
let y = [1, 2, 3];
switch y {
@@ -50,7 +50,7 @@ fn test_switch() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine.eval_with_scope::<INT>(
&mut scope,
r"
"
let y = #{a:1, b:true, c:'x'};
switch y {
@@ -98,7 +98,7 @@ fn test_switch_condition() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine.eval_with_scope::<INT>(
&mut scope,
r"
"
switch x / 2 {
21 if x > 40 => 1,
0 if x < 100 => 2,
@@ -113,7 +113,7 @@ fn test_switch_condition() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine.eval_with_scope::<INT>(
&mut scope,
r"
"
switch x / 2 {
21 if x < 40 => 1,
0 if x < 100 => 2,
@@ -128,7 +128,7 @@ fn test_switch_condition() -> Result<(), Box<EvalAltResult>> {
assert!(matches!(
*engine
.compile(
r"
"
switch x {
21 if x < 40 => 1,
21 if x == 10 => 10,

View File

@@ -57,7 +57,7 @@ fn test_custom_syntax() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine.eval::<INT>(
r"
"
let x = 0;
let foo = (exec |x| -> { x += 2 } while x < 42) * 10;
foo
@@ -67,7 +67,7 @@ fn test_custom_syntax() -> Result<(), Box<EvalAltResult>> {
);
assert_eq!(
engine.eval::<INT>(
r"
"
let x = 0;
exec |x| -> { x += 1 } while x < 42;
x
@@ -77,7 +77,7 @@ fn test_custom_syntax() -> Result<(), Box<EvalAltResult>> {
);
assert_eq!(
engine.eval::<INT>(
r"
"
exec |x| -> { x += 1 } while x < 42;
x
"

View File

@@ -40,7 +40,7 @@ fn test_timestamp() -> Result<(), Box<EvalAltResult>> {
);
assert!(engine.eval::<bool>(
r"
"
let time1 = timestamp();
for x in range(0, 10000) {}
let time2 = timestamp();

View File

@@ -49,7 +49,7 @@ fn test_tokens_custom_operator_identifiers() -> Result<(), Box<EvalAltResult>> {
#[cfg(not(feature = "no_function"))]
assert_eq!(
engine.eval::<INT>(
r"
"
fn foo(x, y) { y - x }
1 + 2 * 3 foo 4 - 5 / 6
"
@@ -87,7 +87,7 @@ fn test_tokens_custom_operator_symbol() -> Result<(), Box<EvalAltResult>> {
fn test_tokens_unicode_xid_ident() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
let result = engine.eval::<INT>(
r"
"
fn すべての答え() { 42 }
すべての答え()
",
@@ -99,7 +99,7 @@ fn test_tokens_unicode_xid_ident() -> Result<(), Box<EvalAltResult>> {
assert!(result.is_err());
let result = engine.eval::<INT>(
r"
"
fn _1() { 1 }
_1()
",

View File

@@ -6,7 +6,7 @@ fn test_while() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine.eval::<INT>(
r"
"
let x = 0;
while x < 10 {
@@ -31,7 +31,7 @@ fn test_do() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine.eval::<INT>(
r"
"
let x = 0;
do {