backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / tests / unit / lib / test_stringify.py
1 # Copyright (C) 2013-2015 Nippon Telegraph and Telephone Corporation.
2 # Copyright (C) 2013-2015 YAMAMOTO Takashi <yamamoto at valinux co jp>
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #    http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 from __future__ import print_function
18
19 import base64
20 import six
21 import unittest
22 from nose.tools import eq_
23
24 from ryu.lib import stringify
25
26
27 class C1(stringify.StringifyMixin):
28     def __init__(self, a, c):
29         print("init %s %s" % (a, c))
30         self.a = a
31         self._b = 'B'
32         self.c = c
33
34
35 class Test_stringify(unittest.TestCase):
36     """ Test case for ryu.lib.stringify
37     """
38
39     def setUp(self):
40         pass
41
42     def tearDown(self):
43         pass
44
45     def test_jsondict(self):
46         if six.PY3:
47             def b64encode(s):
48                 return base64.b64encode(s).decode('ascii')
49         else:
50             b64encode = base64.b64encode
51         j = {'C1': {'a': 'QUFB', 'c': 'Q0ND'}}
52         eq_(j['C1']['a'], b64encode(b'AAA'))
53         eq_(j['C1']['c'], b64encode(b'CCC'))
54         c = C1(a=b'AAA', c=b'CCC')
55         c2 = C1.from_jsondict(j['C1'])
56         eq_(c.__class__, c2.__class__)
57         eq_(c.__dict__, c2.__dict__)
58         eq_(j, c.to_jsondict())
59
60     def test_jsondict2(self):
61
62         def my_encode(x):
63             return x.lower()
64
65         def my_decode(x):
66             return x.upper()
67
68         j = {'C1': {'a': 'aaa', 'c': 'ccc'}}
69         eq_(j['C1']['a'], my_encode('AAA'))
70         eq_(j['C1']['c'], my_encode('CCC'))
71         c = C1(a='AAA', c='CCC')
72         c2 = C1.from_jsondict(j['C1'], decode_string=my_decode)
73         eq_(c.__class__, c2.__class__)
74         eq_(c.__dict__, c2.__dict__)
75         eq_(j, c.to_jsondict(encode_string=my_encode))