JSONValueTests.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 FirebaseAI
  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 testEncodeNumber() throws {
  76. let jsonData = try encoder.encode(JSONValue.number(numberValue))
  77. let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
  78. XCTAssertEqual(json, "\(numberValue)")
  79. }
  80. func testEncodeString() throws {
  81. let jsonData = try encoder.encode(JSONValue.string(stringValue))
  82. let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
  83. XCTAssertEqual(json, "\"\(stringValue)\"")
  84. }
  85. func testEncodeBool() throws {
  86. let boolValue = true
  87. let jsonData = try encoder.encode(JSONValue.bool(boolValue))
  88. let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
  89. XCTAssertEqual(json, "\(boolValue)")
  90. }
  91. func testEncodeObject() throws {
  92. let objectValue: JSONObject = [
  93. numberKey: .number(numberValue),
  94. stringKey: .string(stringValue),
  95. ]
  96. let jsonData = try encoder.encode(JSONValue.object(objectValue))
  97. let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
  98. XCTAssertEqual(
  99. json,
  100. "{\"\(stringKey)\":\"\(stringValue)\",\"\(numberKey)\":\(numberValueEncoded)}"
  101. )
  102. }
  103. func testEncodeArray() throws {
  104. let arrayValue: [JSONValue] = [.null, .number(numberValue)]
  105. let jsonData = try encoder.encode(JSONValue.array(arrayValue))
  106. let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
  107. XCTAssertEqual(json, "[null,\(numberValueEncoded)]")
  108. }
  109. }