From 0097c7f3887ad8dd5493cb3696ac6368dc4a1014 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 13 Sep 2022 08:27:59 -0700 Subject: [ruby/fiddle] Add `sym_defined?` methods to test if a symbol is defined (https://github.com/ruby/fiddle/pull/108) I would like to check if a symbol is defined before trying to access it. Some symbols aren't available on all platforms, so instead of raising an exception, I want to check if it's defined first. Today we have to do: ```ruby begin addr = Fiddle::Handle.sym("something") # do something rescue Fiddle::DLError end ``` I want to write this: ```ruby if Fiddle::Handle.sym_defined?("something") addr = Fiddle::Handle.sym("something") # do something end ``` https://github.com/ruby/fiddle/commit/9d3371de13 Co-authored-by: Sutou Kouhei --- test/fiddle/test_handle.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'test') diff --git a/test/fiddle/test_handle.rb b/test/fiddle/test_handle.rb index 7e3ff9d844..3bb80b75e2 100644 --- a/test/fiddle/test_handle.rb +++ b/test/fiddle/test_handle.rb @@ -22,12 +22,14 @@ module Fiddle def test_static_sym_unknown assert_raise(DLError) { Fiddle::Handle.sym('fooo') } assert_raise(DLError) { Fiddle::Handle['fooo'] } + refute Fiddle::Handle.sym_defined?('fooo') end def test_static_sym begin # Linux / Darwin / FreeBSD refute_nil Fiddle::Handle.sym('dlopen') + assert Fiddle::Handle.sym_defined?('dlopen') assert_equal Fiddle::Handle.sym('dlopen'), Fiddle::Handle['dlopen'] return rescue @@ -54,6 +56,7 @@ module Fiddle handle = Fiddle::Handle.new(LIBC_SO) assert_raise(DLError) { handle.sym('fooo') } assert_raise(DLError) { handle['fooo'] } + refute handle.sym_defined?('fooo') end def test_sym_with_bad_args @@ -66,6 +69,7 @@ module Fiddle handle = Handle.new(LIBC_SO) refute_nil handle.sym('calloc') refute_nil handle['calloc'] + assert handle.sym_defined?('calloc') end def test_handle_close -- cgit v1.2.3