test/test_bencoding.rb in simonmenke-bencoding-0.0.1 vs test/test_bencoding.rb in simonmenke-bencoding-0.0.2
- old
+ new
@@ -1,17 +1,72 @@
-require "stringio"
require File.dirname(__FILE__) + '/test_helper.rb'
class TestBencoding < Test::Unit::TestCase
-
- def setup
+
+ def test_load_string
+ test_load "5:hello", "hello"
end
- def test_truth
- data = ["hello", "bye", 5, { "simon" => 60 }]
- str = "l5:hello3:byei5ed5:simoni60eee"
- p str
- out = Bencoding::Parser.load(StringIO.new(str))
- p out
- assert data == out
+ def test_load_integer
+ test_load "i17e", 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_dump_string
+ test_dump "hello", "5:hello"
+ end
+
+ def test_dump_integer
+ test_dump 25, "i25e"
+ 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