|
| 1 | +require 'jsii' |
| 2 | + |
| 3 | +describe Jsii::Kernel::Callbacks do |
| 4 | + let(:callbacks_class) do |
| 5 | + Class.new do |
| 6 | + include Jsii::Kernel::Callbacks |
| 7 | + end |
| 8 | + end |
| 9 | + let(:callbacks) { callbacks_class.new } |
| 10 | + |
| 11 | + before do |
| 12 | + allow(Jsii::Kernel).to receive(:instance).and_return(double('kernel')) |
| 13 | + end |
| 14 | + |
| 15 | + describe '#handle_callback_invoke' do |
| 16 | + it 'escapes reserved words in method names correctly' do |
| 17 | + objref = 'mock@123' |
| 18 | + instance = double('JsiiObject') |
| 19 | + allow(Jsii::Object).to receive(:find_by_ref).with(objref).and_return(instance) |
| 20 | + |
| 21 | + invoke_req = { |
| 22 | + 'objref' => objref, |
| 23 | + 'method' => 'send', # JS name is 'send' |
| 24 | + 'args' => [] |
| 25 | + } |
| 26 | + |
| 27 | + # We expect __send__ to be called with '_send' as the method name |
| 28 | + expect(instance).to receive(:__send__).with('_send') |
| 29 | + |
| 30 | + callbacks.send(:handle_callback_invoke, invoke_req) |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + describe '#handle_callback_get' do |
| 35 | + it 'escapes reserved words in property names correctly' do |
| 36 | + objref = 'mock@123' |
| 37 | + instance = double('JsiiObject') |
| 38 | + allow(Jsii::Object).to receive(:find_by_ref).with(objref).and_return(instance) |
| 39 | + |
| 40 | + get_req = { |
| 41 | + 'objref' => objref, |
| 42 | + 'property' => 'class' # JS name is 'class' |
| 43 | + } |
| 44 | + |
| 45 | + expect(instance).to receive(:__send__).with('_class') |
| 46 | + callbacks.send(:handle_callback_get, get_req) |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + describe '#handle_callback_set' do |
| 51 | + it 'escapes reserved words in property setters correctly' do |
| 52 | + objref = 'mock@123' |
| 53 | + instance = double('JsiiObject') |
| 54 | + allow(Jsii::Object).to receive(:find_by_ref).with(objref).and_return(instance) |
| 55 | + |
| 56 | + set_req = { |
| 57 | + 'objref' => objref, |
| 58 | + 'property' => 'class', # JS name is 'class' |
| 59 | + 'value' => 'new_value' |
| 60 | + } |
| 61 | + |
| 62 | + expect(instance).to receive(:__send__).with('_class=', 'new_value') |
| 63 | + callbacks.send(:handle_callback_set, set_req) |
| 64 | + end |
| 65 | + end |
| 66 | +end |
0 commit comments