Skip to content

Commit

Permalink
Test and fix math_consts_context! macro.
Browse files Browse the repository at this point in the history
Closes #173
  • Loading branch information
ISibboI committed Oct 25, 2024
1 parent 2dee218 commit 31f1959
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,12 +419,12 @@ macro_rules! context_map {
}};
// add an integer value, and chain the eventual error with the ones in the next values
( ($ctx:expr) $k:expr => int $v:expr , $($tt:tt)*) => {{
$crate::ContextWithMutableVariables::set_value($ctx, $k.into(), Value::from_int($v.into()))
$crate::ContextWithMutableVariables::set_value($ctx, $k.into(), $crate::Value::from_int($v.into()))
.and($crate::context_map!(($ctx) $($tt)*))
}};
// add a float value, and chain the eventual error with the ones in the next values
( ($ctx:expr) $k:expr => float $v:expr , $($tt:tt)*) => {{
$crate::ContextWithMutableVariables::set_value($ctx, $k.into(), Value::from_float($v.into()))
$crate::ContextWithMutableVariables::set_value($ctx, $k.into(), $crate::Value::from_float($v.into()))
.and($crate::context_map!(($ctx) $($tt)*))
}};
// add a value, and chain the eventual error with the ones in the next values
Expand Down
21 changes: 19 additions & 2 deletions src/context/predefined/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,28 @@ macro_rules! math_consts_context {
)
};
($($name:ident),*) => {{
use $crate::ContextWithMutableVariables;
$crate::context_map! {
$(
stringify!($name) => core::f64::consts::$name,
stringify!($name) => float core::f64::consts::$name,
)*
}
}};
}

#[cfg(test)]
mod tests {

#[test]
fn math_consts_context() {
let context: crate::HashMapContext = math_consts_context!().unwrap();

{
use crate::{Context, Value};

assert_eq!(
context.get_value("PI"),
Some(&Value::Float(core::f64::consts::PI))
);
}
}
}

0 comments on commit 31f1959

Please sign in to comment.