diff options
author | Aaron Patterson <[email protected]> | 2022-09-13 08:27:59 -0700 |
---|---|---|
committer | Hiroshi SHIBATA <[email protected]> | 2022-10-07 15:18:51 +0900 |
commit | 0097c7f3887ad8dd5493cb3696ac6368dc4a1014 (patch) | |
tree | 1dc23f79efaf436a1ba97550add50f6dee15848a /test/fiddle/test_handle.rb | |
parent | 755d99e8789b84a7d73d5a30f6b5d582e06f7f45 (diff) |
[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 <[email protected]>
Diffstat (limited to 'test/fiddle/test_handle.rb')
-rw-r--r-- | test/fiddle/test_handle.rb | 4 |
1 files changed, 4 insertions, 0 deletions
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 |