1 # Copyright (C) 2013-2015 Nippon Telegraph and Telephone Corporation.
2 # Copyright (C) 2013-2015 YAMAMOTO Takashi <yamamoto at valinux co jp>
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 from __future__ import print_function
22 from nose.tools import eq_
24 from ryu.lib import stringify
27 class C1(stringify.StringifyMixin):
28 def __init__(self, a, c):
29 print("init %s %s" % (a, c))
35 class Test_stringify(unittest.TestCase):
36 """ Test case for ryu.lib.stringify
45 def test_jsondict(self):
48 return base64.b64encode(s).decode('ascii')
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())
60 def test_jsondict2(self):
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))