###################################################################### # # Tests for the preprocessor. # ###################################################################### require 'test_helper' # publicize the private methods so we can test them easily class C::Preprocessor public :shellquote, :full_command end class PreprocessorTest < Test::Unit::TestCase attr_accessor :cpp def setup @cpp = C::Preprocessor.new @cpp.include_path << 'dir1' << 'dir 2' @cpp.macros['V'] = nil @cpp.macros['I'] = 5 @cpp.macros['S'] = '"blah"' @cpp.macros['SWAP(a,b)'] = 'a ^= b ^= a ^= b' FileUtils.rm_rf(TEST_DIR) FileUtils.mkdir_p(TEST_DIR) end def teardown FileUtils.rm_rf(TEST_DIR) end def test_shellquote assert_equal('a', cpp.shellquote('a')) assert_equal("'a b'", cpp.shellquote('a b')) assert_equal("'a$b'", cpp.shellquote('a$b')) assert_equal("'a\"b'", cpp.shellquote("a\"b")) assert_equal("'\\'", cpp.shellquote("\\")) assert_equal("\"a'b\"", cpp.shellquote("a'b")) assert_equal("\"a\\\\\\$\\\"'\"", cpp.shellquote("a\\$\"'")) end def test_full_command original_command = C::Preprocessor.command C::Preprocessor.command = 'COMMAND' assert_equal("COMMAND -Idir1 '-Idir 2' -DV -DI=5 '-DS=\"blah\"' '-DSWAP(a,b)=a ^= b ^= a ^= b' 'a file.c'", cpp.full_command('a file.c')) ensure C::Preprocessor.command = original_command end def test_preprocess output = cpp.preprocess("I S SWAP(x, y)") assert_match(/5/, output) assert_match(/"blah"/, output) assert_match(/x \^= y \^= x \^= y/, output) end def test_preprocess_include one_h = "#{TEST_DIR}/one.h" two_h = "#{TEST_DIR}/foo/two.h" File.open(one_h, 'w'){|f| f.puts "int one = 1;"} FileUtils.mkdir(File.dirname(two_h)) File.open(two_h, 'w'){|f| f.puts "int two = 2;"} output = nil FileUtils.cd(TEST_DIR) do output = cpp.preprocess(<