Fix parsing f64 from Scalar::Int (#777)
This commit is contained in:
parent
f914322ef4
commit
46cde4fb85
1 changed files with 44 additions and 3 deletions
|
@ -325,7 +325,11 @@ where
|
|||
|
||||
fn from_str<'a>(value: ScalarToken<'a>) -> ParseScalarResult<'a, S> {
|
||||
match value {
|
||||
ScalarToken::Int(v) | ScalarToken::Float(v) => v
|
||||
ScalarToken::Int(v) => v
|
||||
.parse()
|
||||
.map_err(|_| ParseError::UnexpectedToken(Token::Scalar(value)))
|
||||
.map(|s: i32| f64::from(s).into()),
|
||||
ScalarToken::Float(v) => v
|
||||
.parse()
|
||||
.map_err(|_| ParseError::UnexpectedToken(Token::Scalar(value)))
|
||||
.map(|s: f64| s.into()),
|
||||
|
@ -334,7 +338,7 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
/// Utillity type to define read-only schemas
|
||||
/// Utility type to define read-only schemas
|
||||
///
|
||||
/// If you instantiate `RootNode` with this as the mutation, no mutation will be
|
||||
/// generated for the schema.
|
||||
|
@ -457,12 +461,13 @@ impl<T> Default for EmptySubscription<T> {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{EmptyMutation, EmptySubscription, ID};
|
||||
use crate::{
|
||||
parser::ScalarToken,
|
||||
value::{DefaultScalarValue, ParseScalarValue},
|
||||
};
|
||||
|
||||
use super::{EmptyMutation, EmptySubscription, ID};
|
||||
|
||||
#[test]
|
||||
fn test_id_from_string() {
|
||||
let actual = ID::from(String::from("foo"));
|
||||
|
@ -505,6 +510,42 @@ mod tests {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_f64_from_int() {
|
||||
for (v, expected) in &[
|
||||
("0", 0),
|
||||
("128", 128),
|
||||
("1601942400", 1601942400),
|
||||
("1696550400", 1696550400),
|
||||
("-1", -1),
|
||||
] {
|
||||
let n = <f64 as ParseScalarValue<DefaultScalarValue>>::from_str(ScalarToken::Int(v));
|
||||
assert!(n.is_ok(), "A parsing error occurred: {:?}", n.unwrap_err());
|
||||
|
||||
let n: Option<f64> = n.unwrap().into();
|
||||
assert!(n.is_some(), "No f64 returned");
|
||||
assert_eq!(n.unwrap(), f64::from(*expected));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_f64_from_float() {
|
||||
for (v, expected) in &[
|
||||
("0.", 0.),
|
||||
("1.2", 1.2),
|
||||
("1601942400.", 1601942400.),
|
||||
("1696550400.", 1696550400.),
|
||||
("-1.2", -1.2),
|
||||
] {
|
||||
let n = <f64 as ParseScalarValue<DefaultScalarValue>>::from_str(ScalarToken::Float(v));
|
||||
assert!(n.is_ok(), "A parsing error occurred: {:?}", n.unwrap_err());
|
||||
|
||||
let n: Option<f64> = n.unwrap().into();
|
||||
assert!(n.is_some(), "No f64 returned");
|
||||
assert_eq!(n.unwrap(), *expected);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_mutation_is_send() {
|
||||
fn check_if_send<T: Send>() {}
|
||||
|
|
Loading…
Reference in a new issue