Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent and misleading? behavior for FluentArgs::set #271

Merged
merged 2 commits into from
Oct 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 31 additions & 4 deletions fluent-bundle/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,10 @@ impl<'args> FluentArgs<'args> {
V: Into<FluentValue<'args>>,
{
let key = key.into();
let idx = match self.0.binary_search_by_key(&&key, |(k, _)| k) {
Ok(idx) => idx,
Err(idx) => idx,
match self.0.binary_search_by_key(&&key, |(k, _)| k) {
Ok(idx) => self.0[idx] = (key, value.into()),
Err(idx) => self.0.insert(idx, (key, value.into())),
};
self.0.insert(idx, (key, value.into()));
}

pub fn iter(&self) -> impl Iterator<Item = (&str, &FluentValue)> {
Expand Down Expand Up @@ -118,3 +117,31 @@ impl<'args> IntoIterator for FluentArgs<'args> {
self.0.into_iter()
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn replace_existing_arguments() {
let mut args = FluentArgs::new();

args.set("name", "John");
args.set("emailCount", 5);
assert_eq!(args.0.len(), 2);
assert_eq!(
args.get("name"),
Some(&FluentValue::String(Cow::Borrowed("John")))
);
assert_eq!(args.get("emailCount"), Some(&FluentValue::try_number(5)));

args.set("name", "Jane");
args.set("emailCount", 7);
assert_eq!(args.0.len(), 2);
assert_eq!(
args.get("name"),
Some(&FluentValue::String(Cow::Borrowed("Jane")))
);
assert_eq!(args.get("emailCount"), Some(&FluentValue::try_number(7)));
}
}