|
6 | 6 | (["a", "b", "c", "d"], "a"), |
7 | 7 | ("a", "a"), |
8 | 8 | ("minura", "a"), |
| 9 | + ("hello world", "world"), |
9 | 10 | ({"a": "val1", "b": "val2"}, "a"), |
10 | 11 | ({"b": 6, "a": 2, "c": 16}, {"a": 2, "b": 6}), |
11 | 12 | ({"b": 6, "a": 2, "c": 16}, {"a": 2}), |
|
15 | 16 | (["a", "b"], "b"), |
16 | 17 | (["a", ["b"]], ["b"]), |
17 | 18 | (["a"], "a"), |
| 19 | + ([1, 2, 3], 2), |
| 20 | + ([1, [2, 3]], [2, 3]), |
18 | 21 | ] |
19 | 22 |
|
20 | 23 | checks_failing = [ |
21 | 24 | (["a", "b", "c", "d"], "e"), |
22 | | - ("e", ["a", "b", "c", "d"]), |
23 | | - (2, "a"), |
24 | | - ("3", 3), |
25 | | - ("c", ["a", "b"]), |
| 25 | + ("hello", "world"), |
| 26 | + ("abc", "def"), |
| 27 | + ({"a": "val1", "b": "val2"}, "c"), |
| 28 | + ({"b": 6, "a": 2, "c": 16}, {"a": 3}), |
| 29 | + ({"b": 6, "a": 2, "c": 16}, {"a": 2, "d": 4}), |
| 30 | + ({"b": 6, "a": 2, "c": 16}, {"a": 2, "b": 7}), |
| 31 | + (["a", "b"], "c"), |
| 32 | + ([1, 2, 3], 4), |
| 33 | + ([1, [2, 3]], [3, 4]), |
26 | 34 | ] |
27 | 35 |
|
28 | 36 | checks_unsupported = [ |
29 | 37 | (2, "a"), |
30 | | - ("3", 3), |
31 | | - (1, 1), |
32 | | - (1, 2), |
33 | | - (["a"], ["a", "b", "c", "d"]), |
| 38 | + (1.5, "test"), |
| 39 | + (True, "boolean"), |
| 40 | + (None, "none"), |
34 | 41 | ] |
35 | 42 |
|
36 | 43 | evaluator = Contains() |
|
41 | 48 | @mark.parametrize("evaluator_input,evaluator_data", checks_passing) |
42 | 49 | def test_evaluate_passing(evaluator_input, evaluator_data): |
43 | 50 | result = evaluator.evaluate(evaluator_input, evaluator_data) |
44 | | - assert result["passed"] == True # {"passed": True, "message": f"Found {evaluator_input} inside {evaluator_data}"} |
| 51 | + assert result["passed"] |
| 52 | + assert "Found" in result["message"] |
| 53 | + assert "inside" in result["message"] |
45 | 54 |
|
46 | 55 |
|
47 | | -# # pytest -v -m failing |
| 56 | +# pytest -v -m failing |
48 | 57 | @mark.failing |
49 | 58 | @mark.parametrize("evaluator_input,evaluator_data", checks_failing) |
50 | 59 | def test_evaluate_failing(evaluator_input, evaluator_data): |
51 | 60 | result = evaluator.evaluate(evaluator_input, evaluator_data) |
52 | | - assert ( |
53 | | - result["passed"] == False |
54 | | - ) # {"passed": True, "message": f"Failed to find {evaluator_input} inside {evaluator_data}"} |
| 61 | + assert not result["passed"] |
| 62 | + assert "Failed to find" in result["message"] |
| 63 | + assert "inside" in result["message"] |
55 | 64 |
|
56 | 65 |
|
57 | | -# pytest -v -m failing |
58 | | -@mark.failing |
| 66 | +# pytest -v -m unsupported |
59 | 67 | @mark.parametrize("evaluator_input,evaluator_data", checks_unsupported) |
60 | 68 | def test_evaluate_unsupported(evaluator_input, evaluator_data): |
61 | 69 | result = evaluator.evaluate(evaluator_input, evaluator_data) |
62 | | - assert ( |
63 | | - result["passed"] == False |
64 | | - ) # {"passed": True, "message": f"Failed to find {evaluator_input} inside {evaluator_data}"} |
| 70 | + assert not result["passed"] |
| 71 | + assert "unsupported data type" in result["message"] |
| 72 | + |
| 73 | + |
| 74 | +# Test specific string scenarios |
| 75 | +def test_string_contains(): |
| 76 | + # Test case-sensitive string containment |
| 77 | + result = evaluator.evaluate("Hello World", "World") |
| 78 | + assert result["passed"] |
| 79 | + assert "Found" in result["message"] |
| 80 | + |
| 81 | + result = evaluator.evaluate("Hello World", "world") # case-sensitive |
| 82 | + assert not result["passed"] |
| 83 | + assert "Failed to find" in result["message"] |
| 84 | + |
| 85 | + |
| 86 | +def test_empty_string(): |
| 87 | + # Empty string should be contained in any string |
| 88 | + result = evaluator.evaluate("hello", "") |
| 89 | + assert result["passed"] |
| 90 | + |
| 91 | + # But non-empty string should not be in empty string |
| 92 | + result = evaluator.evaluate("", "hello") |
| 93 | + assert not result["passed"] |
| 94 | + |
| 95 | + |
| 96 | +def test_list_contains(): |
| 97 | + # Test nested list containment |
| 98 | + result = evaluator.evaluate([[1, 2], [3, 4]], [1, 2]) |
| 99 | + assert result["passed"] |
| 100 | + |
| 101 | + # Test mixed types in list |
| 102 | + result = evaluator.evaluate([1, "hello", 3.14], "hello") |
| 103 | + assert result["passed"] |
| 104 | + |
| 105 | + result = evaluator.evaluate([1, "hello", 3.14], "world") |
| 106 | + assert not result["passed"] |
| 107 | + |
| 108 | + |
| 109 | +def test_dict_contains(): |
| 110 | + # Test partial dict containment |
| 111 | + input_dict = {"name": "John", "age": 30, "city": "NYC"} |
| 112 | + |
| 113 | + # Should pass - subset of key-value pairs |
| 114 | + result = evaluator.evaluate(input_dict, {"name": "John", "age": 30}) |
| 115 | + assert result["passed"] |
| 116 | + |
| 117 | + # Should fail - wrong value |
| 118 | + result = evaluator.evaluate(input_dict, {"name": "John", "age": 25}) |
| 119 | + assert not result["passed"] |
| 120 | + |
| 121 | + # Should fail - non-existent key |
| 122 | + result = evaluator.evaluate(input_dict, {"name": "John", "country": "USA"}) |
| 123 | + assert not result["passed"] |
| 124 | + |
| 125 | + |
| 126 | +def test_dict_key_contains(): |
| 127 | + # Test single key containment |
| 128 | + input_dict = {"name": "John", "age": 30} |
| 129 | + |
| 130 | + result = evaluator.evaluate(input_dict, "name") |
| 131 | + assert result["passed"] |
| 132 | + |
| 133 | + result = evaluator.evaluate(input_dict, "address") |
| 134 | + assert not result["passed"] |
| 135 | + |
| 136 | + |
| 137 | +def test_empty_containers(): |
| 138 | + # Empty list |
| 139 | + result = evaluator.evaluate([], "anything") |
| 140 | + assert not result["passed"] |
| 141 | + |
| 142 | + # Empty dict |
| 143 | + result = evaluator.evaluate({}, "anything") |
| 144 | + assert not result["passed"] |
| 145 | + |
| 146 | + # Empty dict with empty dict |
| 147 | + result = evaluator.evaluate({}, {}) |
| 148 | + assert result["passed"] |
| 149 | + |
| 150 | + |
| 151 | +def test_error_handling(): |
| 152 | + # Test that exceptions are handled gracefully |
| 153 | + # This shouldn't happen in normal use, but let's test defensive coding |
| 154 | + try: |
| 155 | + result = evaluator.evaluate(None, None) |
| 156 | + assert not result["passed"] |
| 157 | + assert result["message"] == "Not evaluated" |
| 158 | + except Exception: |
| 159 | + pass # If it throws an exception, that's also acceptable |
| 160 | + |
| 161 | + |
| 162 | +def test_message_formatting(): |
| 163 | + # Test that messages are properly formatted |
| 164 | + result = evaluator.evaluate("test string", "test") |
| 165 | + assert result["passed"] |
| 166 | + assert '"test"' in result["message"] |
| 167 | + assert '"test string"' in result["message"] |
| 168 | + |
| 169 | + result = evaluator.evaluate([1, 2, 3], 4) |
| 170 | + assert not result["passed"] |
| 171 | + assert "4" in result["message"] |
| 172 | + assert "[1, 2, 3]" in result["message"] |
0 commit comments