# -*- coding: utf-8 -*-

"""Test the bib lexer."""

from bibpy.lexers.biblexer import BibLexer


def token_types(tokens):
    return [token.type for token in tokens]


def test_lex_entry_no_fields():
    assert token_types(BibLexer().lex('@entry{key,}')) ==\
        ['entry', 'name', 'lbrace', 'name', 'comma', 'rbrace']


def test_lex_entry_with_fields():
    assert token_types(BibLexer().lex('@entry{key,author ={bib}}')) ==\
        ['entry', 'name', 'lbrace', 'name', 'comma',
         'name', 'equals', 'lbrace', 'content', 'rbrace', 'rbrace']


def test_lex_string_entry():
    assert token_types(BibLexer().lex('@string{variable = value }')) ==\
        ['entry', 'name', 'lbrace', 'name', 'equals', 'name', 'rbrace']

    assert token_types(BibLexer().lex('@string(variable = value )')) ==\
        ['entry', 'name', 'lparen', 'name', 'equals', 'name', 'rparen']


def test_lex_preamble_entry():
    assert token_types(BibLexer().lex('@preamble{Bla bla bla}')) ==\
        ['entry', 'name', 'lbrace', 'content', 'rbrace']

    assert token_types(BibLexer().lex('@preamble(Bla bla bla)')) ==\
        ['entry', 'name', 'lparen', 'content', 'rparen']


def test_lex_comment_entry():
    assert token_types(BibLexer().lex('@comment{Bla bla bla}')) ==\
        ['entry', 'name', 'lbrace', 'content', 'rbrace']

    assert token_types(BibLexer().lex('@comment(Bla bla bla)')) ==\
        ['entry', 'name', 'lparen', 'content', 'rparen']


def test_lex_with_comments():
    string = """This is a comment
@entry{
    key,
    year = 2001,
}

This is another"""

    assert token_types(BibLexer().lex(string)) ==\
        ['comment', 'entry', 'name', 'lbrace', 'name', 'comma', 'name',
         'equals', 'number', 'comma', 'rbrace', 'comment']

@@ -45,16 +45,29 @@
 er.%22%22%22%0A%0A
+import bibpy%0A
 from bib
@@ -101,16 +101,30 @@
 ibLexer%0A
+import pytest%0A
 %0A%0Adef to
@@ -1767,8 +1767,159 @@
 mment'%5D%0A
+%0A%0Adef test_lexer_fail():%0A    with pytest.raises(bibpy.lexers.base_lexer.LexerError):%0A        print list(BibLexer().lex('@entry!%7Bkey,author%7D =%7Bbib%7D%7D'))%0A

Test bib lexer failure