% $Id: LexerToolsTest.oz,v 1.10 2012/01/08 13:17:01 leavens Exp $ % AUTHOR: Gary T. Leavens \insert 'TestingNoStop.oz' %% from the course library \insert 'OzLexer.oz' declare [File] = {Module.link ['File.ozf']} fun {ScrubToken Tok} % ENSURES: Result is like Tok but without its record. % (This is useful for making tests easier to write.) case Tok of key(K ...) then key(K) [] varId(X ...) then varId(X) [] atomExp(A ...) then atomExp(A) [] strTok(S ...) then strTok(S) [] intLit(I ...) then intLit(I) [] floatLit(F ...) then floatLit(F) [] label(atomExp(A ...) ...) then label(atomExp(A)) end end fun {OzLF Cs FileName} {Map {OzLexer Cs FileName} ScrubToken} end fun {OzL Cs} {OzLF Cs 'input'} end {StartTesting 'LexerToolsTest $Revision: 1.10 $'} {Test {OzLexer "1\n2" input} '==' [intLit(1 pos:pos(input 1 1)) intLit(2 pos:pos(input 2 1))]} {Test {OzLexer " a % ok\n\n x \t\ty\nz" input} '==' [atomExp(a pos:pos(input 1 2)) atomExp(x pos:pos(input 3 3)) atomExp(y pos:pos(input 3 16)) atomExp(z pos:pos(input 4 1))]} {Test {OzL "foo(1 2)"} '==' [label(atomExp('foo')) key('(') intLit(1) intLit(2) key(')')]} {Test {OzL "_"} '==' [varId('_')]} {Test {OzL "'X'"} '==' [atomExp('X')]} {Test {OzL "'if'"} '==' [atomExp('if')]} {Test {OzL "1"} '==' [intLit(1)]} {Test {OzL "1.0"} '==' [floatLit(1.0)]} {Test {OzL "1.0e5"} '==' [floatLit(1.0e5)]} {Test {OzL "3.141E~6"} '==' [floatLit(3.141E~6)]} {Test {OzL "1 2 if"} '==' [intLit(1) intLit(2) key('if')]} {Test {OzL "fred"} '==' [atomExp(fred)]} {Test {OzL "X"} '==' [varId('X')]} {Test {OzL "\"\""} '==' [strTok("")]} {Test {OzL "\"A\""} '==' [strTok("A")]} {Test {OzL "\"hi there\""} '==' [strTok("hi there")]} {Test {OzL "\"hi there\" fred X"} '==' [strTok("hi there") atomExp(fred) varId('X')]} {Test {OzL "declare"} '==' [key('declare')]} {Test {OzL "{F 3}"} '==' [key('{') varId('F') intLit(3) key('}')]} {Test {OzL "case {F 3} of X|Xs then nil end"} '==' [key('case') key('{') varId('F') intLit(3) key('}') key('of') varId('X') key('|') varId('Xs') key('then') atomExp('nil') key('end')]} {Test {OzL "declare fun {F X} case X of nil then nil end"} '==' [key('declare') key('fun') key('{') varId('F') varId('X') key('}') key('case') varId('X') key('of') atomExp(nil) key('then') atomExp(nil) key('end')]} {Test {OzLexer "% a comment\n declare" input} '==' [key('declare' pos:pos(input 2 2))]} {Test {OzL "%% a comment\n\ndeclare"} '==' [key('declare')]} {Test {OzL "5*7 | 3 div 4 > ~332232432"} '==' [intLit(5) key('*') intLit(7) key('|') intLit(3) key('div') intLit(4) key('>') intLit(~332232432)]} % {StartTesting 'double right curly'} {Test {OzL "{F E}} "} '==' [key('{') varId('F') varId('E') key('}') key('}') ]} {Test {OzL "} {AppendMap"} '==' [key('}') key('{') varId('AppendMap')]} % {StartTesting here} {Test {OzL "{F E} {AppendMap "} '==' [key('{') varId('F') varId('E') key('}') key('{') varId('AppendMap')]} % {StartTesting 'AppendMap string'} {Test {OzL "AppendMap"} '==' [varId('AppendMap')]} {Test {OzL "{AppendMap"} '==' [key('{') varId('AppendMap')]} {Test {OzL " {AppendMap "} '==' [key('{') varId('AppendMap')]} {Test {OzL "{Append {F E} {AppendMap Es"} '==' [key('{') varId('Append') key('{') varId('F') varId('E') key('}') key('{') varId('AppendMap') varId('Es')]} {StartTesting 'AppendMap key'} {Test {OzL "declare\nfun {AppendMap L F}\n %% ENSURES\n case L of nil then nil\n [] E|Es then {Append {F E} {AppendMap Es F}}\n end\nend\n"} '==' [key('declare') key('fun') key('{') varId('AppendMap') varId('L') varId('F') key('}') key('case') varId('L') key('of') atomExp(nil) key('then') atomExp(nil) key('[]') varId('E') key('|') varId('Es') key('then') key('{') varId('Append') key('{') varId('F') varId('E') key('}') key('{') varId('AppendMap') varId('Es') varId('F') key('}') key('}') key('end') key('end')]} % {StartTesting 'AppendMap whole'} local LA = {OzL {Append {Append "declare\nfun {AppendMap L F}\n %% ENSURES\n case L of" " nil then nil\n [] E|Es then {Append {F E} {AppendMap Es F}}\n"} " end\nend\n"}} LE = key('declare') |key('fun')|key('{')|varId('AppendMap')|varId('L')|varId('F')|key('}') |key('case')|varId('L')|key('of') |atomExp(nil)|key('then')|atomExp(nil) |key('[]')|varId('E')|key('|')|varId('Es')|key('then') |key('{')|varId('Append')|key('{')|varId('F')|varId('E')|key('}') |key('{')|varId('AppendMap')|varId('Es')|varId('F') |key('}')|key('}') |key('end') |key('end')|nil in {Test LA '==' LE} {Test {List.drop LA 18} '==' {List.drop LE 18}} end {StartTesting 'AppendMap file'} {Test {OzLF {File.readList 'AppendMap.oz'} "AppendMap.oz"} '==' key('declare') |key('fun')|key('{')|varId('AppendMap')|varId('L')|varId('F')|key('}') |key('case')|varId('L')|key('of') |varId('E')|key('|')|varId('Es')|key('then') |key('{')|varId('Append')|key('{')|varId('F')|varId('E')|key('}') |key('{')|varId('AppendMap')|varId('Es')|varId('F') |key('}')|key('}') |key('else')|atomExp(nil) |key('end') |key('end')|nil} {DoneTesting} % {Browse {OzLexer {File.readList 'AppendMap.oz'} 'AppendMap.oz'}}