% $Id: ParserFunctionsTest.oz,v 1.9 2012/01/07 16:19:58 leavens Exp $ % AUTHOR: Gary T. Leavens \insert 'ParserFunctions.oz' \insert 'TestingNoStop.oz' %% from the course library declare % Test1 is a testing helper used to deal with the laziness in parsers proc {Test1 E1 Connective E2} {Test {List.take E1 1} Connective E2} end {StartTesting 'ParserFunctionsTest $Revision: 1.9 $'} {Test {{P_Succeed 42} "abc"} '==' [42#"abc"]} {Test {P_Fail "abc"} '==' nil} {StartTesting 'P_Satisfy'} {Test {{P_Satisfy Digit} "1"} '==' [&1#nil]} {Test {{P_Satisfy Digit} "3ab"} '==' [&3#"ab"]} {Test {{P_Satisfy Digit} "ab"} '==' nil} {Test {{P_Satisfy Letter} "ab"} '==' [&a#"b"]} {StartTesting 'P_Literal'} {Test {{P_Literal &;} ";;+*"} '==' [&;#";+*"]} {Test {{P_Literal &t} "then a ford"} '==' [&t#"hen a ford"]} {Test {{P_Literal "ogga"} "then a ford"} '==' nil} {StartTesting 'P_Alt'} {Test1 {{P_Alt {P_Literal &;} {P_Literal &1}} ";2"} '==' [&;#"2"]} {Test1 {{P_Alt {P_Literal &;} {P_Literal &1}} "1;2"} '==' [&1#";2"]} {Test1 {{P_Alt {P_Literal &;} {P_Literal &1}} "+"} '==' nil} {Test1 {{P_Alt {P_Literal &;} {P_Alt {P_Literal &1} {P_Literal &+}}} "+"} '==' [&+#nil]} {StartTesting 'P_Then'} {Test1 {{P_Then {P_Satisfy Digit} {P_Satisfy Letter}} "3ab"} '==' [(&3#&a)#"b"]} {Test1 {{P_Then {P_Satisfy Letter} {P_Satisfy Digit}} "a3b"} '==' [(&a#&3)#"b"]} {Test1 {{P_Then {P_Satisfy Letter} {P_Satisfy Digit}} "ab"} '==' nil} {Test1 {{P_Then {P_Satisfy Digit} {P_Satisfy Letter}} "ab"} '==' nil} {StartTesting 'Using'} {Test1 {{Using P_Number fun {$ _} 1 end} "1"} '==' [1#nil]} {Test1 {{Using P_Number StringToInt} "4020"} '==' [4020#nil]} {StartTesting 'P_XThen'} {Test1 {{P_XThen {P_Literal &;} P_Number} ";86"} '==' ["86"#nil]} {Test1 {{P_XThen {P_Literal &+} P_Number} "+89"} '==' ["89"#nil]} {StartTesting 'P_ThenX'} {Test1 {{P_ThenX P_Number {P_Literal &;}} "86;"} '==' ["86"#nil]} {Test1 {{P_ThenX P_Number {P_Literal &+}} "89+"} '==' ["89"#nil]} {StartTesting 'P_Many'} {Test1 {{P_Many {P_Satisfy Digit}} "1"} '==' ["1"#nil]} {Test1 {{P_Many {P_Satisfy Digit}} "1234"} '==' ["1234"#nil]} {Test1 {{P_Many {P_Satisfy Digit}} "abcd"} '==' [""#"abcd"]} {StartTesting 'P_Many with P_Then'} {Test1 {{P_Then {P_Satisfy Digit} {P_Many {P_Satisfy Digit}}} "1"} '==' [(&1#nil)#nil]} {Test1 {{P_Then {P_Satisfy Digit} {P_Many {P_Satisfy Digit}}} "123"} '==' [(&1#"23")#nil]} {Test1 {{P_Then {P_Satisfy Digit} {P_Many {P_Satisfy Digit}}} "123xy"} '==' [(&1#"23")#"xy"]} {StartTesting 'P_Some'} {Test1 {{P_Some {P_Satisfy Digit}} "1234"} '==' ["1234"#nil]} {Test1 {{P_Some {P_Satisfy Letter}} "abcd"} '==' ["abcd"#nil]} {Test1 {{P_Some {P_Satisfy Letter}} "a"} '==' ["a"#nil]} {Test1 {{P_Some {P_Satisfy Letter}} "123a"} '==' nil} {StartTesting 'P_Number'} {Test1 {P_Number "1"} '==' ["1"#nil]} {Test1 {P_Number "4020"} '==' ["4020"#nil]} {Test1 {P_Number "12345678990"} '==' ["12345678990"#nil]} {StartTesting 'P_Word'} {Test1 {P_Word "ab"} '==' ["ab"#nil]} {StartTesting 'P_Some_Sep_By'} {Test1 {{{P_Some_Sep_By {P_Literal &;}} {P_Satisfy Digit}} "1;2;3;4"} '==' ["1234"#nil]} {Test1 {{{P_Some_Sep_By {P_Literal &,}} {Using P_Number StringToInt}} "12,33,04"} '==' [[12 33 4]#nil]} {Test1 {{{P_Some_Sep_By {P_Literal &,}} {Using P_Number StringToInt}} "-5"} '==' nil} {StartTesting 'P_Many_Sep_By'} {Test1 {{{P_Many_Sep_By {P_Literal &,}} {Using P_Number StringToInt}} "86,89,724123,007"} '==' [[86 89 724123 7]#nil]} {Test1 {{{P_Many_Sep_By {P_Literal &,}} {Using P_Number StringToInt}} ""} '==' [nil#nil]} {Test1 {{{P_Many_Sep_By {P_Literal &,}} {Using P_Number StringToInt}} "abc"} '==' [nil#"abc"]} {StartTesting 'P_Alphanum'} {Test1 {P_Alphanum "a2b3"} '==' ["a2b3"#nil]} {Test1 {P_Alphanum "2ab3"} '==' ["2ab3"#nil]} {StartTesting 'P_These_Lits'} {Test1 {{P_These_Lits ":+="} ":+="} '==' [":+="#nil]} {Test1 {{P_These_Lits ":+="} ":+"} '==' nil} {StartTesting 'P_Nibble'} {Test1 {{P_Nibble P_Number} " 123 "} '==' ["123"#nil]} {StartTesting 'P_Any'} {Test1 {{{P_Any P_Literal} "+-*/"} "-"} '==' [&-#nil]} {Test1 {{{P_Any P_Literal} "+-*/"} "/"} '==' [&/#nil]} {StartTesting 'P_Symbol'} {Test1 {{P_Symbol "then"} " then {"} '==' ["then"#"{"]} {Test1 {{P_Symbol "else"} "else return"} '==' ["else"#"return"]} {StartTesting 'P_Opt'} {Test1 {{P_Opt {P_Symbol "else"} "skip"} "foo"} '==' ["skip"#"foo"]} {Test1 {{P_Opt {P_Symbol "else"} "skip"} " else "} '==' ["else"#nil]} {StartTesting 'P_Return'} {Test1 {{{P_Return P_Word} 42} "abc"} '==' [42#nil]} {StartTesting 'P_Into'} {Test1 {{P_Into {P_ThenX P_Word {P_Literal &;}} fun {$ FirstWord Remaining} {{Using {P_Alt P_Number P_Word} fun {$ It} FirstWord#It end} Remaining} end} "okay;4020"} '==' [("okay"#"4020")#nil]} {StartTesting 'P_Debug'} MyCell = {NewCell false} {Test1 {{P_Debug proc {$} MyCell := true end {{P_Many_Sep_By {P_Literal &,}} {Using P_Number StringToInt}}} "86,89,724123,007"} '==' [[86 89 724123 7]#nil]} {Assert @MyCell} {Test1 {{P_Debug proc {$} {System.showInfo "calling P_Symbol"} end {P_Symbol "then"}} " then {"} '==' ["then"#"{"]} {DoneTesting}