Match compiler: Add unit test for C string replacements
This commit is contained in:
parent
7452613479
commit
027ce0e81f
|
@ -29,7 +29,7 @@ class MatchCompilerTest(unittest.TestCase):
|
||||||
self.assertEqual(self.mc.parseMatch(' Token::Match(tok, ";") ', 2), [
|
self.assertEqual(self.mc.parseMatch(' Token::Match(tok, ";") ', 2), [
|
||||||
'Token::Match(tok, ";")', 'tok', ' ";"'])
|
'Token::Match(tok, ";")', 'tok', ' ";"'])
|
||||||
self.assertEqual(self.mc.parseMatch(' Token::Match(tok,', 2), None)
|
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), [
|
self.assertEqual(self.mc.parseMatch(' Token::Match(Token::findsimplematch(tok,")"), ";")', 2), [
|
||||||
'Token::Match(Token::findsimplematch(tok,")"), ";")', 'Token::findsimplematch(tok,")")', ' ";"']) # inner function call
|
'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, len(self.mc._matchStrs))
|
||||||
self.assertEqual(1, self.mc._matchStrs['foobar'])
|
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__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
Loading…
Reference in New Issue