<commit_before>
"""
unit tests of the pdf writer
"""

import unittest
import subprocess
import tempfile
import os
import sys

from pyth.plugins.pdf.writer import PDFWriter
from pyth.plugins.python.reader import *

class TestWritePDF(unittest.TestCase):

    def pdf_to_html(self, pdf):
        """
        We are using pdftohtml to convert the pdf document to an html
        document.

        Since it is difficult to check a pdf document, this allow us
        to first convert it into html, and then perform the checks on
        this html document.
        """
        # pdftohtml needs its input from a file so we first save the
        # pdf into a temporary file.
        _, filename = tempfile.mkstemp(suffix='.pdf')
        file = open(filename, "wb")
        try:
            file.write(pdf)
            file.close()
            command = ["pdftohtml", "-stdout", filename]
            try:
                proc = subprocess.Popen(command, stdout=subprocess.PIPE)
            except OSError:
                print "Make sure that pdftohtml is installed"
                raise
            ret = proc.communicate()[0]
            return ret
        finally:                    # Make sure to remove the tmp file
            file.close()
            os.remove(filename)

    def test_basic(self):
        doc = PythonReader.read([])
        pdf = PDFWriter.write(doc).getvalue()
        html = self.pdf_to_html(pdf)

    def test_paragraph(self):
        doc = PythonReader.read(P[u"the text"])
        pdf = PDFWriter.write(doc).getvalue()
        html = self.pdf_to_html(pdf)
        assert "the text" in html
        
if __name__ == '__main__':
    unittest.main()<commit_msg>Add some more pdf unit tests<commit_after>"""
unit tests of the pdf writer
"""

import unittest
import subprocess
import tempfile
import os
import sys
import BeautifulSoup

from pyth.plugins.pdf.writer import PDFWriter
from pyth.plugins.python.reader import *


class TestWritePDF(unittest.TestCase):

    def pdf_to_html(self, pdf):
        """
        We are using pdftohtml to convert the pdf document to an html
        document.

        Since it is difficult to check a pdf document, this allow us
        to first convert it into html, and then perform the checks on
        this html document.
        """
        # pdftohtml needs its input from a file so we first save the
        # pdf into a temporary file.
        _, filename = tempfile.mkstemp(suffix='.pdf')
        file = open(filename, "wb")
        try:
            file.write(pdf)
            file.close()
            command = ["pdftohtml", "-stdout", filename]
            try:
                proc = subprocess.Popen(command, stdout=subprocess.PIPE)
            except OSError:
                print "Make sure that pdftohtml is installed"
                raise
            ret = proc.communicate()[0]
            return ret
        finally:                    # Make sure to remove the tmp file
            file.close()
            os.remove(filename)

    def test_basic(self):
        """
        Try to create an empty pdf document
        """
        doc = PythonReader.read([])
        pdf = PDFWriter.write(doc).getvalue()
        html = self.pdf_to_html(pdf)

    def test_paragraph(self):
        """
        Try a simple document with one paragraph
        """
        doc = PythonReader.read(P[u"the text"])
        pdf = PDFWriter.write(doc).getvalue()
        html = self.pdf_to_html(pdf)
        assert "the text" in html

    def test_bold(self):
        doc = PythonReader.read([P[T(BOLD)[u"bold text"]]])
        pdf = PDFWriter.write(doc).getvalue()
        html = self.pdf_to_html(pdf)
        soup = BeautifulSoup.BeautifulSoup(html)
        node = soup.find("b")
        assert node
        assert node.string == "bold text"

    def test_italic(self):
        doc = PythonReader.read([P[T(ITALIC)[u"italic text"]]])
        pdf = PDFWriter.write(doc).getvalue()
        html = self.pdf_to_html(pdf)
        soup = BeautifulSoup.BeautifulSoup(html)
        node = soup.find("i")
        assert node
        assert node.string == "italic text"

if __name__ == '__main__':
    unittest.main()
{'fraction_non_alphanumeric': 0.09350275713258212, 'fraction_numerical': 0.0004795013186286262, 'mean_word_length': 2.005763688760807, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7354826', 'n_tokens_mistral': 1178, 'n_tokens_neox': 1165, 'n_words': 362}