<commit_before># coding: utf-8

from __future__ import (absolute_import, division, print_function,
                        unicode_literals)
from citeproc.py2compat import *

import os

from io import StringIO
from unittest import TestCase

from citeproc.source.bibtex.bibparse import BibTeXParser, BibTeXDecodeError


TEST_BIB = os.path.join(os.path.dirname(__file__), 'test.bib')


class TestBibTeXParser(TestCase):
    def test_parse(self):
        file = StringIO(sample)
        bib = BibTeXParser(file)
        self.print_entries(bib)
        # TODO: perform useful checks

    def test_parse_file(self):
        test_bib = BibTeXParser(TEST_BIB, encoding='utf-8')
        self.print_entries(test_bib)

    def test_parse_file_bad_encoding(self):
        try:
            BibTeXParser(TEST_BIB)
            self.assertTrue(False)
        except BibTeXDecodeError:
            self.assertTrue(True)

    @staticmethod
    def print_entries(bib):
        for key, entry in bib.items():
            print(key)
            for name, value in entry.items():
                print('   {}: {}'.format(name, value))


# based on the sample BibTeX database by <NAME>
# http://artis.imag.fr/~Xavier.Decoret/resources/xdkbibtex/bibtex_summary.html

sample = r"""
@Article(py03,<commit_msg>Properly test for the BibTeXDecodeError exception<commit_after># coding: utf-8

from __future__ import (absolute_import, division, print_function,
                        unicode_literals)
from citeproc.py2compat import *

import os

from io import StringIO
from unittest import TestCase

from citeproc.source.bibtex.bibparse import BibTeXParser, BibTeXDecodeError


TEST_BIB = os.path.join(os.path.dirname(__file__), 'test.bib')


class TestBibTeXParser(TestCase):
    def test_parse(self):
        file = StringIO(sample)
        bib = BibTeXParser(file)
        self.print_entries(bib)
        # TODO: perform useful checks

    def test_parse_file(self):
        test_bib = BibTeXParser(TEST_BIB, encoding='utf-8')
        self.print_entries(test_bib)

    def test_parse_file_bad_encoding(self):
        with self.assertRaises(BibTeXDecodeError):
            BibTeXParser(TEST_BIB)

    @staticmethod
    def print_entries(bib):
        for key, entry in bib.items():
            print(key)
            for name, value in entry.items():
                print('   {}: {}'.format(name, value))


# based on the sample BibTeX database by <NAME>
# http://artis.imag.fr/~Xavier.Decoret/resources/xdkbibtex/bibtex_summary.html

sample = r"""
@Article(py03,
{'fraction_non_alphanumeric': 0.09767625049231982, 'fraction_numerical': 0.003938558487593541, 'mean_word_length': 3.006309148264984, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1270379', 'n_tokens_mistral': 847, 'n_tokens_neox': 770, 'n_words': 175}