|
| 1 | +"""Tests for the Boolean Type.""" |
| 2 | + |
| 3 | +from typing import Any, Callable |
| 4 | + |
| 5 | +import pytest |
| 6 | +from pydantic import ValidationError, create_model |
| 7 | + |
| 8 | +from lean_spec.types.boolean import Boolean |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.parametrize("valid_value", [True, False]) |
| 12 | +def test_pydantic_validation_accepts_valid_bool(valid_value: bool) -> None: |
| 13 | + """Tests that Pydantic validation correctly accepts a valid boolean.""" |
| 14 | + model = create_model("Model", value=(Boolean, ...)) |
| 15 | + instance: Any = model(value=valid_value) |
| 16 | + assert isinstance(instance.value, Boolean) |
| 17 | + assert instance.value == Boolean(valid_value) |
| 18 | + |
| 19 | + |
| 20 | +@pytest.mark.parametrize("invalid_value", [1, 0, 1.0, "True"]) |
| 21 | +def test_pydantic_strict_mode_rejects_invalid_types(invalid_value: Any) -> None: |
| 22 | + """Tests that Pydantic's strict mode rejects types that are not `bool`.""" |
| 23 | + model = create_model("Model", value=(Boolean, ...)) |
| 24 | + with pytest.raises(ValidationError): |
| 25 | + model(value=invalid_value) |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.parametrize("valid_value", [True, False, 1, 0]) |
| 29 | +def test_instantiation_from_valid_types(valid_value: bool | int) -> None: |
| 30 | + """Tests that a Boolean can be instantiated from valid bools and ints.""" |
| 31 | + boolean_instance = Boolean(valid_value) |
| 32 | + assert int(boolean_instance) == int(valid_value) |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.parametrize("invalid_int", [-1, 2, 100]) |
| 36 | +def test_instantiation_from_invalid_int_raises_error(invalid_int: int) -> None: |
| 37 | + """Tests that instantiating with an int other than 0 or 1 raises ValueError.""" |
| 38 | + with pytest.raises(ValueError, match="Boolean value must be 0 or 1"): |
| 39 | + Boolean(invalid_int) |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.parametrize("invalid_type", [1.0, "True", b"\x01", None]) |
| 43 | +def test_instantiation_from_invalid_types_raises_error(invalid_type: Any) -> None: |
| 44 | + """Tests that instantiating with non-bool/non-int types raises a TypeError.""" |
| 45 | + with pytest.raises(TypeError, match="Expected bool or int"): |
| 46 | + Boolean(invalid_type) |
| 47 | + |
| 48 | + |
| 49 | +def test_instantiation_and_type() -> None: |
| 50 | + """Tests that a Boolean is an instance of `int` and its own class.""" |
| 51 | + value = Boolean(True) |
| 52 | + assert isinstance(value, int) |
| 53 | + assert isinstance(value, Boolean) |
| 54 | + |
| 55 | + |
| 56 | +def test_to_bytes() -> None: |
| 57 | + r"""Tests that serialization to bytes matches the SSZ spec.""" |
| 58 | + assert Boolean(True).to_bytes() == b"\x01" |
| 59 | + assert Boolean(False).to_bytes() == b"\x00" |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.parametrize( |
| 63 | + "op", |
| 64 | + [ |
| 65 | + lambda a, b: a + b, |
| 66 | + lambda a, b: a - b, |
| 67 | + lambda a, b: 1 + b, |
| 68 | + lambda a, b: 1 - b, |
| 69 | + ], |
| 70 | +) |
| 71 | +def test_arithmetic_operators_raise_error(op: Callable[[Any, Any], Any]) -> None: |
| 72 | + """Tests that all arithmetic operators are disabled and raise TypeError.""" |
| 73 | + with pytest.raises(TypeError, match="Arithmetic operations are not supported"): |
| 74 | + op(Boolean(True), Boolean(False)) |
| 75 | + |
| 76 | + |
| 77 | +def test_bitwise_operators() -> None: |
| 78 | + """Tests all standard bitwise operators between Boolean instances.""" |
| 79 | + b_true = Boolean(True) |
| 80 | + b_false = Boolean(False) |
| 81 | + |
| 82 | + assert b_true & b_true == b_true |
| 83 | + assert b_true & b_false == b_false |
| 84 | + assert b_true | b_false == b_true |
| 85 | + assert b_false | b_false == b_false |
| 86 | + assert b_true ^ b_true == b_false |
| 87 | + assert b_true ^ b_false == b_true |
| 88 | + |
| 89 | + |
| 90 | +@pytest.mark.parametrize("invalid_operand", [1, True, 0.0, "a"]) |
| 91 | +def test_bitwise_operators_with_other_types_raise_error(invalid_operand: Any) -> None: |
| 92 | + """Tests that bitwise operations with non-Boolean types raise TypeError.""" |
| 93 | + with pytest.raises(TypeError): |
| 94 | + _ = Boolean(True) & invalid_operand |
| 95 | + with pytest.raises(TypeError): |
| 96 | + _ = Boolean(True) | invalid_operand |
| 97 | + with pytest.raises(TypeError): |
| 98 | + _ = Boolean(True) ^ invalid_operand |
| 99 | + |
| 100 | + |
| 101 | +def test_strict_equality_with_same_type() -> None: |
| 102 | + """Tests the strict `==` and `!=` operators between two Boolean instances.""" |
| 103 | + assert Boolean(True) == Boolean(True) |
| 104 | + assert Boolean(False) == Boolean(False) |
| 105 | + assert Boolean(True) != Boolean(False) |
| 106 | + |
| 107 | + |
| 108 | +@pytest.mark.parametrize( |
| 109 | + "left_operand, right_operand, expected_result", |
| 110 | + [ |
| 111 | + # --- Comparisons between two Boolean instances --- |
| 112 | + (Boolean(True), Boolean(False), False), |
| 113 | + (Boolean(True), Boolean(True), True), |
| 114 | + (Boolean(False), Boolean(False), True), |
| 115 | + # --- Comparisons with compatible native types (Boolean on the left) --- |
| 116 | + (Boolean(True), True, True), |
| 117 | + (Boolean(True), 1, True), |
| 118 | + (Boolean(True), False, False), |
| 119 | + (Boolean(True), 0, False), |
| 120 | + # --- Comparisons with compatible native types (Boolean on the right) --- |
| 121 | + (True, Boolean(True), True), |
| 122 | + (1, Boolean(True), True), |
| 123 | + (False, Boolean(True), False), |
| 124 | + (0, Boolean(True), False), |
| 125 | + # --- Comparisons with incompatible types --- |
| 126 | + (Boolean(True), "a string", False), |
| 127 | + ("a string", Boolean(True), False), |
| 128 | + (Boolean(True), 1.0, False), |
| 129 | + (Boolean(True), None, False), |
| 130 | + (None, Boolean(True), False), |
| 131 | + ], |
| 132 | +) |
| 133 | +def test_equality_operator(left_operand: Any, right_operand: Any, expected_result: bool) -> None: |
| 134 | + """Tests the `__eq__` equality operator (`==`) for various type combinations.""" |
| 135 | + assert (left_operand == right_operand) is expected_result |
| 136 | + |
| 137 | + |
| 138 | +@pytest.mark.parametrize( |
| 139 | + "left_operand, right_operand, expected_result", |
| 140 | + [ |
| 141 | + # --- Comparisons between two Boolean instances --- |
| 142 | + (Boolean(True), Boolean(False), True), |
| 143 | + (Boolean(True), Boolean(True), False), |
| 144 | + (Boolean(False), Boolean(False), False), |
| 145 | + # --- Comparisons with compatible native types (Boolean on the left) --- |
| 146 | + (Boolean(True), True, False), |
| 147 | + (Boolean(True), 1, False), |
| 148 | + (Boolean(True), False, True), |
| 149 | + (Boolean(True), 0, True), |
| 150 | + # --- Comparisons with compatible native types (Boolean on the right) --- |
| 151 | + (True, Boolean(True), False), |
| 152 | + (1, Boolean(True), False), |
| 153 | + (False, Boolean(True), True), |
| 154 | + (0, Boolean(True), True), |
| 155 | + # --- Comparisons with incompatible types --- |
| 156 | + (Boolean(True), "a string", True), |
| 157 | + ("a string", Boolean(True), True), |
| 158 | + (Boolean(True), 1.0, True), |
| 159 | + (Boolean(True), None, True), |
| 160 | + (None, Boolean(True), True), |
| 161 | + ], |
| 162 | +) |
| 163 | +def test_inequality_operator(left_operand: Any, right_operand: Any, expected_result: bool) -> None: |
| 164 | + """Tests the `__ne__` inequality operator (`!=`) for various type combinations.""" |
| 165 | + assert (left_operand != right_operand) is expected_result |
| 166 | + |
| 167 | + |
| 168 | +def test_repr_and_str() -> None: |
| 169 | + """Tests the string and official representations.""" |
| 170 | + assert str(Boolean(True)) == "True" |
| 171 | + assert repr(Boolean(True)) == "Boolean(True)" |
| 172 | + assert str(Boolean(False)) == "False" |
| 173 | + assert repr(Boolean(False)) == "Boolean(False)" |
| 174 | + |
| 175 | + |
| 176 | +def test_hash() -> None: |
| 177 | + """Tests that the hash is distinct from a raw bool.""" |
| 178 | + assert hash(Boolean(True)) != hash(True) |
| 179 | + assert hash(Boolean(False)) != hash(False) |
| 180 | + assert hash(Boolean(True)) == hash(Boolean(1)) |
| 181 | + assert hash(Boolean(True)) != hash(Boolean(False)) |
0 commit comments