<s> import os <EOL> import io <EOL> import sys <EOL> import codecs <EOL> import unittest <EOL> TESTS_ROOT = os . path . abspath ( os . path . dirname ( __file__ ) ) <EOL> sys . path . insert ( <NUM_LIT:0> , os . path . realpath ( os . path . join ( TESTS_ROOT , '<STR_LIT:..>' ) ) ) <EOL> import pdfkit <EOL> class TestPDFKitInitialization ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_html_source ( self ) : <EOL> r = pdfkit . PDFKit ( '<STR_LIT>' , '<STR_LIT:string>' ) <EOL> self . assertTrue ( r . source . isString ( ) ) <EOL> def test_url_source ( self ) : <EOL> r = pdfkit . PDFKit ( '<STR_LIT>' , '<STR_LIT:url>' ) <EOL> self . assertTrue ( r . source . isUrl ( ) ) <EOL> def test_file_source ( self ) : <EOL> r = pdfkit . PDFKit ( '<STR_LIT>' , '<STR_LIT:file>' ) <EOL> self . assertTrue ( r . source . isFile ( ) ) <EOL> def test_file_object_source ( self ) : <EOL> with open ( '<STR_LIT>' ) as fl : <EOL> r = pdfkit . PDFKit ( fl , '<STR_LIT:file>' ) <EOL> self . assertTrue ( r . source . isFileObj ( ) ) <EOL> def test_file_source_with_path ( self ) : <EOL> r = pdfkit . PDFKit ( '<STR_LIT:test>' , '<STR_LIT:string>' ) <EOL> with io . open ( '<STR_LIT>' ) as f : <EOL> self . assertTrue ( r . source . isFile ( path = f ) ) <EOL> with codecs . open ( '<STR_LIT>' , encoding = '<STR_LIT>' ) as f : <EOL> self . assertTrue ( r . source . isFile ( path = f ) ) <EOL> def test_options_parsing ( self ) : <EOL> r = pdfkit . PDFKit ( '<STR_LIT:html>' , '<STR_LIT:string>' , options = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertTrue ( r . options [ '<STR_LIT>' ] ) <EOL> def test_options_parsing_with_dashes ( self ) : <EOL> r = pdfkit . PDFKit ( '<STR_LIT:html>' , '<STR_LIT:string>' , options = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertTrue ( r . options [ '<STR_LIT>' ] ) <EOL> def test_custom_configuration ( self ) : <EOL> conf = pdfkit . configuration ( ) <EOL> self . assertEqual ( '<STR_LIT>' , conf . meta_tag_prefix ) <EOL> conf = pdfkit . configuration ( meta_tag_prefix = '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' , conf . meta_tag_prefix ) <EOL> with self . assertRaises ( IOError ) : <EOL> conf = pdfkit . configuration ( wkhtmltopdf = '<STR_LIT>' ) <EOL> class TestPDFKitCommandGeneration ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_command_construction ( self ) : <EOL> r = pdfkit . PDFKit ( '<STR_LIT:html>' , '<STR_LIT:string>' , options = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:12> } ) <EOL> command = r . command ( ) <EOL> self . assertEqual ( command [ <NUM_LIT:0> ] , r . wkhtmltopdf ) <EOL> self . assertEqual ( command [ command . index ( '<STR_LIT>' ) + <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> self . assertEqual ( command [ command . index ( '<STR_LIT>' ) + <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> def test_lists_of_input_args ( self ) : <EOL> urls = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> paths = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> r = pdfkit . PDFKit ( urls , '<STR_LIT:url>' ) <EOL> r2 = pdfkit . PDFKit ( paths , '<STR_LIT:file>' ) <EOL> cmd = r . command ( ) <EOL> cmd2 = r2 . command ( ) <EOL> self . assertEqual ( cmd [ - <NUM_LIT:3> : ] , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:->' ] ) <EOL> self . assertEqual ( cmd2 [ - <NUM_LIT:3> : ] , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:->' ] ) <EOL> def test_read_source_from_stdin ( self ) : <EOL> r = pdfkit . PDFKit ( '<STR_LIT:html>' , '<STR_LIT:string>' ) <EOL> self . assertEqual ( r . command ( ) [ - <NUM_LIT:2> : ] , [ '<STR_LIT:->' , '<STR_LIT:->' ] ) <EOL> def test_url_in_command ( self ) : <EOL> r = pdfkit . PDFKit ( '<STR_LIT>' , '<STR_LIT:url>' ) <EOL> self . assertEqual ( r . command ( ) [ - <NUM_LIT:2> : ] , [ '<STR_LIT>' , '<STR_LIT:->' ] ) <EOL> def test_file_path_in_command ( self ) : <EOL> path = '<STR_LIT>' <EOL> r = pdfkit . PDFKit ( path , '<STR_LIT:file>' ) <EOL> self . assertEqual ( r . command ( ) [ - <NUM_LIT:2> : ] , [ path , '<STR_LIT:->' ] ) <EOL> def test_output_path ( self ) : <EOL> out = '<STR_LIT>' <EOL> r = pdfkit . PDFKit ( '<STR_LIT:html>' , '<STR_LIT:string>' ) <EOL> self . assertEqual ( r . command ( out ) [ - <NUM_LIT:1> : ] , [ '<STR_LIT>' ] ) <EOL> def test_pdfkit_meta_tags ( self ) : <EOL> body = """<STR_LIT>""" <EOL> r = pdfkit . PDFKit ( body , '<STR_LIT:string>' ) <EOL> command = r . command ( ) <EOL> self . assertEqual ( command [ command . index ( '<STR_LIT>' ) + <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> self . assertEqual ( command [ command . index ( '<STR_LIT>' ) + <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> def test_pdfkit_meta_tags_in_bad_markup ( self ) : <EOL> body = """<STR_LIT>""" <EOL> r = pdfkit . PDFKit ( body , '<STR_LIT:string>' ) <EOL> command = r . command ( ) <EOL> self . assertEqual ( command [ command . index ( '<STR_LIT>' ) + <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> self . assertEqual ( command [ command . index ( '<STR_LIT>' ) + <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> def test_skip_nonpdfkit_tags ( self ) : <EOL> body = """<STR_LIT>""" <EOL> r = pdfkit . PDFKit ( body , '<STR_LIT:string>' ) <EOL> command = r . command ( ) <EOL> self . assertEqual ( command [ command . index ( '<STR_LIT>' ) + <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> def test_toc_handling_without_options ( self ) : <EOL> r = pdfkit . PDFKit ( '<STR_LIT>' , '<STR_LIT:string>' , toc = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( r . command ( ) [ <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> self . assertEqual ( r . command ( ) [ <NUM_LIT:2> ] , '<STR_LIT>' ) <EOL> def test_toc_with_options ( self ) : <EOL> options = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : "<STR_LIT>" <EOL> } <EOL> r = pdfkit . PDFKit ( '<STR_LIT:html>' , '<STR_LIT:string>' , options = options , toc = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( r . command ( ) [ <NUM_LIT:1> + len ( options ) * <NUM_LIT:2> ] , '<STR_LIT>' ) <EOL> self . assertEqual ( r . command ( ) [ <NUM_LIT:1> + len ( options ) * <NUM_LIT:2> + <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> def test_cover_without_options ( self ) : <EOL> r = pdfkit . PDFKit ( '<STR_LIT:html>' , '<STR_LIT:string>' , cover = '<STR_LIT>' ) <EOL> self . assertEqual ( r . command ( ) [ <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> self . assertEqual ( r . command ( ) [ <NUM_LIT:2> ] , '<STR_LIT>' ) <EOL> def test_cover_with_options ( self ) : <EOL> options = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : "<STR_LIT>" <EOL> } <EOL> r = pdfkit . PDFKit ( '<STR_LIT:html>' , '<STR_LIT:string>' , options = options , cover = '<STR_LIT>' ) <EOL> self . assertEqual ( r . command ( ) [ <NUM_LIT:1> + len ( options ) * <NUM_LIT:2> ] , '<STR_LIT>' ) <EOL> self . assertEqual ( r . command ( ) [ <NUM_LIT:1> + len ( options ) * <NUM_LIT:2> + <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> def test_cover_and_toc ( self ) : <EOL> options = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : "<STR_LIT>" <EOL> } <EOL> r = pdfkit . PDFKit ( '<STR_LIT:html>' , '<STR_LIT:string>' , options = options , toc = { '<STR_LIT>' : '<STR_LIT>' } , cover = '<STR_LIT>' ) <EOL> command = r . command ( ) <EOL> self . assertEqual ( command [ - <NUM_LIT:7> : ] , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:->' , '<STR_LIT:->' ] ) <EOL> def test_outline_options ( self ) : <EOL> options = { <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : <NUM_LIT:1> <EOL> } <EOL> r = pdfkit . PDFKit ( '<STR_LIT>' , '<STR_LIT:url>' , options = options ) <EOL> cmd = r . command ( ) <EOL> self . assertIn ( '<STR_LIT>' , cmd ) <EOL> self . assertEqual ( cmd [ cmd . index ( '<STR_LIT>' ) + <NUM_LIT:1> ] , '<STR_LIT:1>' ) <EOL> def test_filter_empty_and_none_values_in_opts ( self ) : <EOL> options = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : False <EOL> } <EOL> r = pdfkit . PDFKit ( '<STR_LIT:html>' , '<STR_LIT:string>' , options = options ) <EOL> cmd = r . command ( ) <EOL> self . assertEqual ( len ( cmd ) , <NUM_LIT:6> ) <EOL> class TestPDFKitGeneration ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> pass <EOL> def tearDown ( self ) : <EOL> if os . path . exists ( '<STR_LIT>' ) : <EOL> os . remove ( '<STR_LIT>' ) <EOL> def test_pdf_generation ( self ) : <EOL> r = pdfkit . PDFKit ( '<STR_LIT:html>' , '<STR_LIT:string>' , options = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> pdf = r . to_pdf ( '<STR_LIT>' ) <EOL> self . assertTrue ( pdf ) <EOL> def test_raise_error_with_invalid_url ( self ) : <EOL> r = pdfkit . PDFKit ( '<STR_LIT>' , '<STR_LIT:url>' ) <EOL> with self . assertRaises ( IOError ) : <EOL> r . to_pdf ( '<STR_LIT>' ) <EOL> def test_raise_error_with_invalid_file_path ( self ) : <EOL> paths = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> with self . assertRaises ( IOError ) : <EOL> pdfkit . PDFKit ( '<STR_LIT>' , '<STR_LIT:file>' ) <EOL> with self . assertRaises ( IOError ) : <EOL> pdfkit . PDFKit ( paths , '<STR_LIT:file>' ) <EOL> def test_stylesheet_adding_to_the_head ( self ) : <EOL> r = pdfkit . PDFKit ( '<STR_LIT>' , '<STR_LIT:string>' , <EOL> css = '<STR_LIT>' ) <EOL> with open ( '<STR_LIT>' ) as f : <EOL> css = f . read ( ) <EOL> r . _prepend_css ( '<STR_LIT>' ) <EOL> self . assertIn ( '<STR_LIT>' % css , r . source . to_s ( ) ) <EOL> def test_stylesheet_adding_without_head_tag ( self ) : <EOL> r = pdfkit . PDFKit ( '<STR_LIT>' , '<STR_LIT:string>' , <EOL> options = { '<STR_LIT>' : None } , css = '<STR_LIT>' ) <EOL> with open ( '<STR_LIT>' ) as f : <EOL> css = f . read ( ) <EOL> r . _prepend_css ( '<STR_LIT>' ) <EOL> self . assertIn ( '<STR_LIT>' % css , r . source . to_s ( ) ) <EOL> def test_multiple_stylesheets_adding_to_the_head ( self ) : <EOL> css_files = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> r = pdfkit . PDFKit ( '<STR_LIT>' , '<STR_LIT:string>' , <EOL> css = css_files ) <EOL> css = [ ] <EOL> for css_file in css_files : <EOL> with open ( css_file ) as f : <EOL> css . append ( f . read ( ) ) <EOL> r . _prepend_css ( css_files ) <EOL> self . assertIn ( '<STR_LIT>' % "<STR_LIT:\n>" . join ( css ) , r . source . to_s ( ) ) <EOL> def test_multiple_stylesheet_adding_without_head_tag ( self ) : <EOL> css_files = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> r = pdfkit . PDFKit ( '<STR_LIT>' , '<STR_LIT:string>' , <EOL> options = { '<STR_LIT>' : None } , css = css_files ) <EOL> css = [ ] <EOL> for css_file in css_files : <EOL> with open ( css_file ) as f : <EOL> css . append ( f . read ( ) ) <EOL> r . _prepend_css ( css_files ) <EOL> self . assertIn ( '<STR_LIT>' % "<STR_LIT:\n>" . join ( css ) , r . source . to_s ( ) ) <EOL> def test_stylesheet_throw_error_when_url ( self ) : <EOL> r = pdfkit . PDFKit ( '<STR_LIT>' , '<STR_LIT:url>' , css = '<STR_LIT>' ) <EOL> with self . assertRaises ( r . ImproperSourceError ) : <EOL> r . to_pdf ( ) <EOL> def test_stylesheet_adding_to_file_with_option ( self ) : <EOL> css = '<STR_LIT>' <EOL> r = pdfkit . PDFKit ( '<STR_LIT>' , '<STR_LIT:file>' , css = css ) <EOL> self . assertEqual ( r . css , css ) <EOL> r . _prepend_css ( css ) <EOL> self . assertIn ( '<STR_LIT>' , r . source . to_s ( ) ) <EOL> def test_wkhtmltopdf_error_handling ( self ) : <EOL> r = pdfkit . PDFKit ( '<STR_LIT>' , '<STR_LIT:url>' ) <EOL> with self . assertRaises ( IOError ) : <EOL> r . to_pdf ( ) <EOL> def test_pdf_generation_from_file_like ( self ) : <EOL> with open ( '<STR_LIT>' , '<STR_LIT:r>' ) as f : <EOL> r = pdfkit . PDFKit ( f , '<STR_LIT:file>' ) <EOL> output = r . to_pdf ( ) <EOL> self . assertEqual ( output [ : <NUM_LIT:4> ] . decode ( '<STR_LIT:utf-8>' ) , '<STR_LIT>' ) <EOL> def test_raise_error_with_wrong_css_path ( self ) : <EOL> css = '<STR_LIT>' <EOL> r = pdfkit . PDFKit ( '<STR_LIT>' , '<STR_LIT:file>' , css = css ) <EOL> with self . assertRaises ( IOError ) : <EOL> r . to_pdf ( ) <EOL> def test_raise_error_if_bad_wkhtmltopdf_option ( self ) : <EOL> r = pdfkit . PDFKit ( '<STR_LIT>' , '<STR_LIT:string>' , <EOL> options = { '<STR_LIT>' : None } ) <EOL> with self . assertRaises ( IOError ) as cm : <EOL> r . to_pdf ( ) <EOL> raised_exception = cm . exception <EOL> self . assertRegexpMatches ( str ( raised_exception ) , '<STR_LIT>' ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
