|
32 | 32 |
|
33 | 33 | #include "core/variant/variant_utility.h" |
34 | 34 |
|
| 35 | +#include "scene/main/node.h" |
35 | 36 | #include "tests/test_macros.h" |
36 | 37 |
|
37 | 38 | namespace TestVariantUtility { |
@@ -128,4 +129,206 @@ TEST_CASE("[VariantUtility] Type conversion") { |
128 | 129 | } |
129 | 130 | } |
130 | 131 |
|
| 132 | +TEST_CASE("[VariantUtility] is_equal deep equality checks") { |
| 133 | + SUBCASE("Different Types") { |
| 134 | + Variant v_int = 5; |
| 135 | + Variant v_str = "hello"; |
| 136 | + Variant v_arr = Array(); |
| 137 | + Variant v_dict = Dictionary(); |
| 138 | + Variant v_null_obj = Variant(); |
| 139 | + Node *node = memnew(Node); |
| 140 | + |
| 141 | + CHECK_FALSE(VariantUtilityFunctions::is_equal(v_int, v_str)); |
| 142 | + CHECK_FALSE(VariantUtilityFunctions::is_equal(v_str, v_int)); |
| 143 | + CHECK_FALSE(VariantUtilityFunctions::is_equal(v_int, v_arr)); |
| 144 | + CHECK_FALSE(VariantUtilityFunctions::is_equal(v_arr, v_dict)); |
| 145 | + CHECK_FALSE(VariantUtilityFunctions::is_equal(v_dict, node)); |
| 146 | + CHECK_FALSE(VariantUtilityFunctions::is_equal(node, v_null_obj)); |
| 147 | + CHECK_FALSE(VariantUtilityFunctions::is_equal(v_int, v_null_obj)); |
| 148 | + } |
| 149 | + |
| 150 | + SUBCASE("Basic Types") { |
| 151 | + CHECK(VariantUtilityFunctions::is_equal(Variant(10), Variant(10))); |
| 152 | + CHECK(VariantUtilityFunctions::is_equal(Variant(3.14), Variant(3.14))); |
| 153 | + CHECK(VariantUtilityFunctions::is_equal(Variant(true), Variant(true))); |
| 154 | + CHECK(VariantUtilityFunctions::is_equal(Variant("test"), Variant("test"))); |
| 155 | + CHECK(VariantUtilityFunctions::is_equal(Variant(Vector2(1, 2)), Variant(Vector2(1, 2)))); |
| 156 | + CHECK(VariantUtilityFunctions::is_equal(Variant(), Variant())); // Nil == Nil |
| 157 | + |
| 158 | + CHECK_FALSE(VariantUtilityFunctions::is_equal(Variant(10), Variant(11))); |
| 159 | + CHECK_FALSE(VariantUtilityFunctions::is_equal(Variant(3.14), Variant(3.141))); |
| 160 | + CHECK_FALSE(VariantUtilityFunctions::is_equal(Variant(true), Variant(false))); |
| 161 | + CHECK_FALSE(VariantUtilityFunctions::is_equal(Variant("test"), Variant("Test"))); |
| 162 | + CHECK_FALSE(VariantUtilityFunctions::is_equal(Variant("test"), Variant(""))); |
| 163 | + CHECK_FALSE(VariantUtilityFunctions::is_equal(Variant(Vector2(1, 2)), Variant(Vector2(1, 3)))); |
| 164 | + CHECK_FALSE(VariantUtilityFunctions::is_equal(Variant(5), Variant())); |
| 165 | + } |
| 166 | + |
| 167 | + SUBCASE("Arrays") { |
| 168 | + Array a1, a2; |
| 169 | + CHECK_MESSAGE(VariantUtilityFunctions::is_equal(a1, a2), "Empty arrays should be equal."); |
| 170 | + |
| 171 | + a1.append(1); |
| 172 | + a1.append("hello"); |
| 173 | + a2.append(1); |
| 174 | + a2.append("hello"); |
| 175 | + CHECK_MESSAGE(VariantUtilityFunctions::is_equal(a1, a2), "Arrays with identical basic elements should be equal."); |
| 176 | + |
| 177 | + Array a3; |
| 178 | + a3.append(1); |
| 179 | + a3.append("world"); |
| 180 | + CHECK_FALSE_MESSAGE(VariantUtilityFunctions::is_equal(a1, a3), "Arrays with different element values should not be equal."); |
| 181 | + |
| 182 | + Array a4; |
| 183 | + a4.append(1); |
| 184 | + a4.append(2); |
| 185 | + CHECK_FALSE_MESSAGE(VariantUtilityFunctions::is_equal(a1, a4), "Arrays with different element types at the same index should not be equal."); |
| 186 | + |
| 187 | + Array a5; |
| 188 | + a5.append(1); |
| 189 | + CHECK_FALSE_MESSAGE(VariantUtilityFunctions::is_equal(a1, a5), "Arrays with different sizes should not be equal."); |
| 190 | + CHECK_FALSE_MESSAGE(VariantUtilityFunctions::is_equal(a5, a1), "Arrays with different sizes should not be equal."); |
| 191 | + |
| 192 | + Array nested1, nested2, inner1, inner2; |
| 193 | + inner1.append(true); |
| 194 | + inner1.append(2.5); |
| 195 | + inner2.append(true); |
| 196 | + inner2.append(2.5); |
| 197 | + nested1.append("outer"); |
| 198 | + nested1.append(inner1); |
| 199 | + nested2.append("outer"); |
| 200 | + nested2.append(inner2); |
| 201 | + CHECK_MESSAGE(VariantUtilityFunctions::is_equal(nested1, nested2), "Arrays with equal nested arrays should be equal."); |
| 202 | + |
| 203 | + Array nested3, inner3; |
| 204 | + inner3.append(true); |
| 205 | + inner3.append(3.0); |
| 206 | + nested3.append("outer"); |
| 207 | + nested3.append(inner3); |
| 208 | + CHECK_FALSE_MESSAGE(VariantUtilityFunctions::is_equal(nested1, nested3), "Arrays with unequal nested arrays should not be equal."); |
| 209 | + } |
| 210 | + |
| 211 | + SUBCASE("Dictionaries") { |
| 212 | + Dictionary d1, d2; |
| 213 | + CHECK_MESSAGE(VariantUtilityFunctions::is_equal(d1, d2), "Empty dictionaries should be equal."); |
| 214 | + |
| 215 | + d1["key1"] = 100; |
| 216 | + d1["key2"] = "val"; |
| 217 | + d2["key1"] = 100; |
| 218 | + d2["key2"] = "val"; |
| 219 | + CHECK_MESSAGE(VariantUtilityFunctions::is_equal(d1, d2), "Dictionaries with identical key-value pairs should be equal."); |
| 220 | + |
| 221 | + Dictionary d3; |
| 222 | + d3["key2"] = "val"; |
| 223 | + d3["key1"] = 100; |
| 224 | + CHECK_MESSAGE(VariantUtilityFunctions::is_equal(d1, d3), "Dictionaries with identical key-value pairs in different order should be equal."); |
| 225 | + |
| 226 | + Dictionary d4; |
| 227 | + d4["key1"] = 100; |
| 228 | + d4["key2"] = "VAL"; |
| 229 | + CHECK_FALSE_MESSAGE(VariantUtilityFunctions::is_equal(d1, d4), "Dictionaries with different values for the same key should not be equal."); |
| 230 | + |
| 231 | + Dictionary d5; |
| 232 | + d5["key1"] = 100; |
| 233 | + d5["key_other"] = "val"; |
| 234 | + CHECK_FALSE_MESSAGE(VariantUtilityFunctions::is_equal(d1, d5), "Dictionaries with different keys should not be equal."); |
| 235 | + CHECK_FALSE_MESSAGE(VariantUtilityFunctions::is_equal(d5, d1), "Dictionaries with different keys should not be equal."); |
| 236 | + |
| 237 | + Dictionary d6; |
| 238 | + d6["key1"] = 100; |
| 239 | + CHECK_FALSE_MESSAGE(VariantUtilityFunctions::is_equal(d1, d6), "Dictionaries with different sizes should not be equal."); |
| 240 | + CHECK_FALSE_MESSAGE(VariantUtilityFunctions::is_equal(d6, d1), "Dictionaries with different sizes should not be equal."); |
| 241 | + |
| 242 | + Dictionary nested_d1, nested_d2, inner_d1, inner_d2; |
| 243 | + inner_d1["sub_key"] = 9.9; |
| 244 | + inner_d2["sub_key"] = 9.9; |
| 245 | + nested_d1["level1"] = inner_d1; |
| 246 | + nested_d1["another"] = false; |
| 247 | + nested_d2["level1"] = inner_d2; |
| 248 | + nested_d2["another"] = false; |
| 249 | + CHECK_MESSAGE(VariantUtilityFunctions::is_equal(nested_d1, nested_d2), "Dictionaries with equal nested dictionaries should be equal."); |
| 250 | + |
| 251 | + Dictionary nested_d3, inner_d3; |
| 252 | + inner_d3["sub_key"] = 10.0; |
| 253 | + nested_d3["level1"] = inner_d3; |
| 254 | + nested_d3["another"] = false; |
| 255 | + CHECK_FALSE_MESSAGE(VariantUtilityFunctions::is_equal(nested_d1, nested_d3), "Dictionaries with unequal nested dictionaries should not be equal."); |
| 256 | + } |
| 257 | + |
| 258 | + SUBCASE("Objects") { |
| 259 | + Variant null_obj1; |
| 260 | + Variant null_obj2; |
| 261 | + CHECK_MESSAGE(VariantUtilityFunctions::is_equal(null_obj1, null_obj2), "Two null object Variants should be equal."); |
| 262 | + |
| 263 | + Node *node1 = memnew(Node); |
| 264 | + Node *node2 = memnew(Node); |
| 265 | + Node *node3 = memnew(Node); |
| 266 | + |
| 267 | + CHECK_FALSE_MESSAGE(VariantUtilityFunctions::is_equal(null_obj1, node1), "A null object Variant and a non-null object Variant should not be equal."); |
| 268 | + CHECK_FALSE_MESSAGE(VariantUtilityFunctions::is_equal(node1, null_obj1), "A non-null object Variant and a null object Variant should not be equal."); |
| 269 | + |
| 270 | + CHECK_MESSAGE(VariantUtilityFunctions::is_equal(node1, node2), "Two distinct Node instances with default properties should be equal."); |
| 271 | + |
| 272 | + node1->set_name("TestNode"); |
| 273 | + node2->set_name("TestNode"); |
| 274 | + CHECK_MESSAGE(VariantUtilityFunctions::is_equal(node1, node2), "Two Node instances with the same property value should be equal."); |
| 275 | + |
| 276 | + node3->set_name("AnotherNode"); |
| 277 | + CHECK_FALSE_MESSAGE(VariantUtilityFunctions::is_equal(node1, node3), "Two Node instances with different property values should not be equal."); |
| 278 | + |
| 279 | + Object *base_obj1_ptr = memnew(Object); |
| 280 | + Object *base_obj2_ptr = memnew(Object); |
| 281 | + Variant v_base_obj1 = base_obj1_ptr; |
| 282 | + Variant v_base_obj2 = base_obj2_ptr; |
| 283 | + |
| 284 | + CHECK_MESSAGE(VariantUtilityFunctions::is_equal(v_base_obj1, v_base_obj2), "Two distinct base Object instances should be equal if properties match."); |
| 285 | + |
| 286 | + base_obj1_ptr->set_meta("info", "data1"); |
| 287 | + base_obj2_ptr->set_meta("info", "data1"); |
| 288 | + CHECK_MESSAGE(VariantUtilityFunctions::is_equal(v_base_obj1, v_base_obj2), "Two objects with the same meta values should be equal."); |
| 289 | + |
| 290 | + base_obj2_ptr->set_meta("info", "data2"); |
| 291 | + CHECK_FALSE_MESSAGE(VariantUtilityFunctions::is_equal(v_base_obj1, v_base_obj2), "Two objects with different meta values should not be equal."); |
| 292 | + |
| 293 | + memdelete(base_obj1_ptr); |
| 294 | + memdelete(base_obj2_ptr); |
| 295 | + } |
| 296 | + |
| 297 | + SUBCASE("Mixed Recursive Types") { |
| 298 | + Array mixed_a1, mixed_a2; |
| 299 | + Dictionary mixed_d1a, mixed_d1b; |
| 300 | + mixed_d1a["value"] = 50; |
| 301 | + mixed_d1b["value"] = 50; |
| 302 | + mixed_a1.append(mixed_d1a); |
| 303 | + mixed_a1.append("common"); |
| 304 | + mixed_a2.append(mixed_d1b); |
| 305 | + mixed_a2.append("common"); |
| 306 | + CHECK_MESSAGE(VariantUtilityFunctions::is_equal(mixed_a1, mixed_a2), "Arrays containing equal dictionaries should be equal."); |
| 307 | + |
| 308 | + Array mixed_a3; |
| 309 | + Dictionary mixed_d1c; |
| 310 | + mixed_d1c["value"] = 51; |
| 311 | + mixed_a3.append(mixed_d1c); |
| 312 | + mixed_a3.append("common"); |
| 313 | + CHECK_FALSE_MESSAGE(VariantUtilityFunctions::is_equal(mixed_a1, mixed_a3), "Arrays containing unequal dictionaries should not be equal."); |
| 314 | + |
| 315 | + Dictionary mixed_d2a, mixed_d2b; |
| 316 | + Array mixed_a1a, mixed_a1b; |
| 317 | + mixed_a1a.append(true); |
| 318 | + mixed_a1b.append(true); |
| 319 | + mixed_d2a["list"] = mixed_a1a; |
| 320 | + mixed_d2a["id"] = 1; |
| 321 | + mixed_d2b["list"] = mixed_a1b; |
| 322 | + mixed_d2b["id"] = 1; |
| 323 | + CHECK_MESSAGE(VariantUtilityFunctions::is_equal(mixed_d2a, mixed_d2b), "Dictionaries containing equal arrays should be equal."); |
| 324 | + |
| 325 | + Dictionary mixed_d2c; |
| 326 | + Array mixed_a1c; |
| 327 | + mixed_a1c.append(false); |
| 328 | + mixed_d2c["list"] = mixed_a1c; |
| 329 | + mixed_d2c["id"] = 1; |
| 330 | + CHECK_FALSE_MESSAGE(VariantUtilityFunctions::is_equal(mixed_d2a, mixed_d2c), "Dictionaries containing unequal arrays should not be equal."); |
| 331 | + } |
| 332 | +} |
| 333 | + |
131 | 334 | } // namespace TestVariantUtility |
0 commit comments