@@ -147,3 +147,50 @@ fn replace_vars<'h>(haystack: &'h str, variables: &HashMap<&str, &str>) -> Strin
147147 } )
148148 . into_owned ( )
149149}
150+
151+ #[ cfg( test) ]
152+ mod tests {
153+ use std:: collections:: HashMap ;
154+
155+ use super :: * ;
156+
157+ #[ test]
158+ fn test_basic_replacement ( ) {
159+ let mut variables = HashMap :: new ( ) ;
160+ variables. insert ( "name" , "Mati" ) ;
161+ variables. insert ( "greeting" , "Hello" ) ;
162+
163+ let haystack = "%{greeting}, %{name}!" ;
164+ assert_eq ! ( replace_vars( haystack, & variables) , "Hello, Mati!" ) ;
165+ }
166+
167+ #[ test]
168+ #[ should_panic]
169+ fn test_missing_variable ( ) {
170+ let variables = HashMap :: new ( ) ;
171+ let haystack = "Hi %{name}!" ;
172+
173+ replace_vars ( haystack, & variables) ;
174+ }
175+
176+ #[ test]
177+ fn test_multiple_occurrences ( ) {
178+ let mut variables = HashMap :: new ( ) ;
179+ variables. insert ( "word" , "rust" ) ;
180+
181+ let haystack = "I love %{word}! %{word} is great!" ;
182+ assert_eq ! (
183+ replace_vars( haystack, & variables) ,
184+ "I love rust! rust is great!"
185+ ) ;
186+ }
187+
188+ #[ test]
189+ #[ should_panic]
190+ fn test_multiple_missing_variables ( ) {
191+ let variables = HashMap :: new ( ) ;
192+ let haystack = "Hi %{name}, welcome to %{city}!" ;
193+
194+ replace_vars ( haystack, & variables) ;
195+ }
196+ }
0 commit comments