zpy

A lisp like language written in hare
Log | Files | Refs

+test.ha (1829B)


      1 use memio;
      2 use strings;
      3 
      4 // checks that should return tokens
      5 
      6 fn check(lex: *lexer, expect: [](types | invalid)) void = {
      7 	for (let exp ..	expect) {
      8 		let n = next(lex);
      9 
     10 		match (exp) {
     11 		case let ty: types => assert((n as token).ty == ty);
     12 		case invalid => assert(n is error);
     13 		};
     14 	};
     15 };
     16 
     17 @test
     18 fn NoArgExpr() void = {
     19 	let lex = new(&memio::fixed(strings::toutf8("(foo)")));
     20 
     21 	check(lex, [types::OBRACE, types::NAME, types::CBRACE, types::EOF]);
     22 };
     23 
     24 @test
     25 fn HasArgsExpr() void = {
     26 	let lex = new(&memio::fixed(strings::toutf8("(foo 1 2 3)")));
     27 
     28 	check(lex, [types::OBRACE, types::NAME, types::NUM, types::NUM, types::NUM, types::CBRACE, types::EOF]);
     29 };
     30 
     31 @test
     32 fn NestedArgsExpr() void = {
     33 	let lex = new(&memio::fixed(strings::toutf8("(foo (bar 1 2) 3)")));
     34 
     35 	check(lex, [types::OBRACE, types::NAME, types::OBRACE, types::NAME, types::NUM, types::NUM, types::CBRACE, types::NUM, types::CBRACE, types::EOF]);
     36 };
     37 
     38 @test
     39 fn NameNumExpr() void = {
     40 	let lex = new(&memio::fixed(strings::toutf8("(foo bar 1)")));
     41 
     42 	check(lex, [types::OBRACE, types::NAME, types::NAME, types::NUM, types::CBRACE, types::EOF]);
     43 };
     44 
     45 @test
     46 fn NumNameExpr() void = {
     47 	let lex = new(&memio::fixed(strings::toutf8("(foo 1 bar)")));
     48 
     49 	check(lex, [types::OBRACE, types::NAME, types::NUM, types::NAME, types::CBRACE, types::EOF]);
     50 };
     51 
     52 
     53 // checks that should return errors
     54 
     55 @test
     56 fn EmptyExpr() void = {
     57 	let lex = new(&memio::fixed(strings::toutf8("")));
     58 
     59 	check(lex, [invalid]);
     60 };
     61 
     62 @test
     63 fn SingleOBraceExpr() void = {
     64 	let lex = new(&memio::fixed(strings::toutf8("(")));
     65 
     66 	check(lex, [types::OBRACE, invalid]);
     67 };
     68 
     69 @test
     70 fn SingleCBraceExpr() void = {
     71 	let lex = new(&memio::fixed(strings::toutf8(")")));
     72 
     73 	check(lex, [invalid]);
     74 };
     75 
     76 @test
     77 fn SingleNameExpr() void = {
     78 	let lex = new(&memio::fixed(strings::toutf8("foo")));
     79 
     80 	check(lex, [invalid]);
     81 };