require File.dirname(__FILE__) + '/test_helper.rb' class TestBencoding < Test::Unit::TestCase def test_load_string test_load "5:hello", "hello" end def test_load_integer test_load "i17e", 17 end def test_load_negative_integer test_load "i-17e", -17 end def test_load_list test_load "l5:helloi17ee", ["hello", 17] end def test_load_dictionary test_load "d5:key_ai16e5:a_key5:helloe", { "key_a" => 16, "a_key" => "hello" } end def test_load_fake str = "d5:key_ai16e5:a_key4:helloe" assert_raise Bencoding::ParseError do out = Bencoding.load!(str) end end def test_load_partial_integer str = "i12" assert_raise Bencoding::ParseError do out = Bencoding.load!(str) end end def test_load_partial_integer_in_list str = "li12i45ee" assert_raise Bencoding::ParseError do out = Bencoding.load!(str) end end def test_load_partial_string str = "5:hel" assert_raise Bencoding::ParseError do out = Bencoding.load!(str) end end def test_load_form_uri uri = "http://torrents.thepiratebay.org/4385574/Ubuntu_8.10_(Intrepid_Ibex)_Alpha_5.4385574.TPB.torrent" assert Bencoding.load!(uri)['announce']=='http://tpb.tracker.thepiratebay.org:80/announce' end def test_dump_string test_dump "hello", "5:hello" end def test_dump_integer test_dump 25, "i25e" end def test_dump_negative_integer test_dump -25, "i-25e" end def test_dump_float test_dump 25.4, "i25e" end def test_dump_list test_dump([25.4, "hello"], "li25e5:helloe") end def test_dump_dictionary test_dump({ :key_a => 25.4, "a_key" => "hello"}, "d5:a_key5:hello5:key_ai25ee") end class DummyObject def initialize @name = "Simon Menke" @job = "Web Developer" end end def test_dump_object test_dump(DummyObject.new, "d3:job13:Web Developer4:name11:Simon Menkee") end protected def test_load(input, output) data = Bencoding.load(input) assert output == data end def test_dump(input, output) data = Bencoding.dump(input) assert data == output end end