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