"""
$URL: svn+ssh://sting.mems-exchange.org/repos/trunk/DurusWorks/qpy/test/utest_quoted.py $
$Id: utest_quoted.py 32721 2010-09-08 15:04:42Z dbinger $
"""
from sancho.utest import UTest
from qpy import xml_quote, xml, join_xml, join_str
import sys


class TestQuotedRefs (UTest):

    def refs_quote(self):
        refs = sys.gettotalrefcount
        before = 0
        after = 0
        before = refs()
        xml()
        after = refs()
        assert after - before == 0, after - before
        before = refs()
        xml_quote(None)
        xml_quote("<>'\"")
        after = refs()
        assert after - before == 0, after - before

    def refs_join_str(self):
        refs = sys.gettotalrefcount
        before = 0
        after = 0
        for a in  ([], range(3), 'abc', (xml('<'), 'ok', None, 1)):
            join_str(a) # The refs do change on the first call.
            before = refs()
            join_str(a)
            after = refs()
            assert after - before == 0, after - before

    def refs_join_xml(self):
        refs = sys.gettotalrefcount
        before = 0
        after = 0
        for a in  ([], range(3), 'abc', (xml('<'), 'ok', None, 1)):
            join_xml(a)
            before = refs()
            join_xml(a)
            after = refs()
            assert after - before == 0, after - before

    def a(self):
        emptyrefs = sys.getrefcount(xml())
        refs = sys.gettotalrefcount
        before = 0
        after = 0
        xml('<') + '>'
        xml('<%s>') % '<ok>'
        xml('<%s, %s>') % (1, '<ok>')
        xml('<%(a)s, %(b)s>') % dict(a='<', b=None, c='<')
        r = range(100)
        xml().join(a for a in r)
        x = 0
        before = refs()
        for x in r:
            xml('<') + '>'
            xml('<%s>') % '<ok>'
            xml('<%s, %s>') % (1, '<ok>')
            xml('<%(a)s, %(b)s>') % dict(a='<', b=None, c='<')
            xml().join(a for a in r)
        after = refs()
        assert after - before in (0, 1), after - before
        emptyrefs_after = sys.getrefcount(xml())
        assert emptyrefs_after - emptyrefs == 0, emptyrefs_after

if __name__ == '__main__':
    if hasattr(sys, 'gettotalrefcount'):
        TestQuotedRefs()

