@@ -64,32 +64,63 @@ final class OrderJSONTests: XCTestCase {
6464 let jsonObj = try ? JsonParser . parse ( text: json)
6565 XCTAssertNotNil ( jsonObj)
6666 }
67+
68+ /// 包含转义字符
69+ //func testContactEscapeJson() {
70+ // let escapeJson = """
71+ // """
72+ // let unicodeJson = """
73+ // {"unicode":"\u4e2d\u6587"}
74+ // """
75+ // let jsonObj = try? JsonParser.parse(text: unicodeJson)
76+ // XCTAssertNotNil(jsonObj)
77+ //}
6778 // MARK: - 解析转化为 字典
68- let yapiJSON =
79+
80+ private func transformOrderJSON( ) -> OrderJSON ? {
81+ let json =
82+ """
83+ { " stringValue " : " 字符串 " , " intValue " :20, " doubleValue " :12.8, " boolValue " :false, " objectValue " :{ " objectKey1 " : " value1 " , " objectKey2 " :2, " objectKey3 " :{ " key " : " value " }}, " arrayValue1 " :[1,2,3], " arrayValue2 " :[{ " name " : " 小明 " , " age " :18}], " emptyArray " :[], " emptyObject " :{}, " nullValue " :null}
6984 """
70- { " type " : " object " , " title " : " empty object " , " properties " :{ " data " :{ " type " : " object " , " properties " :{ " stringValue " :{ " type " : " string " , " description " : " 字符串类型 " , " mock " :{ " mock " : " 字符串 " }}, " integerValue " :{ " type " : " integer " , " description " : " 整型数据类型 " , " mock " :{ " mock " : " 20 " }}, " numberValue " :{ " type " : " number " , " description " : " 浮点数据类型 " , " mock " :{ " mock " : " 15.5 " }}, " booleanValue " :{ " type " : " boolean " , " description " : " 布尔类型 " , " mock " :{ " mock " : " true " }}, " arrayValue " :{ " type " : " array " , " items " :{ " type " : " string " , " mock " :{ " mock " : " 数组字符串 " }}, " description " : " 数组类型 " }}, " required " :[ " stringValue " , " integerValue " , " numberValue " , " booleanValue " , " arrayValue " ]}, " code " :{ " type " : " string " , " mock " :{ " mock " : " 200 " }}}, " required " :[ " data " , " code " ]}
71- """
72- private func transformYapiData( ) -> OrderJSON ? {
73- try ? JsonParser . parse ( text: yapiJSON)
85+ return try ? JsonParser . parse ( text: json)
7486 }
7587 func testTransformYapiDataToJSON( ) {
76- let jsonObj = transformYapiData ( )
88+ let jsonObj = transformOrderJSON ( )
7789 XCTAssertNotNil ( jsonObj)
7890
7991 }
8092
81- // func testJSONAnyVale() {
82- // guard let jsonObj = transformYapiData() else {
83- // return
84- // }
85- // let anyValue = jsonObj.any()
86- // guard let dic = anyValue as? [String: Any] else {
87- // XCTAssertTrue(true, "通过YApij son字符串创建的JSON对象, 扩展 any() 方法返回值转字必须成功")
88- // return
89- // }
90- //
91- // XCTAssertTrue(dic["type"] as! String == "object")
92- // XCTAssertTrue(dic["required"] as! [String] == ["data","code"])
93- // }
94-
93+ private func transformStringToArray( _ string: String ) -> [ String ] {
94+ if string. isEmpty {
95+ return [ ]
96+ }
97+ return string. components ( separatedBy: " . " )
98+ }
99+
100+ /// 测试某路径下的 key 顺序
101+ func testJSONKeys( ) {
102+ guard let jsonObject = transformOrderJSON ( ) else {
103+ XCTAssertFalse ( false )
104+ return
105+ }
106+ let objectValuePath = " objectValue "
107+ let rootPath = " "
108+ let rootKeys = jsonObject. subKeysFor ( keyPath: transformStringToArray ( rootPath) )
109+ XCTAssertTrue ( rootKeys == [ " stringValue " , " intValue " , " doubleValue " , " boolValue " , " objectValue " , " arrayValue1 " , " arrayValue2 " , " emptyArray " , " emptyObject " , " nullValue " , ] )
110+ let dataKeysWhenRight = [ " objectKey1 " , " objectKey2 " , " objectKey3 " ]
111+ let dataKeys = jsonObject. subKeysFor ( keyPath: transformStringToArray ( objectValuePath) )
112+ XCTAssertTrue ( dataKeys == dataKeysWhenRight)
113+
114+ let stringValuePath = " stringValue "
115+ let stringValueKeys = jsonObject. subKeysFor ( keyPath: transformStringToArray ( stringValuePath) )
116+ XCTAssertTrue ( stringValueKeys. isEmpty)
117+
118+ let objectArrayValuePath = " arrayValue2 "
119+ let objectValuePathKeys = jsonObject. subKeysFor ( keyPath: transformStringToArray ( objectArrayValuePath) )
120+ XCTAssertTrue ( objectValuePathKeys == [ " name " , " age " ] )
121+
122+ let multiPath = " objectValue.objectKey3 "
123+ let multiPathPathKeys = jsonObject. subKeysFor ( keyPath: transformStringToArray ( multiPath) )
124+ XCTAssertTrue ( multiPathPathKeys == [ " key " ] )
125+ }
95126}
0 commit comments