Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Update tests to be more succinct.

  • Loading branch information...
commit ab5d6028258e57ae49eea3d5a5fbedcd630a2665 1 parent 14b24a5
@blatyo blatyo authored
View
53 spec/bencodr/dictionary_spec.rb
@@ -3,49 +3,30 @@
describe BEncodr::Dictionary do
describe "#bencode" do
- it "should encode an empty hash" do
- BEncodr::Dictionary.bencode({}).should == "de"
- end
+ it{ should bencode({}).to("de") }
+ it{ should bencode({:a => 1, "A" => 1, 1=> 1}).to("d1:1i1e1:Ai1e1:ai1ee")}
context "a key should always be encoded as a string" do
- it "should encode a string key as a string" do
- BEncodr::Dictionary.bencode({"string" => "string"}).should == "d6:string6:stringe"
- end
-
- it "should encode a symbol key as a string" do
- BEncodr::Dictionary.bencode({:symbol => :symbol}).should == "d6:symbol6:symbole"
- end
-
- it "should encode a uri key as a string" do
+ it{ should bencode({"string" => "string"}).to("d6:string6:stringe") }
+ it{ should bencode({:symbol => :symbol}).to("d6:symbol6:symbole") }
+ it{ should bencode({1 => 1}).to("d1:1i1ee")}
+ it{ should bencode({1.1 => 1.1}).to("d3:1.1i1ee") }
+ it{ should bencode({{} => {}}).to("d2:{}dee") }
+
+ it{
uri = URI.parse("http://github.com/blatyo/bencode")
- BEncodr::Dictionary.bencode({uri => uri}).should == "d32:http://github.com/blatyo/bencode32:http://github.com/blatyo/bencodee"
- end
-
- it "should encode an integer key as a string" do
- BEncodr::Dictionary.bencode({1 => 1}).should == "d1:1i1ee"
- end
+ should bencode({uri => uri}).to("d32:http://github.com/blatyo/bencode32:http://github.com/blatyo/bencodee")
+ }
- it "should encode a float key as a string" do
- BEncodr::Dictionary.bencode({1.1 => 1.1}).should == "d3:1.1i1ee"
- end
-
- it "should encode a time key as a string" do
+ it{
time = Time.utc(0)
- BEncodr::Dictionary.bencode({time => time}).should == "d23:0000-01-01 00:00:00 UTC23:0000-01-01 00:00:00 UTCe"
- end
+ should bencode({time => time}).to("d23:0000-01-01 00:00:00 UTCi-62167219200ee")
+ }
- it "should encode an array key as a string" do
+ it{
array = (1..4).to_a
- BEncodr::Dictionary.bencode({array => array}).should == "d12:[1, 2, 3, 4]li1ei2ei3ei4eee"
- end
-
- it "should encode a hash key as a string" do
- BEncodr::Dictionary.bencode({{} => {}}).should == "d2:{}dee"
- end
- end
-
- it "should encode keys in sorted (as raw strings) order" do
- BEncodr::Dictionary.bencode({:a => 1, "A" => 1, 1=> 1}).should == "d1:1i1e1:Ai1e1:ai1ee"
+ should bencode({array => array}).to("d12:[1, 2, 3, 4]li1ei2ei3ei4eee")
+ }
end
end
end
View
44 spec/bencodr/integer_spec.rb
@@ -3,40 +3,14 @@
describe BEncodr::Integer do
describe "#bencodr" do
- it "should encode a positive integer" do
- BEncodr::Integer.bencode(1).should == "i1e"
- end
-
- it "should encode a negative integer" do
- BEncodr::Integer.bencode(-1).should == "i-1e"
- end
-
- it "should encode a positive big integer" do
- BEncodr::Integer.bencode(10_000_000_000).should == "i10000000000e"
- end
-
- it "should encode a negative big integer" do
- BEncodr::Integer.bencode(-10_000_000_000).should == "i-10000000000e"
- end
-
- it "should encode a positive float with precision loss" do
- BEncodr::Integer.bencode(1.1).should == "i1e"
- end
-
- it "should encode a negative float with precision loss" do
- BEncodr::Integer.bencode(-1.1).should == "i-1e"
- end
-
- it "should encode an positive exponential float" do
- BEncodr::Integer.bencode(1e10).should == "i10000000000e"
- end
-
- it "should encode an negative exponential float" do
- BEncodr::Integer.bencode(-1e10).should == "i-10000000000e"
- end
-
- it "should encode a time" do
- BEncodr::Integer.bencode(Time.at(4)).should == "i4e"
- end
+ it{ should bencode(1).to("i1e") }
+ it{ should bencode(-1).to("i-1e") }
+ it{ should bencode(10_000_000_000).to("i10000000000e") }
+ it{ should bencode(-10_000_000_000).to("i-10000000000e") }
+ it{ should bencode(1.1).to("i1e") }
+ it{ should bencode(-1.1).to("i-1e") }
+ it{ should bencode(1e10).to("i10000000000e") }
+ it{ should bencode(-1e10).to("i-10000000000e") }
+ it{ should bencode(Time.at(4)).to("i4e") }
end
end
View
9 spec/bencodr/list_spec.rb
@@ -3,12 +3,7 @@
describe BEncodr::List do
describe "#bencode" do
- it "should encode an empty array" do
- BEncodr::List.bencode([]).should == "le"
- end
-
- it "should encode an array filled with bencodable objects" do
- BEncodr::List.bencode([:e, "a", 1, Time.at(11)]).should == "l1:e1:ai1ei11ee"
- end
+ it{ should bencode([]).to("le") }
+ it{ should bencode([:e, "a", 1, Time.at(11)]).to("l1:e1:ai1ei11ee")
end
end
View
44 spec/bencodr/string_spec.rb
@@ -3,41 +3,13 @@
describe BEncodr::String do
describe "#bencode" do
- it "should encode a string" do
- BEncodr::String.bencode("string").should == "6:string"
- end
-
- it "should encode a zero length string" do
- BEncodr::String.bencode("").should == "0:"
- end
-
- it "should encode a symbol" do
- BEncodr::String.bencode(:symbol).should == "6:symbol"
- end
-
- it "should encode a http uri" do
- uri = URI.parse("http://github.com/blatyo/bencodr")
- BEncodr::String.bencode(uri).should == "32:http://github.com/blatyo/bencodr"
- end
-
- it "should encode a https uri" do
- uri = URI.parse("https://github.com/blatyo/bencodr")
- BEncodr::String.bencode(uri).should == "33:https://github.com/blatyo/bencodr"
- end
-
- it "should encode a ftp uri" do
- uri = URI.parse("ftp://github.com/blatyo/bencodr")
- BEncodr::String.bencode(uri).should == "31:ftp://github.com/blatyo/bencodr"
- end
-
- it "should encode a ldap uri" do
- uri = URI.parse("ldap://github.com/blatyo/bencodr")
- BEncodr::String.bencode(uri).should == "32:ldap://github.com/blatyo/bencodr"
- end
-
- it "should encode a mailto uri" do
- uri = URI.parse("mailto:sudo@sudoers.su")
- BEncodr::String.bencode(uri).should == "22:mailto:sudo@sudoers.su"
- end
+ it{ should bencode("string").to("6:string") }
+ it{ should bencode("").to("0:") }
+ it{ should bencode(:symbol).to("6:symbol") }
+ it{ should bencode(URI.parse("http://github.com/blatyo/bencodr")).to("32:http://github.com/blatyo/bencodr") }
+ it{ should bencode(URI.parse("https://github.com/blatyo/bencodr")).to("33:https://github.com/blatyo/bencodr") }
+ it{ should bencode(URI.parse("ftp://github.com/blatyo/bencodr")).to("31:ftp://github.com/blatyo/bencodr") }
+ it{ should bencode(URI.parse("ldap://github.com/blatyo/bencodr")).to("32:ldap://github.com/blatyo/bencodr") }
+ it{ should bencode(URI.parse("mailto:sudo@sudoers.su")).to("22:mailto:sudo@sudoers.su") }
end
end
View
23 spec/custom_matchers.rb
@@ -0,0 +1,23 @@
+RSpec::Matchers.define :bencode_to do |expected|
+ match do |actual|
+ actual.bencode.should equal(expected)
+ end
+
+ failure_message_for_should do |actual|
+ "expected that #{actual} would bencode to #{expected}"
+ end
+end
+
+RSpec::Matchers.define :bencode do |actual|
+ chain :to do |_expected|
+ @_expected = _expected
+ end
+
+ match do |klass|
+ klass.bencode(actual).should == @_expected
+ end
+
+ failure_message_for_should do |klass|
+ "expected #{klass.name} to bencode #{actual} to #{@_expected}"
+ end
+end
View
1  spec/spec_helper.rb
@@ -1,6 +1,7 @@
require File.join(File.dirname(__FILE__), '..', 'lib', 'bencodr')
require 'rspec'
require 'fuubar'
+require 'custom_matchers'
Rspec.configure do |c|
c.formatter = Fuubar
Please sign in to comment.
Something went wrong with that request. Please try again.