def test_lex_comment_no_newline(): # Given a lexer loaded with a comment without the newline marker lexer = gherkin.Lexer(' test comment') # When we lex the input string new_state = lexer.lex_comment_metadata_value() # Then we see the whole line was captured lexer.tokens.should.equal([ (1, gherkin.TOKEN_META_VALUE, 'test comment'), ]) # And we also see that the next new_state.should.equal(lexer.lex_text)
def test_lex_comment_until_newline(): "Lexer.lex_comment() Should parse comments until the newline character" # Given a lexer loaded with comments containing a metadata field lexer = gherkin.Lexer('# one line\n# another line') # When I run the lexer tokens = lexer.run() # Then we see both lines were captured lexer.tokens.should.equal([ (1, gherkin.TOKEN_COMMENT, 'one line'), (1, gherkin.TOKEN_NEWLINE, '\n'), (2, gherkin.TOKEN_COMMENT, 'another line'), (2, gherkin.TOKEN_EOF, ''), ])
def test_lex_comment_full(): "Lexer.run() Should be able to process metadata in comments" # Given a lexer loaded with comments containing a metadata field lexer = gherkin.Lexer('some text # metadata-field: blah-value\ntext') # When I run the lexer tokens = lexer.run() # Then I see the tokens collected match some text, a field, more # text and EOF tokens.should.equal([(1, gherkin.TOKEN_TEXT, 'some text '), (1, gherkin.TOKEN_META_LABEL, 'metadata-field'), (1, gherkin.TOKEN_META_VALUE, 'blah-value'), (1, gherkin.TOKEN_NEWLINE, '\n'), (2, gherkin.TOKEN_TEXT, 'text'), (2, gherkin.TOKEN_EOF, '')])
def test_lex_text_with_label(): "Lexer.run() Should be able to parse a label with some text" # Given a lexer loaded with a feature lexer = gherkin.Lexer( 'Feature: A cool feature\n some more text\n even more text') # When we run the lexer tokens = lexer.run() # Then we see the token list matches the label, text, text EOF # sequence tokens.should.equal([(1, gherkin.TOKEN_LABEL, 'Feature'), (1, gherkin.TOKEN_TEXT, 'A cool feature'), (1, gherkin.TOKEN_NEWLINE, '\n'), (2, gherkin.TOKEN_TEXT, 'some more text'), (2, gherkin.TOKEN_NEWLINE, '\n'), (3, gherkin.TOKEN_TEXT, 'even more text'), (3, gherkin.TOKEN_EOF, '')])
def test_lex_text_with_labels(): "Lexer.run() Should be able to tokenize a feature with a scenario" # Given a lexer with a more complete feature+scenario lexer = gherkin.Lexer(''' Feature: Some descriptive text In order to parse a Gherkin file As a parser I want to be able to parse scenarios Even more text Scenario: The user wants to describe a feature ''') # When we run the lexer tokens = lexer.run() # Then we see it was broken down into the right list of tokens tokens.should.equal([ (1, gherkin.TOKEN_NEWLINE, '\n'), (2, gherkin.TOKEN_NEWLINE, '\n'), (3, gherkin.TOKEN_LABEL, 'Feature'), (3, gherkin.TOKEN_TEXT, 'Some descriptive text'), (3, gherkin.TOKEN_NEWLINE, '\n'), (4, gherkin.TOKEN_TEXT, 'In order to parse a Gherkin file'), (4, gherkin.TOKEN_NEWLINE, '\n'), (5, gherkin.TOKEN_TEXT, 'As a parser'), (5, gherkin.TOKEN_NEWLINE, '\n'), (6, gherkin.TOKEN_TEXT, 'I want to be able to parse scenarios'), (6, gherkin.TOKEN_NEWLINE, '\n'), (7, gherkin.TOKEN_NEWLINE, '\n'), (8, gherkin.TOKEN_TEXT, 'Even more text'), (8, gherkin.TOKEN_NEWLINE, '\n'), (9, gherkin.TOKEN_NEWLINE, '\n'), (10, gherkin.TOKEN_LABEL, 'Scenario'), (10, gherkin.TOKEN_TEXT, 'The user wants to describe a feature'), (10, gherkin.TOKEN_NEWLINE, '\n'), (11, gherkin.TOKEN_EOF, '') ])