<s> from decimal import Decimal <EOL> from unittest import TestCase <EOL> from StringIO import StringIO <EOL> import simplejson as json <EOL> class TestDecimal ( TestCase ) : <EOL> NUMS = "<STR_LIT:1.0>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" <EOL> def dumps ( self , obj , ** kw ) : <EOL> sio = StringIO ( ) <EOL> json . dump ( obj , sio , ** kw ) <EOL> res = json . dumps ( obj , ** kw ) <EOL> self . assertEquals ( res , sio . getvalue ( ) ) <EOL> return res <EOL> def loads ( self , s , ** kw ) : <EOL> sio = StringIO ( s ) <EOL> res = json . loads ( s , ** kw ) <EOL> self . assertEquals ( res , json . load ( sio , ** kw ) ) <EOL> return res <EOL> def test_decimal_encode ( self ) : <EOL> for d in map ( Decimal , self . NUMS ) : <EOL> self . assertEquals ( self . dumps ( d , use_decimal = True ) , str ( d ) ) <EOL> def test_decimal_decode ( self ) : <EOL> for s in self . NUMS : <EOL> self . assertEquals ( self . loads ( s , parse_float = Decimal ) , Decimal ( s ) ) <EOL> def test_decimal_roundtrip ( self ) : <EOL> for d in map ( Decimal , self . NUMS ) : <EOL> self . assertEquals ( <EOL> self . loads ( <EOL> self . dumps ( d , use_decimal = True ) , parse_float = Decimal ) , <EOL> d ) <EOL> self . assertEquals ( <EOL> self . loads ( <EOL> self . dumps ( [ d ] , use_decimal = True ) , parse_float = Decimal ) , <EOL> [ d ] ) <EOL> def test_decimal_defaults ( self ) : <EOL> d = Decimal ( <NUM_LIT:1> ) <EOL> sio = StringIO ( ) <EOL> self . assertRaises ( TypeError , json . dumps , d , use_decimal = False ) <EOL> self . assertRaises ( TypeError , json . dumps , d ) <EOL> self . assertRaises ( TypeError , json . dump , d , sio , use_decimal = False ) <EOL> self . assertRaises ( TypeError , json . dump , d , sio ) </s>
