@@ -74,11 +74,10 @@ impl<'args> FluentArgs<'args> {
74
74
V : Into < FluentValue < ' args > > ,
75
75
{
76
76
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 ( ) ) ) ,
80
80
} ;
81
- self . 0 . insert ( idx, ( key, value. into ( ) ) ) ;
82
81
}
83
82
84
83
pub fn iter ( & self ) -> impl Iterator < Item = ( & str , & FluentValue ) > {
@@ -118,3 +117,31 @@ impl<'args> IntoIterator for FluentArgs<'args> {
118
117
self . 0 . into_iter ( )
119
118
}
120
119
}
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