def test_define_python_function_with_name(self): def XYZ(x): return "123" pp = cmonster.Preprocessor("test.c", data="ABC(123)") pp.define("ABC", XYZ) toks = [tok for tok in pp] self.assertEqual(1, len(toks)) self.assertEqual("123", str(toks[0]))
def test_define_python_function(self): def ABC(arg): return "".join(reversed(str(arg))) pp = cmonster.Preprocessor("test.c", data="ABC(321)") pp.define(ABC) toks = [tok for tok in pp] self.assertEqual(1, len(toks)) self.assertEqual("123", str(toks[0]))
def test_add(self): pp = cmonster.Preprocessor("test.c", data="ABC\n123") toks = [tok for tok in pp] self.assertEqual(2, len(toks)) self.assertEqual("ABC", str(toks[0])) self.assertEqual("123", str(toks[1])) loc = toks[0].location self.assertEqual(1, loc.line) loc += len(toks[0]) + 1 self.assertEqual(2, loc.line)
def test_subtract(self): pp = cmonster.Preprocessor("test.c", data="ABC\n123") toks = [tok for tok in pp] self.assertEqual(2, len(toks)) self.assertEqual("ABC", str(toks[0])) self.assertEqual("123", str(toks[1])) loc = toks[1].location self.assertEqual(2, loc.line) self.assertEqual(1, loc.column) loc -= 1 self.assertEqual(1, loc.line)
def test_include_cwd(self): with tempfile.NamedTemporaryFile("w", dir=".") as f: f.write("abc\n") f.flush() self.assertTrue(os.path.exists(os.path.basename(f.name))) pp = cmonster.Preprocessor("test.c", data="#include \"%s\"" % os.path.basename(f.name)) tokens = [t for t in pp] self.assertEqual(1, len(tokens)) self.assertEqual(cmonster.tok_identifier, tokens[0].token_id) self.assertEqual("abc", str(tokens[0]))
def test_include_filename(self): with tempfile.TemporaryDirectory() as d: with open(d + "/" + get_new_filename(), "w") as f: f.write("abc\n") pp = cmonster.Preprocessor("test.c", data="#include \"%s\"" % os.path.basename(f.name)) pp.add_include_dir(os.path.dirname(f.name)) tokens = [t for t in pp] self.assertEqual(1, len(tokens)) self.assertEqual(cmonster.tok_identifier, tokens[0].token_id) self.assertEqual("abc", str(tokens[0]))
def test_include_fail(self): pp = cmonster.Preprocessor("test.c", data="#include \"%s\"" % get_new_filename()) with self.assertRaises(Exception): tokens = [t for t in pp]
def test_define_simple_function(self): pp = cmonster.Preprocessor("test.c", data="ABC(123)") pp.define("ABC(X)", "X") toks = [tok for tok in pp] self.assertEqual(1, len(toks)) self.assertEqual("123", str(toks[0]))
def test_define_object(self): pp = cmonster.Preprocessor("test.c", data="ABC") pp.define("ABC", "123") toks = [tok for tok in pp] self.assertEqual(1, len(toks)) self.assertEqual("123", str(toks[0]))
def test_token_length(self): pp = cmonster.Preprocessor("test.c", data="ABC") toks = [tok for tok in pp] self.assertEqual(1, len(toks)) self.assertEqual("ABC", str(toks[0])) self.assertEqual(3, len(toks[0]))