Skip to content

Commit

Permalink
Add test for when the serializer returns bytes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jasongrout committed Aug 9, 2017
1 parent 40cd07c commit 349f556
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions ipywidgets/widgets/tests/test_set_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,24 @@ class DataInstance():
def __init__(self, data=None):
self.data = data

def serializer(instance, widget):
def mview_serializer(instance, widget):
return { 'data': memoryview(instance.data) if instance.data else None }

def bytes_serializer(instance, widget):
return { 'data': memoryview(instance.data).tobytes() if instance.data else None }

def deserializer(json_data, widget):
return DataInstance( json_data['data'].tobytes() if json_data else None )
return DataInstance( memoryview(json_data['data']).tobytes() if json_data else None )

class DataWidget(SimpleWidget):
d = Instance(DataInstance).tag(sync=True, to_json=serializer, from_json=deserializer)


d = Instance(DataInstance).tag(sync=True, to_json=mview_serializer, from_json=deserializer)

# A widget that has a buffer that might be changed on reception:
def truncate_deserializer(json_data, widget):
return DataInstance( json_data['data'][:20].tobytes() if json_data else None )

class TruncateDataWidget(SimpleWidget):
d = Instance(DataInstance).tag(sync=True, to_json=serializer, from_json=truncate_deserializer)

d = Instance(DataInstance).tag(sync=True, to_json=bytes_serializer, from_json=truncate_deserializer)


#
Expand Down

0 comments on commit 349f556

Please sign in to comment.