# -*- coding: utf-8 -*-
#
# Copyright (C) 2013 Jason R. Coombs <jaraco@jaraco.com>
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.

import doctest
import unittest

import jsonpickle
import jsonpickle.handlers


class CustomObject(object):
    "A class to be serialized by a custom handler"
    def __init__(self, name=None, creator=None):
        self.name = name
        self.creator = creator

    def __eq__(self, other):
        return self.name == other.name


class A(CustomObject):
    pass


class B(A):
    pass


class NullHandler(jsonpickle.handlers.BaseHandler):

    def flatten(self, obj, data):
        data['name'] = obj.name
        return data

    def restore(self, obj):
        return CustomObject(obj['name'], creator=type(self))


class DecoratedBase(CustomObject):
    pass


class DecoratedChild(DecoratedBase):
    pass


@jsonpickle.handlers.register(DecoratedBase, base=True)
class DecoratedHandler(NullHandler):
    pass


class HandlerTestCase(unittest.TestCase):

    def setUp(self):
        jsonpickle.handlers.register(CustomObject, NullHandler)

    def tearDown(self):
        jsonpickle.handlers.unregister(CustomObject)

    def roundtrip(self, ob):
        encoded = jsonpickle.encode(ob)
        decoded = jsonpickle.decode(encoded)
        self.assertEqual(decoded, ob)
        return decoded

    def test_custom_handler(self):
        """Ensure that the custom handler is indeed used"""
        expect = CustomObject('hello')
        encoded = jsonpickle.encode(expect)
        actual = jsonpickle.decode(encoded)
        self.assertEqual(expect.name, actual.name)
        self.assertTrue(expect.creator is None)
        self.assertTrue(actual.creator is NullHandler)

    def test_references(self):
        """
        Ensure objects handled by a custom handler are properly dereferenced.
        """
        ob = CustomObject()
        # create a dictionary which contains several references to ob
        subject = dict(a=ob, b=ob, c=ob)
        # ensure the subject can be roundtripped
        new_subject = self.roundtrip(subject)
        self.assertEqual(new_subject['a'], new_subject['b'])
        self.assertEqual(new_subject['b'], new_subject['c'])
        self.assertTrue(new_subject['a'] is new_subject['b'])
        self.assertTrue(new_subject['b'] is new_subject['c'])

    def test_invalid_class(self):
        self.assertRaises(TypeError, jsonpickle.handlers.register, 'foo', NullHandler)

    def test_base_handler(self):
        a = A('a')
        self.assertTrue(a.creator is None)
        self.assertTrue(jsonpickle.decode(jsonpickle.encode(a)).creator is None)

        b = B('b')
        self.assertTrue(b.creator is None)
        self.assertTrue(jsonpickle.decode(jsonpickle.encode(b)).creator is None)

        OtherHandler = type('OtherHandler', (NullHandler,), {})
        jsonpickle.handlers.register(A, OtherHandler, base=True)
        self.assertTrue(self.roundtrip(a).creator is OtherHandler)
        self.assertTrue(self.roundtrip(b).creator is OtherHandler)

        SpecializedHandler = type('SpecializedHandler', (NullHandler,), {})
        jsonpickle.handlers.register(B, SpecializedHandler)
        self.assertTrue(self.roundtrip(a).creator is OtherHandler)
        self.assertTrue(self.roundtrip(b).creator is SpecializedHandler)

    def test_decorated_register(self):
        db = DecoratedBase('db')
        dc = DecoratedChild('dc')

        self.assertTrue(self.roundtrip(db).creator is DecoratedHandler)
        self.assertTrue(self.roundtrip(dc).creator is DecoratedHandler)


def suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(HandlerTestCase))
    suite.addTest(doctest.DocTestSuite(jsonpickle.handlers))
    return suite


if __name__ == '__main__':
    unittest.main(defaultTest='suite')

@@ -567,16 +567,22 @@
 %0A%0Aclass 
+Custom
 A(Custom
@@ -607,18 +607,30 @@
 %0A%0Aclass 
-B(
+CustomB(Custom
 A):%0A    
@@ -2610,15 +2610,21 @@
 a = 
+Custom
 A('a')%0A
-
     
@@ -2756,16 +2756,22 @@
     b = 
+Custom
 B('b')%0A 
@@ -2995,16 +2995,22 @@
 egister(
+Custom
 A, Other
@@ -3232,32 +3232,32 @@
 lHandler,), %7B%7D)%0A
-
         jsonpick
@@ -3277,16 +3277,22 @@
 egister(
+Custom
 B, Speci

rename A, B to CustomA, CustomB for readability