Esempio n. 1
0
    def test_load_dictionary(self):
        def assertEqual(a, b):
            self.assertEqual(a._dict, b)

        for contents, expected in (
            ('{"S": "a"}'       , {('S', ): 'a'    }),
            ('{"S": "\xc3\xb1"}', {('S', ): u'\xf1'}),
            ('{"S": "\xf1"}'    , {('S', ): u'\xf1'}),
        ):
            assertEqual(load_dictionary(make_dict(contents)), expected)
        
        with self.assertRaises(DictionaryLoaderException):
            load_dictionary(make_dict('foo'))
Esempio n. 2
0
    def test_load_dictionary(self):
        def assertEqual(a, b):
            self.assertEqual(a._dict, b)

        for contents, expected in (
            (u'{"S": "a"}'       , {('S', ): 'a'    }),
            # Default encoding is utf-8.
            (u'{"S": "café"}'.encode('utf-8'), {('S', ): u'café'}),
            # But if that fails, the implementation
            # must automatically retry with latin-1.
            (u'{"S": "café"}'.encode('latin-1'), {('S', ): u'café'}),
        ):
            with make_dict(contents) as filename:
                assertEqual(load_dictionary(filename), expected)

        for contents, exception in (
            # Invalid JSON.
            (u'{"foo", "bar",}', ValueError),
            # Invalid JSON.
            (u'foo', ValueError),
            # Cannot convert to dict.
            (u'"foo"', ValueError),
            # Ditto.
            (u'4.2', TypeError),
        ):
            with make_dict(contents) as filename:
                self.assertRaises(exception, load_dictionary, filename)
Esempio n. 3
0
    def test_load_dictionary(self):
        def assertEqual(a, b):
            self.assertEqual(a._dict, b)

        for contents, expected in (
            (u'{"S": "a"}', {("S",): "a"}),
            # Default encoding is utf-8.
            (u'{"S": "café"}'.encode("utf-8"), {("S",): u"café"}),
            # But if that fails, the implementation
            # must automatically retry with latin-1.
            (u'{"S": "café"}'.encode("latin-1"), {("S",): u"café"}),
        ):
            assertEqual(load_dictionary(make_dict(contents)), expected)

        with self.assertRaises(DictionaryLoaderException):
            load_dictionary(make_dict("foo"))
Esempio n. 4
0
    def test_load_dictionary(self):
        def assertEqual(a, b):
            self.assertEqual(a._dict, b)

        for contents, expected in (
            (u'{"S": "a"}'.encode('utf-8'), {('S', ): u'a'}),
            # Default encoding is utf-8.
            (u'{"S": "café"}'.encode('utf-8'), {('S', ): u'café'}),
            # But if that fails, the implementation
            # must automatically retry with latin-1.
            (u'{"S": "café"}'.encode('latin-1'), {('S', ): u'café'}),
        ):
            with make_dict(contents) as filename:
                assertEqual(load_dictionary(filename), expected)

        for contents, exception in (
            # Invalid JSON.
            (u'{"foo", "bar",}', ValueError),
            # Invalid JSON.
            (u'foo', ValueError),
            # Cannot convert to dict.
            (u'"foo"', ValueError),
            # Ditto.
            (u'4.2', TypeError),
        ):
            with make_dict(contents.encode('utf-8')) as filename:
                self.assertRaises(exception, load_dictionary, filename)
Esempio n. 5
0
    def test_load_dictionary(self):
        def assertEqual(a, b):
            self.assertEqual(a._dict, b)

        for contents, expected in (
            ('{"S": "a"}', {
                ('S', ): 'a'
            }),
            ('{"S": "\xc3\xb1"}', {
                ('S', ): u'\xf1'
            }),
            ('{"S": "\xf1"}', {
                ('S', ): u'\xf1'
            }),
        ):
            assertEqual(load_dictionary(make_dict(contents)), expected)

        with self.assertRaises(DictionaryLoaderException):
            load_dictionary(make_dict('foo'))
Esempio n. 6
0
    def test_load_dictionary(self):
        def assertEqual(a, b):
            self.assertEqual(a._dict, b)

        for contents, expected in (
            (u'{"S": "a"}', {
                ('S', ): 'a'
            }),
                # Default encoding is utf-8.
            (u'{"S": "café"}'.encode('utf-8'), {
                ('S', ): u'café'
            }),
                # But if that fails, the implementation
                # must automatically retry with latin-1.
            (u'{"S": "café"}'.encode('latin-1'), {
                ('S', ): u'café'
            }),
        ):
            assertEqual(load_dictionary(make_dict(contents)), expected)

        with self.assertRaises(DictionaryLoaderException):
            load_dictionary(make_dict('foo'))