From 2912e22000bff5b83b77daeb2b5ed111c47268b8 Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Tue, 8 Jan 2019 16:29:30 +1100 Subject: Packrat.Errors specification and tests --- test/ratnest-tests.adb | 144 +++++++++++++++++++++++++++++++++++++++++++++++++ test/ratnest-tests.ads | 22 ++++++++ test/test_main.adb | 4 ++ 3 files changed, 170 insertions(+) (limited to 'test') diff --git a/test/ratnest-tests.adb b/test/ratnest-tests.adb index 3309b2a..2e3478a 100644 --- a/test/ratnest-tests.adb +++ b/test/ratnest-tests.adb @@ -12,12 +12,156 @@ package body Ratnest.Tests is package Latin renames Ada.Characters.Latin_1; package Strmaps renames Ada.Strings.Maps; + package PE renames Packrat.Errors; package PU renames Packrat.Util; + function Valid_Message_Check + return Test_Result is + begin + if PE.Valid_Message ("abcde") or PE.Valid_Message ("sSYM") or + PE.Valid_Message ("sSYMp") or PE.Valid_Message ("p345") or + PE.Valid_Message ("pOne") or PE.Valid_Message ("sSYMp1sNEXT") or + PE.Valid_Message ("sSYMp1.2") or PE.Valid_Message ("sABcDp12") or + not PE.Valid_Message ("sSYMp0") or not PE.Valid_Message ("sAp12") or + not PE.Valid_Message ("sNAMEp34sSYMp02") or not PE.Valid_Message ("") or + not PE.Valid_Message ("sA_Bp3") + then + return Failure; + end if; + return Success; + end Valid_Message_Check; + + + function Valid_Identifier_Check + return Test_Result + is + Pass_Array : PE.Error_Info_Array := + ((+"A", 1), (+"ABC", 2), (+"AB_CD", 3), (+"A_B_CD", 4)); + Fail_Array : PE.Error_Info_Array := + ((+"_", 1), (+"_A", 2), (+"A_", 3), (+"A__B", 4)); + begin + for EI of Pass_Array loop + if not PE.Valid_Identifier (EI.Symbol) or + not PE.Valid_Identifier (-EI.Symbol) + then + return Failure; + end if; + end loop; + for EI of Fail_Array loop + if PE.Valid_Identifier (EI.Symbol) or + PE.Valid_Identifier (-EI.Symbol) + then + return Failure; + end if; + end loop; + if not PE.Valid_Identifier_Array (Pass_Array) or + PE.Valid_Identifier_Array (Fail_Array) + then + return Failure; + end if; + return Success; + end Valid_Identifier_Check; + + + function Join_Check + return Test_Result + is + Array_1 : PE.Error_Info_Array := ((+"A", 1), (+"B", 2)); + Msg_1 : PE.Error_Message := PE.Encode_Array (Array_1); + + Array_2 : PE.Error_Info_Array := ((+"C", 3), (+"D", 4)); + Msg_2 : PE.Error_Message := PE.Encode_Array (Array_2); + + Msg_3 : PE.Error_Message := PE.Join (Msg_1, Msg_2); + + Array_4 : PE.Error_Info_Array := ((+"A", 1), (+"B", 2), (+"C", 3), (+"D", 4)); + Msg_4 : PE.Error_Message := PE.Encode_Array (Array_4); + + Array_5 : PE.Error_Info_Array := ((+"A", 1), (+"B", 4)); + Msg_5 : PE.Error_Message := PE.Encode_Array (Array_5); + + Array_6 : PE.Error_Info_Array := ((+"A", 1), (+"C", 3)); + Msg_6 : PE.Error_Message := PE.Encode_Array (Array_6); + + Msg_7 : PE.Error_Message := PE.Join (Msg_5, Msg_6); + + Array_8 : PE.Error_Info_Array := ((+"A", 1), (+"B", 4), (+"C", 3)); + Msg_8 : PE.Error_Message := PE.Encode_Array (Array_8); + begin + if Msg_3 /= Msg_4 or Msg_7 /= Msg_8 then + return Failure; + end if; + return Success; + end Join_Check; + + + function Encode_1_Check + return Test_Result is + begin + -- Encode with a String and a Natural + if PE.Encode ("ABC", 15) /= "sABCp15" then + return Failure; + end if; + return Success; + end Encode_1_Check; + + + function Encode_2_Check + return Test_Result is + begin + -- Encode with an Unbounded_String and a Natural + if PE.Encode (+"ABC", 15) /= "sABCp15" then + return Failure; + end if; + return Success; + end Encode_2_Check; + + + function Encode_3_Check + return Test_Result is + begin + -- Encode with an Error_Info + if PE.Encode ((+"ABC", 15)) /= "sABCp15" then + return Failure; + end if; + return Success; + end Encode_3_Check; + + + function Encode_4_Check + return Test_Result is + begin + -- Encode with an Error_Info_Array + if PE.Encode_Array (((+"A", 3), (+"BC", 2), (+"ABC", 1), (+"B", 4))) /= + "sAp3sBCp2sABCp1sBp4" + then + return Failure; + end if; + return Success; + end Encode_4_Check; + + + function Decode_Check + return Test_Result + is + use type PE.Error_Info_Array; + begin + if PE.Decode ("sAp1sBp3sCp10sDEFp456") /= + ((+"A", 1), (+"B", 3), (+"C", 10), (+"DEF", 456)) + then + return Failure; + end if; + return Success; + end Decode_Check; + + + + + function In_Set_Check return Test_Result is diff --git a/test/ratnest-tests.ads b/test/ratnest-tests.ads index fdcbe71..db20313 100644 --- a/test/ratnest-tests.ads +++ b/test/ratnest-tests.ads @@ -3,6 +3,28 @@ package Ratnest.Tests is + function Valid_Message_Check return Test_Result; + function Valid_Identifier_Check return Test_Result; + function Join_Check return Test_Result; + function Encode_1_Check return Test_Result; + function Encode_2_Check return Test_Result; + function Encode_3_Check return Test_Result; + function Encode_4_Check return Test_Result; + function Decode_Check return Test_Result; + + Error_Tests : Test_Array := + ((+"Valid_Message", Valid_Message_Check'Access), + (+"Valid_Identifier", Valid_Identifier_Check'Access), + (+"Join", Join_Check'Access), + (+"Encode_1", Encode_1_Check'Access), + (+"Encode_2", Encode_2_Check'Access), + (+"Encode_3", Encode_3_Check'Access), + (+"Encode_4", Encode_4_Check'Access), + (+"Decode", Decode_Check'Access)); + + + + function In_Set_Check return Test_Result; function Not_In_Set_Check return Test_Result; diff --git a/test/test_main.adb b/test/test_main.adb index 3ab5269..0ce72b1 100644 --- a/test/test_main.adb +++ b/test/test_main.adb @@ -14,6 +14,10 @@ use procedure Test_Main is begin + Put_Line ("Running tests for Packrat.Errors..."); + Run_Tests (Error_Tests); + New_Line; + Put_Line ("Running tests for Packrat.Util..."); Put_Line ("Testing set predicates..."); Run_Tests (Set_Predicate_Tests); -- cgit