1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
@_spi(PluginMessage) import SwiftCompilerPluginMessageHandling
import XCTest
final class JSONTests: XCTestCase {
func testPrimitive() {
assertRoundTrip(of: true, expectedJSON: "true")
assertRoundTrip(of: false, expectedJSON: "false")
assertRoundTrip(of: Bool?.none, expectedJSON: "null")
assertRoundTrip(of: "", expectedJSON: "\"\"")
assertRoundTrip(of: 0, expectedJSON: "0")
assertRoundTrip(of: 0 as Int8, expectedJSON: "0")
assertRoundTrip(of: 0.0 as Float, expectedJSON: "0.0")
assertRoundTrip(of: 0.0 as Double, expectedJSON: "0.0")
}
func testEmptyStruct() {
let value = EmptyStruct()
assertRoundTrip(of: value, expectedJSON: "{}")
}
func testEmptyClass() {
let value = EmptyClass()
assertRoundTrip(of: value, expectedJSON: "{}")
}
func testTrivialEnumDefault() {
assertRoundTrip(of: Direction.left, expectedJSON: #"{"left":{}}"#)
assertRoundTrip(of: Direction.right, expectedJSON: #"{"right":{}}"#)
}
func testTrivialEnumRawValue() {
assertRoundTrip(of: Animal.dog, expectedJSON: #""dog""#)
assertRoundTrip(of: Animal.cat, expectedJSON: #""cat""#)
}
func testTrivialEnumCustom() {
assertRoundTrip(of: Switch.off, expectedJSON: "false")
assertRoundTrip(of: Switch.on, expectedJSON: "true")
}
func testEnumWithAssociated() {
let tree: Tree = .dictionary([
"name": .string("John Doe"),
"data": .array([.int(12), .string("foo")]),
])
assertRoundTrip(
of: tree,
expectedJSON: #"""
{"dictionary":{"_0":{"data":{"array":{"_0":[{"int":{"_0":12}},{"string":{"_0":"foo"}}]}},"name":{"string":{"_0":"John Doe"}}}}}
"""#
)
}
func testArrayOfInt() {
let arr: [Int] = [12, 42]
assertRoundTrip(of: arr, expectedJSON: "[12,42]")
let empty: [Int] = []
assertRoundTrip(of: empty, expectedJSON: "[]")
}
func testComplexStruct() {
let empty = ComplexStruct(result: nil, diagnostics: [], elapsed: 0.0)
assertRoundTrip(of: empty, expectedJSON: #"{"diagnostics":[],"elapsed":0.0}"#)
let value = ComplexStruct(
result: "\tresult\nfoo",
diagnostics: [
.init(
message: "error 🛑",
animal: .cat,
data: [nil, 42]
)
],
elapsed: 42.3e32
)
assertRoundTrip(
of: value,
expectedJSON: #"""
{"diagnostics":[{"animal":"cat","data":[null,42],"message":"error 🛑"}],"elapsed":4.23e+33,"result":"\tresult\nfoo"}
"""#
)
}
func testEscapedString() {
assertRoundTrip(
of: "\n\"\\\u{A9}\u{0}\u{07}\u{1B}",
expectedJSON: #"""
"\n\"\\©\u0000\u0007\u001B"
"""#
)
}
func testParseError() {
assertParseError(
#"{"foo": 1"#,
message: "unexpected end of file"
)
assertParseError(
#""foo"#,
message: "unexpected end of file"
)
assertParseError(
"\n",
message: "unexpected end of file"
)
assertParseError(
"trua",
message: "unexpected character 'a'; expected 'e'"
)
assertParseError(
"[true, #foo]",
message: "unexpected character '#'; value start"
)
assertParseError(
"{}true",
message: "unexpected character 't'; after top-level value"
)
}
func testInvalidStringDecoding() {
assertInvalidStrng(#""foo\"#) // EOF after '\'
assertInvalidStrng(#""\x""#) // Unknown character after '\'
assertInvalidStrng(#""\u1""#) // Missing 4 digits after '\u'
assertInvalidStrng(#""\u12""#)
assertInvalidStrng(#""\u123""#)
assertInvalidStrng(#""\uEFGH""#) // Invalid HEX characters.
}
func testStringSurrogatePairDecoding() {
// FIXME: Escaped surrogate pairs are not supported.
// Currently parsed as "invalid", but this should be valid '𐐷' (U+10437) character
assertInvalidStrng(#"\uD801\uDC37"#)
}
func testTypeCoercion() {
assertRoundTripTypeCoercionFailure(of: [false, true], as: [Int].self)
assertRoundTripTypeCoercionFailure(of: [false, true], as: [Int8].self)
assertRoundTripTypeCoercionFailure(of: [false, true], as: [Int16].self)
assertRoundTripTypeCoercionFailure(of: [false, true], as: [Int32].self)
assertRoundTripTypeCoercionFailure(of: [false, true], as: [Int64].self)
assertRoundTripTypeCoercionFailure(of: [false, true], as: [UInt].self)
assertRoundTripTypeCoercionFailure(of: [false, true], as: [UInt8].self)
assertRoundTripTypeCoercionFailure(of: [false, true], as: [UInt16].self)
assertRoundTripTypeCoercionFailure(of: [false, true], as: [UInt32].self)
assertRoundTripTypeCoercionFailure(of: [false, true], as: [UInt64].self)
assertRoundTripTypeCoercionFailure(of: [false, true], as: [Float].self)
assertRoundTripTypeCoercionFailure(of: [false, true], as: [Double].self)
assertRoundTripTypeCoercionFailure(of: [0, 1] as [Int], as: [Bool].self)
assertRoundTripTypeCoercionFailure(of: [0, 1] as [Int8], as: [Bool].self)
assertRoundTripTypeCoercionFailure(of: [0, 1] as [Int16], as: [Bool].self)
assertRoundTripTypeCoercionFailure(of: [0, 1] as [Int32], as: [Bool].self)
assertRoundTripTypeCoercionFailure(of: [0, 1] as [Int64], as: [Bool].self)
assertRoundTripTypeCoercionFailure(of: [0, 1] as [UInt], as: [Bool].self)
assertRoundTripTypeCoercionFailure(of: [0, 1] as [UInt8], as: [Bool].self)
assertRoundTripTypeCoercionFailure(of: [0, 1] as [UInt16], as: [Bool].self)
assertRoundTripTypeCoercionFailure(of: [0, 1] as [UInt32], as: [Bool].self)
assertRoundTripTypeCoercionFailure(of: [0, 1] as [UInt64], as: [Bool].self)
assertRoundTripTypeCoercionFailure(of: [0.0, 1.0] as [Float], as: [Bool].self)
assertRoundTripTypeCoercionFailure(of: [0.0, 1.0] as [Double], as: [Bool].self)
}
func testFloatingPointBufferBoundary() throws {
// Make sure floating point parsing does not read past the decoding JSON buffer.
var str = "0.199"
try str.withUTF8 { buf in
let truncated = UnsafeBufferPointer(rebasing: buf[0..<3])
XCTAssertEqual(try JSON.decode(Double.self, from: truncated), 0.1)
XCTAssertEqual(try JSON.decode(Float.self, from: truncated), 0.1)
}
}
private func assertRoundTrip<T: Codable & Equatable>(
of value: T,
expectedJSON: String,
file: StaticString = #filePath,
line: UInt = #line
) {
let payload: [UInt8]
do {
payload = try JSON.encode(value)
} catch let error {
XCTFail("Failed to encode \(T.self) to JSON: \(error)", file: file, line: line)
return
}
let jsonStr = String(decoding: payload, as: UTF8.self)
XCTAssertEqual(jsonStr, expectedJSON, file: file, line: line)
let decoded: T
do {
decoded = try payload.withUnsafeBufferPointer {
try JSON.decode(T.self, from: $0)
}
} catch let error {
XCTFail("Failed to decode \(T.self) from JSON: \(error)", file: file, line: line)
return
}
XCTAssertEqual(value, decoded, file: file, line: line)
}
private func assertRoundTripTypeCoercionFailure<T: Codable, U: Codable>(
of value: T,
as type: U.Type,
file: StaticString = #filePath,
line: UInt = #line
) {
do {
let data = try JSONEncoder().encode(value)
let _ = try JSONDecoder().decode(U.self, from: data)
XCTFail("Coercion from \(T.self) to \(U.self) was expected to fail.", file: file, line: line)
} catch DecodingError.typeMismatch(_, _) {
// Success
} catch {
XCTFail("unexpected error", file: file, line: line)
}
}
private func assertInvalidStrng(_ json: String, file: StaticString = #filePath, line: UInt = #line) {
do {
var json = json
_ = try json.withUTF8 { try JSON.decode(String.self, from: $0) }
XCTFail("decoding should fail", file: file, line: line)
} catch {}
}
private func assertParseError(_ json: String, message: String, file: StaticString = #filePath, line: UInt = #line) {
do {
var json = json
_ = try json.withUTF8 { try JSON.decode(Bool.self, from: $0) }
XCTFail("decoding should fail", file: file, line: line)
} catch DecodingError.dataCorrupted(let context) {
XCTAssertEqual(
String(describing: try XCTUnwrap(context.underlyingError, file: file, line: line)),
message
)
} catch {
XCTFail("unexpected error", file: file, line: line)
}
}
}
// MARK: - Test Types
fileprivate struct EmptyStruct: Codable, Equatable {
static func == (_ lhs: EmptyStruct, _ rhs: EmptyStruct) -> Bool {
return true
}
}
fileprivate class EmptyClass: Codable, Equatable {
static func == (_ lhs: EmptyClass, _ rhs: EmptyClass) -> Bool {
return true
}
}
fileprivate enum Direction: Codable {
case right
case left
}
fileprivate enum Animal: String, Codable {
case dog
case cat
}
fileprivate enum Switch: Codable {
case off
case on
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
switch try container.decode(Bool.self) {
case false: self = .off
case true: self = .on
}
}
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .off: try container.encode(false)
case .on: try container.encode(true)
}
}
}
fileprivate enum Tree: Codable, Equatable {
indirect case int(Int)
indirect case string(String)
indirect case array([Self])
indirect case dictionary([String: Self])
}
fileprivate struct ComplexStruct: Codable, Equatable {
struct Diagnostic: Codable, Equatable {
var message: String
var animal: Animal
var data: [Int?]
}
var result: String?
var diagnostics: [Diagnostic]
var elapsed: Double
}
|