import unittest

try:
    from plone.app.linkintegrity import info
    from plone.app.linkintegrity.browser import confirmation
    from plone.app.linkintegrity.browser import remove
except ImportError:
    pass
else:
    from base64 import b64encode, b64decode
    from zlib import compress, decompress
    
    encodeRequestData = confirmation.encode
    decodeRequestData = remove.decode
    encodeInts = info.encode
    decodeInts = info.decode
    
    class LinkIntegrityPatchTests(unittest.TestCase):
        def testEncodeRequestData(self):
            body = 'foo\nbar'
            env = dict(spam='eggs')
            self.assertEqual(
                encodeRequestData((body, env)),
                b64encode(compress('7:foo\nbar4:spam4:eggs')))
                
        def testDecodeRequestData(self):
            body = 'foo\nbar'
            env = dict(spam='eggs')
            self.assertEqual(
                decodeRequestData(
                    b64encode(compress('7:foo\nbar4:spam4:eggs'))),
                (body, env))
        
        def testRequestDataRoundtrip(self):
            body = 'foo\nbar'
            env = dict(spam='eggs', monty='python')
            self.assertEqual(
                decodeRequestData(encodeRequestData((body, env))),
                (body, env))
        
        def testEncodeInts(self):
            self.assertEqual(encodeInts(range(5)), '0:1:2:3:4')
        
        def testDecodeInts(self):
            self.assertEqual(decodeInts('0:1:2:3:4'), range(5))
            
        def testIntsRoundtrip(self):
            data = [42, 7, 12345]
            self.assertEqual(decodeInts(encodeInts(data)), data)

def test_suite():
    try:
        from plone.app.linkintegrity.tests.testDocTests import test_suite
    except ImportError:
        return unittest.TestSuite()
    
    suite = test_suite()
    suite.addTest(unittest.makeSuite(LinkIntegrityPatchTests))
    return suite
