1. Ali Afshar
  2. Yukon-CSS

Commits

moraes  committed f003561

More lexer tests.

  • Participants
  • Parent commits 91d7ed5
  • Branches default

Comments (0)

Files changed (2)

File tests/test_lexer.py

View file
 # -*- coding: utf-8 -*-
-from yukon.lexer import Lexer, MACROS
+from yukon.lexer import Lexer, MACROS, include
 
 class AllTokensTester(Lexer):
     macros = MACROS
     tokens = {
-        'root': [
+        'basics': [
             (r'{comment}', 'COMMENT'),
             (r'{s}', 'S'),
+            (r'([\&]?({selector}|{pselector}){w})+'
+             r'(\,{w}({selector}{w})+)*{w}'
+             r'(\([^{markers}]*\))?{w}\{', 'SELECTOR', 'selector'),
+            (r'([\.\#]{name}){w}(\([^{markers}]*\))?{w}\;', 'MIXIN'),
+            (r'\${name}{w}\={w}[^{markers}]*\;', 'VARIABLE'),
+            (r'\}', 'LBRACE', '#pop'),
+        ],
+        'root': [
+            include('basics'),
+            (r'\@charset ({string})\;', 'CHARSET'),
+            (r'\@{M}{E}{D}{I}{A}{s}({name}(?:{w}\,{w}{name})*){w}\{', 'MEDIA', 'media'),
+            # STRING should not be in root. It is here just for testing.
             (r'{string}', 'STRING'),
-            (r'([\&]?({selector}|{pselector}){w})+(\,{w}({selector}{w})+)*{w}(\([^{markers}]*\))?{w}\{', 'SELECTOR', 'selector'),
-            (r'\}', 'LBRACE', '#pop'),
-            (r'([\.\#]{name}){w}(\([^{markers}]*\))?{w}\;', 'MIXIN'),
         ],
         'selector': [
-            (r'([\&]?({selector}|{pselector}){w})+(\,{w}({selector}{w})+)*{w}(\([^{markers}]*\))?{w}\{', 'SELECTOR', 'selector'),
-            (r'\}', 'LBRACE', '#pop'),
-            (r'([\.\#]{name}){w}(\([^{markers}]*\))?{w}\;', 'MIXIN'),
+            include('basics'),
+            (r'{ident}{w}\:{w}[^{markers}]*\;', 'PROPERTY'),
         ],
+        'media': [
+            include('basics'),
+        ]
     }
 
+class IgnoreUselessSpacesLexer(AllTokensTester):
+    # wraps the itetator to not return whitespaces
+    def token_iter(self, source, stack=('root',)):
+        for token in super(IgnoreUselessSpacesLexer, self).token_iter(source,
+            stack=stack):
+            if token.type != 'S':
+                yield token
 
 class TestLexer:
     def setup_class(self):
         self.lexer = AllTokensTester()
+        self.no_ws_lexer = IgnoreUselessSpacesLexer()
 
     def setup_method(self, method):
         pass
 
-    def _check_tokens(self, source, expected):
+    def _check_any_lexer(self, token_iter, expected):
         i = 0
-        for token in self.lexer.token_iter(source):
-            assert token.type == expected[i][0]
-            assert token.value == expected[i][1]
+        num_tokens = len(expected)
+        for token in token_iter:
+            # Ignore __EOF__ token, which all results will have.
+            if i < num_tokens:
+                assert token.type == expected[i][0]
+                assert token.value == expected[i][1]
             i += 1
 
+    def _check_lexer(self, source, expected):
+        self._check_any_lexer(self.lexer.token_iter(source), expected)
+
+    def _check_no_ws_lexer(self, source, expected):
+        self._check_any_lexer(self.no_ws_lexer.token_iter(source), expected)
+
     def test_comment(self):
         source = """/* a comment */"""
         expected = [
             ('COMMENT', '/* a comment */'),
-            ('__EOF__', '')
         ]
-        self._check_tokens(source, expected)
+        self._check_lexer(source, expected)
 
     def test_whitespace(self):
         source = """     /* a comment */          """
             ('S', '     '),
             ('COMMENT', '/* a comment */'),
             ('S', '          '),
-            ('__EOF__', '')
         ]
-        self._check_tokens(source, expected)
+        self._check_lexer(source, expected)
 
     def test_string(self):
         source = source = """     "I am a ' string"    'I am another " string'          """
             ('S', '    '),
             ('STRING', "'I am another \" string'"),
             ('S', '          '),
-            ('__EOF__', '')
         ]
-        self._check_tokens(source, expected)
+        self._check_lexer(source, expected)
 
     def test_selector(self):
         source = """.foo {}"""
         expected = [
             ('SELECTOR', '.foo {'),
             ('LBRACE', '}'),
-            ('__EOF__', '')
         ]
-        self._check_tokens(source, expected)
+        self._check_lexer(source, expected)
 
     def test_selector2(self):
         source = """.foo, #bar .baz, .ding ($arg1=#FFF) {}"""
         expected = [
             ('SELECTOR', '.foo, #bar .baz, .ding ($arg1=#FFF) {'),
             ('LBRACE', '}'),
-            ('__EOF__', '')
         ]
-        self._check_tokens(source, expected)
+        self._check_lexer(source, expected)
 
     def test_selector3(self):
         source = """.foo {#bar {}}"""
             ('SELECTOR', '#bar {'),
             ('LBRACE', '}'),
             ('LBRACE', '}'),
-            ('__EOF__', '')
         ]
-        self._check_tokens(source, expected)
+        self._check_lexer(source, expected)
 
     def test_mixin(self):
         source = """.foo ($arg1=#FFF);"""
         expected = [
             ('MIXIN', '.foo ($arg1=#FFF);'),
-            ('__EOF__', '')
         ]
-        self._check_tokens(source, expected)
+        self._check_lexer(source, expected)
+
+    def test_variable(self):
+        source = """$foo = #FFF;"""
+        expected = [
+            ('VARIABLE', '$foo = #FFF;'),
+        ]
+        self._check_lexer(source, expected)
+
+    def test_property(self):
+        source = """.foo { background-color: #FFF; }"""
+        expected = [
+            ('SELECTOR', '.foo {'),
+            ('S', ' '),
+            ('PROPERTY', 'background-color: #FFF;'),
+            ('S', ' '),
+            ('LBRACE', '}'),
+        ]
+        self._check_lexer(source, expected)
+
+    def test_property_no_ws(self):
+        source = """.foo { background-color: #FFF; }"""
+        expected = [
+            ('SELECTOR', '.foo {'),
+            ('PROPERTY', 'background-color: #FFF;'),
+            ('LBRACE', '}'),
+        ]
+        self._check_no_ws_lexer(source, expected)
+
+    def test_media_no_ws(self):
+        source = """@media screen { .foo { background-color: #FFF; } }"""
+        expected = [
+            ('MEDIA', '@media screen {'),
+            ('SELECTOR', '.foo {'),
+            ('PROPERTY', 'background-color: #FFF;'),
+            ('LBRACE', '}'),
+            ('LBRACE', '}'),
+        ]
+        self._check_no_ws_lexer(source, expected)
+
+    def test_charset(self):
+        source = """@charset "UTF-8"; @media screen { .foo { background-color: #FFF; #baz { } } }"""
+        expected = [
+            ('CHARSET', '@charset "UTF-8";'),
+            ('MEDIA', '@media screen {'),
+            ('SELECTOR', '.foo {'),
+            ('PROPERTY', 'background-color: #FFF;'),
+            ('SELECTOR', '#baz {'),
+            ('LBRACE', '}'),
+            ('LBRACE', '}'),
+            ('LBRACE', '}'),
+        ]
+        self._check_no_ws_lexer(source, expected)

File tests/test_parser.py

View file
         f.close()
 
         f = open(os.path.join(path, 'css', filename + '.css'), 'r')
-        self.result = f.read().strip()
+        self.expected = f.read().strip()
         f.close()
 
         env = Environment()
         self.parser = Parser(environment=env)
         self.writer = Writer()
 
+    def _check_expected(self):
+        root = self.parser.parse(self.source)
+        css = self.writer.write(root)
+        assert css == self.expected
+
     def test_nesting(self):
-        root = self.parser.parse(self.source)
-        result = self.writer.write(root)
-        assert result == self.result
+        self._check_expected()
 
     def test_multiple_nesting(self):
-        root = self.parser.parse(self.source)
-        result = self.writer.write(root)
-        assert result == self.result
+        self._check_expected()
 
     def test_multiple_nesting2(self):
-        root = self.parser.parse(self.source)
-        result = self.writer.write(root)
-        assert result == self.result
+        self._check_expected()
 
     def test_operator_nesting(self):
-        root = self.parser.parse(self.source)
-        result = self.writer.write(root)
-        assert result == self.result
+        self._check_expected()
 
     def test_operator_multiple_nesting(self):
-        root = self.parser.parse(self.source)
-        result = self.writer.write(root)
-        assert result == self.result
+        self._check_expected()
 
     def test_pseudo_class_nesting(self):
-        root = self.parser.parse(self.source)
-        result = self.writer.write(root)
-        assert result == self.result
+        self._check_expected()
 
     def test_pseudo_class_multiple_nesting(self):
-        root = self.parser.parse(self.source)
-        result = self.writer.write(root)
-        assert result == self.result
+        self._check_expected()
 
     def test_pseudo_class_multiple_nesting2(self):
-        root = self.parser.parse(self.source)
-        result = self.writer.write(root)
-        assert result == self.result
+        self._check_expected()
 
     def test_media_nesting(self):
-        root = self.parser.parse(self.source)
-        result = self.writer.write(root)
-        assert result == self.result
+        self._check_expected()
 
     def test_media_multiple_nesting(self):
-        root = self.parser.parse(self.source)
-        result = self.writer.write(root)
-        assert result == self.result
+        self._check_expected()
 
     def test_mixin_nesting(self):
-        root = self.parser.parse(self.source)
-        result = self.writer.write(root)
-        assert result == self.result
+        self._check_expected()
 
     def test_mixin_multiple_nesting(self):
-        root = self.parser.parse(self.source)
-        result = self.writer.write(root)
-        assert result == self.result
+        self._check_expected()
 
     def test_variable_simple(self):
-        root = self.parser.parse(self.source)
-        result = self.writer.write(root)
-        assert result == self.result
+        self._check_expected()
 
     def test_variable_scoped(self):
-        root = self.parser.parse(self.source)
-        result = self.writer.write(root)
-        assert result == self.result
+        self._check_expected()
 
     def test_variable_scoped2(self):
-        root = self.parser.parse(self.source)
-        result = self.writer.write(root)
-        assert result == self.result
+        self._check_expected()
 
     def test_variable_and_mixin(self):
-        root = self.parser.parse(self.source)
-        result = self.writer.write(root)
-        assert result == self.result
+        self._check_expected()
 
     def test_import_mixin(self):
-        root = self.parser.parse(self.source)
-        result = self.writer.write(root)
-        assert result == self.result
+        self._check_expected()
 
     def test_import_mixin_multiple(self):
-        root = self.parser.parse(self.source)
-        result = self.writer.write(root)
-        assert result == self.result
+        self._check_expected()
 
     def test_import_variable_multiple(self):
-        root = self.parser.parse(self.source)
-        result = self.writer.write(root)
-        assert result == self.result
+        self._check_expected()
 
     def test_charset_import(self):
-        root = self.parser.parse(self.source)
-        result = self.writer.write(root)
-        assert result == self.result
+        self._check_expected()