From 0a23aec48fa09e82bfbf5df6c784024dc0e69b37 Mon Sep 17 00:00:00 2001 From: leftibot Date: Fri, 10 Apr 2026 18:39:35 -0600 Subject: [PATCH] Add tests for passing derived objects to functions expecting Base Tests cover: free functions calling base methods on derived objects, polymorphic dispatch through containers, base attribute access on derived objects, and multi-level inheritance (GrandChild : Derived : Base). Co-Authored-By: Claude Opus 4.6 (1M context) --- unittests/class_inheritance.chai | 64 ++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/unittests/class_inheritance.chai b/unittests/class_inheritance.chai index 9b222950..64ab621a 100644 --- a/unittests/class_inheritance.chai +++ b/unittests/class_inheritance.chai @@ -58,3 +58,67 @@ class Derived2 : Base auto d2 = Derived2() assert_equal(500, d2.do_something()) + +// Test passing a derived object to a free function that calls base methods +def call_do_something(obj) { + return obj.do_something() +} + +auto d3 = Derived() +assert_equal(20, call_do_something(d3)) + +// Base still works through the same free function +auto b2 = Base() +assert_equal(20, call_do_something(b2)) + +// Test passing derived to a function that accesses base attributes +def get_x(obj) { + return obj.x +} + +assert_equal(10, get_x(d3)) +assert_equal(10, get_x(b2)) + +// Test polymorphic behavior: override is called for Derived2 +auto d4 = Derived2() +assert_equal(500, call_do_something(d4)) + +// Test mixing base and derived in a container, calling base methods +var objects = [Base(), Derived(), Derived2()] +assert_equal(20, objects[0].do_something()) +assert_equal(20, objects[1].do_something()) +assert_equal(500, objects[2].do_something()) + +// Test that derived objects still report correct type +auto d5 = Derived() +assert_true(d5.is_type("Derived")) + +// Test multi-level inheritance +class GrandChild : Derived +{ + attr z + + def GrandChild() + { + this.x = 1 + this.y = 2 + this.z = 3 + } + + def do_grandchild() + { + return this.z * 4 + } +} + +auto gc = GrandChild() +assert_equal(1, gc.x) +assert_equal(2, gc.y) +assert_equal(3, gc.z) +assert_equal(2, gc.do_something()) // Base method +assert_equal(6, gc.do_other()) // Derived method +assert_equal(12, gc.do_grandchild()) // Own method + +// Test passing grandchild to function that uses base methods +assert_equal(2, call_do_something(gc)) +assert_equal(1, get_x(gc))