cssutils-hg-merge / src / tests /

"""Testcases for cssutils.css.CSSFontFaceRule"""
__version__ = '$Id$'

import xml.dom
import test_cssrule
import cssutils

class CSSFontFaceRuleTestCase(test_cssrule.CSSRuleTestCase):

    def setUp(self):
        super(CSSFontFaceRuleTestCase, self).setUp()
        self.r = cssutils.css.CSSFontFaceRule()
        self.rRO = cssutils.css.CSSFontFaceRule(readonly=True)
        self.r_type = cssutils.css.CSSFontFaceRule.FONT_FACE_RULE#
        self.r_typeString = 'FONT_FACE_RULE'

    def test_init(self):
        super(CSSFontFaceRuleTestCase, self).test_init()

        r = cssutils.css.CSSFontFaceRule()
        self.assertEqual(cssutils.css.CSSStyleDeclaration, type(

        # until any properties
        self.assertEqual(u'', r.cssText)

        # only possible to set @... similar name
        self.assertRaises(xml.dom.InvalidModificationErr, self.r._setAtkeyword, 'x')

        def checkrefs(ff):
            for p in
                self.assertEqual(, p.parent)

                    style=cssutils.css.CSSStyleDeclaration('font-family: x')))
        r = cssutils.css.CSSFontFaceRule()
        r.cssText = '@font-face { font-family: x }'
        r = cssutils.css.CSSFontFaceRule()'font-family', 'y')

        r = cssutils.css.CSSFontFaceRule()['font-family'] = 'z'

        r = cssutils.css.CSSFontFaceRule() = 'a'

    def test_cssText(self):
        tests = {
            u'''@font-face {
    font-family: x;
    src: url(../fonts/LateefRegAAT.ttf) format("truetype-aat"), url(../fonts/LateefRegOT.ttf) format("opentype");
    font-style: italic;
    font-weight: 500;
    font-stretch: condensed;
    unicode-range: u+1-ff, u+111
    }''': None,
            u'@font-face{font-family: x;}': u'@font-face {\n    font-family: x\n    }',
            u'@font-face  {  font-family: x;  }': u'@font-face {\n    font-family: x\n    }',
            u'@f\\ont\\-face{font-family : x;}': u'@font-face {\n    font-family: x\n    }',
            # comments
            u'@font-face/*1*//*2*/{font-family: x;}':
                u'@font-face /*1*/ /*2*/ {\n    font-family: x\n    }',
            # WS
            u'@font-face\n\t\f {\n\t\f font-family:x;\n\t\f }': 
                u'@font-face {\n    font-family: x\n    }',

        tests = {
            u'@font-face;': xml.dom.SyntaxErr,
            u'@font-face }': xml.dom.SyntaxErr,
        self.do_raise_p(tests) # parse
            u'@font-face {': xml.dom.SyntaxErr, # no }
            # trailing
            u'@font-face {}1': xml.dom.SyntaxErr, 
            u'@font-face {}/**/': xml.dom.SyntaxErr, 
            u'@font-face {} ': xml.dom.SyntaxErr, 
        self.do_raise_r(tests) # set cssText

    def test_style(self):
        " (and references)"
        r = cssutils.css.CSSFontFaceRule()
        s1 =
        self.assertEqual(r, s1.parentRule)
        self.assertEqual(u'', s1.cssText)
        # set rule.cssText
        r.cssText = '@font-face { font-family: x1 }'
        self.failIfEqual(, s1)
        self.assertEqual(r.cssText, u'@font-face {\n    font-family: x1\n    }')
        self.assertEqual(, u'font-family: x1')
        self.assertEqual(s1.cssText, u'')
        s2 =
        # set invalid rule.cssText
            r.cssText = '@font-face { $ }'
        except xml.dom.SyntaxErr, e:
        self.assertEqual(, s2)
        self.assertEqual(r, s2.parentRule)
        self.assertEqual(r.cssText, u'@font-face {\n    font-family: x1\n    }')
        self.assertEqual(s2.cssText, u'font-family: x1')
        self.assertEqual(, u'font-family: x1')

        # set = 'font-family: x2'
        self.assertEqual(, s2)
        self.assertEqual(r, s2.parentRule)
        self.assertEqual(r.cssText, u'@font-face {\n    font-family: x2\n    }')
        self.assertEqual(s2.cssText, u'font-family: x2')
        self.assertEqual(, u'font-family: x2')

        # set new style object s2
        sn = cssutils.css.CSSStyleDeclaration('font-family: y1') = sn
        self.assertEqual(, sn)
        self.assertEqual(r, sn.parentRule)
        self.assertEqual(r.cssText, u'@font-face {\n    font-family: y1\n    }')
        self.assertEqual(sn.cssText, u'font-family: y1')
        self.assertEqual(, u'font-family: y1')
        self.assertEqual(s2.cssText, u'font-family: x2') # old

        # set s2.cssText
        sn.cssText = 'font-family: y2'
        self.assertEqual(, sn)
        self.assertEqual(r.cssText, u'@font-face {\n    font-family: y2\n    }')
        self.assertEqual(, u'font-family: y2')
        self.assertEqual(s2.cssText, u'font-family: x2') # old
        # set invalid s2.cssText
            sn.cssText = '$'
        except xml.dom.SyntaxErr, e:
        self.assertEqual(, sn)
        self.assertEqual(, u'font-family: y2')
        self.assertEqual(r.cssText, u'@font-face {\n    font-family: y2\n    }')
        # set with text = 'font-family: z'
        self.failIfEqual(, sn)
        self.assertEqual(r.cssText, u'@font-face {\n    font-family: z\n    }')
        self.assertEqual(, u'font-family: z')
        self.assertEqual(sn.cssText, u'font-family: y2')

    def test_properties(self):
        " properties"
        r = cssutils.css.CSSFontFaceRule() = '''
        src: url(x)
        exp = u'''@font-face {
    src: url(x)
        self.assertEqual(exp, r.cssText)
        tests = {
            'font-family': [#('serif', True),
#                            ('x', True),
#                            ('"x"', True),
                            ('x, y', False),
                            ('"x", y', False),
                            ('x, "y"', False),
#                            ('"x", "y"', False)
        for n, t in tests.items():
            for (v, valid) in t:
                r = cssutils.css.CSSFontFaceRule()
      [n] = v
                self.assertEqual(, valid)

    def test_incomplete(self):
        "CSSFontFaceRule (incomplete)"
        tests = {
                u'', # no } and no content
            u'@font-face { ':
                u'', # no } and no content
            u'@font-face { font-family: x':
                u'@font-face {\n    font-family: x\n    }', # no }
        self.do_equal_p(tests) # parse

    def test_InvalidModificationErr(self):
        "CSSFontFaceRule.cssText InvalidModificationErr"
        tests = {
            u'@font-fac {}': xml.dom.InvalidModificationErr,

    def test_valid(self):
        r = cssutils.css.CSSFontFaceRule()
        self.assertEqual(False, r.valid)
        N = 'font-family: x; src: local(x);'
        tests = {
            True: (N,
                   N + 'font-style: italic; font-weight: bold',
            False: ('',
                    'font-family: x, y; src: local(x);',
                    N + 'font-style: inherit',
                    N + 'invalid: 1')
        for valid, testlist in tests.items():
            for test in testlist:
       = test
                self.assertEqual(valid, r.valid)

    def test_reprANDstr(self):
        "CSSFontFaceRule.__repr__(), .__str__()"
        style='src: url(x)'        
        s = cssutils.css.CSSFontFaceRule(style=style)
        self.assert_(style in str(s))

        s2 = eval(repr(s))
        self.assert_(isinstance(s2, s.__class__))
        self.assert_(style ==

if __name__ == '__main__':
    import unittest
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.