| @@ -284,3 +284,119 @@ |
| 284 | 284 | # A function-valued context binding takes precedence over a built-in of the |
| 285 | 285 | # same name (host override; ADR-23). |
| 286 | 286 | assert feel.evaluate("count(1, 2)", {"count": lambda a, b: a * b}) == 2 |
| 287 | + |
| 288 | + |
| 289 | +# --- `instance of`: structural shape checks over Python objects (ADR-26) -- # |
| 290 | +# |
| 291 | +# FEEL is *structurally* typed (like TypeScript): `value instance of context<…>` |
| 292 | +# asserts the value has *at least* the named fields with conforming types — it does |
| 293 | +# NOT consult the Python class name (that would be `isinstance`, which is nominal and |
| 294 | +# non-portable). So `instance of` is a host-agnostic "duck-typed shape" assertion. |
| 295 | + |
| 296 | +def test_instance_of_builtin_types(): |
| 297 | + # Over native values, against the built-in types. |
| 298 | + assert feel.evaluate('"foo" instance of string') is True |
| 299 | + assert feel.evaluate("42 instance of number") is True |
| 300 | + assert feel.evaluate("42 instance of string") is False |
| 301 | + # A null is an instance only of `Null`; a type that can't be mapped -> null. |
| 302 | + assert feel.evaluate("null instance of Null") is True |
| 303 | + assert feel.evaluate("null instance of string") is False |
| 304 | + assert feel.evaluate("42 instance of list") is None # bare `list` needs a parameter |
| 305 | + |
| 306 | + |
| 307 | +def test_instance_of_checks_dict_shape(): |
| 308 | + # The common case: a JSON-like dict. Width subtyping — extra keys are fine. |
| 309 | + order = {"id": 7, "customer": "Ada"} |
| 310 | + assert feel.evaluate("o instance of context<customer: string>", {"o": order}) is True |
| 311 | + assert feel.evaluate( |
| 312 | + "o instance of context<id: number, customer: string>", {"o": order} |
| 313 | + ) is True |
| 314 | + assert feel.evaluate("o instance of context<customer: number>", {"o": order}) is False |
| 315 | + assert feel.evaluate("o instance of context<missing: string>", {"o": order}) is False |
| 316 | + |
| 317 | + |
| 318 | +def test_instance_of_checks_object_shape(): |
| 319 | + # A class instance is materialised from its instance ``__dict__`` for the check, |
| 320 | + # so a plain dataclass works exactly like the dict above. |
| 321 | + from dataclasses import dataclass |
| 322 | + |
| 323 | + @dataclass |
| 324 | + class Person: |
| 325 | + name: str |
| 326 | + age: int |
| 327 | + |
| 328 | + p = Person("Ada", 36) |
| 329 | + assert feel.evaluate("p instance of context<name: string>", {"p": p}) is True |
| 330 | + assert feel.evaluate( |
| 331 | + "p instance of context<name: string, age: number>", {"p": p} |
| 332 | + ) is True |
| 333 | + assert feel.evaluate("p instance of context<age: number>", {"p": p}) is True |
| 334 | + assert feel.evaluate("p instance of context<name: number>", {"p": p}) is False |
| 335 | + |
| 336 | + |
| 337 | +def test_instance_of_sees_instance_data_not_properties(): |
| 338 | + # CAVEAT: the shape check materialises the instance ``__dict__`` (real data |
| 339 | + # attributes). A computed ``@property`` is *not* part of that view — even though |
| 340 | + # the FEEL dot (``r.doubled``) *does* fire it. The two paths differ on purpose: |
| 341 | + # bulk materialisation for `instance of` vs. lazy attribute access for the dot. |
| 342 | + class Row: |
| 343 | + def __init__(self): |
| 344 | + self.total = 10 |
| 345 | + |
| 346 | + @property |
| 347 | + def doubled(self): |
| 348 | + return self.total * 2 |
| 349 | + |
| 350 | + r = Row() |
| 351 | + assert feel.evaluate("r.doubled", {"r": r}) == 20 # the dot fires the property |
| 352 | + assert feel.evaluate("r instance of context<total: number>", {"r": r}) is True |
| 353 | + # The property is invisible to the structural check: |
| 354 | + assert feel.evaluate("r instance of context<doubled: number>", {"r": r}) is False |
| 355 | + |
| 356 | + |
| 357 | +def test_instance_of_slots_object_is_not_introspectable(): |
| 358 | + # CAVEAT: a ``__slots__`` object has no ``__dict__``, so it cannot be materialised |
| 359 | + # into a context — the predicate is False (not an error). Lazy dot access still |
| 360 | + # works. |
| 361 | + class Point: |
| 362 | + __slots__ = ("x", "y") |
| 363 | + |
| 364 | + def __init__(self, x, y): |
| 365 | + self.x = x |
| 366 | + self.y = y |
| 367 | + |
| 368 | + pt = Point(1, 2) |
| 369 | + assert feel.evaluate("pt.x", {"pt": pt}) == 1 # the dot still works |
| 370 | + assert feel.evaluate("pt instance of context<x: number>", {"pt": pt}) is False |
| 371 | + |
| 372 | + |
| 373 | +def test_instance_of_field_may_be_null(): |
| 374 | + # A *missing* field fails the check, but a field that is present and ``None`` |
| 375 | + # conforms to any type — in the FEEL lattice ``Null`` is a subtype of every type. |
| 376 | + # So this asserts "has a ``name`` slot (possibly null) that, when non-null, is a |
| 377 | + # string", not "has a non-null string name". |
| 378 | + class Person: |
| 379 | + def __init__(self, name): |
| 380 | + self.name = name |
| 381 | + |
| 382 | + assert feel.evaluate("p instance of context<name: string>", {"p": Person(None)}) is True |
| 383 | + assert feel.evaluate("p instance of context<name: string>", {"p": Person("Ada")}) is True |
| 384 | + |
| 385 | + |
| 386 | +def test_instance_of_nested_and_list_shapes(): |
| 387 | + # Generics compose: ``list<…>`` over elements and nested ``context<…>``. |
| 388 | + people = [{"name": "Ada"}, {"name": "Grace"}] |
| 389 | + assert feel.evaluate( |
| 390 | + "ps instance of list<context<name: string>>", {"ps": people} |
| 391 | + ) is True |
| 392 | + # One element of the wrong shape fails the whole list. |
| 393 | + assert feel.evaluate( |
| 394 | + "ps instance of list<context<name: number>>", {"ps": people} |
| 395 | + ) is False |
| 396 | + nested = {"owner": {"name": "Ada"}} |
| 397 | + assert feel.evaluate( |
| 398 | + "c instance of context<owner: context<name: string>>", {"c": nested} |
| 399 | + ) is True |
| 400 | + assert feel.evaluate( |
| 401 | + "c instance of context<owner: context<name: number>>", {"c": nested} |
| 402 | + ) is False |