Skip to content

Commit

Permalink
Test coverage back to 99% and array compare fix
Browse files Browse the repository at this point in the history
  • Loading branch information
evermeer committed Dec 21, 2015
1 parent 474851a commit 30622f0
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 8 deletions.
2 changes: 1 addition & 1 deletion EVReflection.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Pod::Spec.new do |s|
#

s.name = "EVReflection"
s.version = "2.13.2"
s.version = "2.14.0"
s.summary = "iOS: Swift helper library with reflection functions"
s.description = "Swift helper library with reflection functions including support for NSCoding, Printable, Hashable, Equatable and JSON"
s.homepage = "https://github.com/evermeer/EVReflection"
Expand Down
12 changes: 11 additions & 1 deletion EVReflection/EVReflection.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
7F7BBC661C009EE90046F05E /* EVReflectionMappingTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F7BBC651C009EE80046F05E /* EVReflectionMappingTest.swift */; };
7F7BBC671C00A22B0046F05E /* EVReflectionMappingTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F7BBC651C009EE80046F05E /* EVReflectionMappingTest.swift */; };
7F8818F91C04447100E17072 /* EVReflectionPropertyMappingTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F8818F81C04447100E17072 /* EVReflectionPropertyMappingTest.swift */; };
7F9336951C23F2EB00AF7076 /* EVReflectionEVObjectTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F59F9DA1C07999300C14CF9 /* EVReflectionEVObjectTests.swift */; };
7F9336961C23F2F100AF7076 /* EVReflectionPropertyMappingTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F8818F81C04447100E17072 /* EVReflectionPropertyMappingTest.swift */; };
7F9336971C23F2F700AF7076 /* EVReflectionAssociatedTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F59F9D81C07986200C14CF9 /* EVReflectionAssociatedTests.swift */; };
7F9336981C23F2FD00AF7076 /* EVRelfectionEnheritanceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F59F9DE1C09BFB900C14CF9 /* EVRelfectionEnheritanceTests.swift */; };
7F9336991C23F33700AF7076 /* EVReflectionNumbersTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F59F9DC1C079A7300C14CF9 /* EVReflectionNumbersTests.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -126,11 +131,11 @@
7F59F9D81C07986200C14CF9 /* EVReflectionAssociatedTests.swift */,
7F41ECA91B91F306003A2236 /* ProfilePhotoUrlListTest.swift */,
7F59F9DE1C09BFB900C14CF9 /* EVRelfectionEnheritanceTests.swift */,
7F59F9DC1C079A7300C14CF9 /* EVReflectionNumbersTests.swift */,
7F41ECAB1B91F306003A2236 /* EVReflectionWorkaroundsTests.swift */,
7F41ECAC1B91F306003A2236 /* EVReflectionWorkaroundSwiftGenericsTests.swift */,
7F43822A1B62D41E00EF74C7 /* TemporaryTests */,
7F07C5761AF101A9000DF1F3 /* Supporting Files */,
7F59F9DC1C079A7300C14CF9 /* EVReflectionNumbersTests.swift */,
);
path = EVReflectionTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -287,17 +292,22 @@
files = (
7F3E31961BB909A7001E6788 /* EVReflectionWorkaroundsTests.swift in Sources */,
7F3E31971BB909A7001E6788 /* EVReflectionWorkaroundSwiftGenericsTests.swift in Sources */,
7F9336991C23F33700AF7076 /* EVReflectionNumbersTests.swift in Sources */,
7F3E31901BB909A1001E6788 /* EVExtensions.swift in Sources */,
7F3E31931BB909A7001E6788 /* EVReflectionJsonTests.swift in Sources */,
7F9336981C23F2FD00AF7076 /* EVRelfectionEnheritanceTests.swift in Sources */,
7F3E31981BB909B1001E6788 /* WorkaroundEnumTest.swift in Sources */,
7F9336951C23F2EB00AF7076 /* EVReflectionEVObjectTests.swift in Sources */,
7F7BBC671C00A22B0046F05E /* EVReflectionMappingTest.swift in Sources */,
7F3E31911BB909A7001E6788 /* TestObjects.swift in Sources */,
7F3E318D1BB909A1001E6788 /* EVReflection.swift in Sources */,
7F3E318F1BB909A1001E6788 /* EVObjectDescription.swift in Sources */,
7F3E31951BB909A7001E6788 /* ProfilePhotoUrlListTest.swift in Sources */,
7F3E31941BB909A7001E6788 /* EVReflectionTests.swift in Sources */,
7F9336961C23F2F100AF7076 /* EVReflectionPropertyMappingTest.swift in Sources */,
7F3E31921BB909A7001E6788 /* EVObjectDescriptionTests.swift in Sources */,
7F3E318E1BB909A1001E6788 /* EVObject.swift in Sources */,
7F9336971C23F2F700AF7076 /* EVReflectionAssociatedTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
1 change: 0 additions & 1 deletion EVReflection/EVReflectionTests/EVReflectionJsonTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ class EVReflectionJsonTests: XCTestCase {
XCTAssertEqual(a.myFloat, 0, "myFloat should contain 2.1")
XCTAssertEqual(a.myBool, true, "myBool should contain true")
}


}

Expand Down
54 changes: 54 additions & 0 deletions EVReflection/EVReflectionTests/EVReflectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,60 @@ class EVReflectionTests: XCTestCase {
XCTAssert(theObject != result, "Pass") // The objects are not the same
}


/**
Get a dictionary from an object, then create an object of a diffrent type and set the properties based on the dictionary from the first object. You can initiate a diffrent type. Only the properties with matching dictionary keys will be set.
*/
func testClassToJsonWithDateFormatter() {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyyMMddHHmmss"
EVReflection.setDateFormatter(dateFormatter)

let theObject = TestObject4()
theObject.myString = "string"
theObject.myInt = 4
theObject.myDate = NSDate()
let json = theObject.toJsonString()
NSLog("toJson = \(json)")
XCTAssert(!(json.containsString(".") || json.containsString("/") || json.containsString("-")), "Pass") // The objects are not the same

let newObject = TestObject4(json: json)
XCTAssertEqual(theObject, newObject, "Should still be the same")
theObject.myDate = NSDate().dateByAddingTimeInterval(3600)
XCTAssert(theObject != newObject, "Should not be the same")
}

func testArrayPropertyCompare() {
let theObject = TestObject4()
theObject.myString = "string"
theObject.myInt = 4
let dict = theObject.toDictionary()
NSLog("toDict = \(dict)")
let newObject = TestObject4(dictionary: dict)

theObject.array.append(TestObject2())
XCTAssert(theObject != newObject, "Should not be the same")
newObject.array.append(TestObject2())
XCTAssert(theObject == newObject, "Should be the same again")
(theObject.array[0]).objectValue = "X"
XCTAssert(theObject != newObject, "Should not be the same")
(newObject.array[0]).objectValue = "X"
XCTAssert(theObject == newObject, "Should be the same again")

theObject.array3[0] = "Y"
XCTAssert(theObject != newObject, "Should not be the same")
newObject.array3[0] = "Y"
XCTAssert(theObject == newObject, "Should be the same again")
}

func testXMLDictStructure() {
// When using XMLDict, an array will be nested in a singel xml node which you probably want to skip in your object structure
let xmlDict = ["myString": "STR", "array": ["object2":[["objectValue":"STR2"], ["objectValue":"STR3"]]]]
let obj = TestObject4(dictionary: xmlDict)
XCTAssertEqual(obj.myString, "STR", "object myString value should have been STR")
XCTAssertEqual(obj.array.count, 2, "There should be 1 vallue in the array")
XCTAssertEqual(obj.array[0].objectValue, "STR2", "The first array object myString value should have been STR2")
}

/**
Test if we can work with an object that contains all types of arrays
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class EVRelfectionEnheritanceTests: XCTestCase {
let quz = Quz()
quz.fooBar = Bar()
quz.fooBaz = Baz()
quz.fooArray = [Bar(), Baz()]
quz.fooFoo = Foo()
quz.fooArray = [Bar(), Baz(), Foo()]

// The object JSON
let json = quz.toJsonString()
Expand All @@ -60,6 +61,7 @@ class Quz: EVObject {
var fooArray: Array<Foo> = []
var fooBar: Foo?
var fooBaz: Foo?
var fooFoo: Foo?
}

class Foo: EVObject {
Expand Down
6 changes: 6 additions & 0 deletions EVReflection/EVReflectionTests/TestObjects.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ public class TestObject4: EVObject {
var myNSNumber: NSNumber?
var invalid_character: String?
var list: [TestObject4] = []
var myAny: Any?
var myAnyObject: AnyObject?
var rect: CGRect?
var array: [TestObject2] = [TestObject2(), TestObject2()]
var array2: NSMutableArray = NSMutableArray()
var array3: [String] = ["test"]
}


Expand Down
Binary file modified EVReflection/coverage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 26 additions & 4 deletions EVReflection/pod/EVReflection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ final public class EVReflection {
var original:NSObject? = nil
if mapping != nil {
original = properties[mapping!] as? NSObject

}
if let dictValue = dictionaryAndArrayConversion(types[mapping ?? k as! String], original: original, dictValue: v) {
if let key:String = keyMapping[k as! String] {
Expand Down Expand Up @@ -274,6 +275,19 @@ final public class EVReflection {
let (lhsdict,_) = toDictionary(lhs, performKeyCleanup:false)
let (rhsdict,_) = toDictionary(rhs, performKeyCleanup:false)

return dictionariesAreEqual(lhsdict, rhsdict: rhsdict)
}


/**
Compare 2 dictionaries

- parameter lhsdict: Compare this dictionary
- parameter rhsdict: Compare with this dictionary

- returns: Are the dictionaries equal or not
*/
public class func dictionariesAreEqual(lhsdict: NSDictionary, rhsdict: NSDictionary) -> Bool {
for (key, value) in rhsdict {
if let compareTo = lhsdict[key as! String] {
if let dateCompareTo = compareTo as? NSDate, dateValue = value as? NSDate {
Expand All @@ -283,9 +297,19 @@ final public class EVReflection {
return false
}
} else if let array = compareTo as? NSArray {
guard let arr = value as? NSArray else { return false }
if arr.count != array.count {
return false
}
for (index, arrayValue) in array.enumerate() {
if !areEqual(arrayValue as! NSObject, rhs: (value as! NSArray)[index] as! NSObject) {
return false
if arrayValue as? NSDictionary != nil {
if !dictionariesAreEqual(arrayValue as! NSDictionary, rhsdict: arr[index] as! NSDictionary) {
return false
}
} else {
if !arrayValue.isEqual(arr[index]) {
return false
}
}
}
} else if !compareTo.isEqual(value) {
Expand All @@ -296,7 +320,6 @@ final public class EVReflection {
return true
}


// MARK: - Reflection helper functions

/**
Expand Down Expand Up @@ -877,7 +900,6 @@ final public class EVReflection {
return "\(value)"
}
}

}


0 comments on commit 30622f0

Please sign in to comment.