require 'test/unit' require "#{File.dirname(__FILE__)}/../lib/bencode" class BdecodeTest < Test::Unit::TestCase def test_string assert_equal "foo", "3:foo".bdecode end def test_integer assert_equal 42, "i42e".bdecode assert_raise BdecodeError do "i01e".bdecode end assert_raise BdecodeError do "i-01e".bdecode end end def test_array assert_equal [1, 2, 3], "li1ei2ei3ee".bdecode end def test_hash 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 end end def test_valid assert "3:foo".bencoded? assert !"3:foo3:bar".bencoded? assert "i42e".bencoded? assert !"i42ei101e".bencoded? end end