@@ -2343,3 +2343,47 @@ fn test_comments() {
2343
2343
Ok ( Value :: Int ( 21 ) )
2344
2344
) ;
2345
2345
}
2346
+
2347
+ #[ test]
2348
+ fn test_clear ( ) {
2349
+ let mut context = HashMapContext :: new ( ) ;
2350
+ context. set_value ( "abc" . into ( ) , "def" . into ( ) ) . unwrap ( ) ;
2351
+ assert_eq ! ( context. get_value( "abc" ) , Some ( & ( "def" . into( ) ) ) ) ;
2352
+ context. clear_functions ( ) ;
2353
+ assert_eq ! ( context. get_value( "abc" ) , Some ( & ( "def" . into( ) ) ) ) ;
2354
+ context. clear_variables ( ) ;
2355
+ assert_eq ! ( context. get_value( "abc" ) , None ) ;
2356
+
2357
+ context
2358
+ . set_function (
2359
+ "abc" . into ( ) ,
2360
+ Function :: new ( |input| Ok ( Value :: String ( format ! ( "{input}" ) ) ) ) ,
2361
+ )
2362
+ . unwrap ( ) ;
2363
+ assert_eq ! (
2364
+ eval_with_context( "abc(5)" , & context) . unwrap( ) ,
2365
+ Value :: String ( "5" . into( ) )
2366
+ ) ;
2367
+ context. clear_variables ( ) ;
2368
+ assert_eq ! (
2369
+ eval_with_context( "abc(5)" , & context) . unwrap( ) ,
2370
+ Value :: String ( "5" . into( ) )
2371
+ ) ;
2372
+ context. clear_functions ( ) ;
2373
+ assert ! ( eval_with_context( "abc(5)" , & context) . is_err( ) ) ;
2374
+
2375
+ context. set_value ( "five" . into ( ) , 5 . into ( ) ) . unwrap ( ) ;
2376
+ context
2377
+ . set_function (
2378
+ "abc" . into ( ) ,
2379
+ Function :: new ( |input| Ok ( Value :: String ( format ! ( "{input}" ) ) ) ) ,
2380
+ )
2381
+ . unwrap ( ) ;
2382
+ assert_eq ! (
2383
+ eval_with_context( "abc(five)" , & context) . unwrap( ) ,
2384
+ Value :: String ( "5" . into( ) )
2385
+ ) ;
2386
+ context. clear ( ) ;
2387
+ assert ! ( context. get_value( "five" ) . is_none( ) ) ;
2388
+ assert ! ( eval_with_context( "abc(5)" , & context) . is_err( ) ) ;
2389
+ }
0 commit comments