Permalink
Browse files

finish file spec and update code

  • Loading branch information...
1 parent 77bdc4b commit 3fc40ac10b375eee7a1d67703d810d6ce8ac2e15 @avalanche123 committed May 28, 2012
Showing with 150 additions and 65 deletions.
  1. +60 −65 lib/uv/file.rb
  2. +90 −0 spec/uv/file_spec.rb
View
125 lib/uv/file.rb
@@ -42,98 +42,93 @@ def write(data, offset = 0, &block)
@write_block = block
@write_buffer_length = data.respond_to?(:bytesize) ? data.bytesize : data.size
@write_buffer = FFI::MemoryPointer.from_string(data)
- check_result! UV.fs_write(
- loop.to_ptr,
- UV.create_request(:uv_fs),
- @fd,
- @write_buffer,
- @write_buffer_length,
- offset,
- callback(:on_write)
- )
+
+ check_result! UV.fs_write(loop.to_ptr, UV.create_request(:uv_fs), @fd, @write_buffer, @write_buffer_length, offset, callback(:on_write))
+
+ self
end
def stat(&block)
- raise ArgumentError, "no block given", caller unless block_given?
+ assert_block(block)
+ assert_arity(2, block)
+
@stat_block = block
- check_result! UV.fs_fstat(
- loop.to_ptr,
- UV.create_request(:uv_fs),
- @fd,
- callback(:on_stat)
- )
+
+ check_result! UV.fs_fstat(loop.to_ptr, UV.create_request(:uv_fs), @fd, callback(:on_stat))
+
+ self
end
def sync(&block)
- raise ArgumentError, "no block given", caller unless block_given?
+ assert_block(block)
+ assert_arity(1, block)
+
@sync_block = block
- check_result! UV.fs_fsync(
- loop.to_ptr,
- UV.create_request(:uv_fs),
- @fd,
- callback(:on_sync)
- )
+
+ check_result! UV.fs_fsync(loop.to_ptr, UV.create_request(:uv_fs), @fd, callback(:on_sync))
+
+ self
end
def datasync(&block)
- raise ArgumentError, "no block given", caller unless block_given?
+ assert_block(block)
+ assert_arity(1, block)
+
@datasync_block = block
- check_result! UV.fs_fdatasync(
- loop.to_ptr,
- UV.create_request(:uv_fs),
- @fd,
- callback(:on_datasync)
- )
+
+ check_result! UV.fs_fdatasync(loop.to_ptr, UV.create_request(:uv_fs), @fd, callback(:on_datasync))
+
+ self
end
def truncate(offset, &block)
- raise ArgumentError, "no block given", caller unless block_given?
+ assert_block(block)
+ assert_arity(1, block)
+ assert_type(Integer, offset, "offset must be an Integer")
+
@truncate_block = block
- check_result! UV.fs_ftruncate(
- loop.to_ptr,
- UV.create_request(:uv_fs),
- @fd,
- Integer(offset),
- callback(:on_truncate)
- )
+
+ check_result! UV.fs_ftruncate(loop.to_ptr, UV.create_request(:uv_fs), @fd, offset, callback(:on_truncate))
+
+ self
end
def utime(atime, mtime, &block)
- raise ArgumentError, "no block given", caller unless block_given?
+ assert_block(block)
+ assert_arity(1, block)
+ assert_type(Integer, atime, "atime must be an Integer")
+ assert_type(Integer, mtime, "mtime must be an Integer")
+
@utime_block = block
- check_result! UV.fs_futime(
- loop.to_ptr,
- UV.create_request(:uv_fs),
- @fd,
- Integer(atime),
- Integer(mtime),
- callback(:on_utime)
- )
+
+ check_result! UV.fs_futime(loop.to_ptr, UV.create_request(:uv_fs), @fd, atime, mtime, callback(:on_utime))
+
+ self
end
def chmod(mode, &block)
- raise ArgumentError, "no block given", caller unless block_given?
+ assert_block(block)
+ assert_arity(1, block)
+ assert_type(Integer, mode, "mode must be an Integer")
+
@chmod_block = block
- check_result! UV.fs_fchmod(
- loop.to_ptr,
- UV.create_request(:uv_fs),
- @fd,
- Integer(mode),
- callback(:on_chmod)
- )
+
+ check_result! UV.fs_fchmod(loop.to_ptr, UV.create_request(:uv_fs), @fd, mode, callback(:on_chmod))
+
+ self
end
def chown(uid, gid, &block)
- raise ArgumentError, "no block given", caller unless block_given?
+ assert_block(block)
+ assert_arity(1, block)
+ assert_type(Integer, uid, "uid must be an Integer")
+ assert_type(Integer, gid, "gid must be an Integer")
+
@chown_block = block
- check_result! UV.fs_fchown(
- loop.to_ptr,
- UV.create_request(:uv_fs),
- @fd,
- Integer(uid),
- Integer(gid),
- callback(:on_chown)
- )
+
+ check_result! UV.fs_fchown(loop.to_ptr, UV.create_request(:uv_fs), @fd, uid, gid, callback(:on_chown))
+
+ self
end
private
@@ -176,9 +171,9 @@ def on_stat(req)
stat = Stat.new(*values)
end
- @stat_block.call(e, stat)
UV.fs_req_cleanup(req)
UV.free(req)
+ @stat_block.call(e, stat)
end
def on_sync(req)
View
90 spec/uv/file_spec.rb
@@ -65,23 +65,113 @@
end
describe "#stat" do
+ let(:stat_request) { double() }
+
+ it "requires a block" do
+ expect { subject.stat }.to raise_error(ArgumentError)
+ end
+
+ it "calls UV.fs_fstat" do
+ UV.should_receive(:create_request).with(:uv_fs).and_return(stat_request)
+ UV.should_receive(:fs_fstat).with(loop_pointer, stat_request, fd, subject.method(:on_stat))
+
+ subject.stat { |e, stat| }
+ end
end
describe "#sync" do
+ let(:sync_request) { double() }
+
+ it "requires a block" do
+ expect { subject.sync }.to raise_error(ArgumentError)
+ end
+
+ it "calls UV.fs_fsync" do
+ UV.should_receive(:create_request).with(:uv_fs).and_return(sync_request)
+ UV.should_receive(:fs_fsync).with(loop_pointer, sync_request, fd, subject.method(:on_sync))
+
+ subject.sync { |e| }
+ end
end
describe "#datasync" do
+ let(:datasync_request) { double() }
+
+ it "requires a block" do
+ expect { subject.datasync }.to raise_error(ArgumentError)
+ end
+
+ it "calls UV.fs_fdatasync" do
+ UV.should_receive(:create_request).with(:uv_fs).and_return(datasync_request)
+ UV.should_receive(:fs_fdatasync).with(loop_pointer, datasync_request, fd, subject.method(:on_datasync))
+
+ subject.datasync { |e| }
+ end
end
describe "#truncate" do
+ let(:offset) { 0 }
+ let(:truncate_request) { double() }
+
+ it "requires a block" do
+ expect { subject.truncate(offset) }.to raise_error(ArgumentError)
+ end
+
+ it "calls UV.fs_ftruncate" do
+ UV.should_receive(:create_request).with(:uv_fs).and_return(truncate_request)
+ UV.should_receive(:fs_ftruncate).with(loop_pointer, truncate_request, fd, offset, subject.method(:on_truncate))
+
+ subject.truncate(offset) { |e| }
+ end
end
describe "#utime" do
+ let(:atime) { 1291404900 } # 2010-12-03 20:35:00
+ let(:mtime) { 400497753 } # 1982-09-10 11:22:33
+ let(:utime_request) { double() }
+
+ it "requires a block" do
+ expect { subject.utime(atime, mtime) }.to raise_error(ArgumentError)
+ end
+
+ it "calls UV.fs_futime" do
+ UV.should_receive(:create_request).with(:uv_fs).and_return(utime_request)
+ UV.should_receive(:fs_futime).with(loop_pointer, utime_request, fd, atime, mtime, subject.method(:on_utime))
+
+ subject.utime(atime, mtime) { |e| }
+ end
end
describe "#chmod" do
+ let(:mode) { 0755 }
+ let(:chmod_request) { double() }
+
+ it "requires a block" do
+ expect { subject.chmod(mode) }.to raise_error(ArgumentError)
+ end
+
+ it "calls UV.fs_fchmod" do
+ UV.should_receive(:create_request).with(:uv_fs).and_return(chmod_request)
+ UV.should_receive(:fs_fchmod).with(loop_pointer, chmod_request, fd, mode, subject.method(:on_chmod))
+
+ subject.chmod(mode) { |e| }
+ end
end
describe "#chown" do
+ let(:uid) { 0 }
+ let(:gid) { 0 }
+ let(:chown_request) { double() }
+
+ it "requires a block" do
+ expect { subject.chown(uid, gid) }.to raise_error(ArgumentError)
+ end
+
+ it "calls UV.fs_fchown" do
+ UV.should_receive(:create_request).with(:uv_fs).and_return(chown_request)
+ UV.should_receive(:fs_fchown).with(loop_pointer, chown_request, fd, uid, gid, subject.method(:on_chown))
+
+ subject.chown(uid, gid) { |e| }
+ end
end
end

0 comments on commit 3fc40ac

Please sign in to comment.