JSONValueTests.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 FirebaseVertexAI
  16. final class JSONValueTests: XCTestCase {
  17. let decoder = JSONDecoder()
  18. let encoder = JSONEncoder()
  19. let numberKey = "pi"
  20. let numberValue = 3.14159
  21. let numberValueEncoded = "3.14159"
  22. let stringKey = "hello"
  23. let stringValue = "Hello, world!"
  24. override func setUp() {
  25. encoder.outputFormatting = .sortedKeys
  26. }
  27. func testDecodeNull() throws {
  28. let jsonData = try XCTUnwrap("null".data(using: .utf8))
  29. let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))
  30. XCTAssertEqual(jsonObject, .null)
  31. }
  32. func testDecodeNumber() throws {
  33. let jsonData = try XCTUnwrap("\(numberValue)".data(using: .utf8))
  34. let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))
  35. XCTAssertEqual(jsonObject, .number(numberValue))
  36. }
  37. func testDecodeString() throws {
  38. let jsonData = try XCTUnwrap("\"\(stringValue)\"".data(using: .utf8))
  39. let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))
  40. XCTAssertEqual(jsonObject, .string(stringValue))
  41. }
  42. func testDecodeBool() throws {
  43. let expectedBool = true
  44. let jsonData = try XCTUnwrap("\(expectedBool)".data(using: .utf8))
  45. let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))
  46. XCTAssertEqual(jsonObject, .bool(expectedBool))
  47. }
  48. func testDecodeObject() throws {
  49. let expectedObject: JSONObject = [
  50. numberKey: .number(numberValue),
  51. stringKey: .string(stringValue),
  52. ]
  53. let json = """
  54. {
  55. "\(numberKey)": \(numberValue),
  56. "\(stringKey)": "\(stringValue)"
  57. }
  58. """
  59. let jsonData = try XCTUnwrap(json.data(using: .utf8))
  60. let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))
  61. XCTAssertEqual(jsonObject, .object(expectedObject))
  62. }
  63. func testDecodeArray() throws {
  64. let expectedArray: [JSONValue] = [.null, .number(numberValue)]
  65. let jsonData = try XCTUnwrap("[ null, \(numberValue) ]".data(using: .utf8))
  66. let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))
  67. XCTAssertEqual(jsonObject, .array(expectedArray))
  68. }
  69. func testEncodeNull() throws {
  70. let jsonData = try encoder.encode(JSONValue.null)
  71. let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
  72. XCTAssertEqual(json, "null")
  73. }
  74. func testEncodeNumber() throws {
  75. let jsonData = try encoder.encode(JSONValue.number(numberValue))
  76. let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
  77. XCTAssertEqual(json, "\(numberValue)")
  78. }
  79. func testEncodeString() throws {
  80. let jsonData = try encoder.encode(JSONValue.string(stringValue))
  81. let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
  82. XCTAssertEqual(json, "\"\(stringValue)\"")
  83. }
  84. func testEncodeBool() throws {
  85. let boolValue = true
  86. let jsonData = try encoder.encode(JSONValue.bool(boolValue))
  87. let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
  88. XCTAssertEqual(json, "\(boolValue)")
  89. }
  90. func testEncodeObject() throws {
  91. let objectValue: JSONObject = [
  92. numberKey: .number(numberValue),
  93. stringKey: .string(stringValue),
  94. ]
  95. let jsonData = try encoder.encode(JSONValue.object(objectValue))
  96. let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
  97. XCTAssertEqual(
  98. json,
  99. "{\"\(stringKey)\":\"\(stringValue)\",\"\(numberKey)\":\(numberValueEncoded)}"
  100. )
  101. }
  102. func testEncodeArray() throws {
  103. let arrayValue: [JSONValue] = [.null, .number(numberValue)]
  104. let jsonData = try encoder.encode(JSONValue.array(arrayValue))
  105. let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
  106. XCTAssertEqual(json, "[null,\(numberValueEncoded)]")
  107. }
  108. }