Skip to content

Commit

Permalink
Merge pull request #3 from quaternion/fix_file_bdecode
Browse files Browse the repository at this point in the history
Fix file decode
  • Loading branch information
blatyo committed Mar 23, 2012
2 parents dbf7436 + 3dbe519 commit 99f3568
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ You can also write and read bencodings.
# read from file
File.bdecode("a.bencode") #=> "string"

file = File.open("a.bencode", "wb")
file = File.open("a.bencode", "rb")
file.bdecode #=> "string"
```

Expand Down
4 changes: 2 additions & 2 deletions bencodr.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ Gem::Specification.new do |s|
s.description = "This gem provides a way to encode and decode bencode used by the Bit Torrent protocol. Normal ruby objects can be marshalled as bencode and demarshalled back to ruby."

s.required_rubygems_version = ">= 1.3.6"
s.add_development_dependency "rspec", "~> 2.1.0"
s.add_development_dependency "rspec", "~> 2.8.0"
s.add_development_dependency "bundler", ">= 1.0.0"
s.add_development_dependency "fuubar", ">= 0.0.1"
s.add_development_dependency "fuubar", ">= 1.0.0"

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
Expand Down
12 changes: 6 additions & 6 deletions lib/bencodr/io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ module ClassMethods
def bencode(fd, object)
open(fd, "wb") {|file| file.bencode(object)}
end

def bdecode(fd)
open(fd, "rb") {|file| file.bdecode}
end
end

def bencode(object)
write(Object.bencode(object))
end

def bdecode
Object.bdecode(read.force_encoding('UTF-8'))
Object.bdecode(read)
end

def self.included(base)
base.extend ClassMethods
end
end
end
end
71 changes: 49 additions & 22 deletions spec/bencodr/io_spec.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,62 @@
# encoding: UTF-8
require "spec_helper"
require 'tempfile'

describe File do
before :all do
@path = "tmp"
Dir.mkdir(@path) unless File.exists? @path
BEncodr.include!
end

before :each do
@file = File.join(@path, 'test.bencodr')
@object = "string"
File.bencode(@file, @object)
let(:file) { Tempfile.new('test.bencodr') }
let(:object) { "string" }

describe ".bencode" do
it "should encode object to file" do
File.bencode(file, object)
file.rewind
file.read.should == "6:string"
end
end

describe "#bencode" do
subject{ File }

it{ File.should exist(@file) }
end

describe "#bdecode" do
subject{ File }
it{ should bdecode(@file).to(@object) }
end

after :each do
File.delete(@file)
it "should encode object to file" do
file.bencode(object)
file.rewind
file.read.should == "6:string"
end
end

after :all do
Dir.delete(@path) if File.exists? @path
context "decode" do
let(:sample_path) { 'spec/samples/bencodr.torrent' }

before do
file.bencode(object)
file.rewind
end

describe ".bdecode" do
subject{ File }

it "should decode object from file" do
should bdecode(file).to(object)
end

it "should decode sample file without error" do
expect{ File.bdecode(sample_path) }.to_not raise_error
end
end

describe "#bdecode" do
subject{ file }

it "should decode object from file" do
should bdecode_to(object)
end

it "should decode sample file without error" do
f = File.open(sample_path, 'rb')
expect{ f.bdecode }.to_not raise_error
end
end
end
end
end
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require 'custom_matchers'
require 'shared_examples'

Rspec.configure do |c|
RSpec.configure do |c|
c.formatter = Fuubar
c.color_enabled = true
end
end

0 comments on commit 99f3568

Please sign in to comment.