diff --git a/FlexibleModelProtocol/FlexibleModelProtocol.xcodeproj/project.pbxproj b/FlexibleModelProtocol/FlexibleModelProtocol.xcodeproj/project.pbxproj index 0b19966..01c60fd 100644 --- a/FlexibleModelProtocol/FlexibleModelProtocol.xcodeproj/project.pbxproj +++ b/FlexibleModelProtocol/FlexibleModelProtocol.xcodeproj/project.pbxproj @@ -8,11 +8,15 @@ /* Begin PBXBuildFile section */ 78CE30692534674E0020E536 /* FlexibleModelProtocol.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 78CE305F2534674E0020E536 /* FlexibleModelProtocol.framework */; }; - 78CE306E2534674E0020E536 /* FlexibleModelProtocolTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 78CE306D2534674E0020E536 /* FlexibleModelProtocolTests.swift */; }; 78CE30702534674E0020E536 /* FlexibleModelProtocol.h in Headers */ = {isa = PBXBuildFile; fileRef = 78CE30622534674E0020E536 /* FlexibleModelProtocol.h */; settings = {ATTRIBUTES = (Public, ); }; }; 78CE307C2534677E0020E536 /* FlexibleModelProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 78CE307B2534677E0020E536 /* FlexibleModelProtocol.swift */; }; 78D95D7D2535B0FB009A379F /* HWXMLEelement.swift in Sources */ = {isa = PBXBuildFile; fileRef = 78D95D7B2535B0FB009A379F /* HWXMLEelement.swift */; }; 78D95D7E2535B0FB009A379F /* HWXMLParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 78D95D7C2535B0FB009A379F /* HWXMLParser.swift */; }; + 78D95D842535B5C9009A379F /* FromTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 78D95D832535B5C9009A379F /* FromTest.swift */; }; + 78D95D882535B5D4009A379F /* ToTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 78D95D872535B5D4009A379F /* ToTest.swift */; }; + 78D95D9F2535D7C2009A379F /* TestModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 78D95D9C2535D7C2009A379F /* TestModel.swift */; }; + 78D95DA02535D7C2009A379F /* SampleXML.xml in Resources */ = {isa = PBXBuildFile; fileRef = 78D95D9D2535D7C2009A379F /* SampleXML.xml */; }; + 78D95DA12535D7C2009A379F /* SampleJson.json in Resources */ = {isa = PBXBuildFile; fileRef = 78D95D9E2535D7C2009A379F /* SampleJson.json */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -30,11 +34,15 @@ 78CE30622534674E0020E536 /* FlexibleModelProtocol.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FlexibleModelProtocol.h; sourceTree = ""; }; 78CE30632534674E0020E536 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 78CE30682534674E0020E536 /* FlexibleModelProtocolTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = FlexibleModelProtocolTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; - 78CE306D2534674E0020E536 /* FlexibleModelProtocolTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FlexibleModelProtocolTests.swift; sourceTree = ""; }; 78CE306F2534674E0020E536 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 78CE307B2534677E0020E536 /* FlexibleModelProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FlexibleModelProtocol.swift; sourceTree = ""; }; 78D95D7B2535B0FB009A379F /* HWXMLEelement.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HWXMLEelement.swift; sourceTree = ""; }; 78D95D7C2535B0FB009A379F /* HWXMLParser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HWXMLParser.swift; sourceTree = ""; }; + 78D95D832535B5C9009A379F /* FromTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FromTest.swift; sourceTree = ""; }; + 78D95D872535B5D4009A379F /* ToTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ToTest.swift; sourceTree = ""; }; + 78D95D9C2535D7C2009A379F /* TestModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestModel.swift; sourceTree = ""; }; + 78D95D9D2535D7C2009A379F /* SampleXML.xml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = SampleXML.xml; sourceTree = ""; }; + 78D95D9E2535D7C2009A379F /* SampleJson.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = SampleJson.json; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -89,12 +97,24 @@ 78CE306C2534674E0020E536 /* FlexibleModelProtocolTests */ = { isa = PBXGroup; children = ( - 78CE306D2534674E0020E536 /* FlexibleModelProtocolTests.swift */, + 78D95D9B2535D7C2009A379F /* Mock */, 78CE306F2534674E0020E536 /* Info.plist */, + 78D95D832535B5C9009A379F /* FromTest.swift */, + 78D95D872535B5D4009A379F /* ToTest.swift */, ); path = FlexibleModelProtocolTests; sourceTree = ""; }; + 78D95D9B2535D7C2009A379F /* Mock */ = { + isa = PBXGroup; + children = ( + 78D95D9C2535D7C2009A379F /* TestModel.swift */, + 78D95D9D2535D7C2009A379F /* SampleXML.xml */, + 78D95D9E2535D7C2009A379F /* SampleJson.json */, + ); + path = Mock; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXHeadersBuildPhase section */ @@ -194,6 +214,8 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + 78D95DA02535D7C2009A379F /* SampleXML.xml in Resources */, + 78D95DA12535D7C2009A379F /* SampleJson.json in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -214,7 +236,9 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 78CE306E2534674E0020E536 /* FlexibleModelProtocolTests.swift in Sources */, + 78D95D9F2535D7C2009A379F /* TestModel.swift in Sources */, + 78D95D882535B5D4009A379F /* ToTest.swift in Sources */, + 78D95D842535B5C9009A379F /* FromTest.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/FlexibleModelProtocol/FlexibleModelProtocol/FlexibleModelProtocol.swift b/FlexibleModelProtocol/FlexibleModelProtocol/FlexibleModelProtocol.swift index f302473..04554e7 100644 --- a/FlexibleModelProtocol/FlexibleModelProtocol/FlexibleModelProtocol.swift +++ b/FlexibleModelProtocol/FlexibleModelProtocol/FlexibleModelProtocol.swift @@ -17,9 +17,9 @@ protocol FlexibleModelProtocol:Codable,Equatable { static func fromNSDictionary(nsDictionary:NSDictionary,object:T) -> T? func toJson() -> String - func toXML() -> String - func toDictionary() -> Dictionary - func toNSDictionary() -> NSDictionary + func toXML() -> String? + func toDictionary() -> Dictionary? + func toNSDictionary() -> NSDictionary? } @@ -84,9 +84,13 @@ extension FlexibleModelProtocol { } func toXML() -> String? { - let dic = toDictionary() - let parser:HWXMLParser = HWXMLParser() - return parser.toXMLString(dictionary: dic) + var returnValue:String? = nil + if let dic = toDictionary() { + let parser:HWXMLParser = HWXMLParser() + returnValue = parser.toXMLString(dictionary: dic) + } + + return returnValue } func toDictionary() -> Dictionary? { @@ -103,7 +107,11 @@ extension FlexibleModelProtocol { } func toNSDictionary() -> NSDictionary? { - return self.toDictionary() as NSDictionary + var returnValue:NSDictionary? = nil + if let dic = self.toDictionary() { + returnValue = dic as NSDictionary + } + return returnValue } } diff --git a/FlexibleModelProtocol/FlexibleModelProtocolTests/FlexibleModelProtocolTests.swift b/FlexibleModelProtocol/FlexibleModelProtocolTests/FlexibleModelProtocolTests.swift deleted file mode 100644 index 3b85751..0000000 --- a/FlexibleModelProtocol/FlexibleModelProtocolTests/FlexibleModelProtocolTests.swift +++ /dev/null @@ -1,33 +0,0 @@ -// -// FlexibleModelProtocolTests.swift -// FlexibleModelProtocolTests -// -// Created by hanwe on 2020/10/12. -// - -import XCTest -@testable import FlexibleModelProtocol - -class FlexibleModelProtocolTests: XCTestCase { - - override func setUpWithError() throws { - // Put setup code here. This method is called before the invocation of each test method in the class. - } - - override func tearDownWithError() throws { - // Put teardown code here. This method is called after the invocation of each test method in the class. - } - - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct results. - } - - func testPerformanceExample() throws { - // This is an example of a performance test case. - self.measure { - // Put the code you want to measure the time of here. - } - } - -} diff --git a/FlexibleModelProtocol/FlexibleModelProtocolTests/FromTest.swift b/FlexibleModelProtocol/FlexibleModelProtocolTests/FromTest.swift new file mode 100644 index 0000000..7bde82a --- /dev/null +++ b/FlexibleModelProtocol/FlexibleModelProtocolTests/FromTest.swift @@ -0,0 +1,85 @@ +// +// FromTest.swift +// FlexibleModelProtocolTests +// +// Created by hanwe on 2020/10/13. +// + +import UIKit +import XCTest +@testable import FlexibleModelProtocol + +class FromTest: XCTestCase { + override func setUpWithError() throws { + + } + + override func tearDownWithError() throws { + + } + + func testFromJson() { + if let path = Bundle(for: type(of: self)).path(forResource: "SampleJson", ofType: "json") { + do { + let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe) + if let myModel:MyModel = MyModel.fromJson(jsonData: data, object: MyModel()) { + let mainModel = myModel.lostArticleBizInfo + if mainModel.list_total_count == "" { + XCTAssertTrue(false) + } + if mainModel.result.code == "" { + XCTAssertTrue(false) + } + if mainModel.result.message == "" { + XCTAssertTrue(false) + } + if mainModel.row.isEmpty { + XCTAssertTrue(false) + } + XCTAssertTrue(true) + } + else { + XCTAssertTrue(false) + } + } catch { + XCTAssertTrue(false) + } + } + else { + XCTAssertTrue(false) + } + } + + func testFromXml() { + if let path = Bundle(for: type(of: self)).path(forResource: "SampleXML", ofType: "xml") { + do { + let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe) + if let myModel:MyModel = MyModel.fromXML(xmlData: data, object: MyModel()) { + let mainModel = myModel.lostArticleBizInfo + if mainModel.list_total_count == "" { + XCTAssertTrue(false) + } + if mainModel.result.code == "" { + XCTAssertTrue(false) + } + if mainModel.result.message == "" { + XCTAssertTrue(false) + } + if mainModel.row.isEmpty { + XCTAssertTrue(false) + } + XCTAssertTrue(true) + } + else { + XCTAssertTrue(false) + } + } catch { + XCTAssertTrue(false) + } + } + else { + XCTAssertTrue(false) + } + } + +} diff --git a/FlexibleModelProtocol/FlexibleModelProtocolTests/Mock/SampleJson.json b/FlexibleModelProtocol/FlexibleModelProtocolTests/Mock/SampleJson.json new file mode 100644 index 0000000..c8e7c04 --- /dev/null +++ b/FlexibleModelProtocol/FlexibleModelProtocolTests/Mock/SampleJson.json @@ -0,0 +1,41 @@ +{ + "lostArticleBizInfo": { + "list_total_count": "234", + "result": { + "code": "INFO-000", + "message": "Success" + }, + "row": [ + { + "idx": "580", + "code": "t1", + "location": "new york", + "company": "Coach" + }, + { + "idx": "520", + "code": "b2", + "location": "san francisco", + "company": "Facebook" + }, + { + "idx": "521", + "code": "b2", + "location": "paris", + "company": "Sandro" + }, + { + "idx": "588", + "code": "t1", + "location": "seoul", + "company": "Samsung" + }, + { + "idx": "589", + "code": "t1", + "location": "cupertino", + "company": "Apple" + } + ] + } +} diff --git a/FlexibleModelProtocol/FlexibleModelProtocolTests/Mock/SampleXML.xml b/FlexibleModelProtocol/FlexibleModelProtocolTests/Mock/SampleXML.xml new file mode 100644 index 0000000..4d679b6 --- /dev/null +++ b/FlexibleModelProtocol/FlexibleModelProtocolTests/Mock/SampleXML.xml @@ -0,0 +1,39 @@ + + + 234 + + INFO-000 + Success + + + 580 + t1 + new york + Coach + + + 520 + b2 + san francisco + Facebook + + + 521 + b2 + paris + Sandro + + + 588 + t1 + seoul + Samsung + + + 589 + t1 + cupertino + Apple + + + diff --git a/FlexibleModelProtocol/FlexibleModelProtocolTests/Mock/TestModel.swift b/FlexibleModelProtocol/FlexibleModelProtocolTests/Mock/TestModel.swift new file mode 100644 index 0000000..273f2ec --- /dev/null +++ b/FlexibleModelProtocol/FlexibleModelProtocolTests/Mock/TestModel.swift @@ -0,0 +1,32 @@ +// +// TestModel.swift +// FlexibleModelProtocolTests +// +// Created by hanwe on 2020/10/13. +// +@testable import FlexibleModelProtocol +import Foundation + + +struct MyModel: FlexibleModelProtocol { + var lostArticleBizInfo:MySubModel = MySubModel() +} +struct MySubModel: FlexibleModelProtocol { + var list_total_count:String = "" + var result:MyResultModel = MyResultModel() + var row:Array = Array() +} + +struct MyResultModel: FlexibleModelProtocol { + var code:String = "" + var message:String = "" +} + +struct MyRowModel: FlexibleModelProtocol { + var idx:String = "" + var code:String = "" + var location:String = "" + var company:String = "" +} + + diff --git a/FlexibleModelProtocol/FlexibleModelProtocolTests/ToTest.swift b/FlexibleModelProtocol/FlexibleModelProtocolTests/ToTest.swift new file mode 100644 index 0000000..3733b78 --- /dev/null +++ b/FlexibleModelProtocol/FlexibleModelProtocolTests/ToTest.swift @@ -0,0 +1,95 @@ +// +// ToTest.swift +// FlexibleModelProtocolTests +// +// Created by hanwe on 2020/10/13. +// + +import UIKit +import XCTest +@testable import FlexibleModelProtocol + +class ToTest: XCTestCase { + + + + var mockModel:MyModel? = nil + + override func setUpWithError() throws { + self.mockModel = makeMockModel() + } + + override func tearDownWithError() throws { + } + + func makeMockModel() -> MyModel { + var myModel:MyModel = MyModel() + var mySubModel:MySubModel = MySubModel() + let myRowModel1:MyRowModel = MyRowModel(idx: "3832", code: "a1", location: "London", company: "kangol") + let myRowModel2:MyRowModel = MyRowModel(idx: "1138", code: "b6", location: "Seoul", company: "LG") + mySubModel.row.append(myRowModel1) + mySubModel.row.append(myRowModel2) + let myResultModel:MyResultModel = MyResultModel(code: "200", message: "Success") + mySubModel.result = myResultModel + mySubModel.list_total_count = "101" + myModel.lostArticleBizInfo = mySubModel + return myModel + } + + func testToJson() { + guard let mock = self.mockModel else { + XCTAssertTrue(false) + return + } + let jsonString:String = mock.toJson() + if jsonString == "" { + XCTAssertTrue(false) + } + else { + print("jsonString:\(jsonString)") + XCTAssertTrue(true) + } + } + + func testToXml() { + guard let mock = self.mockModel else { + XCTAssertTrue(false) + return + } + if let xmlString:String = mock.toXML() { + print("xmlString:\(xmlString)") + XCTAssertTrue(true) + } + else { + XCTAssertTrue(false) + } + } + + func testToDictionary() { + guard let mock = self.mockModel else { + XCTAssertTrue(false) + return + } + if let dic:[String:Any] = mock.toDictionary() { + print("dictionary:\(dic)") + XCTAssertTrue(true) + } + else { + XCTAssertTrue(false) + } + } + + func testToNSDictionary() { + guard let mock = self.mockModel else { + XCTAssertTrue(false) + return + } + if let nsDic:NSDictionary = mock.toNSDictionary() { + print("NSDictionary:\(nsDic)") + XCTAssertTrue(true) + } + else { + XCTAssertTrue(false) + } + } +}