Skip to content

Commit 681245b

Browse files
Inconsistent and misleading? behavior for FluentArgs::set (#271)
* fix FluentArgs::set * update tests for replacing existing arguments
1 parent 638ce90 commit 681245b

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

fluent-bundle/src/args.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,10 @@ impl<'args> FluentArgs<'args> {
7474
V: Into<FluentValue<'args>>,
7575
{
7676
let key = key.into();
77-
let idx = match self.0.binary_search_by_key(&&key, |(k, _)| k) {
78-
Ok(idx) => idx,
79-
Err(idx) => idx,
77+
match self.0.binary_search_by_key(&&key, |(k, _)| k) {
78+
Ok(idx) => self.0[idx] = (key, value.into()),
79+
Err(idx) => self.0.insert(idx, (key, value.into())),
8080
};
81-
self.0.insert(idx, (key, value.into()));
8281
}
8382

8483
pub fn iter(&self) -> impl Iterator<Item = (&str, &FluentValue)> {
@@ -118,3 +117,31 @@ impl<'args> IntoIterator for FluentArgs<'args> {
118117
self.0.into_iter()
119118
}
120119
}
120+
121+
#[cfg(test)]
122+
mod tests {
123+
use super::*;
124+
125+
#[test]
126+
fn replace_existing_arguments() {
127+
let mut args = FluentArgs::new();
128+
129+
args.set("name", "John");
130+
args.set("emailCount", 5);
131+
assert_eq!(args.0.len(), 2);
132+
assert_eq!(
133+
args.get("name"),
134+
Some(&FluentValue::String(Cow::Borrowed("John")))
135+
);
136+
assert_eq!(args.get("emailCount"), Some(&FluentValue::try_number(5)));
137+
138+
args.set("name", "Jane");
139+
args.set("emailCount", 7);
140+
assert_eq!(args.0.len(), 2);
141+
assert_eq!(
142+
args.get("name"),
143+
Some(&FluentValue::String(Cow::Borrowed("Jane")))
144+
);
145+
assert_eq!(args.get("emailCount"), Some(&FluentValue::try_number(7)));
146+
}
147+
}

0 commit comments

Comments
 (0)