Match compiler: Add unit test for C string replacements

This commit is contained in:
Thomas Jarosch 2015-01-09 20:20:14 +01:00
parent 7452613479
commit 027ce0e81f
1 changed files with 32 additions and 1 deletions

View File

@ -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()