Skip to content

Commit 5a30029

Browse files
committed
Improve code coverage
1 parent bf6ebd9 commit 5a30029

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

eip712_structs/domain_separator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def make_domain(name=None, version=None, chainId=None, verifyingContract=None, s
88
"""
99

1010
if all(i is None for i in [name, version, chainId, verifyingContract, salt]):
11-
raise ValueError('At least on argument must be given.')
11+
raise ValueError('At least one argument must be given.')
1212

1313
class EIP712Domain(eip712_structs.EIP712Struct):
1414
pass

tests/test_domain_separator.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import os
2-
from eip712_structs import make_domain
2+
3+
import pytest
34
from eth_utils.crypto import keccak
45

6+
from eip712_structs import make_domain
7+
58

69
def test_domain_sep_create():
710
salt = os.urandom(32)
@@ -13,6 +16,9 @@ def test_domain_sep_create():
1316
expected_data = b''.join([keccak(text='name'), salt])
1417
assert domain_struct.encode_value() == expected_data
1518

19+
with pytest.raises(ValueError, match='At least one argument must be given'):
20+
make_domain()
21+
1622

1723
def test_domain_sep_types():
1824
salt = os.urandom(32)

tests/test_encode_data.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import string
44

55
from eth_utils.crypto import keccak
6+
import pytest
67

78
from eip712_structs import Address, Array, Boolean, Bytes, Int, String, Uint, EIP712Struct, make_domain
89

@@ -158,3 +159,31 @@ class Foo(EIP712Struct):
158159
empty_string_hash = keccak(text='')
159160
assert encoded_val[0:32] == empty_string_hash
160161
assert encoded_val[32:] == bytes(32)
162+
163+
164+
def test_validation_errors():
165+
bytes_type = Bytes(10)
166+
int_type = Int(8) # -128 <= i < 128
167+
uint_type = Uint(8) # 0 <= i < 256
168+
bool_type = Boolean()
169+
170+
with pytest.raises(ValueError, match='bytes10 was given bytes with length 11'):
171+
bytes_type.encode_value(os.urandom(11))
172+
173+
with pytest.raises(OverflowError, match='too big'):
174+
int_type.encode_value(128)
175+
with pytest.raises(OverflowError, match='too big'):
176+
int_type.encode_value(-129)
177+
178+
with pytest.raises(OverflowError, match='too big'):
179+
uint_type.encode_value(256)
180+
assert uint_type.encode_value(0) == bytes(32)
181+
with pytest.raises(OverflowError, match='negative int to unsigned'):
182+
uint_type.encode_value(-1)
183+
184+
assert bool_type.encode_value(True) == bytes(31) + b'\x01'
185+
assert bool_type.encode_value(False) == bytes(32)
186+
with pytest.raises(ValueError, match='Must be True or False.'):
187+
bool_type.encode_value(0)
188+
with pytest.raises(ValueError, match='Must be True or False.'):
189+
bool_type.encode_value(1)

0 commit comments

Comments
 (0)