Skip to content

Commit

Permalink
Merge pull request ffi#662 from IgorJorobus/master
Browse files Browse the repository at this point in the history
Added specs for FFI::LastError.winapi_error()
  • Loading branch information
larskanis committed Jan 10, 2019
2 parents d976d4a + bba6858 commit 1ddc987
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
21 changes: 19 additions & 2 deletions spec/ffi/errno_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,28 @@ module LibTest
extend FFI::Library
ffi_lib TestLibrary::PATH
attach_function :setLastError, [ :int ], :void
attach_function :setErrno, [ :int ], :void
end

it "FFI.errno contains errno from last function" do
it "FFI.errno contains errno from last function, FFI::LastError.winapi_error works differently per OS" do
# setup
LibTest.setErrno(0)
LibTest.setLastError(0)
LibTest.setLastError(0x12345678)
expect(FFI.errno).to eq(0x12345678)
# cases
case FFI::Platform::OS
when "cygwin"
expect(FFI::LastError.winapi_error).to eq(0x12345678)
LibTest.setErrno(0x2A)
expect(FFI.errno).to eq(0x2A)
when "windows"
expect(FFI::LastError.winapi_error).to eq(0x12345678)
expect(FFI.errno).to eq(0x12345678)
else
# Linux, and else
expect {FFI::LastError.winapi_error}.to raise_exception(NoMethodError)
expect {FFI::LastError.winapi_error = 0}.to raise_exception(NoMethodError)
expect(FFI.errno).to eq(0x12345678)
end
end
end
8 changes: 5 additions & 3 deletions spec/ffi/fixtures/LastErrorTest.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
# include <errno.h>
#endif

int setLastError(int error) {
#if defined(_WIN32) || defined(__WIN32__)
void setLastError(int error) {
#if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
SetLastError(error);
#else
errno = error;
#endif
return -1;
}

void setErrno(int error) {
errno = error;
}
1 change: 1 addition & 0 deletions spec/ffi/fixtures/classes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ module TestEnum3
#
module LibTest
attach_function :setLastError, [ :int ], :void
attach_function :setErrno, [ :int ], :void
end

#
Expand Down

0 comments on commit 1ddc987

Please sign in to comment.