Skip to content

Commit

Permalink
Refactored to remove inter-class dependency.
Browse files Browse the repository at this point in the history
- Extracted token types into their own class
  • Loading branch information
waynemoore committed Dec 27, 2012
1 parent 630a896 commit f9389a3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
13 changes: 8 additions & 5 deletions gherkin.py
Expand Up @@ -3,10 +3,13 @@
from StringIO import StringIO


class GherkinParser(object):
class Tokens(object):
TEXT = 1
GROUP = 2


class GherkinParser(object):

def __init__(self, gherkin_text):
self._gherkin_text = StringIO(gherkin_text)
self._tokens = []
Expand Down Expand Up @@ -37,11 +40,11 @@ def _new_group(self):

def _store_and_reset_group(self):
if len(self._group) > 0:
self._tokens.append((GherkinParser.GROUP, self._group))
self._tokens.append((Tokens.GROUP, self._group))
self._new_group()

def _add_text_token(self, text):
self._tokens.append((GherkinParser.TEXT, text))
self._tokens.append((Tokens.TEXT, text))

def _finish(self):
self._store_and_reset_group()
Expand All @@ -55,9 +58,9 @@ def __init__(self):
def format(self, parsed):
for token_type, token in parsed:

if token_type == GherkinParser.TEXT:
if token_type == Tokens.TEXT:
self._emit(token)
elif token_type == GherkinParser.GROUP:
elif token_type == Tokens.GROUP:
self._format_group(token)
else:
raise Exception('unsupported token type %s' % token_type)
Expand Down
28 changes: 14 additions & 14 deletions tests/gherkin_test.py
@@ -1,7 +1,7 @@
import sure
import unittest

from gherkin import GherkinParser, GherkinFormatter
from gherkin import GherkinParser, GherkinFormatter, Tokens


class GherkinParserTestCase(unittest.TestCase):
Expand All @@ -14,9 +14,9 @@ def test_it_should_parse_non_example_tables_as_text(self):

tokens = GherkinParser(feature).parse()
tokens.should.have.length_of(3)
tokens[0].should.equal((GherkinParser.TEXT, 'Feature: As a tester'))
tokens[1].should.equal((GherkinParser.TEXT, 'I want my non example table text to remain intact'))
tokens[2].should.equal((GherkinParser.TEXT, 'So that I don\'t want kill the author of this plugin'))
tokens[0].should.equal((Tokens.TEXT, 'Feature: As a tester'))
tokens[1].should.equal((Tokens.TEXT, 'I want my non example table text to remain intact'))
tokens[2].should.equal((Tokens.TEXT, 'So that I don\'t want kill the author of this plugin'))

def test_it_should_parse_example_groups(self):
example_text = "\
Expand All @@ -28,7 +28,7 @@ def test_it_should_parse_example_groups(self):
tokens.should.have.length_of(1)

group = tokens[0]
group[0].should.equal(GherkinParser.GROUP)
group[0].should.equal(Tokens.GROUP)

examples = group[1]
examples.should.have.length_of(3)
Expand All @@ -49,13 +49,13 @@ def test_it_should_parse_text_and_groups(self):
tokens = GherkinParser(feature).parse()
tokens.should.have.length_of(5)

tokens[0].should.equal((GherkinParser.TEXT, 'Feature: As a crazy cat person'))
tokens[1].should.equal((GherkinParser.TEXT, 'I want to write a list of cat breeds'))
tokens[2].should.equal((GherkinParser.TEXT, 'So that my codez is odd'))
tokens[3].should.equal((GherkinParser.TEXT, ''))
tokens[0].should.equal((Tokens.TEXT, 'Feature: As a crazy cat person'))
tokens[1].should.equal((Tokens.TEXT, 'I want to write a list of cat breeds'))
tokens[2].should.equal((Tokens.TEXT, 'So that my codez is odd'))
tokens[3].should.equal((Tokens.TEXT, ''))

group = tokens[4]
group[0].should.equal(GherkinParser.GROUP)
group[0].should.equal(Tokens.GROUP)

examples = group[1]
examples.should.have.length_of(3)
Expand All @@ -73,16 +73,16 @@ def test_it_should_parse_multiple_text_and_group_sections(self):
tokens = GherkinParser(feature).parse()
tokens.should.have.length_of(4)

tokens[0].should.equal((GherkinParser.TEXT, 'foo'))
tokens[0].should.equal((Tokens.TEXT, 'foo'))

group = tokens[1]
group[0].should.equal(GherkinParser.GROUP)
group[0].should.equal(Tokens.GROUP)
group[1].should.equal([['exampleA']])

tokens[2].should.equal((GherkinParser.TEXT, 'bar'))
tokens[2].should.equal((Tokens.TEXT, 'bar'))

group = tokens[3]
group[0].should.equal(GherkinParser.GROUP)
group[0].should.equal(Tokens.GROUP)
group[1].should.equal([['example1', 'example2']])


Expand Down

0 comments on commit f9389a3

Please sign in to comment.