| | 165 | def test_bytestring(self): |
| | 166 | class UnicodeObject: |
| | 167 | def __unicode__(self): |
| | 168 | return u'MÃötley CrÃÃÂŒe' |
| | 169 | |
| | 170 | class StrObject: |
| | 171 | def __str__(self): |
| | 172 | return u'MÃötley CrÃÃÂŒe' |
| | 173 | |
| | 174 | class ReprObject: |
| | 175 | def __repr__(self): |
| | 176 | return u'MÃötley CrÃÃÂŒe' |
| | 177 | |
| | 178 | self.encoder.writeString(UnicodeObject()) |
| | 179 | self.assertEquals(self.buf.getvalue(), '\x02\x00\x15M\xc3\x83\xc3\x82' |
| | 180 | '\xc2\xb6tley Cr\xc3\x83\xc3\x82\xc2\xbce') |
| | 181 | self.buf.truncate() |
| | 182 | |
| | 183 | self.encoder.writeString(StrObject()) |
| | 184 | self.assertEquals(self.buf.getvalue(), '\x02\x00\x15M\xc3\x83\xc3\x82' |
| | 185 | '\xc2\xb6tley Cr\xc3\x83\xc3\x82\xc2\xbce') |
| | 186 | self.buf.truncate() |
| | 187 | |
| | 188 | self.encoder.writeString(ReprObject()) |
| | 189 | self.assertEquals(self.buf.getvalue(), '\x02\x00\x15M\xc3\x83\xc3\x82' |
| | 190 | '\xc2\xb6tley Cr\xc3\x83\xc3\x82\xc2\xbce') |
| | 191 | self.buf.truncate() |
| | 192 | |
| | 193 | self.encoder.writeString('M\xc3\x83\xc3\x82\xc2\xb6tley Cr\xc3\x83\xc3\x82\xc2\xbce') |
| | 194 | self.assertEquals(self.buf.getvalue(), '\x02\x00\x15M\xc3\x83\xc3\x82' |
| | 195 | '\xc2\xb6tley Cr\xc3\x83\xc3\x82\xc2\xbce') |
| | 196 | |
| | 414 | |
| | 415 | def test_getstate(self): |
| | 416 | tc = self |
| | 417 | tc.executed = False |
| | 418 | |
| | 419 | class Foo(object): |
| | 420 | def __getstate__(self): |
| | 421 | tc.executed = True |
| | 422 | return {'spam': 'hello', 'eggs': True} |
| | 423 | |
| | 424 | pyamf.register_class(Foo, 'foo') |
| | 425 | |
| | 426 | foo = Foo() |
| | 427 | self.encoder.writeElement(foo) |
| | 428 | |
| | 429 | self.assertEquals(self.buf.getvalue(), '\x10\x00\x03\x66\x6f\x6f\x00' |
| | 430 | '\x04\x65\x67\x67\x73\x01\x01\x00\x04\x73\x70\x61\x6d\x02\x00\x05' |
| | 431 | '\x68\x65\x6c\x6c\x6f\x00\x00\x09') |
| | 432 | self.assertTrue(self.executed) |
| | 433 | |
| | 434 | pyamf.unregister_class(Foo) |
| | 667 | def test_setstate_newstyle(self): |
| | 668 | self.executed = False |
| | 669 | |
| | 670 | class Foo(object): |
| | 671 | tc = self |
| | 672 | |
| | 673 | def __init__(self, *args, **kwargs): |
| | 674 | self.tc.fail("__init__ called") |
| | 675 | |
| | 676 | def __setstate__(self, state): |
| | 677 | self.tc.executed = True |
| | 678 | self.__dict__.update(state) |
| | 679 | |
| | 680 | pyamf.register_class(Foo, 'foo') |
| | 681 | |
| | 682 | self.buf.write('\x10\x00\x03\x66\x6f\x6f\x00\x04\x65\x67\x67\x73\x01\x01\x00\x04') |
| | 683 | self.buf.write('\x73\x70\x61\x6d\x02\x00\x05\x68\x65\x6c\x6c\x6f\x00\x00\x09') |
| | 684 | self.buf.seek(0) |
| | 685 | |
| | 686 | foo = self.decoder.readElement() |
| | 687 | |
| | 688 | self.assertEquals(foo.spam, 'hello') |
| | 689 | self.assertEquals(foo.eggs, True) |
| | 690 | self.assertTrue(self.executed) |
| | 691 | |
| | 692 | pyamf.unregister_class(Foo) |
| | 693 | |
| | 694 | def test_setstate_classic(self): |
| | 695 | self.executed = False |
| | 696 | |
| | 697 | class Foo: |
| | 698 | tc = self |
| | 699 | |
| | 700 | def __init__(self, *args, **kwargs): |
| | 701 | self.tc.fail("__init__ called") |
| | 702 | |
| | 703 | def __setstate__(self, state): |
| | 704 | self.tc.executed = True |
| | 705 | self.__dict__.update(state) |
| | 706 | |
| | 707 | pyamf.register_class(Foo, 'foo') |
| | 708 | |
| | 709 | self.buf.write('\x10\x00\x03\x66\x6f\x6f\x00\x04\x65\x67\x67\x73\x01\x01\x00\x04') |
| | 710 | self.buf.write('\x73\x70\x61\x6d\x02\x00\x05\x68\x65\x6c\x6c\x6f\x00\x00\x09') |
| | 711 | self.buf.seek(0) |
| | 712 | |
| | 713 | foo = self.decoder.readElement() |
| | 714 | |
| | 715 | self.assertTrue(self.executed) |
| | 716 | self.assertTrue(isinstance(foo, Foo)) |
| | 717 | self.assertEquals(foo.spam, 'hello') |
| | 718 | self.assertEquals(foo.eggs, True) |
| | 719 | |
| | 720 | pyamf.unregister_class(Foo) |
| | 721 | |