require 'test/unit' require "#{File.dirname(__FILE__)}/../lib/bencode" class BdecodeTest < Test::Unit::TestCase def test_string assert_equal "", "0:".bdecode assert_equal "foo", "3:foo".bdecode assert_equal "foo bar", "7:foo bar".bdecode end def test_integer assert_equal 0, "i0e".bdecode assert_equal 42, "i42e".bdecode assert_equal -7, "i-7e".bdecode end def test_invalid_integer assert_raise BdecodeError do "i01e".bdecode end assert_raise BdecodeError do "i-01e".bdecode end end def test_array assert_equal [], "le".bdecode assert_equal ["foo"], "l3:fooe".bdecode assert_equal [1, 2, 3], "li1ei2ei3ee".bdecode end def test_hash assert_equal Hash.new, "de".bdecode hsh = {"a" => "monkey", "h" => "elephant", "z" => "zebra"} assert_equal hsh, "d1:a6:monkey1:h8:elephant1:z5:zebrae".bdecode end def test_invalid assert_raises BdecodeError do "foobar".bdecode "i1ei2e".bdecode "4:foo".bdecode "2:bar".bdecode end end def test_valid assert "3:foo".bencoded? assert !"3:foo3:bar".bencoded? assert "i42e".bencoded? assert !"i42ei101e".bencoded? end end