@@ -2024,6 +2024,40 @@ public function test_export_import_variable_context(): void {
20242024 self ::assertNotNull ($ e );
20252025 }
20262026
2027+ public function test_import_single_variable (): void {
2028+ // Prepare an empty evaluator.
2029+ $ evaluator = new evaluator ();
2030+
2031+ // Add variables.
2032+ $ evaluator ->import_single_variable ('_foo ' , new variable ('_foo ' , 'value ' , token::STRING ));
2033+ $ evaluator ->import_single_variable ('bar ' , new variable ('bar ' , 1234 , token::NUMBER ));
2034+
2035+ // Verify the evaluator contains the correct variables with the correct values.
2036+ $ variables = $ evaluator ->export_variable_list ();
2037+ self ::assertCount (2 , $ variables );
2038+ self ::assertContains ('_foo ' , $ variables );
2039+ self ::assertContains ('bar ' , $ variables );
2040+
2041+ $ foo = $ evaluator ->export_single_variable ('_foo ' );
2042+ self ::assertEquals (token::STRING , $ foo ->type );
2043+ self ::assertEquals ('value ' , $ foo ->value );
2044+
2045+ $ bar = $ evaluator ->export_single_variable ('bar ' );
2046+ self ::assertEquals (token::NUMBER , $ bar ->type );
2047+ self ::assertEquals (1234 , $ bar ->value );
2048+
2049+ // Try to set variable again without specifying the overwrite parameter. The value should not change.
2050+ $ evaluator ->import_single_variable ('bar ' , new variable ('bar ' , 5678 , token::NUMBER ));
2051+ $ bar = $ evaluator ->export_single_variable ('bar ' );
2052+ self ::assertEquals (1234 , $ bar ->value );
2053+
2054+ // Try again, forcing overwrite this time. The value should thus change.
2055+ $ evaluator ->import_single_variable ('bar ' , new variable ('bar ' , 5678 , token::NUMBER ), true );
2056+ $ bar = $ evaluator ->export_single_variable ('bar ' );
2057+ self ::assertEquals (5678 , $ bar ->value );
2058+ }
2059+
2060+
20272061 /**
20282062 * Provide answers of answer type number.
20292063 *
0 commit comments