MC: Switch to assertEqual function from python's unittest
This commit is contained in:
parent
3bb8237cdc
commit
06a7d301e2
|
@ -23,130 +23,123 @@ class MatchCompilerTest(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.mc = matchcompiler.MatchCompiler(verify_mode=False)
|
self.mc = matchcompiler.MatchCompiler(verify_mode=False)
|
||||||
|
|
||||||
def _assertEquals(self, actual, expected):
|
|
||||||
if actual != expected:
|
|
||||||
print ('Assertion failed:')
|
|
||||||
print ('"' + actual + '"')
|
|
||||||
print ('"' + expected + '"')
|
|
||||||
assert actual == expected
|
|
||||||
|
|
||||||
def test_parseMatch(self):
|
def test_parseMatch(self):
|
||||||
self._assertEquals(self.mc.parseMatch(' Token::Match(tok, ";") ',2), ['Token::Match(tok, ";")','tok',' ";"'])
|
self.assertEqual(self.mc.parseMatch(' Token::Match(tok, ";") ',2), ['Token::Match(tok, ";")','tok',' ";"'])
|
||||||
self._assertEquals(self.mc.parseMatch(' Token::Match(tok,', 2), None) # multiline Token::Match is not supported yet
|
self.assertEqual(self.mc.parseMatch(' Token::Match(tok,', 2), None) # multiline Token::Match is not supported yet
|
||||||
self._assertEquals(self.mc.parseMatch(' Token::Match(Token::findsimplematch(tok,")"), ";")', 2), ['Token::Match(Token::findsimplematch(tok,")"), ";")', 'Token::findsimplematch(tok,")")', ' ";"']) # inner function call
|
self.assertEqual(self.mc.parseMatch(' Token::Match(Token::findsimplematch(tok,")"), ";")', 2), ['Token::Match(Token::findsimplematch(tok,")"), ";")', 'Token::findsimplematch(tok,")")', ' ";"']) # inner function call
|
||||||
|
|
||||||
def test_replaceTokenMatch(self):
|
def test_replaceTokenMatch(self):
|
||||||
input = 'if (Token::Match(tok, "foobar")) {'
|
input = 'if (Token::Match(tok, "foobar")) {'
|
||||||
output = self.mc._replaceTokenMatch(input)
|
output = self.mc._replaceTokenMatch(input)
|
||||||
self._assertEquals(output, 'if (match1(tok)) {')
|
self.assertEqual(output, 'if (match1(tok)) {')
|
||||||
self._assertEquals(1, len(self.mc._matchStrs))
|
self.assertEqual(1, len(self.mc._matchStrs))
|
||||||
self._assertEquals(1, self.mc._matchStrs['foobar'])
|
self.assertEqual(1, self.mc._matchStrs['foobar'])
|
||||||
|
|
||||||
input = 'if (Token::Match(tok->next()->next(), "foobar %type% %num%")) {'
|
input = 'if (Token::Match(tok->next()->next(), "foobar %type% %num%")) {'
|
||||||
output = self.mc._replaceTokenMatch(input)
|
output = self.mc._replaceTokenMatch(input)
|
||||||
self._assertEquals(output, 'if (match2(tok->next()->next())) {')
|
self.assertEqual(output, 'if (match2(tok->next()->next())) {')
|
||||||
self._assertEquals(2, len(self.mc._matchStrs))
|
self.assertEqual(2, len(self.mc._matchStrs))
|
||||||
|
|
||||||
input = 'if (Token::Match(tok, "foo\"special\"bar %num%")) {'
|
input = 'if (Token::Match(tok, "foo\"special\"bar %num%")) {'
|
||||||
output = self.mc._replaceTokenMatch(input)
|
output = self.mc._replaceTokenMatch(input)
|
||||||
# FIXME: Currently detected as non-static pattern
|
# FIXME: Currently detected as non-static pattern
|
||||||
self._assertEquals(output, 'if (Token::Match(tok, "foo"special"bar %num%")) {')
|
self.assertEqual(output, 'if (Token::Match(tok, "foo"special"bar %num%")) {')
|
||||||
# self._assertEquals(3, len(self.mc._matchStrs))
|
# self.assertEqual(3, len(self.mc._matchStrs))
|
||||||
|
|
||||||
def test_replaceTokenMatchWithVarId(self):
|
def test_replaceTokenMatchWithVarId(self):
|
||||||
input = 'if (Token::Match(tok, "foobar %varid%", 123)) {'
|
input = 'if (Token::Match(tok, "foobar %varid%", 123)) {'
|
||||||
output = self.mc._replaceTokenMatch(input)
|
output = self.mc._replaceTokenMatch(input)
|
||||||
self._assertEquals(output, 'if (match1(tok, 123)) {')
|
self.assertEqual(output, 'if (match1(tok, 123)) {')
|
||||||
self._assertEquals(1, len(self.mc._matchStrs))
|
self.assertEqual(1, len(self.mc._matchStrs))
|
||||||
|
|
||||||
input = 'if (Token::Match(tok->next()->next(), "%varid% foobar", tok->varId())) {'
|
input = 'if (Token::Match(tok->next()->next(), "%varid% foobar", tok->varId())) {'
|
||||||
output = self.mc._replaceTokenMatch(input)
|
output = self.mc._replaceTokenMatch(input)
|
||||||
self._assertEquals(output, 'if (match2(tok->next()->next(), tok->varId())) {')
|
self.assertEqual(output, 'if (match2(tok->next()->next(), tok->varId())) {')
|
||||||
self._assertEquals(1, len(self.mc._matchStrs))
|
self.assertEqual(1, len(self.mc._matchStrs))
|
||||||
|
|
||||||
input = 'if (Token::Match(tok, "foo\"special\"bar %type% %varid%", my_varid_cache)) {'
|
input = 'if (Token::Match(tok, "foo\"special\"bar %type% %varid%", my_varid_cache)) {'
|
||||||
output = self.mc._replaceTokenMatch(input)
|
output = self.mc._replaceTokenMatch(input)
|
||||||
# FIXME: Currently detected as non-static pattern
|
# FIXME: Currently detected as non-static pattern
|
||||||
self._assertEquals(output, 'if (Token::Match(tok, "foo"special"bar %type% %varid%", my_varid_cache)) {')
|
self.assertEqual(output, 'if (Token::Match(tok, "foo"special"bar %type% %varid%", my_varid_cache)) {')
|
||||||
# self._assertEquals(1, len(self.mc._matchStrs))
|
# self.assertEqual(1, len(self.mc._matchStrs))
|
||||||
|
|
||||||
# test caching: reuse existing matchX()
|
# test caching: reuse existing matchX()
|
||||||
input = 'if (Token::Match(tok, "foobar %varid%", 123)) {'
|
input = 'if (Token::Match(tok, "foobar %varid%", 123)) {'
|
||||||
output = self.mc._replaceTokenMatch(input)
|
output = self.mc._replaceTokenMatch(input)
|
||||||
self._assertEquals(output, 'if (match1(tok, 123)) {')
|
self.assertEqual(output, 'if (match1(tok, 123)) {')
|
||||||
self._assertEquals(1, len(self.mc._matchStrs))
|
self.assertEqual(1, len(self.mc._matchStrs))
|
||||||
|
|
||||||
# two in one line
|
# two in one line
|
||||||
input = 'if (Token::Match(tok, "foobar2 %varid%", 123) || Token::Match(tok, "%type% %varid%", 123)) {'
|
input = 'if (Token::Match(tok, "foobar2 %varid%", 123) || Token::Match(tok, "%type% %varid%", 123)) {'
|
||||||
output = self.mc._replaceTokenMatch(input)
|
output = self.mc._replaceTokenMatch(input)
|
||||||
self._assertEquals(output, 'if (match3(tok, 123) || match4(tok, 123)) {')
|
self.assertEqual(output, 'if (match3(tok, 123) || match4(tok, 123)) {')
|
||||||
self._assertEquals(3, len(self.mc._matchStrs))
|
self.assertEqual(3, len(self.mc._matchStrs))
|
||||||
|
|
||||||
def test_replaceTokenSimpleMatch(self):
|
def test_replaceTokenSimpleMatch(self):
|
||||||
input = 'if (Token::simpleMatch(tok, "foobar")) {'
|
input = 'if (Token::simpleMatch(tok, "foobar")) {'
|
||||||
output = self.mc._replaceTokenMatch(input)
|
output = self.mc._replaceTokenMatch(input)
|
||||||
self._assertEquals(output, 'if (match1(tok)) {')
|
self.assertEqual(output, 'if (match1(tok)) {')
|
||||||
self._assertEquals(1, len(self.mc._matchStrs))
|
self.assertEqual(1, len(self.mc._matchStrs))
|
||||||
self._assertEquals(1, self.mc._matchStrs['foobar'])
|
self.assertEqual(1, self.mc._matchStrs['foobar'])
|
||||||
|
|
||||||
input = 'if (Token::simpleMatch(tok->next()->next(), "foobar")) {'
|
input = 'if (Token::simpleMatch(tok->next()->next(), "foobar")) {'
|
||||||
output = self.mc._replaceTokenMatch(input)
|
output = self.mc._replaceTokenMatch(input)
|
||||||
self._assertEquals(output, 'if (match1(tok->next()->next())) {')
|
self.assertEqual(output, 'if (match1(tok->next()->next())) {')
|
||||||
self._assertEquals(1, len(self.mc._matchStrs))
|
self.assertEqual(1, len(self.mc._matchStrs))
|
||||||
self._assertEquals(1, self.mc._matchStrs['foobar'])
|
self.assertEqual(1, self.mc._matchStrs['foobar'])
|
||||||
|
|
||||||
input = 'if (Token::simpleMatch(tok, "foo\"special\"bar")) {'
|
input = 'if (Token::simpleMatch(tok, "foo\"special\"bar")) {'
|
||||||
output = self.mc._replaceTokenMatch(input)
|
output = self.mc._replaceTokenMatch(input)
|
||||||
# FIXME: Currently detected as non-static pattern
|
# FIXME: Currently detected as non-static pattern
|
||||||
self._assertEquals(output, 'if (Token::simpleMatch(tok, "foo\"special\"bar")) {')
|
self.assertEqual(output, 'if (Token::simpleMatch(tok, "foo\"special\"bar")) {')
|
||||||
self._assertEquals(1, len(self.mc._matchStrs))
|
self.assertEqual(1, len(self.mc._matchStrs))
|
||||||
|
|
||||||
def test_replaceTokenFindSimpleMatch(self):
|
def test_replaceTokenFindSimpleMatch(self):
|
||||||
input = 'if (Token::findsimplematch(tok, "foobar")) {'
|
input = 'if (Token::findsimplematch(tok, "foobar")) {'
|
||||||
output = self.mc._replaceTokenFindMatch(input)
|
output = self.mc._replaceTokenFindMatch(input)
|
||||||
self._assertEquals(output, 'if (findmatch1(tok)) {')
|
self.assertEqual(output, 'if (findmatch1(tok)) {')
|
||||||
self._assertEquals(1, len(self.mc._matchStrs))
|
self.assertEqual(1, len(self.mc._matchStrs))
|
||||||
self._assertEquals(1, self.mc._matchStrs['foobar'])
|
self.assertEqual(1, self.mc._matchStrs['foobar'])
|
||||||
|
|
||||||
input = 'if (Token::findsimplematch(tok->next()->next(), "foobar", tok->link())) {'
|
input = 'if (Token::findsimplematch(tok->next()->next(), "foobar", tok->link())) {'
|
||||||
output = self.mc._replaceTokenFindMatch(input)
|
output = self.mc._replaceTokenFindMatch(input)
|
||||||
self._assertEquals(output, 'if (findmatch2(tok->next()->next(), tok->link())) {')
|
self.assertEqual(output, 'if (findmatch2(tok->next()->next(), tok->link())) {')
|
||||||
self._assertEquals(1, len(self.mc._matchStrs))
|
self.assertEqual(1, len(self.mc._matchStrs))
|
||||||
self._assertEquals(1, self.mc._matchStrs['foobar'])
|
self.assertEqual(1, self.mc._matchStrs['foobar'])
|
||||||
|
|
||||||
input = 'if (Token::findsimplematch(tok, "foo\"special\"bar")) {'
|
input = 'if (Token::findsimplematch(tok, "foo\"special\"bar")) {'
|
||||||
output = self.mc._replaceTokenFindMatch(input)
|
output = self.mc._replaceTokenFindMatch(input)
|
||||||
# FIXME: Currently detected as non-static pattern
|
# FIXME: Currently detected as non-static pattern
|
||||||
self._assertEquals(output, 'if (Token::findsimplematch(tok, "foo\"special\"bar")) {')
|
self.assertEqual(output, 'if (Token::findsimplematch(tok, "foo\"special\"bar")) {')
|
||||||
self._assertEquals(1, len(self.mc._matchStrs))
|
self.assertEqual(1, len(self.mc._matchStrs))
|
||||||
|
|
||||||
def test_replaceTokenFindMatch(self):
|
def test_replaceTokenFindMatch(self):
|
||||||
input = 'if (Token::findmatch(tok, "foobar")) {'
|
input = 'if (Token::findmatch(tok, "foobar")) {'
|
||||||
output = self.mc._replaceTokenFindMatch(input)
|
output = self.mc._replaceTokenFindMatch(input)
|
||||||
self._assertEquals(output, 'if (findmatch1(tok)) {')
|
self.assertEqual(output, 'if (findmatch1(tok)) {')
|
||||||
self._assertEquals(1, len(self.mc._matchStrs))
|
self.assertEqual(1, len(self.mc._matchStrs))
|
||||||
self._assertEquals(1, self.mc._matchStrs['foobar'])
|
self.assertEqual(1, self.mc._matchStrs['foobar'])
|
||||||
|
|
||||||
# findmatch with varid
|
# findmatch with varid
|
||||||
input = 'if (Token::findmatch(tok, "foobar %varid%", tok->varId())) {'
|
input = 'if (Token::findmatch(tok, "foobar %varid%", tok->varId())) {'
|
||||||
output = self.mc._replaceTokenFindMatch(input)
|
output = self.mc._replaceTokenFindMatch(input)
|
||||||
self._assertEquals(output, 'if (findmatch2(tok, tok->varId())) {')
|
self.assertEqual(output, 'if (findmatch2(tok, tok->varId())) {')
|
||||||
self._assertEquals(1, len(self.mc._matchStrs))
|
self.assertEqual(1, len(self.mc._matchStrs))
|
||||||
self._assertEquals(1, self.mc._matchStrs['foobar'])
|
self.assertEqual(1, self.mc._matchStrs['foobar'])
|
||||||
|
|
||||||
# findmatch with end token
|
# findmatch with end token
|
||||||
input = 'if (Token::findmatch(tok->next()->next(), "foobar %type%", tok->link())) {'
|
input = 'if (Token::findmatch(tok->next()->next(), "foobar %type%", tok->link())) {'
|
||||||
output = self.mc._replaceTokenFindMatch(input)
|
output = self.mc._replaceTokenFindMatch(input)
|
||||||
self._assertEquals(output, 'if (findmatch3(tok->next()->next(), tok->link())) {')
|
self.assertEqual(output, 'if (findmatch3(tok->next()->next(), tok->link())) {')
|
||||||
self._assertEquals(2, len(self.mc._matchStrs))
|
self.assertEqual(2, len(self.mc._matchStrs))
|
||||||
self._assertEquals(1, self.mc._matchStrs['foobar'])
|
self.assertEqual(1, self.mc._matchStrs['foobar'])
|
||||||
|
|
||||||
# findmatch with end token and varid
|
# findmatch with end token and varid
|
||||||
input = 'if (Token::findmatch(tok->next()->next(), "foobar %type% %varid%", tok->link(), 123)) {'
|
input = 'if (Token::findmatch(tok->next()->next(), "foobar %type% %varid%", tok->link(), 123)) {'
|
||||||
output = self.mc._replaceTokenFindMatch(input)
|
output = self.mc._replaceTokenFindMatch(input)
|
||||||
self._assertEquals(output, 'if (findmatch4(tok->next()->next(), tok->link(), 123)) {')
|
self.assertEqual(output, 'if (findmatch4(tok->next()->next(), tok->link(), 123)) {')
|
||||||
self._assertEquals(2, len(self.mc._matchStrs))
|
self.assertEqual(2, len(self.mc._matchStrs))
|
||||||
self._assertEquals(1, self.mc._matchStrs['foobar'])
|
self.assertEqual(1, self.mc._matchStrs['foobar'])
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
Loading…
Reference in New Issue