@@ -40,7 +40,7 @@ class ModelRocketTests: XCTestCase {
40
40
// Put setup code here. This method is called before the invocation of each test method in the class.
41
41
42
42
// Setup test model
43
- var jsonString = " { \" string \" : \" Test string \" , \" date \" : \" 2015-02-04T18:30:15.000Z \" , \" color \" : \" #00FF00 \" , \" bool \" : true, \" url \" : \" http://ovenbits.com \" , \" number \" : 3, \" double \" : 7.5, \" float \" : 4.75, \" int \" : -23, \" u_int \" : 25} "
43
+ var jsonString = " { \" string \" : \" Test string \" , \" date \" : \" 2015-02-04T18:30:15.000Z \" , \" color \" : \" #00FF00 \" , \" bool \" : true, \" url \" : \" http://ovenbits.com \" , \" number \" : 3, \" double \" : 7.5, \" float \" : 4.75, \" int \" : -23, \" u_int \" : 25, \" string_enum \" : \" String1 \" , \" int_enum \" : 0 }"
44
44
var jsonData = jsonString. dataUsingEncoding ( NSUTF8StringEncoding, allowLossyConversion: false )
45
45
var json = JSON ( data: jsonData!)
46
46
testModel = TestModel ( json: json)
@@ -93,6 +93,32 @@ class ModelRocketTests: XCTestCase {
93
93
super. tearDown ( )
94
94
}
95
95
96
+ func testProperty( ) {
97
+ XCTAssertEqual ( testModel. string. type, " String " , " Types not equal " )
98
+ XCTAssertEqual ( testModel. string [ ] , " Test string " )
99
+ XCTAssertEqual ( testModel. string. hashValue, " string " . hashValue, " Hash values not equal " )
100
+ }
101
+
102
+ func testEquatableProperty( ) {
103
+ let dateFormatter = NSDateFormatter ( )
104
+ dateFormatter. dateFormat = " yyyy-MM-dd'T'HH:mm:ss.SSS'Z' "
105
+ let date = dateFormatter. dateFromString ( " 2015-02-04T18:30:15.000Z " )
106
+
107
+ XCTAssertTrue ( testModel. string == Property < String > ( key: " string " , defaultValue: " Test string " ) , " Properties not equal " )
108
+ XCTAssertTrue ( testModel. date == Property < NSDate > ( key: " date " , defaultValue: date) , " Properties not equal " )
109
+ XCTAssertTrue ( testModel. color == Property < UIColor > ( key: " color " , defaultValue: . greenColor( ) ) , " Properties not equal " )
110
+ XCTAssertTrue ( testModel. bool == Property < Bool > ( key: " bool " , defaultValue: true ) , " Properties not equal " )
111
+ XCTAssertTrue ( testModel. url == Property < NSURL > ( key: " url " , defaultValue: NSURL ( string: " http://ovenbits.com " ) ) , " Properties not equal " )
112
+ XCTAssertTrue ( testModel. number == Property < NSNumber > ( key: " number " , defaultValue: NSNumber ( int: 3 ) ) , " Properties not equal " )
113
+ XCTAssertTrue ( testModel. double == Property < Double > ( key: " double " , defaultValue: 7.5 ) , " Properties not equal " )
114
+ XCTAssertTrue ( testModel. float == Property < Float > ( key: " float " , defaultValue: 4.75 ) , " Properties not equal " )
115
+ XCTAssertTrue ( testModel. int == Property < Int > ( key: " int " , defaultValue: - 23 ) , " Properties not equal " )
116
+ XCTAssertTrue ( testModel. uInt == Property < UInt > ( key: " u_int " , defaultValue: 25 ) , " Properties not equal " )
117
+ XCTAssertTrue ( testModel. stringEnum == Property < TestStringEnum > ( key: " string_enum " , defaultValue: . String1) , " Properties not equal " )
118
+ XCTAssertTrue ( testModel. intEnum == Property < TestIntEnum > ( key: " int_enum " , defaultValue: . Int1) , " Properties not equal " )
119
+ XCTAssertFalse ( testModel. string == Property < String > ( key: " string " , defaultValue: " Test string here " ) , " Properties shouldn't be equal " )
120
+ }
121
+
96
122
func testString( ) {
97
123
if let string = testModel. string. value {
98
124
XCTAssertEqual ( string, " Test string " , " Strings not equal " )
@@ -141,7 +167,7 @@ class ModelRocketTests: XCTestCase {
141
167
142
168
func testURL( ) {
143
169
if let url = testModel. url. value {
144
- XCTAssertEqual ( url, NSURL ( string: " http://ovenbits.com " ) ! , " URLs not equal " )
170
+ XCTAssertEqual ( url, NSURL ( string: " http://ovenbits.com " ) , " URLs not equal " )
145
171
}
146
172
else {
147
173
XCTAssert ( false , " Test URL should not be nil " )
@@ -201,7 +227,7 @@ class ModelRocketTests: XCTestCase {
201
227
XCTAssertEqual ( json [ " bool " ] . boolValue, true , " Bools not equal " )
202
228
203
229
if let url = json [ " url " ] . URL {
204
- XCTAssertEqual ( url, NSURL ( string: " http://ovenbits.com " ) ! , " URLs not equal " )
230
+ XCTAssertEqual ( url, NSURL ( string: " http://ovenbits.com " ) , " URLs not equal " )
205
231
}
206
232
else {
207
233
XCTAssert ( false , " URL should not be nil " )
@@ -248,7 +274,7 @@ class ModelRocketTests: XCTestCase {
248
274
if let bool = unarchived. bool. value { XCTAssertEqual ( bool, true , " Bools not equal " ) }
249
275
else { XCTAssert ( false , " Coding: bool should not be nil " ) }
250
276
251
- if let url = unarchived. url. value { XCTAssertEqual ( url, NSURL ( string: " http://ovenbits.com " ) ! , " URLs not equal " ) }
277
+ if let url = unarchived. url. value { XCTAssertEqual ( url, NSURL ( string: " http://ovenbits.com " ) , " URLs not equal " ) }
252
278
else { XCTAssert ( false , " Coding: url should not be nil " ) }
253
279
254
280
if let number = unarchived. number. value { XCTAssertEqual ( number, NSNumber ( int: 3 ) , " Numbers not equal " ) }
@@ -277,6 +303,19 @@ class ModelRocketTests: XCTestCase {
277
303
XCTAssertEqual ( testArrayModel. strings. values [ 1 ] , " string2 " , " Strings not equal " )
278
304
XCTAssertEqual ( testArrayModel. strings. values [ 2 ] , " string3 " , " Strings not equal " )
279
305
XCTAssertEqual ( testArrayModel. strings. values [ 3 ] , " string4 " , " Strings not equal " )
306
+
307
+ XCTAssertEqual ( testArrayModel. strings. type, " String " , " Types not equal " )
308
+ XCTAssertEqual ( testArrayModel. strings. count, 4 , " Counts not equal " )
309
+ XCTAssertEqual ( testArrayModel. strings [ 0 ] , " string1 " , " Strings not equal " )
310
+ XCTAssertEqual ( testArrayModel. strings. first, " string1 " , " Strings not equal " )
311
+ XCTAssertEqual ( testArrayModel. strings. last, " string4 " , " Strings not equal " )
312
+ XCTAssertEqual ( testArrayModel. strings. hashValue, " strings " . hashValue, " Hash values not equal " )
313
+
314
+ let property = PropertyArray < String > ( key: " strings " , defaultValues: [ " string1 " , " string2 " , " string3 " , " string4 " ] )
315
+ XCTAssertTrue ( testArrayModel. strings == property, " Properties not equal " )
316
+
317
+ property. values = [ " string1 " ]
318
+ XCTAssertFalse ( testArrayModel. strings == property, " Properties shouldn't be equal " )
280
319
}
281
320
282
321
func testArrayJSON( ) {
@@ -341,6 +380,17 @@ class ModelRocketTests: XCTestCase {
341
380
342
381
if let int3 = testDictionaryModel. ints. values [ " int3 " ] { XCTAssertEqual ( int3, 3 , " Ints not equal " ) }
343
382
else { XCTAssert ( false , " Dictionary: int1 should not be nil " ) }
383
+
384
+ XCTAssertEqual ( testDictionaryModel. ints. type, " Int " , " Type not equal " )
385
+ XCTAssertEqual ( testDictionaryModel. ints. count, 3 , " Counts not equal " )
386
+ XCTAssertEqual ( testDictionaryModel. ints [ " int1 " ] , 1 , " Ints not equal " )
387
+ XCTAssertEqual ( testDictionaryModel. ints. hashValue, " ints " . hashValue, " Hash values not equal " )
388
+
389
+ let property = PropertyDictionary < Int > ( key: " ints " , defaultValues: [ " int1 " : 1 , " int2 " : 2 , " int3 " : 3 ] )
390
+ XCTAssertTrue ( testDictionaryModel. ints == property, " Properties not equal " )
391
+
392
+ property. values = [ " int1 " : 1 ]
393
+ XCTAssertFalse ( testDictionaryModel. ints == property, " Properties shouldn't be equal " )
344
394
}
345
395
346
396
func testDictionaryJSON( ) {
@@ -430,7 +480,7 @@ class ModelRocketTests: XCTestCase {
430
480
XCTAssertEqual ( json [ " bool " ] . boolValue, true , " Bools not equal " )
431
481
432
482
if let jsonURL = json [ " url " ] . URL {
433
- XCTAssertEqual ( jsonURL, NSURL ( string: " http://ovenbits.com " ) ! , " URLs not equal " )
483
+ XCTAssertEqual ( jsonURL, NSURL ( string: " http://ovenbits.com " ) , " URLs not equal " )
434
484
}
435
485
else {
436
486
XCTAssert ( false , " URL should not be nil " )
@@ -488,21 +538,44 @@ class ModelRocketTests: XCTestCase {
488
538
}
489
539
}
490
540
541
+ func testStringEnumTransformable( ) {
542
+ XCTAssertEqual ( TestStringEnum . fromJSON ( " String1 " ) , TestStringEnum . String1)
543
+ XCTAssertEqual ( TestStringEnum . fromJSON ( " String2 " ) , TestStringEnum . String2)
544
+
545
+ let string1 = TestStringEnum . String1. toJSON ( ) as! String
546
+ XCTAssertEqual ( string1, " String1 " )
547
+
548
+ let string2 = TestStringEnum . String2. toJSON ( ) as! String
549
+ XCTAssertEqual ( string2, " String2 " )
550
+ }
551
+
552
+ func testIntEnumTransformable( ) {
553
+ XCTAssertEqual ( TestIntEnum . fromJSON ( 0 ) , TestIntEnum . Int1)
554
+ XCTAssertEqual ( TestIntEnum . fromJSON ( 1 ) , TestIntEnum . Int2)
555
+
556
+ let int1 = TestIntEnum . Int1. toJSON ( ) as! Int
557
+ XCTAssertEqual ( int1, 0 )
558
+
559
+ let int2 = TestIntEnum . Int2. toJSON ( ) as! Int
560
+ XCTAssertEqual ( int2, 1 )
561
+ }
491
562
}
492
563
493
564
// MARK: - Models
494
565
495
566
class TestModel: Model {
496
- let string = Property < String > ( key: " string " )
497
- let date = Property < NSDate > ( key: " date " )
498
- let color = Property < UIColor > ( key: " color " )
499
- let bool = Property < Bool > ( key: " bool " )
500
- let url = Property < NSURL > ( key: " url " )
501
- let number = Property < NSNumber > ( key: " number " )
502
- let double = Property < Double > ( key: " double " )
503
- let float = Property < Float > ( key: " float " )
504
- let int = Property < Int > ( key: " int " )
505
- let uInt = Property < UInt > ( key: " u_int " )
567
+ let string = Property < String > ( key: " string " )
568
+ let date = Property < NSDate > ( key: " date " )
569
+ let color = Property < UIColor > ( key: " color " )
570
+ let bool = Property < Bool > ( key: " bool " )
571
+ let url = Property < NSURL > ( key: " url " )
572
+ let number = Property < NSNumber > ( key: " number " )
573
+ let double = Property < Double > ( key: " double " )
574
+ let float = Property < Float > ( key: " float " )
575
+ let int = Property < Int > ( key: " int " )
576
+ let uInt = Property < UInt > ( key: " u_int " )
577
+ let stringEnum = Property < TestStringEnum > ( key: " string_enum " )
578
+ let intEnum = Property < TestIntEnum > ( key: " int_enum " )
506
579
}
507
580
508
581
class TestArrayModel: Model {
@@ -540,3 +613,11 @@ class TestRequiredModel: Model {
540
613
let requiredString = Property < String > ( key: " required_string " , required: true )
541
614
let unrequiredInt = Property < Int > ( key: " unrequired_int " )
542
615
}
616
+
617
+ enum TestStringEnum: String, JSONTransformable {
618
+ case String1, String2
619
+ }
620
+
621
+ enum TestIntEnum : Int , JSONTransformable {
622
+ case Int1, Int2
623
+ }
0 commit comments