indexing description : "Illustrates usage of a simple unit_test" author: "DM" class SIMPLE_UNIT_TEST inherit UNIT_TEST creation make feature -- Initialization make is -- run the test. do make_test add_boolean_case (agent test_addition) add_boolean_case(agent test_array_items) add_violation_case (agent test_upper_bound_violation) to_html ("tests.html") end feature -- test cases test_addition : BOOLEAN is -- this test case can either pass or fail -- depending on the Result -- Also, a contract violation will cause this -- test case to fail do comment("test_addition: does 1+1=2 ?") Result := ( 1 + 1 = 2) end test_array_items : BOOLEAN is -- if you want to check for multiple things in a test case -- use a 'check result end' at each step -- In this example, we are checking that each item in the array is -- as we would expect it due to the initialization. -- We cannot omit the 'check result end' because `Result' is being -- overwritten in every assignment. local arr : ARRAY[INTEGER] do comment("test_array_items: Are all the items in the array correctly initialized?") arr := <<3,1,4,1,5,9>> Result := (arr @ 1) = 3 check Result end Result := (arr @ 2) = 1 check Result end Result := (arr @ 3) = 4 check Result end Result := (arr @ 4) = 1 check Result end Result := (arr @ 5) = 5 check Result end Result := (arr @ 6) = 9 check Result end end test_upper_bound_violation is -- Notice that unlike the boolean test cases, -- this is a procedure and not a function. -- A violation test case passes if a contract violation occurs -- and fails otherwise local arr : ARRAY[INTEGER] do comment("Should not be able to add a 4th element to an array of size 3") arr := <<1,2,3>> arr.put (4,4) -- should throw a violation since the size of the array is 3 end end -- class SIMPLE_UNIT_TEST