From 027ce0e81fe525f2322f46f5b9f0b8ba013bb710 Mon Sep 17 00:00:00 2001 From: Thomas Jarosch Date: Fri, 9 Jan 2015 20:20:14 +0100 Subject: [PATCH] Match compiler: Add unit test for C string replacements --- tools/test_matchcompiler.py | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/tools/test_matchcompiler.py b/tools/test_matchcompiler.py index 3ba2d1acc..8f1d552ee 100755 --- a/tools/test_matchcompiler.py +++ b/tools/test_matchcompiler.py @@ -29,7 +29,7 @@ class MatchCompilerTest(unittest.TestCase): self.assertEqual(self.mc.parseMatch(' Token::Match(tok, ";") ', 2), [ 'Token::Match(tok, ";")', 'tok', ' ";"']) self.assertEqual(self.mc.parseMatch(' Token::Match(tok,', 2), None) - # multiline Token::Match is not supported yet + # multiline Token::Match is not supported yet self.assertEqual(self.mc.parseMatch(' Token::Match(Token::findsimplematch(tok,")"), ";")', 2), [ 'Token::Match(Token::findsimplematch(tok,")"), ";")', 'Token::findsimplematch(tok,")")', ' ";"']) # inner function call @@ -154,5 +154,36 @@ class MatchCompilerTest(unittest.TestCase): self.assertEqual(1, len(self.mc._matchStrs)) self.assertEqual(1, self.mc._matchStrs['foobar']) + def test_parseStringComparison(self): + input = 'str == "abc"' + res = self.mc._parseStringComparison(input, 5) # offset '5' is chosen as an abritary start offset to look for " + self.assertEqual(2, len(res)) + self.assertEqual(7, res[0]) + self.assertEqual(12, res[1]) + self.assertEqual('str == matchStr', input[:res[0]] + "matchStr" + input[res[1]:]) + + input = 'str == "a\\"b\\"c"' + res = self.mc._parseStringComparison(input, 5) + self.assertEqual(2, len(res)) + self.assertEqual(7, res[0]) + self.assertEqual(16, res[1]) + self.assertEqual('str == matchStr', input[:res[0]] + "matchStr" + input[res[1]:]) + + def test_replaceCStrings(self): + # str() == + input = 'if (tok2->str() == "abc") {' + output = self.mc._replaceCStrings(input) + self.assertEqual("if (tok2->str() == matchStr1) {", output) + + # str() != + input = 'if (tok2->str() != "xyz") {' + output = self.mc._replaceCStrings(input) + self.assertEqual("if (tok2->str() != matchStr2) {", output) + + # strAt() + input = 'if (match16(parent->tokAt(-3)) && tok->strAt(1) == ")")' + output = self.mc._replaceCStrings(input) + self.assertEqual('if (match16(parent->tokAt(-3)) && tok->strAt(1) == matchStr3)', output) + if __name__ == '__main__': unittest.main()