Skip to content

Commit

Permalink
Add test for CodableKey diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
WendellXY committed Aug 19, 2024
1 parent 5464654 commit fd2d385
Showing 1 changed file with 170 additions and 0 deletions.
170 changes: 170 additions & 0 deletions Tests/CodableKitTests/CodableMacroTests+diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,174 @@ final class CodableKitDiagnosticsTests: XCTestCase {
throw XCTSkip("macros are only supported when running tests for the host platform")
#endif
}

func testMacroOnComputeProperty() throws {
#if canImport(CodableKitMacros)
assertMacroExpansion(
"""
@Codable
public struct User {
let id: UUID
let name: String
var age: Int = 24
@CodableKey("hello")
var address: String {
"A"
}
}
""",
expandedSource: """
public struct User {
let id: UUID
let name: String
var age: Int = 24
var address: String {
"A"
}
public func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(id, forKey: .id)
try container.encode(name, forKey: .name)
try container.encode(age, forKey: .age)
}
}
extension User: Codable {
enum CodingKeys: String, CodingKey {
case id
case name
case age
}
public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(UUID.self, forKey: .id)
name = try container.decode(String.self, forKey: .name)
age = try container.decodeIfPresent(Int.self, forKey: .age) ?? 24
}
}
""",
diagnostics: [
.init(message: "Only variable declarations with no accessor block are supported", line: 6, column: 3)
],
macros: macros,
indentationWidth: .spaces(2)
)
#else
throw XCTSkip("macros are only supported when running tests for the host platform")
#endif
}

func testMacroOnStaticComputeProperty() throws {
#if canImport(CodableKitMacros)
assertMacroExpansion(
"""
@Codable
public struct User {
let id: UUID
let name: String
var age: Int = 24
@CodableKey("hello")
static var address: String {
"A"
}
}
""",
expandedSource: """
public struct User {
let id: UUID
let name: String
var age: Int = 24
static var address: String {
"A"
}
public func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(id, forKey: .id)
try container.encode(name, forKey: .name)
try container.encode(age, forKey: .age)
}
}
extension User: Codable {
enum CodingKeys: String, CodingKey {
case id
case name
case age
}
public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(UUID.self, forKey: .id)
name = try container.decode(String.self, forKey: .name)
age = try container.decodeIfPresent(Int.self, forKey: .age) ?? 24
}
}
""",
diagnostics: [
.init(message: "Only variable declarations with no accessor block are supported", line: 6, column: 3)
],
macros: macros,
indentationWidth: .spaces(2)
)
#else
throw XCTSkip("macros are only supported when running tests for the host platform")
#endif
}

func testMacroOnStaticProperty() throws {
#if canImport(CodableKitMacros)
assertMacroExpansion(
"""
@Codable
public struct User {
let id: UUID
let name: String
var age: Int = 24
@CodableKey("hello")
static var address: String = "A"
}
""",
expandedSource: """
public struct User {
let id: UUID
let name: String
var age: Int = 24
static var address: String = "A"
public func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(id, forKey: .id)
try container.encode(name, forKey: .name)
try container.encode(age, forKey: .age)
}
}
extension User: Codable {
enum CodingKeys: String, CodingKey {
case id
case name
case age
}
public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(UUID.self, forKey: .id)
name = try container.decode(String.self, forKey: .name)
age = try container.decodeIfPresent(Int.self, forKey: .age) ?? 24
}
}
""",
diagnostics: [
.init(message: "Only non-static variable declarations are supported", line: 6, column: 3)
],
macros: macros,
indentationWidth: .spaces(2)
)
#else
throw XCTSkip("macros are only supported when running tests for the host platform")
#endif
}
}

0 comments on commit fd2d385

Please sign in to comment.