From 30d59f09f6908aa0de2ec3a58a0736c8030ffda5 Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Thu, 21 Jan 2021 16:33:47 +1100 Subject: Piecewise parsing fixed, unit tested --- test/rat_tests-parsers.adb | 145 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 138 insertions(+), 7 deletions(-) (limited to 'test/rat_tests-parsers.adb') diff --git a/test/rat_tests-parsers.adb b/test/rat_tests-parsers.adb index 8d54168..be3c1cd 100644 --- a/test/rat_tests-parsers.adb +++ b/test/rat_tests-parsers.adb @@ -602,7 +602,7 @@ package body Rat_Tests.Parsers is begin if One_Debug.Status (Result1) /= Packrat.Success or One_Debug.Status (Result2) /= Packrat.Failure or - One_Debug.Status (Result3) /= Packrat.Failure + One_Debug.Status (Result3) /= Packrat.Needs_More then return Fail; end if; @@ -641,7 +641,7 @@ package body Rat_Tests.Parsers is begin if One_Debug.Status (Result1) /= Packrat.Failure or One_Debug.Status (Result2) /= Packrat.Success or - One_Debug.Status (Result3) /= Packrat.Failure + One_Debug.Status (Result3) /= Packrat.Needs_More then return Fail; end if; @@ -679,7 +679,7 @@ package body Rat_Tests.Parsers is begin if One_Debug.Status (Result1) /= Packrat.Failure or One_Debug.Status (Result2) /= Packrat.Success or - One_Debug.Status (Result3) /= Packrat.Failure + One_Debug.Status (Result3) /= Packrat.Needs_More then return Fail; end if; @@ -758,7 +758,7 @@ package body Rat_Tests.Parsers is begin if One_Debug.Status (Result1) /= Packrat.Failure or One_Debug.Status (Result2) /= Packrat.Success or - One_Debug.Status (Result3) /= Packrat.Failure or + One_Debug.Status (Result3) /= Packrat.Needs_More or One_Debug.Status (Result4) /= Packrat.Needs_More then return Fail; @@ -800,7 +800,7 @@ package body Rat_Tests.Parsers is if One_Debug.Status (Result1) /= Packrat.Success or One_Debug.Status (Result2) /= Packrat.Success or One_Debug.Status (Result3) /= Packrat.Needs_More or - One_Debug.Status (Result4) /= Packrat.Failure + One_Debug.Status (Result4) /= Packrat.Needs_More then return Fail; end if; @@ -1000,6 +1000,134 @@ package body Rat_Tests.Parsers is + function Parse_Parts_Check + return Test_Result + is + pragma Polling (On); + + -- 1+2+3+4 + Input1 : String := "1+"; + Input2 : String := "2+3"; + Input3 : String := "+4"; + Input4 : String := ""; + + use Add_Errors; + Add_1 : Parser_Tokens.Token_Type := Parser_Tokens.Create (Sum_Err, 1, ""); + Add_3 : Parser_Tokens.Token_Type := Parser_Tokens.Create (Sum_Err, 3, ""); + Add_5 : Parser_Tokens.Token_Type := Parser_Tokens.Create (Sum_Err, 5, ""); + Add_7 : Parser_Tokens.Token_Type := Parser_Tokens.Create (Sum_Err, 7, ""); + Number_1 : Parser_Tokens.Token_Type := Parser_Tokens.Create (Number_Err, 1, "1"); + Number_3 : Parser_Tokens.Token_Type := Parser_Tokens.Create (Number_Err, 3, "2"); + Number_5 : Parser_Tokens.Token_Type := Parser_Tokens.Create (Number_Err, 5, "3"); + Number_7 : Parser_Tokens.Token_Type := Parser_Tokens.Create (Number_Err, 7, "4"); + + Expected_Graph, Actual_Graph : Parser_Result; + begin + Expected_Graph.Connect ((Add_7, 7), + (1 => (Number_7, 7))); + Expected_Graph.Connect ((Add_5, 7), + ((Number_5, 5), (Add_7, 7))); + Expected_Graph.Connect ((Add_3, 7), + ((Number_3, 3), (Add_5, 7))); + Expected_Graph.Connect ((Add_1, 7), + ((Number_1, 1), (Add_3, 7))); + + Expected_Graph.Set_Root ((1 => (Add_1, 7))); + Adder_Parse_Parts.Reset; + + Adder_Parse_Parts.Parse (Input1, Actual_Graph); + Adder_Parse_Parts.Parse (Input2, Actual_Graph); + Adder_Parse_Parts.Parse (Input3, Actual_Graph); + Adder_Parse_Parts.Parse (Input4, Actual_Graph); + + if Actual_Graph.Isomorphic (Expected_Graph) then + return Pass; + else + return Fail; + end if; + end Parse_Parts_Check; + + + function Parse_Parts_Left_Check + return Test_Result + is + pragma Polling (On); + + -- 1+2+3+4 + Input1 : String := "1+"; + Input2 : String := "2+3"; + Input3 : String := "+4"; + Input4 : String := ""; + + use Left_Sums; + Sum_P : Parser_Tokens.Token_Type := Parser_Tokens.Create (Sum, 1, "+"); + Sum_E : Parser_Tokens.Token_Type := Parser_Tokens.Create (Sum, 1, ""); + Number_1 : Parser_Tokens.Token_Type := Parser_Tokens.Create (Number, 1, "1"); + Number_3 : Parser_Tokens.Token_Type := Parser_Tokens.Create (Number, 3, "2"); + Number_5 : Parser_Tokens.Token_Type := Parser_Tokens.Create (Number, 5, "3"); + Number_7 : Parser_Tokens.Token_Type := Parser_Tokens.Create (Number, 7, "4"); + + Expected_Graph, Actual_Graph : Parser_Result; + begin + Expected_Graph.Connect ((Sum_E, 1), + (1 => (Number_1, 1))); + Expected_Graph.Connect ((Sum_P, 3), + ((Sum_E, 1), (Number_3, 3))); + Expected_Graph.Connect ((Sum_P, 5), + ((Sum_P, 3), (Number_5, 5))); + Expected_Graph.Connect ((Sum_P, 7), + ((Sum_P, 5), (Number_7, 7))); + + Expected_Graph.Set_Root ((1 => (Sum_P, 7))); + Add_Parse_Parts.Reset; + + Add_Parse_Parts.Parse (Input1, Actual_Graph); + Add_Parse_Parts.Parse (Input2, Actual_Graph); + Add_Parse_Parts.Parse (Input3, Actual_Graph); + Add_Parse_Parts.Parse (Input4, Actual_Graph); + + if Actual_Graph.Isomorphic (Expected_Graph) then + return Pass; + else + return Fail; + end if; + end Parse_Parts_Left_Check; + + + function Parse_Parts_Exception_Check + return Test_Result + is + pragma Polling (On); + use type Packrat.Errors.Error_Info_Array; + + -- 1+2+ 3+4 + Input1 : String := "1+"; + Input2 : String := "2+ 3"; + Input3 : String := "+4"; + Input4 : String := ""; + + Expected_Errors : Packrat.Errors.Error_Info_Array := + ((+"EOF_ERR", 2), (+"EOF_ERR", 4), (+"NUMBER_ERR", 5), (+"SUM_ERR", 5)); + Result_Graph : Add_Errors.Parser_Result; + begin + Adder_Parse_Parts.Reset; + Adder_Parse_Parts.Parse (Input1, Result_Graph); + Adder_Parse_Parts.Parse (Input2, Result_Graph); + Adder_Parse_Parts.Parse (Input3, Result_Graph); + Adder_Parse_Parts.Parse (Input4, Result_Graph); + return Fail; + exception + when Msg : Packrat.Parser_Error => + if Packrat.Errors.Equivalent + (Packrat.Errors.Decode (Except.Exception_Message (Msg)), Expected_Errors) + then + return Pass; + else + return Fail; + end if; + end Parse_Parts_Exception_Check; + + function Parse_Once_Check return Test_Result is @@ -1050,10 +1178,13 @@ package body Rat_Tests.Parsers is return Fail; exception when Msg : Packrat.Parser_Error => - if Packrat.Errors.Decode (Except.Exception_Message (Msg)) /= Expected_Errors then + if Packrat.Errors.Equivalent + (Packrat.Errors.Decode (Except.Exception_Message (Msg)), Expected_Errors) + then + return Pass; + else return Fail; end if; - return Pass; end Parse_Once_Exception_Check; -- cgit