# This file is part of Metasm, the Ruby assembly manipulation suite # Copyright (C) 2007 Yoann GUILLOT # # Licence is LGPL, see LICENCE in the top-level directory require 'test/unit' require 'metasm/preprocessor' require 'metasm/parse' # BEWARE OF TEH RUBY PARSER # use single-quoted source strings class TestPreproc < Test::Unit::TestCase def load txt, bt = caller.first p = Metasm::Preprocessor.new bt =~ /^(.*?):(\d+)/ p.feed txt, $1, $2.to_i+1 p end def test_gettok p = load <<'EOS' test boo " bla bla\ \"\\" \ xx EOS assert_equal \ ['test', :space, :string, :eol, :quoted, :space, 'xx', :eol, true], [p.readtok.raw, p.readtok.type, p.readtok.type, p.readtok.type, p.readtok.type, p.readtok.type, p.readtok.raw, p.readtok.type, p.eos?] end def test_comment p = load <<'EOS' foo /*/ bar ' " * /*/ baz kikoo // lol \ asv EOS toks = [] nil while tok = p.readtok and (tok.type == :space or tok.type == :eol) until p.eos? toks << tok.raw nil while tok = p.readtok and (tok.type == :space or tok.type == :eol) end assert_equal %w[foo baz kikoo], toks end def helper_preparse(text, result) p = load(text, caller.first) yield p if block_given? txt = '' until p.eos? or not t = p.readtok txt << t.raw end assert_equal(result, txt.strip) end def test_preproc # ignores eol/space at begin/end helper_preparse(< 2 /* this one too */ blo #elif (1+1)*2 > 2 // true ! blu #elif 4 > 2 // not you ble #else bli #endif EOS helper_preparse(<<'EOS', 'ab#define x') a\ b\ #define x EOS p = load('__LINE__,__DATE__,__TIME__') assert_equal(__LINE__, p.readtok.value) ; p.readtok assert_not_equal('__DATE__', p.readtok.raw) ; p.readtok assert_not_equal('__TIME__', p.readtok.raw) helper_preparse(< out EOS ensure File.unlink('tests/prepro_testinclude.asm') rescue nil end helper_preparse(< out EOS helper_preparse(< 0'] t_float['1.0 > 0'] t_float['1e2 > 10 && 1.0e2 < 1000'] t_float['1.0e+2 > 10'] t_float['10_00e-2 > 1 && 10_00e-2 < 100'] t_float['.1e2 > 1'] #t_float['0x1.p2L > 1 && 0x1p2f < 5'] t_float['0x1.p2L > 1'] t_float['0x1p2f < 5'] end def test_errors test_err = lambda { |txt| assert_raise(Metasm::ParseError) { p = load(txt, caller.first) ; p.readtok until p.eos? } } t_float = lambda { |txt| assert_raise(Metasm::ParseError) { p = load("#if #{txt}\n#endif", caller.first) ; p.readtok } } test_err["\"abc\n\""] test_err['"abc\x"'] test_err['/*'] test_err['#if 0'] test_err["#define toto(tutu,"] test_err["#define toto( (tutu, tata)"] test_err['#error bla'] test_err[<