Skip to content
This repository was archived by the owner on Jan 13, 2023. It is now read-only.

Commit 879dbb2

Browse files
committed
[#49] trits_from_int now defaults to pad=1.
1 parent 888ad00 commit 879dbb2

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

iota/types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
TrytesCompatible = Union[AnyStr, bytearray, 'TryteString']
3535

3636

37-
def trits_from_int(n, pad=None):
37+
def trits_from_int(n, pad=1):
3838
# type: (int, Optional[int]) -> List[int]
3939
"""
4040
Returns a trit representation of an integer value.
@@ -60,7 +60,7 @@ def trits_from_int(n, pad=None):
6060
quotient += 1
6161
remainder = -1
6262

63-
trits = [remainder] + trits_from_int(quotient)
63+
trits = [remainder] + trits_from_int(quotient, pad=0)
6464

6565
if pad:
6666
trits += [0] * max(0, pad - len(trits))

test/types_test.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,31 @@
66

77
from six import binary_type, text_type
88

9-
from iota import Address, AddressChecksum, Hash, Tag, TryteString, TrytesCodec, \
10-
TrytesDecodeError
9+
from iota import Address, AddressChecksum, Hash, Tag, TryteString, \
10+
TrytesCodec, TrytesDecodeError, trits_from_int
11+
12+
13+
class TritsFromIntTestCase(TestCase):
14+
"""
15+
Explicit unit tests for :py:func:`trits_from_int`.
16+
17+
Note that this function is used internally by many different classes
18+
and functions, so we still have good coverage even though this
19+
particular test case has limited scope.
20+
"""
21+
def test_zero(self):
22+
"""
23+
Zero is represented as ``[0]`` by default.
24+
25+
https://github.com/iotaledger/iota.lib.py/issues/49
26+
"""
27+
self.assertEqual(trits_from_int(0), [0])
28+
29+
def test_zero_unpadded(self):
30+
"""
31+
Converting zero to trits, without padding.
32+
"""
33+
self.assertEqual(trits_from_int(0, pad=None), [])
1134

1235

1336
# noinspection SpellCheckingInspection

0 commit comments

Comments
 (0)