Fix panic when adding number to string
For no reason, unwraps were used in the operator module. Fixes #59
This commit is contained in:
parent
d8eed924cc
commit
a3faefb893
@ -32,6 +32,7 @@
|
||||
* Allow variable assignments in eval calls without context.
|
||||
A `HashMapContext` is created automatically now.
|
||||
* The error string for `ExpectedNumber` was wrong
|
||||
* Operators panicked when adding a number to a string
|
||||
|
||||
### Deprecated
|
||||
|
||||
|
@ -165,7 +165,7 @@ impl Operator {
|
||||
}
|
||||
} else {
|
||||
Ok(Value::Float(
|
||||
arguments[0].as_number().unwrap() + arguments[1].as_number().unwrap(),
|
||||
arguments[0].as_number()? + arguments[1].as_number()?,
|
||||
))
|
||||
}
|
||||
},
|
||||
@ -186,7 +186,7 @@ impl Operator {
|
||||
}
|
||||
} else {
|
||||
Ok(Value::Float(
|
||||
arguments[0].as_number().unwrap() - arguments[1].as_number().unwrap(),
|
||||
arguments[0].as_number()? - arguments[1].as_number()?,
|
||||
))
|
||||
}
|
||||
},
|
||||
@ -202,7 +202,7 @@ impl Operator {
|
||||
Err(EvalexprError::negation_error(arguments[0].clone()))
|
||||
}
|
||||
} else {
|
||||
Ok(Value::Float(-arguments[0].as_number().unwrap()))
|
||||
Ok(Value::Float(-arguments[0].as_number()?))
|
||||
}
|
||||
},
|
||||
Mul => {
|
||||
@ -222,7 +222,7 @@ impl Operator {
|
||||
}
|
||||
} else {
|
||||
Ok(Value::Float(
|
||||
arguments[0].as_number().unwrap() * arguments[1].as_number().unwrap(),
|
||||
arguments[0].as_number()? * arguments[1].as_number()?,
|
||||
))
|
||||
}
|
||||
},
|
||||
@ -243,7 +243,7 @@ impl Operator {
|
||||
}
|
||||
} else {
|
||||
Ok(Value::Float(
|
||||
arguments[0].as_number().unwrap() / arguments[1].as_number().unwrap(),
|
||||
arguments[0].as_number()? / arguments[1].as_number()?,
|
||||
))
|
||||
}
|
||||
},
|
||||
@ -264,7 +264,7 @@ impl Operator {
|
||||
}
|
||||
} else {
|
||||
Ok(Value::Float(
|
||||
arguments[0].as_number().unwrap() % arguments[1].as_number().unwrap(),
|
||||
arguments[0].as_number()? % arguments[1].as_number()?,
|
||||
))
|
||||
}
|
||||
},
|
||||
@ -274,10 +274,7 @@ impl Operator {
|
||||
arguments[1].as_number()?;
|
||||
|
||||
Ok(Value::Float(
|
||||
arguments[0]
|
||||
.as_number()
|
||||
.unwrap()
|
||||
.powf(arguments[1].as_number().unwrap()),
|
||||
arguments[0].as_number()?.powf(arguments[1].as_number()?),
|
||||
))
|
||||
},
|
||||
Eq => {
|
||||
@ -316,7 +313,7 @@ impl Operator {
|
||||
Ok(Value::Boolean(false))
|
||||
}
|
||||
} else {
|
||||
if arguments[0].as_number().unwrap() > arguments[1].as_number().unwrap() {
|
||||
if arguments[0].as_number()? > arguments[1].as_number()? {
|
||||
Ok(Value::Boolean(true))
|
||||
} else {
|
||||
Ok(Value::Boolean(false))
|
||||
@ -341,7 +338,7 @@ impl Operator {
|
||||
Ok(Value::Boolean(false))
|
||||
}
|
||||
} else {
|
||||
if arguments[0].as_number().unwrap() < arguments[1].as_number().unwrap() {
|
||||
if arguments[0].as_number()? < arguments[1].as_number()? {
|
||||
Ok(Value::Boolean(true))
|
||||
} else {
|
||||
Ok(Value::Boolean(false))
|
||||
@ -366,7 +363,7 @@ impl Operator {
|
||||
Ok(Value::Boolean(false))
|
||||
}
|
||||
} else {
|
||||
if arguments[0].as_number().unwrap() >= arguments[1].as_number().unwrap() {
|
||||
if arguments[0].as_number()? >= arguments[1].as_number()? {
|
||||
Ok(Value::Boolean(true))
|
||||
} else {
|
||||
Ok(Value::Boolean(false))
|
||||
@ -391,7 +388,7 @@ impl Operator {
|
||||
Ok(Value::Boolean(false))
|
||||
}
|
||||
} else {
|
||||
if arguments[0].as_number().unwrap() <= arguments[1].as_number().unwrap() {
|
||||
if arguments[0].as_number()? <= arguments[1].as_number()? {
|
||||
Ok(Value::Boolean(true))
|
||||
} else {
|
||||
Ok(Value::Boolean(false))
|
||||
|
@ -695,3 +695,20 @@ fn test_operator_assignments() {
|
||||
Ok(true)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_type_errors_in_binary_operators() {
|
||||
// This error is bad. In future, maybe add a special error message for this kind of call.
|
||||
assert_eq!(
|
||||
eval("4 + \"abc\""),
|
||||
Err(EvalexprError::expected_number(Value::from(
|
||||
"abc".to_string()
|
||||
)))
|
||||
);
|
||||
assert_eq!(
|
||||
eval("\"abc\" + 4"),
|
||||
Err(EvalexprError::expected_number(Value::from(
|
||||
"abc".to_string()
|
||||
)))
|
||||
);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user