Skip to content

Commit

Permalink
Removed incorrect Bool.new() staticmethod
Browse files Browse the repository at this point in the history
Added deserialization Enum unit test
  • Loading branch information
arjanz committed Jul 22, 2024
1 parent 6a7cc67 commit ce02a05
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
10 changes: 4 additions & 6 deletions scalecodec/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,6 @@ def example_value(self, _recursion_level: int = 0, max_recursion: int = TYPE_DEC

class Bool(ScalePrimitive):

@classmethod
def new(cls):
return ScaleType(type_def=cls())

def decode(self, data: ScaleBytes) -> bool:

bool_data = data.get_next_bytes(1)
Expand Down Expand Up @@ -898,8 +894,10 @@ def _encode(self, value: Union[str, bytes]) -> ScaleBytes:
def serialize(self, value: bytes) -> str:
return f'0x{value.hex()}'

def deserialize(self, value: str) -> bytes:
return bytes.fromhex(value[2:])
def deserialize(self, value: Union[str, bytes]) -> bytes:
if type(value) is str:
value = bytes.fromhex(value[2:])
return value

def example_value(self, _recursion_level: int = 0, max_recursion: int = TYPE_DECOMP_MAX_RECURSIVE):
return f'0x{str(self.byte_count).zfill(2) * self.byte_count}'
Expand Down
11 changes: 1 addition & 10 deletions test/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Python SCALE Codec Library
#
# Copyright 2018-2020 Stichting Polkascan (Polkascan Foundation).
# Copyright 2018-2024 Stichting Polkascan (Polkascan Foundation).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -13,13 +13,4 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import os


def load_json_file(file_path: str) -> dict:

with open(os.path.abspath(file_path), 'r') as fp:
data = fp.read()

return json.loads(data)
17 changes: 16 additions & 1 deletion test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

class TestEnum(unittest.TestCase):

def test_enum(self):
def test_enum_encode_decode(self):
scale_obj = Enum(Bool=Bool(), Number=U32, None_=None).new()
value = {'Bool': True}

Expand All @@ -45,6 +45,21 @@ def test_enum(self):

self.assertEqual(value, scale_obj.value)

def test_enum_deserialize(self):
scale_obj = Enum(Bool=Bool(), Number=U32, None_=None).new()

scale_obj.deserialize({'Bool': True})
self.assertEqual(('Bool', Bool().new(value=True)), scale_obj.value_object)

scale_obj.deserialize({'Number': 1})
self.assertEqual(('Number', U32.new(value=1)), scale_obj.value_object)

scale_obj.deserialize({'None': None})
self.assertEqual(('None', None), scale_obj.value_object)

scale_obj.deserialize('None')
self.assertEqual(('None', None), scale_obj.value_object)


if __name__ == '__main__':
unittest.main()

0 comments on commit ce02a05

Please sign in to comment.