JSONValueTests.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Copyright 2024 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import XCTest
  15. @testable import FirebaseAILogic
  16. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  17. final class JSONValueTests: XCTestCase {
  18. let decoder = JSONDecoder()
  19. let encoder = JSONEncoder()
  20. let numberKey = "pi"
  21. let numberValue = 3.14159
  22. let numberValueEncoded = "3.14159"
  23. let stringKey = "hello"
  24. let stringValue = "Hello, world!"
  25. override func setUp() {
  26. encoder.outputFormatting = .sortedKeys
  27. }
  28. func testDecodeNull() throws {
  29. let jsonData = try XCTUnwrap("null".data(using: .utf8))
  30. let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))
  31. XCTAssertEqual(jsonObject, .null)
  32. }
  33. func testDecodeNumber() throws {
  34. let jsonData = try XCTUnwrap("\(numberValue)".data(using: .utf8))
  35. let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))
  36. XCTAssertEqual(jsonObject, .number(numberValue))
  37. }
  38. func testDecodeString() throws {
  39. let jsonData = try XCTUnwrap("\"\(stringValue)\"".data(using: .utf8))
  40. let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))
  41. XCTAssertEqual(jsonObject, .string(stringValue))
  42. }
  43. func testDecodeBool() throws {
  44. let expectedBool = true
  45. let jsonData = try XCTUnwrap("\(expectedBool)".data(using: .utf8))
  46. let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))
  47. XCTAssertEqual(jsonObject, .bool(expectedBool))
  48. }
  49. func testDecodeObject() throws {
  50. let expectedObject: JSONObject = [
  51. numberKey: .number(numberValue),
  52. stringKey: .string(stringValue),
  53. ]
  54. let json = """
  55. {
  56. "\(numberKey)": \(numberValue),
  57. "\(stringKey)": "\(stringValue)"
  58. }
  59. """
  60. let jsonData = try XCTUnwrap(json.data(using: .utf8))
  61. let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))
  62. XCTAssertEqual(jsonObject, .object(expectedObject))
  63. }
  64. func testDecodeArray() throws {
  65. let expectedArray: [JSONValue] = [.null, .number(numberValue)]
  66. let jsonData = try XCTUnwrap("[ null, \(numberValue) ]".data(using: .utf8))
  67. let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))
  68. XCTAssertEqual(jsonObject, .array(expectedArray))
  69. }
  70. func testEncodeNull() throws {
  71. let jsonData = try encoder.encode(JSONValue.null)
  72. let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
  73. XCTAssertEqual(json, "null")
  74. }
  75. func testDecodeNestedObject() throws {
  76. let nestedObject: JSONObject = [
  77. "nestedKey": .string("nestedValue"),
  78. ]
  79. let expectedObject: JSONObject = [
  80. "numberKey": .number(numberValue),
  81. "objectKey": .object(nestedObject),
  82. ]
  83. let json = """
  84. {
  85. "numberKey": \(numberValue),
  86. "objectKey": {
  87. "nestedKey": "nestedValue"
  88. }
  89. }
  90. """
  91. let jsonData = try XCTUnwrap(json.data(using: .utf8))
  92. let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))
  93. XCTAssertEqual(jsonObject, .object(expectedObject))
  94. }
  95. func testDecodeNestedArray() throws {
  96. let nestedArray: [JSONValue] = [.string("a"), .string("b")]
  97. let expectedObject: JSONObject = [
  98. "numberKey": .number(numberValue),
  99. "arrayKey": .array(nestedArray),
  100. ]
  101. let json = """
  102. {
  103. "numberKey": \(numberValue),
  104. "arrayKey": ["a", "b"]
  105. }
  106. """
  107. let jsonData = try XCTUnwrap(json.data(using: .utf8))
  108. let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))
  109. XCTAssertEqual(jsonObject, .object(expectedObject))
  110. }
  111. func testEncodeNumber() throws {
  112. let jsonData = try encoder.encode(JSONValue.number(numberValue))
  113. let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
  114. XCTAssertEqual(json, "\(numberValue)")
  115. }
  116. func testEncodeString() throws {
  117. let jsonData = try encoder.encode(JSONValue.string(stringValue))
  118. let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
  119. XCTAssertEqual(json, "\"\(stringValue)\"")
  120. }
  121. func testEncodeBool() throws {
  122. let boolValue = true
  123. let jsonData = try encoder.encode(JSONValue.bool(boolValue))
  124. let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
  125. XCTAssertEqual(json, "\(boolValue)")
  126. }
  127. func testEncodeObject() throws {
  128. let objectValue: JSONObject = [
  129. numberKey: .number(numberValue),
  130. stringKey: .string(stringValue),
  131. ]
  132. let jsonData = try encoder.encode(JSONValue.object(objectValue))
  133. let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
  134. XCTAssertEqual(
  135. json,
  136. "{\"\(stringKey)\":\"\(stringValue)\",\"\(numberKey)\":\(numberValueEncoded)}"
  137. )
  138. }
  139. func testEncodeArray() throws {
  140. let arrayValue: [JSONValue] = [.null, .number(numberValue)]
  141. let jsonData = try encoder.encode(JSONValue.array(arrayValue))
  142. let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
  143. XCTAssertEqual(json, "[null,\(numberValueEncoded)]")
  144. }
  145. func testEncodeNestedObject() throws {
  146. let nestedObject: JSONObject = [
  147. "nestedKey": .string("nestedValue"),
  148. ]
  149. let objectValue: JSONObject = [
  150. "numberKey": .number(numberValue),
  151. "objectKey": .object(nestedObject),
  152. ]
  153. let jsonData = try encoder.encode(JSONValue.object(objectValue))
  154. let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))
  155. XCTAssertEqual(jsonObject, .object(objectValue))
  156. }
  157. func testEncodeNestedArray() throws {
  158. let nestedArray: [JSONValue] = [.string("a"), .string("b")]
  159. let objectValue: JSONObject = [
  160. "numberKey": .number(numberValue),
  161. "arrayKey": .array(nestedArray),
  162. ]
  163. let jsonData = try encoder.encode(JSONValue.object(objectValue))
  164. let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))
  165. XCTAssertEqual(jsonObject, .object(objectValue))
  166. }
  167. }