@@ -12,37 +12,50 @@ import XCTest
1212
1313class BencodeKitTests : XCTestCase {
1414
15+ func testDecoding( ) {
16+ expectException ( " " )
17+ expectException ( " k " )
18+ _ = Bencode ( " hello " . asciiData)
19+ XCTAssertNotEqual ( Bencode ( 0 ) , try ! Bencode ( " 4:null " ) )
20+ expectException ( try Bencode . decodeFile ( atPath: " " ) )
21+ }
22+
1523 func testStringParsing( ) {
16- let data = " 12:123456789123 " . data ( using: . ascii) !
17- let ( match, _) = try ! bdecodeString ( data, data. startIndex)
18- print ( match. description)
19- // testParsing("12:123456789123", .bytes("123456789123"))
24+ testParsing ( " 12:123456789123 " , try ! Bencode ( " 123456789123 " ) )
2025
21- expectException ( " -13:40 " )
22- expectException ( " -13:40 " )
23- expectException ( " -13:40 " )
24- expectException ( " -13:40 " )
26+ expectException ( " -13:40 " , bdecodeString)
2527 expectException ( " 4:lkd " )
28+ expectException ( " 4e:lkd " )
29+ expectException ( " i0e " , bdecodeString)
2630 }
2731
2832 func testListParsing( ) {
29- testParsing ( " li-123456789e4:nulle " , Bencode ( [ . integer( - 123456789 ) , Bencode ( " null " ) ! ] ) )
33+ testParsing ( " li-123456789e4:nulle " , try ! Bencode ( [ . integer( - 123456789 ) , Bencode ( " null " ) ] ) )
3034 compareToReencoding ( " li-123456789e4:nulle " )
35+ expectException ( " l " )
36+ expectException ( " li0e " )
37+ expectException ( " i0e " , bdecodeList)
3138 }
3239
3340 func testDictionaryParsing( ) {
41+ _ = Bencode ( [ ( " " , Bencode ( 0 ) ) ] )
3442 testParsing ( " d4:nulli-123456789e2:hilee " , . dictionary( [ ( " null " , . integer( - 123456789 ) ) , ( " hi " , . list( [ ] ) ) ] ) )
43+ testParsing ( " de " , . dictionary( [ ] ) )
3544 compareToReencoding ( " d4:nulli-123456789e2:hilee " )
45+ compareToReencoding ( " de " )
46+ compareToReencoding ( " d5:hello5:theree " )
3647
3748 expectException ( " d " )
49+ expectException ( " di0e4:nulle " )
50+ expectException ( " d4:nulli0e " )
51+ expectException ( " i0e " , bdecodeDictionary)
3852 }
3953
4054 func testIntegerParsing( ) {
41- let data = " i-123456789e " . data ( using: . ascii) !
42- print ( try ! bdecodeInteger ( data, data. startIndex) )
43-
55+ _ = Bencode ( 0 )
4456 testParsing ( " i-123456789e " , . integer( - 123456789 ) )
45-
57+ compareToReencoding ( " i-123456789e " )
58+ expectException ( " de " , bdecodeInteger)
4659 expectException ( " ie " )
4760 expectException ( " i-0e " )
4861 expectException ( " ioe " )
@@ -52,31 +65,45 @@ class BencodeKitTests: XCTestCase {
5265 }
5366
5467 func testTorrentFiles( ) {
55- Bundle ( for: type ( of: self ) )
56- . paths ( forResourcesOfType : " torrent " , inDirectory : " Torrents " )
68+ let filePaths = Bundle ( for: type ( of: self ) ) . paths ( forResourcesOfType : " torrent " , inDirectory : " Torrents " )
69+ filePaths
5770 . flatMap ( FileManager . default. contents)
5871 . forEach { encoded in
5972 let decoded = try ! Bencode . decode ( encoded)
60- print ( decoded. description)
6173 let reEncoded = decoded. encoded ( )
6274 XCTAssertEqual ( encoded, reEncoded)
75+ _ = try ! decoded. stringRepresentation ( )
76+ }
77+ filePaths
78+ . map { try ! Bencode . decodeFile ( atPath: $0) }
79+ . forEach { decoded in
80+ _ = try ! decoded. stringRepresentation ( )
6381 }
6482 }
6583}
6684
6785func compareToReencoding( _ param: String ) {
6886 let data = param. asciiData
69- let returnedData = try ! Bencode . decode ( data) . encoded ( )
70- XCTAssertEqual ( returnedData, data)
87+ let decoded = try ! Bencode . decode ( data)
88+ XCTAssertEqual ( data, decoded. encoded ( ) )
89+ XCTAssertEqual ( param, try ! decoded. stringRepresentation ( ) )
7190}
7291
7392func testParsing( _ param: String , _ compareTo: Bencode ) {
7493 XCTAssertEqual ( try ! Bencode . decode ( param. asciiData) , compareTo)
7594}
7695
77- func expectException( _ param: String ) {
96+ func expectException( _ param: String , _ f: ( Data , Data . Index ) throws -> ( Bencode , Data . Index ) = { data, index in try ( Bencode . decode ( data) , 0 ) } ) {
97+ do {
98+ let data = param. asciiData
99+ _ = try f ( data, data. startIndex)
100+ XCTFail ( )
101+ } catch { }
102+ }
103+
104+ func expectException< T> ( _ f: @autoclosure ( ) throws -> T ) {
78105 do {
79- _ = try Bencode . decode ( param . asciiData )
106+ _ = try f ( )
80107 XCTFail ( )
81108 } catch { }
82109}
0 commit comments