|
| 1 | +"""Tests for EvaluationMode functionality.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from cel import Context, EvaluationMode, evaluate |
| 5 | + |
| 6 | + |
| 7 | +def test_evaluation_mode_enum_values(): |
| 8 | + """Test that EvaluationMode enum has expected values.""" |
| 9 | + assert EvaluationMode.PYTHON == "python" |
| 10 | + assert EvaluationMode.STRICT == "strict" |
| 11 | + # String representation shows enum name, but equality works with values |
| 12 | + assert str(EvaluationMode.PYTHON) == "EvaluationMode.PYTHON" |
| 13 | + assert str(EvaluationMode.STRICT) == "EvaluationMode.STRICT" |
| 14 | + |
| 15 | + |
| 16 | +def test_default_mode_is_python_compatible(): |
| 17 | + """Test that the default evaluation mode allows mixed arithmetic.""" |
| 18 | + # Without explicit mode, should use python by default |
| 19 | + result = evaluate("1 + 2.5") |
| 20 | + assert result == 3.5 |
| 21 | + |
| 22 | + |
| 23 | +def test_python_mode_explicit(): |
| 24 | + """Test explicit python mode allows mixed arithmetic.""" |
| 25 | + # Using enum |
| 26 | + result = evaluate("1 + 2.5", mode=EvaluationMode.PYTHON) |
| 27 | + assert result == 3.5 |
| 28 | + |
| 29 | + # Using string |
| 30 | + result = evaluate("1 + 2.5", mode="python") |
| 31 | + assert result == 3.5 |
| 32 | + |
| 33 | + |
| 34 | +def test_strict_mode_rejects_mixed_arithmetic(): |
| 35 | + """Test that strict mode rejects mixed int/float arithmetic.""" |
| 36 | + # Using enum |
| 37 | + with pytest.raises(TypeError, match="Unsupported addition operation"): |
| 38 | + evaluate("1 + 2.5", mode=EvaluationMode.STRICT) |
| 39 | + |
| 40 | + # Using string |
| 41 | + with pytest.raises(TypeError, match="Unsupported addition operation"): |
| 42 | + evaluate("1 + 2.5", mode="strict") |
| 43 | + |
| 44 | + |
| 45 | +def test_same_type_arithmetic_works_in_both_modes(): |
| 46 | + """Test that same-type arithmetic works in both modes.""" |
| 47 | + # Integer arithmetic |
| 48 | + assert evaluate("1 + 2", mode=EvaluationMode.PYTHON) == 3 |
| 49 | + assert evaluate("1 + 2", mode=EvaluationMode.STRICT) == 3 |
| 50 | + |
| 51 | + # Float arithmetic |
| 52 | + assert evaluate("1.5 + 2.5", mode=EvaluationMode.PYTHON) == 4.0 |
| 53 | + assert evaluate("1.5 + 2.5", mode=EvaluationMode.STRICT) == 4.0 |
| 54 | + |
| 55 | + |
| 56 | +def test_context_with_mixed_types(): |
| 57 | + """Test evaluation modes with context containing mixed types.""" |
| 58 | + context = {"x": 1, "y": 2.5} |
| 59 | + |
| 60 | + # Python mode should promote and work |
| 61 | + result = evaluate("x + y", context, mode=EvaluationMode.PYTHON) |
| 62 | + assert result == 3.5 |
| 63 | + |
| 64 | + # Strict should fail |
| 65 | + with pytest.raises(TypeError, match="Unsupported addition operation"): |
| 66 | + evaluate("x + y", context, mode=EvaluationMode.STRICT) |
| 67 | + |
| 68 | + |
| 69 | +def test_context_object_with_mixed_types(): |
| 70 | + """Test evaluation modes with Context object containing mixed types.""" |
| 71 | + context = Context(variables={"a": 5, "b": 3.0}) |
| 72 | + |
| 73 | + # Python mode should work |
| 74 | + result = evaluate("a + b", context, mode=EvaluationMode.PYTHON) |
| 75 | + assert result == 8.0 |
| 76 | + |
| 77 | + # Strict should fail |
| 78 | + with pytest.raises(TypeError, match="Unsupported addition operation"): |
| 79 | + evaluate("a + b", context, mode=EvaluationMode.STRICT) |
| 80 | + |
| 81 | + |
| 82 | +def test_invalid_mode_string(): |
| 83 | + """Test that invalid mode strings raise appropriate errors.""" |
| 84 | + with pytest.raises(TypeError, match="Invalid EvaluationMode"): |
| 85 | + evaluate("1 + 2", mode="InvalidMode") |
| 86 | + |
| 87 | + |
| 88 | +def test_context_type_promotion_in_python_mode(): |
| 89 | + """Test that python mode promotes context integers when floats are present.""" |
| 90 | + context = {"int_val": 10, "float_val": 2.5} |
| 91 | + |
| 92 | + # This should work in python mode due to type promotion |
| 93 | + result = evaluate("int_val * float_val", context, mode=EvaluationMode.PYTHON) |
| 94 | + assert result == 25.0 |
| 95 | + |
| 96 | + |
| 97 | +def test_expression_preprocessing_in_python_mode(): |
| 98 | + """Test that python mode preprocesses integer literals when mixed with floats.""" |
| 99 | + # This expression has mixed literals, should work in python mode |
| 100 | + result = evaluate("10 + 2.5", mode=EvaluationMode.PYTHON) |
| 101 | + assert result == 12.5 |
| 102 | + |
| 103 | + # Should fail in strict mode |
| 104 | + with pytest.raises(TypeError): |
| 105 | + evaluate("10 + 2.5", mode=EvaluationMode.STRICT) |
| 106 | + |
| 107 | + |
| 108 | +def test_non_arithmetic_expressions_work_in_both_modes(): |
| 109 | + """Test that non-arithmetic expressions work the same in both modes.""" |
| 110 | + # String operations |
| 111 | + assert evaluate('"hello" + " world"', mode=EvaluationMode.PYTHON) == "hello world" |
| 112 | + assert evaluate('"hello" + " world"', mode=EvaluationMode.STRICT) == "hello world" |
| 113 | + |
| 114 | + # Boolean operations |
| 115 | + assert evaluate("true && false", mode=EvaluationMode.PYTHON) is False |
| 116 | + assert evaluate("true && false", mode=EvaluationMode.STRICT) is False |
| 117 | + |
| 118 | + # List operations |
| 119 | + assert evaluate("[1, 2, 3].size()", mode=EvaluationMode.PYTHON) == 3 |
| 120 | + assert evaluate("[1, 2, 3].size()", mode=EvaluationMode.STRICT) == 3 |
| 121 | + |
| 122 | + |
| 123 | +def test_mode_with_custom_functions(): |
| 124 | + """Test that evaluation modes work with custom functions.""" |
| 125 | + |
| 126 | + def add_numbers(a, b): |
| 127 | + return a + b |
| 128 | + |
| 129 | + context = Context(variables={"x": 1, "y": 2.5}, functions={"add": add_numbers}) |
| 130 | + |
| 131 | + # The Python function itself will handle mixed arithmetic |
| 132 | + result = evaluate("add(x, y)", context, mode=EvaluationMode.STRICT) |
| 133 | + assert result == 3.5 # Python function can handle mixed types |
| 134 | + |
| 135 | + # But CEL arithmetic still fails in strict mode |
| 136 | + with pytest.raises(TypeError): |
| 137 | + evaluate("x + y", context, mode=EvaluationMode.STRICT) |
| 138 | + |
| 139 | + |
| 140 | +def test_mode_parameter_positions(): |
| 141 | + """Test that mode parameter works in different positions.""" |
| 142 | + context = {"a": 1, "b": 2} |
| 143 | + |
| 144 | + # Mode as third parameter |
| 145 | + result1 = evaluate("a + b", context, EvaluationMode.PYTHON) |
| 146 | + assert result1 == 3 |
| 147 | + |
| 148 | + # Mode as keyword argument |
| 149 | + result2 = evaluate("a + b", context, mode=EvaluationMode.PYTHON) |
| 150 | + assert result2 == 3 |
| 151 | + |
| 152 | + # Mode without context |
| 153 | + result3 = evaluate("1 + 2", mode=EvaluationMode.PYTHON) |
| 154 | + assert result3 == 3 |
0 commit comments