-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbencode_test.rb
35 lines (29 loc) · 1.34 KB
/
bencode_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
require "./bencode"
require "test/unit"
class TestBencode < Test::Unit::TestCase
def test_integers
assert_equal(32, Bencode.from_string('i32e').decode.data )
assert_equal(0, Bencode.from_string('i0e').decode.data )
assert_equal(-5, Bencode.from_string('i-5e').decode.data )
end
def test_strings
assert_equal('1231', Bencode.from_string('4:1231').decode.data )
assert_equal('', Bencode.from_string('0:').decode.data )
assert_equal('zxcvbnmhjk', Bencode.from_string('10:zxcvbnmhjk').decode.data )
end
def test_lists
assert_equal([], Bencode.from_string('le').decode.data )
assert_equal([1, 2, 3], Bencode.from_string('li1ei2ei3ee').decode.data )
assert_equal(['list', [1,2,3]], Bencode.from_string('l4:listli1ei2ei3eee').decode.data )
end
def test_dictionaries
assert_equal({}, Bencode.from_string('de').decode.data )
assert_equal({test: 'result'}, Bencode.from_string('d4:test6:resulte').decode.data )
assert_equal({test: 'result', dict: {tset: 'tluser'} }, Bencode.from_string('d4:test6:result4:dictd4:tset6:tluseree').decode.data )
end
def test_whole
data = 'd4:testl15:abcdeabcdeabcdeli123ei321e2:zxee5:test2l3:poii-43ed2:fgi0eeee'
result = {test: ['abcdeabcdeabcde', [123, 321, 'zx']], test2: ['poi', -43, {fg: 0}]}
assert_equal(result, Bencode.from_string(data).decode.data )
end
end