Skip to content

Commit

Permalink
Test and fix math_consts_context! macro (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
ISibboI authored Oct 25, 2024
2 parents 2dee218 + d8a8b05 commit 51fad69
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 18 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Change Log

## [12.0.1](https://github.com/ISibboI/evalexpr/compare/12.0.0...12.0.1) - 2024-10-25

### Fixed

* Fixed the `math_consts_context!` macro that was broken by in 12.0.0 (#173)

### Contributors

My warmhearted thanks goes to:

* [jeffrey182](https://github.com/jeffrey182)

## [12.0.0](https://github.com/ISibboI/evalexpr/compare/11.3.1...12.0.0) - 2024-10-17

### Changed
Expand Down
24 changes: 12 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ name = "evalexpr"
path = "src/lib.rs"

[dependencies]
regex = { version = "1.11.0", optional = true }
serde = { version = "1.0.210", features = ["derive"], optional = true }
regex = { version = "1.11.1", optional = true }
serde = { version = "1.0.213", features = ["derive"], optional = true }
rand = { version = "0.8.5", optional = true }

[features]
Expand Down
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 51fad69

Please sign in to comment.