# 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' # 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 test_preproc # ignores eol/space at begin/end t_preparse = proc { |text, result| p = load text, caller.first txt = '' t = nil txt << t.raw until p.eos? or not t = p.readtok assert_equal(result, txt.strip) } t_preparse[< 2 /* this one too */ blo #elif (1+1)*2 > 2 // true ! blu #elif 4 > 2 // not you ble #else bli #endif EOS t_preparse[<<'EOS', 'ab#define x'] a\ b\ #define x EOS p = load('__LINE__') assert_equal(__LINE__.to_s, p.readtok.value) t_preparse[< out EOS ensure File.unlink('tests/prepro_testinclude.asm') rescue nil end p = load <