bencodr 1.2.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +0 -0
- data/.document +0 -0
- data/.gitignore +3 -1
- data/.rvmrc +1 -0
- data/Gemfile +3 -0
- data/LICENSE +0 -0
- data/{README.rdoc → README.md} +88 -53
- data/Rakefile +4 -44
- data/bencodr.gemspec +18 -75
- data/lib/bencodr.rb +23 -40
- data/lib/bencodr/dictionary.rb +19 -47
- data/lib/bencodr/ext.rb +38 -0
- data/lib/bencodr/integer.rb +17 -47
- data/lib/bencodr/io.rb +21 -56
- data/lib/bencodr/list.rb +19 -45
- data/lib/bencodr/object.rb +36 -0
- data/lib/bencodr/parser.rb +0 -14
- data/lib/bencodr/string.rb +17 -46
- data/lib/bencodr/version.rb +3 -0
- data/spec/bencode_spec.rb +17 -22
- data/spec/bencodr/dictionary_spec.rb +1 -74
- data/spec/bencodr/ext_spec.rb +66 -0
- data/spec/bencodr/integer_spec.rb +1 -67
- data/spec/bencodr/io_spec.rb +28 -30
- data/spec/bencodr/list_spec.rb +1 -31
- data/spec/bencodr/object_spec.rb +9 -0
- data/spec/bencodr/parser_spec.rb +38 -173
- data/spec/bencodr/string_spec.rb +1 -68
- data/spec/custom_matchers.rb +87 -0
- data/spec/samples/bencode.rb.torrent +0 -0
- data/spec/samples/mini.bencode +0 -0
- data/spec/samples/python.torrent +0 -0
- data/spec/shared_examples.rb +91 -0
- data/spec/spec_helper.rb +8 -9
- metadata +55 -28
- data/VERSION +0 -1
- data/autotest/discover.rb +0 -3
- data/spec/spec.opts +0 -1
data/spec/bencode_spec.rb
CHANGED
@@ -1,45 +1,40 @@
|
|
1
1
|
# encoding: UTF-8
|
2
|
-
|
3
|
-
require "spec"
|
4
2
|
require "spec_helper"
|
5
3
|
|
6
4
|
describe BEncodr do
|
7
|
-
describe "#
|
8
|
-
# Most of this is covered in other tests. Only difference is this accepts string instead of scanner.
|
5
|
+
describe "#bdecode" do
|
9
6
|
it "should parse a bencoded string" do
|
10
|
-
BEncodr.
|
7
|
+
BEncodr.bdecode("6:string").should == "string"
|
11
8
|
end
|
12
9
|
|
13
10
|
it "should parse a bencoded integer" do
|
14
|
-
BEncodr.
|
11
|
+
BEncodr.bdecode("i4e").should == 4
|
15
12
|
end
|
16
13
|
|
17
14
|
it "should parse a bencoded list" do
|
18
|
-
BEncodr.
|
15
|
+
BEncodr.bdecode("l6:stringeeeee").should == ["string"]
|
19
16
|
end
|
20
17
|
|
21
18
|
it "should parse a bencoded dictionary containing a key value pair" do
|
22
|
-
BEncodr.
|
19
|
+
BEncodr.bdecode("d6:stringi1ee").should == {"string" => 1}
|
23
20
|
end
|
24
21
|
|
25
22
|
it "should raise an error when the type is not recognized" do
|
26
|
-
lambda{BEncodr.
|
23
|
+
lambda{BEncodr.bdecode("freak out!")}.should raise_error BEncodr::BEncodeError
|
27
24
|
end
|
28
25
|
end
|
29
26
|
|
30
|
-
describe "#
|
27
|
+
describe "#bdecode_file" do
|
31
28
|
it "should parse a bencoded file" do
|
32
29
|
dirname = File.dirname(__FILE__)
|
33
|
-
BEncodr.
|
30
|
+
BEncodr.bdecode_file("#{dirname}/samples/mini.bencode").should == {"ba" => 3}
|
34
31
|
end
|
35
32
|
end
|
36
33
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
end
|
42
|
-
end
|
34
|
+
it_behaves_like "BEncodr::String", BEncodr
|
35
|
+
it_behaves_like "BEncodr::Integer", BEncodr
|
36
|
+
it_behaves_like "BEncodr::List", BEncodr
|
37
|
+
it_behaves_like "BEncodr::Dictionary", BEncodr
|
43
38
|
|
44
39
|
describe "#encode_file" do
|
45
40
|
context "when an object gets bencoded and written to a file" do
|
@@ -51,7 +46,7 @@ describe BEncodr do
|
|
51
46
|
before :each do
|
52
47
|
@file = File.join(@path, 'test.bencodr')
|
53
48
|
@object = "string"
|
54
|
-
BEncodr.
|
49
|
+
BEncodr.bencode_file(@file, @object)
|
55
50
|
end
|
56
51
|
|
57
52
|
it "should actually write a file" do
|
@@ -59,7 +54,7 @@ describe BEncodr do
|
|
59
54
|
end
|
60
55
|
|
61
56
|
it "should properly encode the file" do
|
62
|
-
BEncodr.
|
57
|
+
BEncodr.bdecode_file(@file).should == @object
|
63
58
|
end
|
64
59
|
|
65
60
|
after :each do
|
@@ -73,14 +68,14 @@ describe BEncodr do
|
|
73
68
|
|
74
69
|
it "should read a torrent with newlines as part of a string without raising an error" do
|
75
70
|
file = File.join(File.dirname(__FILE__), 'samples', 'python.torrent')
|
76
|
-
lambda{BEncodr.
|
71
|
+
lambda{BEncodr.bdecode_file file}.should_not raise_error
|
77
72
|
end
|
78
73
|
end
|
79
74
|
|
80
75
|
context "when parsing and then encoding" do
|
81
76
|
it "should be equal to the pre-parsed and encoded bencoded string" do
|
82
77
|
file = File.dirname(__FILE__) + "/samples/bencode.rb.torrent"
|
83
|
-
BEncodr.
|
78
|
+
BEncodr.bencode(BEncodr.bdecode_file(file)).should == File.open(file, "rb") {|f| f.read}
|
84
79
|
end
|
85
80
|
end
|
86
|
-
end
|
81
|
+
end
|
@@ -1,79 +1,6 @@
|
|
1
1
|
# encoding: UTF-8
|
2
|
-
|
3
|
-
require "spec"
|
4
2
|
require "spec_helper"
|
5
3
|
|
6
|
-
describe Hash do
|
7
|
-
describe "#bencodr" do
|
8
|
-
it "should encode an empty hash" do
|
9
|
-
{}.bencode.should == "de"
|
10
|
-
end
|
11
|
-
|
12
|
-
context "a key should always be encoded as a string" do
|
13
|
-
it "should encode a string key as a string" do
|
14
|
-
{"string" => "string"}.bencode.should == "d6:string6:stringe"
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should encode a symbol key as a string" do
|
18
|
-
{:symbol => :symbol}.bencode.should == "d6:symbol6:symbole"
|
19
|
-
end
|
20
|
-
|
21
|
-
it "should encode a uri key as a string" do
|
22
|
-
uri = URI.parse("http://github.com/blatyo/bencode")
|
23
|
-
{uri => uri}.bencode.should == "d32:http://github.com/blatyo/bencode32:http://github.com/blatyo/bencodee"
|
24
|
-
end
|
25
|
-
|
26
|
-
it "should encode an integer key as a string" do
|
27
|
-
{1 => 1}.bencode.should == "d1:1i1ee"
|
28
|
-
end
|
29
|
-
|
30
|
-
it "should encode a float key as a string" do
|
31
|
-
{1.1 => 1.1}.bencode.should == "d3:1.1i1ee"
|
32
|
-
end
|
33
|
-
|
34
|
-
it "should encode a time key as a string" do
|
35
|
-
time = Time.utc(0)
|
36
|
-
{time => time}.bencode.should == "d23:2000-01-01 00:00:00 UTCi946684800ee"
|
37
|
-
end
|
38
|
-
|
39
|
-
it "should encode an array key as a string" do
|
40
|
-
array = (1..4).to_a
|
41
|
-
{array => array}.bencode.should == "d12:[1, 2, 3, 4]li1ei2ei3ei4eee"
|
42
|
-
end
|
43
|
-
|
44
|
-
it "should encode a hash key as a string" do
|
45
|
-
{{} => {}}.bencode.should == "d2:{}dee"
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
it "should encode keys in sorted (as raw strings) order" do
|
50
|
-
{:a => 1, "A" => 1, 1=> 1}.bencode.should == "d1:1i1e1:Ai1e1:ai1ee"
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
4
|
describe BEncodr::Dictionary do
|
56
|
-
|
57
|
-
context "once an object has been registered as a BEncode dictionary" do
|
58
|
-
before :all do
|
59
|
-
klass = Class.new do
|
60
|
-
def to_h
|
61
|
-
{:a => "a", :b => "b"}
|
62
|
-
end
|
63
|
-
end
|
64
|
-
BEncodr::Dictionary.register klass
|
65
|
-
@instance = klass.new
|
66
|
-
end
|
67
|
-
|
68
|
-
context "an instance of that object" do
|
69
|
-
it "should respond to bencodr" do
|
70
|
-
@instance.should respond_to :bencode
|
71
|
-
end
|
72
|
-
|
73
|
-
it "should encode to a bencoded dictionary" do
|
74
|
-
@instance.bencode.should == "d1:a1:a1:b1:be"
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
5
|
+
it_behaves_like "BEncodr::Dictionary", BEncodr::Dictionary
|
79
6
|
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require "spec_helper"
|
3
|
+
|
4
|
+
describe BEncodr::Ext do
|
5
|
+
describe "#include!" do
|
6
|
+
before :all do
|
7
|
+
BEncodr::Ext.include!
|
8
|
+
end
|
9
|
+
|
10
|
+
context BEncodr::String do
|
11
|
+
context ::String do
|
12
|
+
it_behaves_like "a BEncodr extension", "", BEncodr::String
|
13
|
+
|
14
|
+
subject{ "" }
|
15
|
+
it { should be_a(BEncodr::Object) }
|
16
|
+
it { should respond_to(:bdecode) }
|
17
|
+
end
|
18
|
+
|
19
|
+
context Symbol do
|
20
|
+
it_behaves_like "a BEncodr extension", :a, BEncodr::String
|
21
|
+
end
|
22
|
+
|
23
|
+
context URI::Generic do
|
24
|
+
it_behaves_like "a BEncodr extension", URI.parse("www.google.com"), BEncodr::String
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context BEncodr::Integer do
|
29
|
+
context Numeric do
|
30
|
+
it_behaves_like "a BEncodr extension", 1, BEncodr::Integer
|
31
|
+
end
|
32
|
+
|
33
|
+
context Time do
|
34
|
+
it_behaves_like "a BEncodr extension", Time.at(0), BEncodr::Integer
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context BEncodr::List do
|
39
|
+
context Array do
|
40
|
+
it_behaves_like "a BEncodr extension", [], BEncodr::List
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context BEncodr::Dictionary do
|
45
|
+
context Hash do
|
46
|
+
it_behaves_like "a BEncodr extension", {}, BEncodr::Dictionary
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context BEncodr::IO do
|
51
|
+
context IO do
|
52
|
+
subject{IO}
|
53
|
+
it{ should respond_to(:bencode) }
|
54
|
+
|
55
|
+
it_behaves_like "a BEncodr extension", IO.new(0), BEncodr::IO
|
56
|
+
end
|
57
|
+
|
58
|
+
context File do
|
59
|
+
subject{File}
|
60
|
+
it{ should respond_to(:bencode) }
|
61
|
+
|
62
|
+
it_behaves_like "a BEncodr extension", File.open(File.dirname(__FILE__)), BEncodr::IO
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -1,72 +1,6 @@
|
|
1
1
|
# encoding: UTF-8
|
2
|
-
|
3
|
-
require "spec"
|
4
2
|
require "spec_helper"
|
5
3
|
|
6
|
-
describe Integer do
|
7
|
-
describe "#bencodr" do
|
8
|
-
it "should encode a positive integer" do
|
9
|
-
1.bencode.should == "i1e"
|
10
|
-
end
|
11
|
-
|
12
|
-
it "should encode a negative integer" do
|
13
|
-
-1.bencode.should == "i-1e"
|
14
|
-
end
|
15
|
-
|
16
|
-
it "should encode a positive big integer" do
|
17
|
-
10_000_000_000.bencode.should == "i10000000000e"
|
18
|
-
end
|
19
|
-
|
20
|
-
it "should encode a negative big integer" do
|
21
|
-
-10_000_000_000.bencode.should == "i-10000000000e"
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
describe Numeric do
|
27
|
-
describe "#bencodr" do
|
28
|
-
it "should encode a positive float with precision loss" do
|
29
|
-
1.1.bencode.should == "i1e"
|
30
|
-
end
|
31
|
-
|
32
|
-
it "should encode a negative float with precision loss" do
|
33
|
-
-1.1.bencode.should == "i-1e"
|
34
|
-
end
|
35
|
-
|
36
|
-
it "should encode an positive exponential float" do
|
37
|
-
1e10.bencode.should == "i10000000000e"
|
38
|
-
end
|
39
|
-
|
40
|
-
it "should encode an negative exponential float" do
|
41
|
-
-1e10.bencode.should == "i-10000000000e"
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
describe Time do
|
47
|
-
describe "#bencodr" do
|
48
|
-
it "should encode to bencoding" do
|
49
|
-
Time.at(4).bencode.should == "i4e"
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
4
|
describe BEncodr::Integer do
|
55
|
-
|
56
|
-
context "once an object has been registered as a BEncode integer" do
|
57
|
-
before :all do
|
58
|
-
BEncodr::Integer.register NilClass
|
59
|
-
end
|
60
|
-
|
61
|
-
context "an instance of that object" do
|
62
|
-
it "should respond to bencodr" do
|
63
|
-
nil.should respond_to :bencode
|
64
|
-
end
|
65
|
-
|
66
|
-
it "should encode to a bencoded integer" do
|
67
|
-
nil.bencode.should == "i0e"
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
5
|
+
it_behaves_like "BEncodr::Integer", BEncodr::Integer
|
72
6
|
end
|
data/spec/bencodr/io_spec.rb
CHANGED
@@ -1,36 +1,34 @@
|
|
1
|
-
require "
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe File do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
end
|
10
|
-
|
11
|
-
before :each do
|
12
|
-
@file = File.join(@path, 'test.bencodr')
|
13
|
-
@object = "string"
|
14
|
-
File.bencode(@file, @object)
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should actually write a file" do
|
18
|
-
File.exists?(@file).should be_true
|
19
|
-
end
|
20
|
-
|
21
|
-
describe "#bdecode" do
|
22
|
-
it "should properly encode the file" do
|
23
|
-
File.bdecode(@file).should == @object
|
24
|
-
end
|
25
|
-
end
|
4
|
+
before :all do
|
5
|
+
@path = "tmp"
|
6
|
+
Dir.mkdir(@path) unless File.exists? @path
|
7
|
+
BEncodr.include!
|
8
|
+
end
|
26
9
|
|
27
|
-
|
28
|
-
|
29
|
-
|
10
|
+
before :each do
|
11
|
+
@file = File.join(@path, 'test.bencodr')
|
12
|
+
@object = "string"
|
13
|
+
File.bencode(@file, @object)
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#bencode" do
|
17
|
+
subject{ File }
|
18
|
+
|
19
|
+
it{ File.should exist(@file) }
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "#bdecode" do
|
23
|
+
subject{ File }
|
24
|
+
it{ should bdecode(@file).to(@object) }
|
25
|
+
end
|
26
|
+
|
27
|
+
after :each do
|
28
|
+
File.delete(@file)
|
29
|
+
end
|
30
30
|
|
31
|
-
|
32
|
-
|
33
|
-
end
|
34
|
-
end
|
31
|
+
after :all do
|
32
|
+
Dir.delete(@path) if File.exists? @path
|
35
33
|
end
|
36
34
|
end
|
data/spec/bencodr/list_spec.rb
CHANGED
@@ -1,36 +1,6 @@
|
|
1
1
|
# encoding: UTF-8
|
2
|
-
|
3
|
-
require "spec"
|
4
2
|
require "spec_helper"
|
5
3
|
|
6
|
-
describe Array do
|
7
|
-
describe "#bencodr" do
|
8
|
-
it "should encode an empty array" do
|
9
|
-
[].bencode.should == "le"
|
10
|
-
end
|
11
|
-
|
12
|
-
it "should encode an array filled with bencodable objects" do
|
13
|
-
[:e, "a", 1, Time.at(11)].bencode.should == "l1:e1:ai1ei11ee"
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
4
|
describe BEncodr::List do
|
19
|
-
|
20
|
-
context "once an object has been registered as a BEncode list" do
|
21
|
-
before :all do
|
22
|
-
BEncodr::List.register Range
|
23
|
-
end
|
24
|
-
|
25
|
-
context "an instance of that object" do
|
26
|
-
it "should respond to bencodr" do
|
27
|
-
(1..2).should respond_to :bencode
|
28
|
-
end
|
29
|
-
|
30
|
-
it "should encode to a bencoded list" do
|
31
|
-
(1..2).bencode.should == "li1ei2ee"
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
5
|
+
it_behaves_like "BEncodr::List", BEncodr::List
|
36
6
|
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require "spec_helper"
|
3
|
+
|
4
|
+
describe BEncodr::Object do
|
5
|
+
it_behaves_like "BEncodr::String", BEncodr::Object
|
6
|
+
it_behaves_like "BEncodr::Integer", BEncodr::Object
|
7
|
+
it_behaves_like "BEncodr::List", BEncodr::Object
|
8
|
+
it_behaves_like "BEncodr::Dictionary", BEncodr::Object
|
9
|
+
end
|
data/spec/bencodr/parser_spec.rb
CHANGED
@@ -1,196 +1,61 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
|
3
|
-
require "
|
3
|
+
require "spec_helper"
|
4
4
|
|
5
5
|
describe BEncodr::Parser do
|
6
6
|
describe "#parse_object" do
|
7
|
-
|
8
|
-
it
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
it "should parse a bencoded integer" do
|
14
|
-
scanner = StringScanner.new("i4e")
|
15
|
-
BEncodr::Parser.parse_object(scanner).should == 4
|
16
|
-
end
|
17
|
-
|
18
|
-
it "should parse a bencoded list" do
|
19
|
-
scanner = StringScanner.new("l6:stringeeeee")
|
20
|
-
BEncodr::Parser.parse_object(scanner).should == ["string"]
|
21
|
-
end
|
22
|
-
|
23
|
-
it "should parse a bencoded dictionary containing a key value pair" do
|
24
|
-
scanner = StringScanner.new("d6:stringi1ee")
|
25
|
-
BEncodr::Parser.parse_object(scanner).should == {"string" => 1}
|
26
|
-
end
|
27
|
-
|
28
|
-
it "should return nil when the type is not recognized" do
|
29
|
-
scanner = StringScanner.new("freak out!")
|
30
|
-
BEncodr::Parser.parse_object(scanner).should == nil
|
31
|
-
end
|
7
|
+
it{ should parse("6:string").as(:object).to("string") }
|
8
|
+
it{ should parse("i4e").as(:object).to(4) }
|
9
|
+
it{ should parse("l6:stringeeeee").as(:object).to(["string"]) }
|
10
|
+
it{ should parse("d6:stringi1ee").as(:object).to({"string" => 1}) }
|
11
|
+
it{ should parse("freak out!").as(:object).to(nil) }
|
32
12
|
end
|
33
13
|
|
34
|
-
describe "#
|
35
|
-
it
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
it "should parse a zero length bencoded string" do
|
41
|
-
scanner = StringScanner.new("0:")
|
42
|
-
BEncodr::Parser.parse_string(scanner).should == ""
|
43
|
-
end
|
44
|
-
|
45
|
-
it "should raise an error if the length is invalid" do
|
46
|
-
scanner = StringScanner.new("fail:")
|
47
|
-
lambda {BEncodr::Parser.parse_string(scanner)}.should raise_error BEncodr::BEncodeError
|
48
|
-
end
|
49
|
-
|
50
|
-
it "should raise an error if length is too long" do
|
51
|
-
scanner = StringScanner.new("3:a")
|
52
|
-
lambda {BEncodr::Parser.parse_string(scanner)}.should raise_error BEncodr::BEncodeError
|
53
|
-
end
|
54
|
-
|
55
|
-
it "should raise an error if the colon is missing" do
|
56
|
-
scanner = StringScanner.new("3aaa")
|
57
|
-
lambda {BEncodr::Parser.parse_string(scanner)}.should raise_error BEncodr::BEncodeError
|
58
|
-
end
|
14
|
+
describe "#parse_string" do
|
15
|
+
it{ should parse("6:string").as(:string).to("string") }
|
16
|
+
it{ should parse("0:").as(:string).to("") }
|
17
|
+
it{ should generate_parse_error(BEncodr::BEncodeError).for(:string).with("fail:") }
|
18
|
+
it{ should generate_parse_error(BEncodr::BEncodeError).for(:string).with("3:a") }
|
19
|
+
it{ should generate_parse_error(BEncodr::BEncodeError).for(:string).with("3aaa") }
|
59
20
|
end
|
60
21
|
|
61
22
|
describe "#parse_integer" do
|
62
|
-
it
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
it "should raise an error if there is no starting i" do
|
68
|
-
scanner = StringScanner.new("4e")
|
69
|
-
lambda{BEncodr::Parser.parse_integer(scanner)}.should raise_error BEncodr::BEncodeError
|
70
|
-
end
|
71
|
-
|
72
|
-
it "should raise an error if there is no integer" do
|
73
|
-
scanner = StringScanner.new("ie")
|
74
|
-
lambda{BEncodr::Parser.parse_integer(scanner)}.should raise_error BEncodr::BEncodeError
|
75
|
-
end
|
76
|
-
|
77
|
-
it "should raise an error if there is no closing e" do
|
78
|
-
scanner = StringScanner.new("i4")
|
79
|
-
lambda{BEncodr::Parser.parse_integer(scanner)}.should raise_error BEncodr::BEncodeError
|
80
|
-
end
|
23
|
+
it{ should parse("i4e").as(:integer).to(4) }
|
24
|
+
it{ should generate_parse_error(BEncodr::BEncodeError).for(:integer).with("4e") }
|
25
|
+
it{ should generate_parse_error(BEncodr::BEncodeError).for(:integer).with("ie") }
|
26
|
+
it{ should generate_parse_error(BEncodr::BEncodeError).for(:integer).with("i4") }
|
81
27
|
end
|
82
28
|
|
83
29
|
describe "#parse_list" do
|
84
|
-
it
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
it
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
it
|
95
|
-
scanner = StringScanner.new("l6:string6:stringe")
|
96
|
-
BEncodr::Parser.parse_list(scanner).should == ["string", "string"]
|
97
|
-
end
|
98
|
-
|
99
|
-
it "should parse a bencoded list containing an integer" do
|
100
|
-
scanner = StringScanner.new("li1ee")
|
101
|
-
BEncodr::Parser.parse_list(scanner).should == [1]
|
102
|
-
end
|
103
|
-
|
104
|
-
it "should parse a bencoded list containing more than one integer" do
|
105
|
-
scanner = StringScanner.new("li1ei2ee")
|
106
|
-
BEncodr::Parser.parse_list(scanner).should == [1, 2]
|
107
|
-
end
|
108
|
-
|
109
|
-
it "should parse a bencoded list containing a list" do
|
110
|
-
scanner = StringScanner.new("llee")
|
111
|
-
BEncodr::Parser.parse_list(scanner).should == [[]]
|
112
|
-
end
|
113
|
-
|
114
|
-
it "should parse a bencoded list containing more than one list" do
|
115
|
-
scanner = StringScanner.new("llelee")
|
116
|
-
BEncodr::Parser.parse_list(scanner).should == [[], []]
|
117
|
-
end
|
118
|
-
|
119
|
-
it "should parse a bencoded list containing a dictionary" do
|
120
|
-
scanner = StringScanner.new("ldee")
|
121
|
-
BEncodr::Parser.parse_list(scanner).should == [{}]
|
122
|
-
end
|
123
|
-
|
124
|
-
it "should parse a bencoded list containing more than one dictionary" do
|
125
|
-
scanner = StringScanner.new("ldedee")
|
126
|
-
BEncodr::Parser.parse_list(scanner).should == [{}, {}]
|
127
|
-
end
|
128
|
-
|
129
|
-
it "should raise an error if there is no starting l" do
|
130
|
-
scanner = StringScanner.new("e")
|
131
|
-
lambda{BEncodr::Parser.parse_list(scanner)}.should raise_error BEncodr::BEncodeError
|
132
|
-
end
|
133
|
-
|
134
|
-
it "should raise an error if there is no closing e" do
|
135
|
-
scanner = StringScanner.new("l")
|
136
|
-
lambda{BEncodr::Parser.parse_list(scanner)}.should raise_error BEncodr::BEncodeError
|
137
|
-
end
|
30
|
+
it{ should parse("le").as(:list).to([]) }
|
31
|
+
it{ should parse("l6:stringeeeee").as(:list).to(["string"]) }
|
32
|
+
it{ should parse("l6:string6:stringe").as(:list).to(["string", "string"]) }
|
33
|
+
it{ should parse("li1ee").as(:list).to([1]) }
|
34
|
+
it{ should parse("li1ei-2ee").as(:list).to([1, -2]) }
|
35
|
+
it{ should parse("llee").as(:list).to([[]]) }
|
36
|
+
it{ should parse("llelee").as(:list).to([[], []]) }
|
37
|
+
it{ should parse("ldee").as(:list).to([{}]) }
|
38
|
+
it{ should parse("ldedee").as(:list).to([{}, {}]) }
|
39
|
+
it{ should generate_parse_error(BEncodr::BEncodeError).for(:list).with("e") }
|
40
|
+
it{ should generate_parse_error(BEncodr::BEncodeError).for(:list).with("l") }
|
138
41
|
end
|
139
42
|
|
140
43
|
describe "#parse_dictionary" do
|
141
|
-
it
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
it
|
147
|
-
|
148
|
-
BEncodr::Parser.parse_dictionary(scanner).should == {"string" => 1}
|
149
|
-
end
|
150
|
-
|
151
|
-
it "should parse a bencoded dictionary containing more than one key value pair" do
|
152
|
-
scanner = StringScanner.new("d7:anotherle6:stringi1ee")
|
153
|
-
BEncodr::Parser.parse_dictionary(scanner).should == {"string" => 1, "another" => []}
|
154
|
-
end
|
155
|
-
|
156
|
-
it "should raise an error if there is no starting d" do
|
157
|
-
scanner = StringScanner.new("e")
|
158
|
-
lambda{BEncodr::Parser.parse_dictionary(scanner)}.should raise_error BEncodr::BEncodeError
|
159
|
-
end
|
160
|
-
|
161
|
-
it "should raise an error if the key is not a string" do
|
162
|
-
scanner = StringScanner.new("di1ei1ee")
|
163
|
-
lambda{BEncodr::Parser.parse_dictionary(scanner)}.should raise_error BEncodr::BEncodeError
|
164
|
-
end
|
165
|
-
|
166
|
-
it "should raise an error if there is no closing e" do
|
167
|
-
scanner = StringScanner.new("d")
|
168
|
-
lambda{BEncodr::Parser.parse_dictionary(scanner)}.should raise_error BEncodr::BEncodeError
|
169
|
-
end
|
170
|
-
|
171
|
-
it "should raise an error if there is a key with no value" do
|
172
|
-
scanner = StringScanner.new("d1:ae")
|
173
|
-
lambda{BEncodr::Parser.parse_dictionary(scanner)}.should raise_error BEncodr::BEncodeError
|
174
|
-
end
|
44
|
+
it{ should parse("de").as(:dictionary).to({}) }
|
45
|
+
it{ should parse("d6:stringi1ee").as(:dictionary).to({"string" => 1}) }
|
46
|
+
it{ should parse("d7:anotherle6:stringi1ee").as(:dictionary).to({"string" => 1, "another" => []}) }
|
47
|
+
it{ should generate_parse_error(BEncodr::BEncodeError).for(:dictionary).with("e") }
|
48
|
+
it{ should generate_parse_error(BEncodr::BEncodeError).for(:dictionary).with("di1ei1ee") }
|
49
|
+
it{ should generate_parse_error(BEncodr::BEncodeError).for(:dictionary).with("d") }
|
50
|
+
it{ should generate_parse_error(BEncodr::BEncodeError).for(:dictionary).with("d1:ae") }
|
175
51
|
end
|
176
52
|
end
|
177
53
|
|
178
54
|
describe String do
|
179
55
|
describe "#bdecode" do
|
180
|
-
it "should
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
it "should decode a bencoded integer into a ruby integer" do
|
185
|
-
"i-1e".bdecode.should == -1
|
186
|
-
end
|
187
|
-
|
188
|
-
it "should decode a bencoded list into a ruby array" do
|
189
|
-
"le".bdecode.should == []
|
190
|
-
end
|
191
|
-
|
192
|
-
it "should decode a bencoded dictionary into a ruby hash" do
|
193
|
-
"de".bdecode.should == {}
|
194
|
-
end
|
56
|
+
it{ "6:string".should bdecode_to("string") }
|
57
|
+
it{ "i-1e".should bdecode_to(-1) }
|
58
|
+
it{ "le".should bdecode_to([]) }
|
59
|
+
it{ "de".should bdecode_to({}) }
|
195
60
|
end
|
196
61
|
end
|