Skip to content

Commit

Permalink
tests: stricter error assertions (#474)
Browse files Browse the repository at this point in the history
Closes #438
use a special contextmanager that asserts the error raised is EXACTLY of
the expected type.
  • Loading branch information
enitrat authored Jan 17, 2025
1 parent 8510774 commit 170de09
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 67 deletions.
24 changes: 12 additions & 12 deletions cairo/tests/ethereum/cancun/vm/instructions/test_arithmetic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import pytest
from hypothesis import given

from ethereum.cancun.vm.instructions.arithmetic import (
Expand All @@ -15,6 +14,7 @@
sub,
)
from tests.utils.args_gen import Evm
from tests.utils.errors import strict_raises
from tests.utils.evm_builder import EvmBuilder

arithmetic_tests_strategy = EvmBuilder().with_stack().with_gas_left().build()
Expand All @@ -26,7 +26,7 @@ def test_add(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("add", evm)
except Exception as cairo_error:
with pytest.raises(type(cairo_error)):
with strict_raises(type(cairo_error)):
add(evm)
return

Expand All @@ -38,7 +38,7 @@ def test_sub(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("sub", evm)
except Exception as cairo_error:
with pytest.raises(type(cairo_error)):
with strict_raises(type(cairo_error)):
sub(evm)
return

Expand All @@ -50,7 +50,7 @@ def test_mul(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("mul", evm)
except Exception as cairo_error:
with pytest.raises(type(cairo_error)):
with strict_raises(type(cairo_error)):
mul(evm)
return

Expand All @@ -63,7 +63,7 @@ def test_div(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("div", evm)
except Exception as cairo_error:
with pytest.raises(type(cairo_error)):
with strict_raises(type(cairo_error)):
div(evm)
return

Expand All @@ -75,7 +75,7 @@ def test_sdiv(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("sdiv", evm)
except Exception as cairo_error:
with pytest.raises(type(cairo_error)):
with strict_raises(type(cairo_error)):
sdiv(evm)
return

Expand All @@ -87,7 +87,7 @@ def test_mod(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("mod", evm)
except Exception as cairo_error:
with pytest.raises(type(cairo_error)):
with strict_raises(type(cairo_error)):
mod(evm)
return

Expand All @@ -99,7 +99,7 @@ def test_smod(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("smod", evm)
except Exception as cairo_error:
with pytest.raises(type(cairo_error)):
with strict_raises(type(cairo_error)):
smod(evm)
return

Expand All @@ -111,7 +111,7 @@ def test_addmod(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("addmod", evm)
except Exception as cairo_error:
with pytest.raises(type(cairo_error)):
with strict_raises(type(cairo_error)):
addmod(evm)
return

Expand All @@ -123,7 +123,7 @@ def test_mulmod(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("mulmod", evm)
except Exception as cairo_error:
with pytest.raises(type(cairo_error)):
with strict_raises(type(cairo_error)):
mulmod(evm)
return

Expand All @@ -135,7 +135,7 @@ def test_exp(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("exp", evm)
except Exception as cairo_error:
with pytest.raises(type(cairo_error)):
with strict_raises(type(cairo_error)):
exp(evm)
return

Expand All @@ -147,7 +147,7 @@ def test_signextend(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("signextend", evm)
except Exception as cairo_error:
with pytest.raises(type(cairo_error)):
with strict_raises(type(cairo_error)):
signextend(evm)
return

Expand Down
18 changes: 9 additions & 9 deletions cairo/tests/ethereum/cancun/vm/instructions/test_bitwise.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import pytest
from hypothesis import given

from ethereum.cancun.vm.exceptions import ExceptionalHalt
Expand All @@ -13,6 +12,7 @@
get_byte,
)
from tests.utils.args_gen import Evm
from tests.utils.errors import strict_raises
from tests.utils.evm_builder import EvmBuilder

bitwise_tests_strategy = EvmBuilder().with_stack().with_gas_left().build()
Expand All @@ -24,7 +24,7 @@ def test_and(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("bitwise_and", evm)
except ExceptionalHalt as cairo_error:
with pytest.raises(type(cairo_error)):
with strict_raises(type(cairo_error)):
bitwise_and(evm)
return

Expand All @@ -36,7 +36,7 @@ def test_or(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("bitwise_or", evm)
except ExceptionalHalt as cairo_error:
with pytest.raises(type(cairo_error)):
with strict_raises(type(cairo_error)):
bitwise_or(evm)
return

Expand All @@ -48,7 +48,7 @@ def test_xor(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("bitwise_xor", evm)
except ExceptionalHalt as cairo_error:
with pytest.raises(type(cairo_error)):
with strict_raises(type(cairo_error)):
bitwise_xor(evm)
return

Expand All @@ -60,7 +60,7 @@ def test_not(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("bitwise_not", evm)
except ExceptionalHalt as cairo_error:
with pytest.raises(type(cairo_error)):
with strict_raises(type(cairo_error)):
bitwise_not(evm)
return

Expand All @@ -72,7 +72,7 @@ def test_get_byte(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("get_byte", evm)
except ExceptionalHalt as cairo_error:
with pytest.raises(type(cairo_error)):
with strict_raises(type(cairo_error)):
get_byte(evm)
return

Expand All @@ -84,7 +84,7 @@ def test_shl(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("bitwise_shl", evm)
except ExceptionalHalt as cairo_error:
with pytest.raises(type(cairo_error)):
with strict_raises(type(cairo_error)):
bitwise_shl(evm)
return

Expand All @@ -96,7 +96,7 @@ def test_shr(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("bitwise_shr", evm)
except ExceptionalHalt as cairo_error:
with pytest.raises(type(cairo_error)):
with strict_raises(type(cairo_error)):
bitwise_shr(evm)
return

Expand All @@ -108,7 +108,7 @@ def test_sar(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("bitwise_sar", evm)
except ExceptionalHalt as cairo_error:
with pytest.raises(type(cairo_error)):
with strict_raises(type(cairo_error)):
bitwise_sar(evm)
return

Expand Down
16 changes: 8 additions & 8 deletions cairo/tests/ethereum/cancun/vm/instructions/test_block.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import pytest
from ethereum_types.numeric import U64, Uint
from hypothesis import given
from hypothesis import strategies as st
Expand All @@ -14,6 +13,7 @@
timestamp,
)
from tests.utils.args_gen import Environment, Evm, TransientStorage
from tests.utils.errors import strict_raises
from tests.utils.evm_builder import EvmBuilder, address_zero
from tests.utils.strategies import (
BLOCK_HASHES_LIST,
Expand Down Expand Up @@ -73,7 +73,7 @@ def test_block_hash(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("block_hash", evm)
except ExceptionalHalt as cairo_error:
with pytest.raises(type(cairo_error)):
with strict_raises(type(cairo_error)):
block_hash(evm)
return

Expand All @@ -85,7 +85,7 @@ def test_coinbase(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("coinbase", evm)
except ExceptionalHalt as cairo_error:
with pytest.raises(type(cairo_error)):
with strict_raises(type(cairo_error)):
coinbase(evm)
return

Expand All @@ -97,7 +97,7 @@ def test_timestamp(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("timestamp", evm)
except ExceptionalHalt as cairo_error:
with pytest.raises(type(cairo_error)):
with strict_raises(type(cairo_error)):
timestamp(evm)
return

Expand All @@ -109,7 +109,7 @@ def test_number(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("number", evm)
except ExceptionalHalt as cairo_error:
with pytest.raises(type(cairo_error)):
with strict_raises(type(cairo_error)):
number(evm)
return

Expand All @@ -121,7 +121,7 @@ def test_prev_randao(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("prev_randao", evm)
except ExceptionalHalt as cairo_error:
with pytest.raises(type(cairo_error)):
with strict_raises(type(cairo_error)):
prev_randao(evm)
return

Expand All @@ -133,7 +133,7 @@ def test_gas_limit(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("gas_limit", evm)
except ExceptionalHalt as cairo_error:
with pytest.raises(type(cairo_error)):
with strict_raises(type(cairo_error)):
gas_limit(evm)
return

Expand All @@ -145,7 +145,7 @@ def test_chain_id(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("chain_id", evm)
except ExceptionalHalt as cairo_error:
with pytest.raises(type(cairo_error)):
with strict_raises(type(cairo_error)):
chain_id(evm)
return

Expand Down
14 changes: 7 additions & 7 deletions cairo/tests/ethereum/cancun/vm/instructions/test_comparison.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import pytest
from hypothesis import given

from ethereum.cancun.vm.exceptions import ExceptionalHalt
Expand All @@ -11,6 +10,7 @@
signed_less_than,
)
from tests.utils.args_gen import Evm
from tests.utils.errors import strict_raises
from tests.utils.evm_builder import EvmBuilder

comparison_tests_strategy = EvmBuilder().with_stack().with_gas_left().build()
Expand All @@ -22,7 +22,7 @@ def test_less_than(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("less_than", evm)
except ExceptionalHalt as cairo_error:
with pytest.raises(type(cairo_error)):
with strict_raises(type(cairo_error)):
less_than(evm)
return

Expand All @@ -34,7 +34,7 @@ def test_greater_than(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("greater_than", evm)
except ExceptionalHalt as cairo_error:
with pytest.raises(type(cairo_error)):
with strict_raises(type(cairo_error)):
greater_than(evm)
return

Expand All @@ -46,7 +46,7 @@ def test_signed_less_than(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("signed_less_than", evm)
except ExceptionalHalt as cairo_error:
with pytest.raises(type(cairo_error)):
with strict_raises(type(cairo_error)):
signed_less_than(evm)
return

Expand All @@ -58,7 +58,7 @@ def test_signed_greater_than(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("signed_greater_than", evm)
except ExceptionalHalt as cairo_error:
with pytest.raises(type(cairo_error)):
with strict_raises(type(cairo_error)):
signed_greater_than(evm)
return

Expand All @@ -70,7 +70,7 @@ def test_equal(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("equal", evm)
except ExceptionalHalt as cairo_error:
with pytest.raises(type(cairo_error)):
with strict_raises(type(cairo_error)):
equal(evm)
return

Expand All @@ -82,7 +82,7 @@ def test_is_zero(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("is_zero", evm)
except ExceptionalHalt as cairo_error:
with pytest.raises(type(cairo_error)):
with strict_raises(type(cairo_error)):
is_zero(evm)
return

Expand Down
Loading

0 comments on commit 170de09

Please sign in to comment.