Skip to content

Commit

Permalink
Update to accept any number of label arguments and fixing unit tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Chad Greenburg <[email protected]>
  • Loading branch information
dkryptr committed Oct 25, 2021
1 parent feb2546 commit 286cff3
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ fn test_opts_trailing_comma() {
/// let opts = histogram_opts!(name,
/// help,
/// vec![1.0, 2.0],
/// labels!{"key".to_string() => "value".to_string(),});
/// labels!{"key" => "value",});
/// assert_eq!(opts.common_opts.name, name);
/// assert_eq!(opts.common_opts.help, help);
/// assert_eq!(opts.buckets.len(), 2);
Expand All @@ -160,9 +160,10 @@ macro_rules! histogram_opts {
hopts.buckets($BUCKETS)
}};

($NAME:expr, $HELP:expr, $BUCKETS:expr, $CONST_LABELS:expr $(,)?) => {{
($NAME:expr, $HELP:expr, $BUCKETS:expr $ ( , $CONST_LABELS:expr ) * $ ( , ) ? ) => {{
use std::collections::HashMap;

let hopts = histogram_opts!($NAME, $HELP, $BUCKETS);

let lbs = HashMap::<String, String>::new();
$(
let mut lbs = lbs;
Expand Down Expand Up @@ -193,13 +194,16 @@ fn test_histogram_opts_trailing_comma() {
name,
help,
vec![1.0, 2.0],
labels! {"key".to_string() => "value".to_string(),},
labels! {"key" => "value",},
labels! {"key2" => "value2",},
);
assert_eq!(opts.common_opts.name, name);
assert_eq!(opts.common_opts.help, help);
assert_eq!(opts.buckets.len(), 2);
assert!(opts.common_opts.const_labels.get("key").is_some());
assert_eq!(opts.common_opts.const_labels.get("key").unwrap(), "value");
assert!(opts.common_opts.const_labels.get("key2").is_some());
assert_eq!(opts.common_opts.const_labels.get("key2").unwrap(), "value2");
}

/// Create a [`Counter`] and registers to default registry.
Expand Down

0 comments on commit 286cff3

Please sign in to comment.