|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +# Suite tests: abstractMembersAreCorrectlyHandled, returnAbstract, |
| 6 | +# unmarshallIntoAbstractType. |
| 7 | +# |
| 8 | +# Abstractness only exists at the TypeScript level — on the wire there are |
| 9 | +# just object refs and member names, and the Ruby proxy generated for an |
| 10 | +# abstract class has a concrete forwarding stub for *every* member (abstract |
| 11 | +# or not). These tests verify both directions: a guest subclass supplying |
| 12 | +# the abstract members to a host driver (abstractMembersAreCorrectlyHandled, |
| 13 | +# via callbacks), and the kernel returning values *declared* as abstract |
| 14 | +# types (returnAbstract, unmarshallIntoAbstractType). |
| 15 | +RSpec.describe 'JSII compliance: abstract types' do |
| 16 | + it 'handles abstract properties and methods correctly', compliance: 'abstractMembersAreCorrectlyHandled' do |
| 17 | + klass = Class.new(JsiiCalc::AbstractSuite) do |
| 18 | + def initialize |
| 19 | + super |
| 20 | + @property_val = nil |
| 21 | + end |
| 22 | + |
| 23 | + def some_method(str) |
| 24 | + "Wrapped<#{str}>" |
| 25 | + end |
| 26 | + |
| 27 | + def property |
| 28 | + @property_val |
| 29 | + end |
| 30 | + |
| 31 | + def property=(value) |
| 32 | + @property_val = "String<#{value}>" |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + abstract_suite = klass.new |
| 37 | + expect(abstract_suite.work_it_all('Oomf!')).to eq('Wrapped<String<Oomf!>>') |
| 38 | + end |
| 39 | + |
| 40 | + # The TS fixture (jsii-calc/lib/compliance.ts) behind this test: |
| 41 | + # |
| 42 | + # export abstract class AbstractClassBase { abstract readonly abstractProperty: string; } |
| 43 | + # export abstract class AbstractClass extends AbstractClassBase |
| 44 | + # implements IInterfaceImplementedByAbstractClass { ... } |
| 45 | + # class ConcreteClass extends AbstractClass { ... } // NOT exported! |
| 46 | + # export class AbstractClassReturner { |
| 47 | + # giveMeAbstract(): AbstractClass { return new ConcreteClass(); } |
| 48 | + # giveMeInterface(): IInterfaceImplementedByAbstractClass { return new ConcreteClass(); } |
| 49 | + # get returnAbstractFromProperty(): AbstractClassBase { |
| 50 | + # return { abstractProperty: 'hello-abstract-property' }; // plain object literal! |
| 51 | + # } |
| 52 | + # } |
| 53 | + # |
| 54 | + # This exercises three progressively nastier hydration cases — see the |
| 55 | + # inline comments. In all of them the key trick is that |
| 56 | + # Registry#build_uninitialized_instance hydrates refs with `klass.allocate` |
| 57 | + # (NOT `klass.new`), so no constructor runs and "you cannot instantiate an |
| 58 | + # abstract class" never comes up. |
| 59 | + it 'returns abstract classes and interfaces from the kernel', compliance: 'returnAbstract' do |
| 60 | + obj = JsiiCalc::AbstractClassReturner.new |
| 61 | + |
| 62 | + # Case 1: the kernel returns an instance of *unexported* ConcreteClass, |
| 63 | + # declared as the *abstract* AbstractClass. ConcreteClass isn't in the |
| 64 | + # assembly, so the kernel labels the ref with the nearest exported |
| 65 | + # ancestor: `$jsii.byref: "jsii-calc.AbstractClass@..."`. The registry |
| 66 | + # resolves that fqn to the generated JsiiCalc::AbstractClass proxy and |
| 67 | + # allocates it without calling initialize. |
| 68 | + obj2 = obj.give_me_abstract |
| 69 | + |
| 70 | + # Calls through the abstract proxy are plain virtual dispatch in JS: the |
| 71 | + # kernel invokes the member on the *real* object, so ConcreteClass's |
| 72 | + # implementation of the abstract method runs... |
| 73 | + expect(obj2.abstract_method('John')).to eq('Hello, John!!') |
| 74 | + # ...the interface-provided getter on AbstractClass answers a `get`... |
| 75 | + expect(obj2.prop_from_interface).to eq('propFromInterfaceValue') |
| 76 | + # ...and the inherited non-abstract base implementation works too. |
| 77 | + expect(obj2.non_abstract_method).to eq(42) |
| 78 | + |
| 79 | + # Case 2: same ConcreteClass instance, but the declared return type is a |
| 80 | + # pure interface. Interface fqns register as Ruby *modules*, which can't |
| 81 | + # be allocated — the registry builds `Jsii::Object.allocate.extend(mod)` |
| 82 | + # instead, so the module's forwarding stubs (and is_a? checks) work. |
| 83 | + iface = obj.give_me_interface |
| 84 | + expect(iface.prop_from_interface).to eq('propFromInterfaceValue') |
| 85 | + |
| 86 | + # Case 3: the JS getter returns a plain `{ abstractProperty: ... }` |
| 87 | + # object literal — no class at all. The kernel still wraps it in a ref, |
| 88 | + # but labels it "Object"; Registry#jsii_deserialize's fallback chain |
| 89 | + # (`$jsii.interfaces`.first, then the ref's fqn prefix) picks the declared |
| 90 | + # abstract type so the property read can be forwarded. |
| 91 | + expect(obj.return_abstract_from_property.abstract_property).to eq('hello-abstract-property') |
| 92 | + end |
| 93 | + |
| 94 | + it 'unmarshalls values into abstract types', compliance: 'unmarshallIntoAbstractType' do |
| 95 | + calc = JsiiCalc::Calculator.new |
| 96 | + calc.add(120) |
| 97 | + expect(calc.curr.value).to eq(120) |
| 98 | + end |
| 99 | + |
| 100 | + describe 'abstract types (extended)' do |
| 101 | + # The jsii-relevant part of subclassing an abstract host class is |
| 102 | + # construction: the kernel must instantiate an *abstract* fqn on behalf |
| 103 | + # of the guest by synthesizing a JS subclass, with the guest's overrides |
| 104 | + # registered (see object/overrides.rb). Calling the overridden members |
| 105 | + # from Ruby would only exercise plain Ruby dispatch — host-driven |
| 106 | + # callback dispatch is covered by abstractMembersAreCorrectlyHandled |
| 107 | + # above — so instead we call the members the guest did NOT override: |
| 108 | + # those go through the generated forwarding stubs to the kernel, proving |
| 109 | + # the instance is live on the JS side. |
| 110 | + it 'can construct native subclasses of abstract host classes' do |
| 111 | + klass = Class.new(JsiiCalc::AbstractClass) do |
| 112 | + def abstract_method(name) |
| 113 | + "Hello, #{name}!" |
| 114 | + end |
| 115 | + |
| 116 | + def abstract_property |
| 117 | + 'native-abstract-property' |
| 118 | + end |
| 119 | + end |
| 120 | + |
| 121 | + obj = klass.new |
| 122 | + expect(obj.jsii_ref).not_to be_nil |
| 123 | + |
| 124 | + # Non-overridden members dispatch through the kernel to the host |
| 125 | + # implementations on AbstractClass. |
| 126 | + expect(obj.non_abstract_method).to eq(42) |
| 127 | + expect(obj.prop_from_interface).to eq('propFromInterfaceValue') |
| 128 | + end |
| 129 | + end |
| 130 | +end |
0 commit comments