diff options
author | Gaurav Jain <gaurav@gauravjain.org> | 2014-04-23 19:27:49 -0400 |
---|---|---|
committer | Gaurav Jain <gaurav@gauravjain.org> | 2014-04-23 19:27:49 -0400 |
commit | 9aed3827db61f0365257bd73a2176b114b51c044 (patch) | |
tree | b97268045a7dcdee77afb84bec4b48024dbf4743 /tests/test_clexer.py | |
parent | 24d72e19ac963000d76a11679448c9e6867feba0 (diff) | |
download | pygments-9aed3827db61f0365257bd73a2176b114b51c044.tar.gz |
Test cases for C lexer for switch/case statements and labels
Diffstat (limited to 'tests/test_clexer.py')
-rw-r--r-- | tests/test_clexer.py | 227 |
1 files changed, 226 insertions, 1 deletions
diff --git a/tests/test_clexer.py b/tests/test_clexer.py index c995bb2b..5d251d2e 100644 --- a/tests/test_clexer.py +++ b/tests/test_clexer.py @@ -9,8 +9,9 @@ import unittest import os +import textwrap -from pygments.token import Text, Number +from pygments.token import Text, Number, Token from pygments.lexers import CLexer @@ -29,3 +30,227 @@ class CLexerTest(unittest.TestCase): wanted.append((Text, ' ')) wanted = [(Text, '')] + wanted[:-1] + [(Text, '\n')] self.assertEqual(list(self.lexer.get_tokens(code)), wanted) + + def testSwitch(self): + fragment = u'''\ + int main() + { + switch (0) + { + case 0: + default: + ; + } + } + ''' + expected = [ + (Token.Text, u''), + (Token.Keyword.Type, u'int'), + (Token.Text, u' '), + (Token.Name.Function, u'main'), + (Token.Text, u''), + (Token.Punctuation, u'('), + (Token.Punctuation, u')'), + (Token.Text, u'\n'), + (Token.Text, u''), + (Token.Punctuation, u'{'), + (Token.Text, u'\n'), + (Token.Text, u' '), + (Token.Keyword, u'switch'), + (Token.Text, u' '), + (Token.Punctuation, u'('), + (Token.Literal.Number.Integer, u'0'), + (Token.Punctuation, u')'), + (Token.Text, u'\n'), + (Token.Text, u' '), + (Token.Punctuation, u'{'), + (Token.Text, u'\n'), + (Token.Text, u' '), + (Token.Keyword, u'case'), + (Token.Text, u' '), + (Token.Literal.Number.Integer, u'0'), + (Token.Operator, u':'), + (Token.Text, u'\n'), + (Token.Text, u' '), + (Token.Keyword, u'default'), + (Token.Operator, u':'), + (Token.Text, u'\n'), + (Token.Text, u' '), + (Token.Punctuation, u';'), + (Token.Text, u'\n'), + (Token.Text, u' '), + (Token.Punctuation, u'}'), + (Token.Text, u'\n'), + (Token.Punctuation, u'}'), + (Token.Text, u'\n'), + (Token.Text, u''), + ] + self.assertEqual(expected, list(self.lexer.get_tokens(textwrap.dedent(fragment)))) + + def testSwitchSpaceBeforeColon(self): + fragment = u'''\ + int main() + { + switch (0) + { + case 0 : + default : + ; + } + } + ''' + expected = [ + (Token.Text, u''), + (Token.Keyword.Type, u'int'), + (Token.Text, u' '), + (Token.Name.Function, u'main'), + (Token.Text, u''), + (Token.Punctuation, u'('), + (Token.Punctuation, u')'), + (Token.Text, u'\n'), + (Token.Text, u''), + (Token.Punctuation, u'{'), + (Token.Text, u'\n'), + (Token.Text, u' '), + (Token.Keyword, u'switch'), + (Token.Text, u' '), + (Token.Punctuation, u'('), + (Token.Literal.Number.Integer, u'0'), + (Token.Punctuation, u')'), + (Token.Text, u'\n'), + (Token.Text, u' '), + (Token.Punctuation, u'{'), + (Token.Text, u'\n'), + (Token.Text, u' '), + (Token.Keyword, u'case'), + (Token.Text, u' '), + (Token.Literal.Number.Integer, u'0'), + (Token.Text, u' '), + (Token.Operator, u':'), + (Token.Text, u'\n'), + (Token.Text, u' '), + (Token.Keyword, u'default'), + (Token.Text, u' '), + (Token.Operator, u':'), + (Token.Text, u'\n'), + (Token.Text, u' '), + (Token.Punctuation, u';'), + (Token.Text, u'\n'), + (Token.Text, u' '), + (Token.Punctuation, u'}'), + (Token.Text, u'\n'), + (Token.Punctuation, u'}'), + (Token.Text, u'\n'), + (Token.Text, u''), + ] + self.assertEqual(expected, list(self.lexer.get_tokens(textwrap.dedent(fragment)))) + + def testLabel(self): + fragment = u'''\ + int main() + { + foo: + goto foo; + } + ''' + expected = [ + (Token.Text, u''), + (Token.Keyword.Type, u'int'), + (Token.Text, u' '), + (Token.Name.Function, u'main'), + (Token.Text, u''), + (Token.Punctuation, u'('), + (Token.Punctuation, u')'), + (Token.Text, u'\n'), + (Token.Text, u''), + (Token.Punctuation, u'{'), + (Token.Text, u'\n'), + (Token.Name.Label, u'foo'), + (Token.Punctuation, u':'), + (Token.Text, u'\n'), + (Token.Text, u' '), + (Token.Keyword, u'goto'), + (Token.Text, u' '), + (Token.Name, u'foo'), + (Token.Punctuation, u';'), + (Token.Text, u'\n'), + (Token.Punctuation, u'}'), + (Token.Text, u'\n'), + (Token.Text, u''), + ] + self.assertEqual(expected, list(self.lexer.get_tokens(textwrap.dedent(fragment)))) + + def testLabelSpaceBeforeColon(self): + fragment = u'''\ + int main() + { + foo : + goto foo; + } + ''' + expected = [ + (Token.Text, u''), + (Token.Keyword.Type, u'int'), + (Token.Text, u' '), + (Token.Name.Function, u'main'), + (Token.Text, u''), + (Token.Punctuation, u'('), + (Token.Punctuation, u')'), + (Token.Text, u'\n'), + (Token.Text, u''), + (Token.Punctuation, u'{'), + (Token.Text, u'\n'), + (Token.Name.Label, u'foo'), + (Token.Text, u' '), + (Token.Punctuation, u':'), + (Token.Text, u'\n'), + (Token.Text, u' '), + (Token.Keyword, u'goto'), + (Token.Text, u' '), + (Token.Name, u'foo'), + (Token.Punctuation, u';'), + (Token.Text, u'\n'), + (Token.Punctuation, u'}'), + (Token.Text, u'\n'), + (Token.Text, u''), + ] + self.assertEqual(expected, list(self.lexer.get_tokens(textwrap.dedent(fragment)))) + + def testLabelFollowedByStatement(self): + fragment = u'''\ + int main() + { + foo:return 0; + goto foo; + } + ''' + expected = [ + (Token.Text, u''), + (Token.Keyword.Type, u'int'), + (Token.Text, u' '), + (Token.Name.Function, u'main'), + (Token.Text, u''), + (Token.Punctuation, u'('), + (Token.Punctuation, u')'), + (Token.Text, u'\n'), + (Token.Text, u''), + (Token.Punctuation, u'{'), + (Token.Text, u'\n'), + (Token.Name.Label, u'foo'), + (Token.Punctuation, u':'), + (Token.Keyword, u'return'), + (Token.Text, u' '), + (Token.Literal.Number.Integer, u'0'), + (Token.Punctuation, u';'), + (Token.Text, u'\n'), + (Token.Text, u' '), + (Token.Keyword, u'goto'), + (Token.Text, u' '), + (Token.Name, u'foo'), + (Token.Punctuation, u';'), + (Token.Text, u'\n'), + (Token.Punctuation, u'}'), + (Token.Text, u'\n'), + (Token.Text, u''), + ] + self.assertEqual(expected, list(self.lexer.get_tokens(textwrap.dedent(fragment)))) |