@@ -89,7 +89,7 @@ class PerfectLibTests: XCTestCase {
8989 XCTAssert ( t1 == 3 )
9090 }
9191
92- func testJSONConvertibleObject ( ) {
92+ func testJSONConvertibleObject1 ( ) {
9393
9494 class Test : JSONConvertibleObject {
9595
@@ -117,6 +117,53 @@ class PerfectLibTests: XCTestCase {
117117 XCTAssert ( false , " Exception \( error) " )
118118 }
119119 }
120+
121+
122+
123+ func testJSONConvertibleObject2( ) {
124+
125+ class User : JSONConvertibleObject {
126+ static let registerName = " user "
127+ var firstName = " "
128+ var lastName = " "
129+ var age = 0
130+ override func setJSONValues( _ values: [ String : Any ] ) {
131+ self . firstName = getJSONValue ( named: " firstName " , from: values, defaultValue: " " )
132+ self . lastName = getJSONValue ( named: " lastName " , from: values, defaultValue: " " )
133+ self . age = getJSONValue ( named: " age " , from: values, defaultValue: 0 )
134+ }
135+ override func getJSONValues( ) -> [ String : Any ] {
136+ return [
137+ JSONDecoding . objectIdentifierKey: User . registerName,
138+ " firstName " : firstName,
139+ " lastName " : lastName,
140+ " age " : age
141+ ]
142+ }
143+ }
144+
145+ // register the class. do this once
146+ JSONDecoding . registerJSONDecodable ( name: User . registerName, creator: { return User ( ) } )
147+
148+ // encode and decode the object
149+ let user = User ( )
150+ user. firstName = " Donnie "
151+ user. lastName = " Darko "
152+ user. age = 17
153+
154+ do {
155+ let encoded = try user. jsonEncodedString ( )
156+ print ( encoded)
157+
158+ guard let user2 = try encoded. jsonDecode ( ) as? User else {
159+ return XCTAssert ( false , " Invalid object \( encoded) " )
160+ }
161+
162+ XCTAssert ( user. firstName == user2. firstName)
163+ XCTAssert ( user. lastName == user2. lastName)
164+ XCTAssert ( user. age == user2. age)
165+ } catch { }
166+ }
120167
121168 func testJSONEncodeDecode( ) {
122169
@@ -524,14 +571,40 @@ class PerfectLibTests: XCTestCase {
524571 XCTAssert ( false , " Error testing file perms: \( error) " )
525572 }
526573 }
574+
575+ func testBytesIO( ) {
576+ let i8 = 254 as UInt8
577+ let i16 = 54045 as UInt16
578+ let i32 = 4160745471 as UInt32
579+ let i64 = 17293541094125989887 as UInt64
580+
581+ let bytes = Bytes ( )
582+
583+ bytes. import64Bits ( from: i64)
584+ . import32Bits ( from: i32)
585+ . import16Bits ( from: i16)
586+ . import8Bits ( from: i8)
587+
588+ let bytes2 = Bytes ( )
589+ bytes2. importBytes ( from: bytes)
590+
591+ XCTAssert ( i64 == bytes2. export64Bits ( ) )
592+ XCTAssert ( i32 == bytes2. export32Bits ( ) )
593+ XCTAssert ( i16 == bytes2. export16Bits ( ) )
594+ bytes2. position -= sizeof ( UInt16 . self)
595+ XCTAssert ( i16 == bytes2. export16Bits ( ) )
596+ XCTAssert ( bytes2. availableExportBytes == 1 )
597+ XCTAssert ( i8 == bytes2. export8Bits ( ) )
598+ }
527599}
528600
529601extension PerfectLibTests {
530602 static var allTests : [ ( String , ( PerfectLibTests ) -> ( ) throws -> Void ) ] {
531603 return [
532604 ( " testConcurrentQueue " , testConcurrentQueue) ,
533605 ( " testSerialQueue " , testSerialQueue) ,
534- ( " testJSONConvertibleObject " , testJSONConvertibleObject) ,
606+ ( " testJSONConvertibleObject1 " , testJSONConvertibleObject1) ,
607+ ( " testJSONConvertibleObject2 " , testJSONConvertibleObject2) ,
535608 ( " testJSONEncodeDecode " , testJSONEncodeDecode) ,
536609 ( " testJSONDecodeUnicode " , testJSONDecodeUnicode) ,
537610 ( " testNetSendFile " , testNetSendFile) ,
@@ -555,7 +628,9 @@ extension PerfectLibTests {
555628 ( " testDirForEach " , testDirForEach) ,
556629
557630 ( " testFilePerms " , testFilePerms) ,
558- ( " testDirPerms " , testDirPerms)
631+ ( " testDirPerms " , testDirPerms) ,
632+
633+ ( " testBytesIO " , testBytesIO)
559634 ]
560635 }
561636}
0 commit comments