diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/ffi/function_spec.rb | 2 | ||||
-rw-r--r-- | spec/ffi/library_spec.rb | 15 | ||||
-rw-r--r-- | spec/ffi/struct_by_ref_spec.rb | 5 | ||||
-rw-r--r-- | spec/ffi/variadic_spec.rb | 29 |
4 files changed, 50 insertions, 1 deletions
diff --git a/spec/ffi/function_spec.rb b/spec/ffi/function_spec.rb index 0d89a7d..badd240 100644 --- a/spec/ffi/function_spec.rb +++ b/spec/ffi/function_spec.rb @@ -64,7 +64,7 @@ describe FFI::Function do end it "should be usable with Ractor", :ractor do - res = Ractor.new(@conninfo) do |conninfo| + res = Ractor.new do function_add = FFI::Function.new(:int, [:int, :int]) { |a, b| a + b } LibTest.testFunctionAdd(10, 10, function_add) end.take diff --git a/spec/ffi/library_spec.rb b/spec/ffi/library_spec.rb index f223d26..97790e5 100644 --- a/spec/ffi/library_spec.rb +++ b/spec/ffi/library_spec.rb @@ -193,6 +193,19 @@ describe "Library" do end expect(mod.bool_return_true).to be true end + + it "can reveal the function type" do + mod = Module.new do |m| + m.extend FFI::Library + ffi_lib File.expand_path(TestLibrary::PATH) + attach_function :bool_return_true, [ :string ], :bool + end + + fun = mod.attached_functions + expect(fun.keys).to eq(["bool_return_true"]) + expect(fun["bool_return_true"].param_types).to eq([FFI::Type::STRING]) + expect(fun["bool_return_true"].result_type).to eq(FFI::Type::BOOL) + end end def gvar_lib(name, type) @@ -320,6 +333,8 @@ describe "Library" do lib.gvar[:data] = i val = GlobalStruct.new(lib.get) expect(val[:data]).to eq(i) + + expect(lib.attached_variables).to eq({ gvar: GlobalStruct }) end end diff --git a/spec/ffi/struct_by_ref_spec.rb b/spec/ffi/struct_by_ref_spec.rb index 0858423..0f48fbb 100644 --- a/spec/ffi/struct_by_ref_spec.rb +++ b/spec/ffi/struct_by_ref_spec.rb @@ -39,5 +39,10 @@ describe FFI::Struct, ' by_ref' do expect { @api.struct_test(other_class.new) }.to raise_error(TypeError) end + + it "can reveal the mapped type converter" do + param_type = @api.attached_functions["struct_test"].param_types[0] + expect(param_type.converter).to be_a(FFI::StructByReference) + end end diff --git a/spec/ffi/variadic_spec.rb b/spec/ffi/variadic_spec.rb index f379ed4..fdaf31c 100644 --- a/spec/ffi/variadic_spec.rb +++ b/spec/ffi/variadic_spec.rb @@ -23,6 +23,8 @@ describe "Function with variadic arguments" do attach_function :testBlockingClose, [ :pointer ], :void attach_function :testCallbackVrDva, :testClosureVrDva, [ :double, :varargs ], :double attach_function :testCallbackVrILva, :testClosureVrILva, [ :int, :long, :varargs ], :long + + freeze end it "takes enum arguments" do @@ -37,6 +39,12 @@ describe "Function with variadic arguments" do expect(LibTest.pack_varargs2(buf, :c1, "ii", :int, :c3, :int, :c4)).to eq(:c2) end + it "can reveal its return and parameters" do + fun = LibTest.attached_functions["testBlockingWRva"] + expect(fun.param_types).to eq([FFI::Type::POINTER, FFI::Type::CHAR, FFI::Type::VARARGS]) + expect(fun.result_type).to eq(FFI::Type::INT8) + end + it 'can wrap a blocking function with varargs' do handle = LibTest.testBlockingOpen expect(handle).not_to be_null @@ -87,12 +95,33 @@ describe "Function with variadic arguments" do expect(LibTest.testCallbackVrDva(3.0, :cbVrD, pr)).to be_within(0.0000001).of(45.0) end + it "can be called as instance method" do + kl = Class.new do + include LibTest + def call + pr = proc { 42.0 } + testCallbackVrDva(3.0, :cbVrD, pr) + end + end + expect(kl.new.call).to be_within(0.0000001).of(45.0) + end + it "call variadic with several callback arguments" do pr1 = proc { |i| i + 1 } pr2 = proc { |l| l + 2 } expect(LibTest.testCallbackVrILva(5, 6, :cbVrI, pr1, :cbVrL, pr2)).to eq(14) end + it "should be usable with Ractor", :ractor do + res = Ractor.new do + pr = proc { 42.0 } + LibTest.testCallbackVrDva(3.0, :cbVrD, pr) + end.take + + expect(res).to be_within(0.0000001).of(45.0) + end + + module Varargs PACK_VALUES = { 'c' => [ 0x12 ], |