Initial code for parsing from stream.

This commit is contained in:
Adi Roiban 2014-02-16 15:54:00 +02:00
parent 923f1b089a
commit 032cfa1a8c
5 changed files with 95 additions and 6 deletions

1
.gitignore vendored
View File

@ -9,3 +9,4 @@ dist
MANIFEST MANIFEST
.tox .tox
.venv* .venv*
*.egg-info/

View File

@ -460,12 +460,16 @@ class LessLexer:
self.lexer.input(f.read()) self.lexer.input(f.read())
return self return self
def input(self, filename): def input(self, file):
""" """
Wrapper for file Load lexer with content from `file` which can be a path or a file
like object.
""" """
with open(filename) as f: if isinstance(file, basestring):
self.lexer.input(f.read()) with open(file) as f:
self.lexer.input(f.read())
else:
self.lexer.input(file.read())
def token(self): def token(self):
""" """

View File

@ -77,7 +77,7 @@ class LessParser(object):
self.result = None self.result = None
self.target = None self.target = None
def parse(self, filename='', debuglevel=0): def parse(self, filename='', filestream=None, debuglevel=0):
""" Parse file. """ Parse file.
kwargs: kwargs:
filename (str): File to parse filename (str): File to parse
@ -87,8 +87,14 @@ class LessParser(object):
print('Compiling target: %s' % filename, file=sys.stderr) print('Compiling target: %s' % filename, file=sys.stderr)
self.scope.push() self.scope.push()
self.target = filename self.target = filename
if filestream:
content = filestream
else:
content = filename
self.result = self.parser.parse( self.result = self.parser.parse(
filename, lexer=self.lex, debug=debuglevel) content, lexer=self.lex, debug=debuglevel)
self.post_parse() self.post_parse()
def post_parse(self): def post_parse(self):

47
test/test_lexer.py Normal file
View File

@ -0,0 +1,47 @@
"""
Unit tests for the lexer.
"""
from StringIO import StringIO
from tempfile import NamedTemporaryFile
import unittest
from lesscpy.lessc.lexer import LessLexer
class TestLessLexer(unittest.TestCase):
"""
Unit tests for LessLexer
"""
def setUp(self):
self.lexer = LessLexer()
def test_input_stream(self):
"""
It can load content from a string.
"""
file = StringIO("""
@simple-var: 1;
""")
self.lexer.input(file)
token = self.lexer.token()
self.assertEqual('@simple-var', token.value)
def test_input_path(self):
"""
It can load content from a path.
"""
file = NamedTemporaryFile()
file.write("""
@simple-var: 1;
""")
file.seek(0)
self.lexer.input(file.name)
token = self.lexer.token()
self.assertEqual('@simple-var', token.value)

31
test/test_parser.py Normal file
View File

@ -0,0 +1,31 @@
"""
Unit test for the parser.
"""
from StringIO import StringIO
import unittest
from lesscpy.lessc.parser import LessParser
class TestLessParser(unittest.TestCase):
"""
Unit tests for LessParser.
"""
def setUp(self):
self.parser = LessParser()
def test_parse_stream(self):
"""
It can parse input from a file stream.
"""
stream = StringIO("""
@nice-blue: #5B83AD;
""")
self.parser.parse(filestream=stream)
# A single object is parser which is the expected variable.
self.assertEqual(1, len(self.parser.result))
variable = self.parser.result[0]
self.assertEqual('@nice-blue', variable.name)
self.assertEqual(['#5b83ad'], variable.value)