"Fossies" - the Fresh Open Source Software Archive

Member "xhtml2pdf-0.2.2/tests/test_utils.py" (16 Apr 2018, 10311 Bytes) of package /linux/www/xhtml2pdf-0.2.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "test_utils.py": 0.2b_vs_0.2.1.

    1 #-*- coding: utf-8 -*-
    2 from unittest import TestCase
    3 
    4 from reportlab.lib.colors import Color
    5 
    6 from xhtml2pdf.tags import int_to_roman
    7 from xhtml2pdf.util import getCoords, getColor, getSize, getFrameDimensions, \
    8     getPos, getBox, pisaTempFile, getBorderStyle, set_value, copy_attrs, transform_attrs
    9 
   10 
   11 class UtilsCoordTestCase(TestCase):
   12 
   13     def test_getCoords_simple(self):
   14 
   15         res = getCoords(1, 1, 10, 10, (10, 10))
   16         self.assertEqual(res, (1, -1, 10, 10))
   17 
   18         # A second time - it's memoized!
   19         res = getCoords(1, 1, 10, 10, (10, 10))
   20         self.assertEqual(res, (1, -1, 10, 10))
   21 
   22     def test_getCoords_x_lt_0(self):
   23         res = getCoords(-1, 1, 10, 10, (10, 10))
   24         self.assertEqual(res, (9, -1, 10, 10))
   25 
   26     def test_getCoords_y_lt_0(self):
   27         res = getCoords(1, -1, 10, 10, (10, 10))
   28         self.assertEqual(res, (1, -9, 10, 10))
   29 
   30     def test_getCoords_w_and_h_none(self):
   31         res = getCoords(1, 1, None, None, (10, 10))
   32         self.assertEqual(res, (1, 9))
   33 
   34     def test_getCoords_w_lt_0(self):
   35         res = getCoords(1, 1, -1, 10, (10, 10))
   36         self.assertEqual(res, (1, -1, 8, 10))
   37 
   38     def test_getCoords_h_lt_0(self):
   39         res = getCoords(1, 1, 10, -1, (10, 10))
   40         self.assertEqual(res, (1, 1, 10, 8))
   41 
   42 
   43 class UtilsColorTestCase(TestCase):
   44 
   45     def test_get_color_simple(self):
   46         res = getColor('red')
   47         self.assertEqual(res, Color(1, 0, 0, 1))
   48 
   49         # Testing it being memoized properly
   50         res = getColor('red')
   51         self.assertEqual(res, Color(1, 0, 0, 1))
   52 
   53     def test_get_color_from_color(self):
   54         # Noop if argument is already a color
   55         res = getColor(Color(1, 0, 0, 1))
   56         self.assertEqual(res, Color(1, 0, 0, 1))
   57 
   58     def test_get_transparent_color(self):
   59         res = getColor('transparent', default='TOKEN')
   60         self.assertEqual(res, 'TOKEN')
   61 
   62         res = getColor('none', default='TOKEN')
   63         self.assertEqual(res, 'TOKEN')
   64 
   65     def test_get_color_for_none(self):
   66         res = getColor(None, default='TOKEN')
   67         self.assertEqual(res, 'TOKEN')
   68 
   69     def test_get_color_for_RGB(self):
   70         res = getColor('#FF0000')
   71         self.assertEqual(res, Color(1, 0, 0, 1))
   72 
   73     def test_get_color_for_RGB_with_len_4(self):
   74         res = getColor('#F00')
   75         self.assertEqual(res, Color(1, 0, 0, 1))
   76 
   77     def test_get_color_for_CSS_RGB_function(self):
   78         # It's regexp based, let's try common cases.
   79         res = getColor('rgb(255,0,0)')
   80         self.assertEqual(res, Color(1, 0, 0, 1))
   81 
   82         res = getColor('<css function: rgb(255,0,0)>')
   83         self.assertEqual(res, Color(1, 0, 0, 1))
   84 
   85 
   86 class UtilsGetSizeTestCase(TestCase):
   87 
   88     def test_get_size_simple(self):
   89         res = getSize('12pt')
   90         self.assertEqual(res, 12.00)
   91 
   92         # Memoized...
   93         res = getSize('12pt')
   94         self.assertEqual(res, 12.00)
   95 
   96     def test_get_size_for_none(self):
   97         res = getSize(None, relative='TOKEN')
   98         self.assertEqual(res, 'TOKEN')
   99 
  100     def test_get_size_for_float(self):
  101         res = getSize(12.00)
  102         self.assertEqual(res, 12.00)
  103 
  104     def test_get_size_for_tuple(self):
  105         # TODO: This is a really strange case. Probably should not work this
  106         # way.
  107         res = getSize(("12", ".12"))
  108         self.assertEqual(res, 12.12)
  109 
  110     def test_get_size_for_cm(self):
  111         res = getSize("1cm")
  112         self.assertEqual(res, 28.346456692913385)
  113 
  114     def test_get_size_for_mm(self):
  115         res = getSize("1mm")
  116         self.assertEqual(res, 2.8346456692913385)
  117 
  118     def test_get_size_for_i(self):
  119         res = getSize("1i")
  120         self.assertEqual(res, 72.00)
  121 
  122     def test_get_size_for_in(self):
  123         res = getSize("1in")
  124         self.assertEqual(res, 72.00)
  125 
  126     def test_get_size_for_inch(self):
  127         res = getSize("1in")
  128         self.assertEqual(res, 72.00)
  129 
  130     def test_get_size_for_pc(self):
  131         res = getSize("1pc")
  132         self.assertEqual(res, 12.00)
  133 
  134     def test_get_size_for_none_str(self):
  135         res = getSize("none")
  136         self.assertEqual(res, 0.0)
  137         res = getSize("0")
  138         self.assertEqual(res, 0.0)
  139         res = getSize("auto")  # Really?
  140         self.assertEqual(res, 0.0)
  141 
  142 
  143 class PisaDimensionTestCase(TestCase):
  144 
  145     def test_FrameDimensions_left_top_width_height(self):
  146         #builder = pisaCSSBuilder(mediumSet=['all'])
  147         dims = {
  148             'left': '10pt',
  149             'top': '20pt',
  150             'width': '30pt',
  151             'height': '40pt',
  152         }
  153         expected = (10.0, 20.0, 30.0, 40.0)
  154         result = getFrameDimensions(dims, 100, 200)
  155         self.assertEquals(expected, result)
  156 
  157     def test_FrameDimensions_left_top_bottom_right(self):
  158         dims = {
  159             'left': '10pt',
  160             'top': '20pt',
  161             'bottom': '30pt',
  162             'right': '40pt',
  163         }
  164         expected = (10.0, 20.0, 50.0, 150.0)
  165         result = getFrameDimensions(dims, 100, 200)
  166         self.assertEquals(expected, result)
  167 
  168     def test_FrameDimensions_bottom_right_width_height(self):
  169         dims = {
  170             'bottom': '10pt',
  171             'right': '20pt',
  172             'width': '70pt',
  173             'height': '80pt',
  174         }
  175         expected = (10.0, 110.0, 70.0, 80.0)
  176         result = getFrameDimensions(dims, 100, 200)
  177         self.assertEquals(expected, result)
  178 
  179     def test_FrameDimensions_left_top_width_height_with_margin(self):
  180         dims = {
  181             'left': '10pt',
  182             'top': '20pt',
  183             'width': '70pt',
  184             'height': '80pt',
  185             'margin-top': '10pt',
  186             'margin-left': '15pt',
  187             'margin-bottom': '20pt',
  188             'margin-right': '25pt',
  189         }
  190         expected = (25.0, 30.0, 30.0, 50.0)
  191         result = getFrameDimensions(dims, 100, 200)
  192         self.assertEquals(expected, result)
  193 
  194     def test_FrameDimensions_bottom_right_width_height_with_margin(self):
  195         dims = {
  196             'bottom': '10pt',
  197             'right': '20pt',
  198             'width': '70pt',
  199             'height': '80pt',
  200             'margin-top': '10pt',
  201             'margin-left': '15pt',
  202             'margin-bottom': '20pt',
  203             'margin-right': '25pt',
  204         }
  205         expected = (25.0, 120.0, 30.0, 50.0)
  206         result = getFrameDimensions(dims, 100, 200)
  207         self.assertEquals(expected, result)
  208 
  209     def test_frame_dimensions_for_box_len_eq_4(self):
  210         dims = {
  211             '-pdf-frame-box': ['12pt', '12,pt', '12pt', '12pt']
  212         }
  213         expected = [12.0, 12.0, 12.0, 12.0]
  214         result = getFrameDimensions(dims, 100, 200)
  215         self.assertEqual(result, expected)
  216 
  217     def test_trame_dimentions_for_height_without_top_or_bottom(self):
  218         dims = {
  219             'left': '10pt',
  220             #'top': '20pt',
  221             'width': '30pt',
  222             'height': '40pt',
  223         }
  224         expected = (10.0, 0.0, 30.0, 200.0)
  225         result = getFrameDimensions(dims, 100, 200)
  226         self.assertEquals(expected, result)
  227 
  228     def test_trame_dimentions_for_width_without_left_or_right(self):
  229         dims = {
  230             #'left': '10pt',
  231             'top': '20pt',
  232             'width': '30pt',
  233             'height': '40pt',
  234         }
  235         expected = (0.0, 20.0, 100.0, 40.0)
  236         result = getFrameDimensions(dims, 100, 200)
  237         self.assertEquals(expected, result)
  238 
  239 
  240 class GetPosTestCase(TestCase):
  241 
  242     def test_get_pos_simple(self):
  243         res = getBox("1pt 1pt 10pt 10pt", (10, 10))
  244         self.assertEqual(res, (1.0, -1.0, 10, 10))
  245 
  246     def test_get_pos_raising(self):
  247         raised = False
  248         try:
  249             getBox("1pt 1pt 10pt", (10, 10))
  250         except Exception:
  251             raised = True
  252         self.assertTrue(raised)
  253 
  254 
  255 class TestTagUtils(TestCase):
  256 
  257     def test_roman_numeral_conversion(self):
  258         self.assertEqual("I", int_to_roman(1))
  259         self.assertEqual("L", int_to_roman(50))
  260         self.assertEqual("XLII", int_to_roman(42))
  261         self.assertEqual("XXVI", int_to_roman(26))
  262 
  263 
  264 class TempFileTestCase(TestCase):
  265 
  266     def test_unicode(self):
  267         """Asserts bytes generated by reportlab are returned"""
  268         src = pisaTempFile()
  269         value = b'%PDF-1.4\r\n%\x93\x8c\x8b\x9e ReportLab Generated PDF document http://www.reportlab.com'
  270         try:
  271             src.write(value)
  272         except UnicodeDecodeError as error:
  273             self.fail(error)
  274 
  275 
  276 class GetBorderStyleTestCase(TestCase):
  277 
  278     def test_will_return_value_if_passed_value_is_not_none_or_hidden(self):
  279         style = getBorderStyle("foo", default="blah")
  280         self.assertEqual(style, "foo")
  281 
  282     def test_will_return_default_if_passed_value_is_non_case_sensitive_none(self):
  283         style = getBorderStyle("None", default="blah")
  284         self.assertEqual(style, "blah")
  285 
  286     def test_will_return_default_if_passed_value_is_non_case_sensitive_hidden(self):
  287         style = getBorderStyle("hidDen", default="defaultPassedArg")
  288         self.assertEqual(style, "defaultPassedArg")
  289 
  290 
  291 class CopyUtils(TestCase):
  292 
  293     class A:
  294         attr = 2
  295         attr1 = 10
  296 
  297     class B:
  298 
  299         def __init__(self, a, b):
  300             self.attr = a
  301             self.attr1 = b
  302 
  303     class C:
  304         pass
  305 
  306     class D:
  307         param1 = 28
  308         param2 = 1
  309 
  310     def test_set_value(self):
  311 
  312         a = self.A()
  313         b = self.B(20, 30)
  314         c = self.C()
  315         set_value(a, ["attr", "attr1"], 8)
  316         set_value(b, ["attr", "attr1"], 8)
  317         set_value(c, ["attr", "attr1"], 8)
  318 
  319         self.assertEqual(a.attr, 8)
  320         self.assertEqual(a.attr1, 8)
  321         self.assertEqual(b.attr, 8)
  322         self.assertEqual(b.attr1, 8)
  323         self.assertEqual(c.attr, 8)
  324         self.assertEqual(c.attr1, 8)
  325 
  326     def test_copy_attrs(self):
  327         a = self.A()
  328         b = self.B(19, 22)
  329         copy_attrs(a, b, ["attr", "attr1"])
  330         self.assertEqual(a.attr, 19)
  331         self.assertEqual(a.attr1, 22)
  332 
  333     def test_transform_attrs(self):
  334         obj = self.D()
  335         container = {'attr': 19, 'attr1': 22}
  336 
  337         transform_attrs(obj,
  338                         (
  339                             ("param1", "attr"),
  340                             ("param2", "attr1"),
  341                         ),
  342                         container,
  343                         str
  344                         )
  345 
  346         self.assertEqual(obj.param1, str(19))
  347         self.assertEqual(obj.param2, str(22))