Skip to content

Commit

Permalink
Ruby3: pass keyword arguments explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
YusukeIwaki committed Dec 24, 2020
1 parent a267ef5 commit ff15ae4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
21 changes: 15 additions & 6 deletions lib/puppeteer/define_async_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@ def define_async_method(async_method_name)
end

original_method = instance_method(async_method_name[6..-1])
define_method(async_method_name) do |*args|
Concurrent::Promises.future do
original_method.bind(self).call(*args)
rescue => err
Logger.new($stderr).warn(err)
raise err
define_method(async_method_name) do |*args, **kwargs|
if kwargs.empty? # for Ruby < 2.7
Concurrent::Promises.future do
original_method.bind(self).call(*args)
rescue => err
Logger.new($stderr).warn(err)
raise err
end
else
Concurrent::Promises.future do
original_method.bind(self).call(*args, **kwargs)
rescue => err
Logger.new($stderr).warn(err)
raise err
end
end
end
end
Expand Down
14 changes: 7 additions & 7 deletions spec/integration/launcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@
args: ['--headless'], # without --headless, test is blocked by welcome dialog
)

Puppeteer.launch(options) do |browser|
Puppeteer.launch(**options) do |browser|
page = browser.new_page
expect(page.evaluate('11 * 11')).to eq(121)
page.close
Expand All @@ -197,7 +197,7 @@
args: ['--headless'],
)

Puppeteer.launch(options) do |browser|
Puppeteer.launch(**options) do |browser|
args = browser.process.spawnargs
expect(args).not_to include(default_args[0])
expect(args).to include(default_args[1])
Expand All @@ -206,7 +206,7 @@
end

it 'should have default URL when launching browser' do
Puppeteer.launch(default_launch_options) do |browser|
Puppeteer.launch(**default_launch_options) do |browser|
pages = browser.pages.map(&:url)
expect(pages).to contain_exactly('about:blank')
end
Expand All @@ -217,7 +217,7 @@
options[:args] ||= []
options[:args] += ['http://example.com/empty.html']

Puppeteer.launch(options) do |browser|
Puppeteer.launch(**options) do |browser|
expect(browser.pages.size).to eq(1)

page = browser.pages.first
Expand All @@ -237,7 +237,7 @@
),
)

Puppeteer.launch(options) do |browser|
Puppeteer.launch(**options) do |browser|
page = browser.new_page
expect(page.evaluate('window.innerWidth')).to eq(456)
expect(page.evaluate('window.innerHeight')).to eq(789)
Expand All @@ -249,7 +249,7 @@
default_viewport: nil,
)

Puppeteer.launch(options) do |browser|
Puppeteer.launch(**options) do |browser|
page = browser.new_page
expect(page.viewport).to be_nil
end
Expand All @@ -267,7 +267,7 @@
default_viewport: nil,
)

Puppeteer.launch(options) do |browser|
Puppeteer.launch(**options) do |browser|
page = browser.new_page
page.goto('http://127.0.0.1:4567/longlong.html')
screenshot = page.screenshot(full_page: true)
Expand Down
5 changes: 4 additions & 1 deletion spec/puppeteer/cdp_session_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
describe '#send_message' do
before {
allow(connection).to receive(:generate_id) { |&block| block.call(SecureRandom.hex(16)) }
allow(connection).to receive(:raw_send) do |id:, message:|
allow(connection).to receive(:raw_send) do |kwargs|
id = kwargs[:id]
message = kwargs[:message]

Thread.new(id) do |message_id|
resp = {
'sessionId' => cdp_session_id,
Expand Down
24 changes: 24 additions & 0 deletions spec/puppeteer/define_async_method_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ def piyo
'-> piyo'
end
private define_async_method :async_piyo

def args_example(arg1, arg2 = nil)
"-> #{arg1},#{arg2}"
end
define_async_method :async_args_example

def keyword_args_example(arg1:, arg2: nil)
"-> #{arg1},#{arg2}"
end
define_async_method :async_keyword_args_example
end

it 'defined async method wrapped with Concurrent::Promises::Future' do
Expand All @@ -27,6 +37,20 @@ def piyo
expect(DefineAsyncMethodExample.method_defined?(:async_piyo)).to eq(false)
end

it 'works with arguments' do
instance = DefineAsyncMethodExample.new
expect(instance.async_args_example(:hoge)).to be_a(Concurrent::Promises::Future)
expect(instance.async_args_example(:hoge).value!).to eq('-> hoge,')
expect(instance.async_args_example(:hoge, :fuga).value!).to eq('-> hoge,fuga')
end

it 'works with keyword arguments' do
instance = DefineAsyncMethodExample.new
expect(instance.async_keyword_args_example(arg1: :hoge)).to be_a(Concurrent::Promises::Future)
expect(instance.async_keyword_args_example(arg1: :hoge).value!).to eq('-> hoge,')
expect(instance.async_keyword_args_example(arg1: :hoge, arg2: :fuga).value!).to eq('-> hoge,fuga')
end

it 'raises exception when async method name does not start with async_' do
expect {
class DefineAsyncMethodExample1
Expand Down

0 comments on commit ff15ae4

Please sign in to comment.