|
1 | | -var assert = require('assert') |
| 1 | +var test = require('tape').test |
2 | 2 | var bencode = require('./lib.js') |
3 | 3 | var data = require('./data.js') |
4 | 4 |
|
5 | | -describe("bencode", function() { |
6 | | - describe("#decode(x)", function() { |
7 | | - it('should be able to decode an integer', function() { |
8 | | - assert.deepEqual(bencode.decode('i123e'), 123); |
9 | | - assert.deepEqual(bencode.decode('i-123e'), -123); |
10 | | - }); |
11 | | - it('should be able to decode a float (as int)', function() { |
12 | | - assert.deepEqual(bencode.decode('i12.3e'), 12); |
13 | | - assert.deepEqual(bencode.decode('i-12.3e'), -12); |
14 | | - }); |
15 | | - it('should be able to decode a string', function() { |
16 | | - assert.deepEqual(bencode.decode('5:asdfe'), new Buffer('asdfe')); |
17 | | - assert.deepEqual(bencode.decode('4:öö'), new Buffer('öö')); |
18 | | - }); |
19 | | - it('should be able to decode "binary keys"', function() { |
20 | | - assert.ok(bencode.decode(data.binKeyData).files.hasOwnProperty(data.binKeyName)); |
21 | | - }); |
| 5 | +test("bencode#decode(x)", function(t) { |
22 | 6 |
|
23 | | - it('should be able to decode a dictionary', function() { |
24 | | - assert.deepEqual( |
25 | | - bencode.decode( 'd3:cow3:moo4:spam4:eggse' ), |
26 | | - { |
27 | | - cow: new Buffer('moo'), |
28 | | - spam: new Buffer('eggs') |
29 | | - } |
30 | | - ) |
31 | | - assert.deepEqual( |
32 | | - bencode.decode( 'd4:spaml1:a1:bee' ), |
33 | | - { spam: [ |
34 | | - new Buffer('a'), |
35 | | - new Buffer('b') |
36 | | - ] } |
37 | | - ) |
38 | | - assert.deepEqual( |
39 | | - bencode.decode( 'd9:publisher3:bob17:publisher-webpage15:www.example.com18:publisher.location4:homee'), |
40 | | - { |
41 | | - 'publisher': new Buffer('bob'), |
42 | | - 'publisher-webpage': new Buffer('www.example.com'), |
43 | | - 'publisher.location': new Buffer('home') |
44 | | - } |
45 | | - ) |
46 | | - }); |
| 7 | + t.test('should be able to decode an integer', function(t) { |
| 8 | + t.plan(2) |
| 9 | + t.equal(bencode.decode('i123e'), 123); |
| 10 | + t.equal(bencode.decode('i-123e'), -123); |
| 11 | + }) |
47 | 12 |
|
48 | | - it('should be able to decode a list', function() { |
49 | | - assert.deepEqual( |
50 | | - bencode.decode( 'l4:spam4:eggse'), |
51 | | - [ new Buffer('spam'), |
52 | | - new Buffer('eggs') ] |
53 | | - ) |
54 | | - }); |
55 | | - it('should return the correct type', function() { |
56 | | - assert.ok(bencode.decode('4:öö') instanceof Buffer); |
57 | | - }); |
58 | | - it('should be able to decode stuff in dicts (issue #12)', function() { |
59 | | - var someData = { |
60 | | - string: 'Hello World', |
61 | | - integer: 12345, |
62 | | - dict: { |
63 | | - key: 'This is a string within a dictionary' |
64 | | - }, |
65 | | - list: [ 1, 2, 3, 4, 'string', 5, {} ] |
| 13 | + t.test('should be able to decode a float (as int)', function(t) { |
| 14 | + t.plan(2) |
| 15 | + t.equal(bencode.decode('i12.3e'), 12); |
| 16 | + t.equal(bencode.decode('i-12.3e'), -12); |
| 17 | + }) |
| 18 | + |
| 19 | + t.test('should be able to decode a string', function(t) { |
| 20 | + t.plan(2) |
| 21 | + t.deepEqual(bencode.decode('5:asdfe'), new Buffer('asdfe')); |
| 22 | + t.deepEqual(bencode.decode(data.binResultData.toString()), data.binStringData); |
| 23 | + }) |
| 24 | + |
| 25 | + t.test('should be able to decode "binary keys"', function(t) { |
| 26 | + t.plan(1) |
| 27 | + t.ok(bencode.decode(data.binKeyData).files.hasOwnProperty(data.binKeyName)); |
| 28 | + }) |
| 29 | + |
| 30 | + t.test('should be able to decode a dictionary', function(t) { |
| 31 | + t.plan(3) |
| 32 | + t.deepEqual( |
| 33 | + bencode.decode( 'd3:cow3:moo4:spam4:eggse' ), |
| 34 | + { |
| 35 | + cow: new Buffer('moo'), |
| 36 | + spam: new Buffer('eggs') |
66 | 37 | } |
67 | | - var result = bencode.encode( someData ) |
68 | | - var dat = bencode.decode ( result ) |
69 | | - assert.equal(dat.integer, 12345) |
70 | | - assert.deepEqual(dat.string, new Buffer("Hello World")) |
71 | | - assert.deepEqual(dat.dict.key, new Buffer("This is a string within a dictionary")) |
72 | | - assert.deepEqual(dat.list, [1, 2, 3, 4, new Buffer('string'), 5, {}]) |
73 | | - }); |
| 38 | + ) |
| 39 | + t.deepEqual( |
| 40 | + bencode.decode( 'd4:spaml1:a1:bee' ), |
| 41 | + { spam: [ |
| 42 | + new Buffer('a'), |
| 43 | + new Buffer('b') |
| 44 | + ] } |
| 45 | + ) |
| 46 | + t.deepEqual( |
| 47 | + bencode.decode( 'd9:publisher3:bob17:publisher-webpage15:www.example.com18:publisher.location4:homee'), |
| 48 | + { |
| 49 | + 'publisher': new Buffer('bob'), |
| 50 | + 'publisher-webpage': new Buffer('www.example.com'), |
| 51 | + 'publisher.location': new Buffer('home') |
| 52 | + } |
| 53 | + ) |
74 | 54 | }); |
75 | | -}); |
| 55 | + |
| 56 | + t.test('should be able to decode a list', function(t) { |
| 57 | + t.plan(1) |
| 58 | + t.deepEqual( |
| 59 | + bencode.decode( 'l4:spam4:eggse'), |
| 60 | + [ new Buffer('spam'), |
| 61 | + new Buffer('eggs') ] |
| 62 | + ) |
| 63 | + }) |
| 64 | + t.test('should return the correct type', function(t) { |
| 65 | + t.plan(1) |
| 66 | + t.ok(Buffer.isBuffer(bencode.decode('4:öö'))); |
| 67 | + }) |
| 68 | + t.test('should be able to decode stuff in dicts (issue #12)', function(t) { |
| 69 | + t.plan(4) |
| 70 | + var someData = { |
| 71 | + string: 'Hello World', |
| 72 | + integer: 12345, |
| 73 | + dict: { |
| 74 | + key: 'This is a string within a dictionary' |
| 75 | + }, |
| 76 | + list: [ 1, 2, 3, 4, 'string', 5, {} ] |
| 77 | + } |
| 78 | + var result = bencode.encode( someData ) |
| 79 | + var dat = bencode.decode ( result ) |
| 80 | + t.equal(dat.integer, 12345) |
| 81 | + t.deepEqual(dat.string, new Buffer("Hello World")) |
| 82 | + t.deepEqual(dat.dict.key, new Buffer("This is a string within a dictionary")) |
| 83 | + t.deepEqual(dat.list, [1, 2, 3, 4, new Buffer('string'), 5, {}]) |
| 84 | + }) |
| 85 | +}) |
0 commit comments