#!/www/python/bin/python

"""$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/lib/test/test_dumper.py $
$Id: test_dumper.py 20909 2003-03-04 14:35:33Z dbinger $

Test for dulcinea.dumper.
"""

import sys, StringIO
from BTrees import IOBTree
from sancho.unittest import TestScenario, parse_args, run_scenarios
from dulcinea.dumper import dumps, dump

tested_modules = ["dulcinea.dumper"]

class DumperTest (TestScenario):

    def check_simple (self):
        self.test_val('dumps(1)', '1\n')
        self.test_val('dumps(1.2)', '1.2\n')
        self.test_val('dumps("abcd\\n\\xff")', "'abcd\\n\\xff'\n")

    def check_objects (self):
        # Checks objects that mention their ID, so test_val() isn't used;
        # instead this test case simply verifies that dumps() doesn't crash.
        self.test_stmt('dumps({1:2, 3:4, 5:(6,7)})')
        self.test_stmt('dumps((3, 5, "hello"))')
        self.test_stmt('dumps([3, 5, "hello", [1, 4.5]])')
        self.test_stmt('dumps( IOBTree.IOBTree({1:2, 4:5}))')

    def check_recursive (self):
        # Make a set of recursive objects
        d = {}
        L = [1,2,3, d]
        L.append(L)
        d[1] = L
        d[2] = d
        self.test_stmt('dumps(L)')

    def check_deep (self):
        # Make a deeply nested object
        L = []
        for i in range(50):
            L = [L]
        self.test_stmt('dumps(L)')

    def check_instance (self):
        class C:
            pass
        self.test_stmt('dumps(C())')


if __name__ == "__main__":
    (scenarios, options) = parse_args()
    run_scenarios(scenarios, options)
