with Ada.Text_IO, Ada.Integer_Text_IO; use Ada.Text_IO, Ada.Integer_Text_IO; with Environment; use Environment; procedure Test_Environment is -- Copyright (C) 2003 David A. Wheeler -- This version dated 2003-12-18. -- Released under the so-called "MIT/X license": -- Permission is hereby granted, free of charge, to any person -- obtaining a copy of this software and associated documentation files -- (the "Software"), to deal in the Software without restriction, -- including without limitation the rights to use, copy, modify, merge, -- publish, distribute, sublicense, and/or sell copies of the Software, -- and to permit persons to whom the Software is furnished to do so, -- subject to the following conditions: -- -- The above copyright notice and this permission notice shall be -- included in all copies or substantial portions of the Software. -- -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE -- FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Original_Path : String := Get_Environment("PATH"); procedure Must_Succeed(Result : Boolean; Error_Message : String) is begin if Result then return; else Put("ERROR: "); Put_Line(Error_Message); raise Constraint_Error; end if; end Must_Succeed; begin -- This automated test presumes PATH is set, -- while NOT-set and NEW_ENV_VAR are not set. Put_Line("Testing the Environment package implementation."); Put_Line("To pass this test, the final line must be 'Test Succeeded!'"); Put_Line("The rest of the text below simply gives additional information,"); Put_Line("so you can check that it is operating as expected."); New_Line; Put("PATH="); Put_Line(Get_Environment("PATH")); Must_Succeed(Exists_In_Environment("PATH"), "Can't find PATH"); Must_Succeed(not Exists_In_Environment("NOT-set"), "Found NOT-set"); Must_Succeed(Get_Environment("NOT-set", "PIGGY") = "PIGGY", "Default failed"); Must_Succeed(Get_Environment("PATH", "PIGGY") /= "PIGGY", "Default failed"); Put("First entry: "); Put_Line(Get_Pair_Position(1)); Must_Succeed( Get_Pair_Position(1) = (Get_Key_Position(1) & "=" & Get_Environment_Position(1)), "Could not reconstruct Get_Pair_Position"); -- Output entire environment, so that it can be inspected. Put("Environment_Count="); Put(Environment_Count); New_Line; Put_Line("Here is the list of all environment variables:"); for I in 1 .. Environment_Count loop Put_Line(Get_Pair_Position(I)); end loop; Set_Environment("PATH", "buggly", False); Must_Succeed( Get_Environment("PATH") = Original_Path, "Whups, overwrite"); Set_Environment("PATH", "buggly"); Must_Succeed( Get_Environment("PATH") = "buggly", "Whups, didn't overwrite"); Set_Environment("PATH", Original_Path); Must_Succeed( Get_Environment("PATH") = Original_Path, "Whups, no reset"); Set_Environment("NEW_ENV_VAR", "test-value"); Must_Succeed( Get_Environment("NEW_ENV_VAR") = "test-value", "Whups, couldn't set a new value"); Set_Environment_Pair("PATH=fiddle"); Must_Succeed( Get_Environment("PATH") = "fiddle", "Pair didn't work"); Set_Environment("PATH", Original_Path); Unset_Environment("PATH"); Must_Succeed(not Exists_In_Environment("PATH"), "Didn't unset PATH"); Set_Environment("PATH", Original_Path); Must_Succeed(Exists_In_Environment("PATH"), "Didn't reset PATH"); Clear_Environment; Must_Succeed(not Exists_In_Environment("PATH"), "Didn't drop PATH"); Must_Succeed(Environment_Count = 0, "Environment_Count nonzero"); Put_Line("Test Succeeded!"); end Test_Environment;