Skip to content

Commit

Permalink
Add tests for Codable options with skipping super coding
Browse files Browse the repository at this point in the history
Add tests for the `@Encodable`, `@Decodable`, and `@Codable` macros 
with the `.skipSuperCoding` option. These tests verify that the 
macros correctly expand to the expected Swift code, ensuring that 
the generated classes handle encoding and decoding without 
inheriting properties from superclasses. This enhances the test 
coverage for the Codable macros and ensures their correct 
functionality in various scenarios.
  • Loading branch information
WendellXY committed Jan 13, 2025
1 parent ca0fb73 commit d14ca19
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 0 deletions.
47 changes: 47 additions & 0 deletions Tests/CodableKitTests/CodableMacroTests+class+inheritance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -736,4 +736,51 @@ final class CodableKitTestsForSubClass: XCTestCase {
)

}

func testMacrosWithCodableOptionSkipSuperCoding() throws {

assertMacroExpansion(
"""
@Codable(options: .skipSuperCoding)
public class User: NSObject {
let id: UUID
let name: String
let age: Int
}
""",
expandedSource: """
public class User: NSObject {
let id: UUID
let name: String
let age: Int
public required 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.decode(Int.self, forKey: .age)
super.init()
}
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
}
}
""",
macroSpecs: macroSpecs,
indentationWidth: .spaces(2)
)

}
}
46 changes: 46 additions & 0 deletions Tests/CodableKitTests/CodableMacroTests+class.swift
Original file line number Diff line number Diff line change
Expand Up @@ -710,4 +710,50 @@ final class CodableKitTestsForClass: XCTestCase {
)

}

func testMacrosWithCodableOptionSkipSuperCoding() throws {

assertMacroExpansion(
"""
@Codable(options: .skipSuperCoding)
public class User {
let id: UUID
let name: String
let age: Int
}
""",
expandedSource: """
public class User {
let id: UUID
let name: String
let age: Int
public required 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.decode(Int.self, forKey: .age)
}
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
}
}
""",
macroSpecs: macroSpecs,
indentationWidth: .spaces(2)
)

}
}
40 changes: 40 additions & 0 deletions Tests/DecodableKitTests/CodableMacroTests+class+inheritance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -602,4 +602,44 @@ final class CodableKitTestsForSubClass: XCTestCase {
)

}

func testMacrosWithCodableOptionSkipSuperCoding() throws {

assertMacroExpansion(
"""
@Decodable(options: .skipSuperCoding)
public class User: NSObject {
let id: UUID
let name: String
let age: Int
}
""",
expandedSource: """
public class User: NSObject {
let id: UUID
let name: String
let age: Int
public required 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.decode(Int.self, forKey: .age)
super.init()
}
}
extension User: Decodable {
enum CodingKeys: String, CodingKey {
case id
case name
case age
}
}
""",
macroSpecs: macroSpecs,
indentationWidth: .spaces(2)
)

}
}
39 changes: 39 additions & 0 deletions Tests/DecodableKitTests/CodableMacroTests+class.swift
Original file line number Diff line number Diff line change
Expand Up @@ -588,4 +588,43 @@ final class CodableKitTestsForClass: XCTestCase {
)

}

func testMacrosWithCodableOptionSkipSuperCoding() throws {

assertMacroExpansion(
"""
@Decodable(options: .skipSuperCoding)
public class User {
let id: UUID
let name: String
let age: Int
}
""",
expandedSource: """
public class User {
let id: UUID
let name: String
let age: Int
public required 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.decode(Int.self, forKey: .age)
}
}
extension User: Decodable {
enum CodingKeys: String, CodingKey {
case id
case name
case age
}
}
""",
macroSpecs: macroSpecs,
indentationWidth: .spaces(2)
)

}
}
39 changes: 39 additions & 0 deletions Tests/EncodableKitTests/CodableMacroTests+class+inheritance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -602,4 +602,43 @@ final class CodableKitTestsForSubClass: XCTestCase {
)

}

func testMacrosWithCodableOptionSkipSuperCoding() throws {

assertMacroExpansion(
"""
@Encodable(options: .skipSuperCoding)
public class User: NSObject {
let id: UUID
let name: String
let age: Int
}
""",
expandedSource: """
public class User: NSObject {
let id: UUID
let name: String
let age: Int
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: Encodable {
enum CodingKeys: String, CodingKey {
case id
case name
case age
}
}
""",
macroSpecs: macroSpecs,
indentationWidth: .spaces(2)
)

}
}
39 changes: 39 additions & 0 deletions Tests/EncodableKitTests/CodableMacroTests+class.swift
Original file line number Diff line number Diff line change
Expand Up @@ -588,4 +588,43 @@ final class CodableKitTestsForClass: XCTestCase {
)

}

func testMacrosWithCodableOptionSkipSuperCoding() throws {

assertMacroExpansion(
"""
@Encodable(options: .skipSuperCoding)
public class User {
let id: UUID
let name: String
let age: Int
}
""",
expandedSource: """
public class User {
let id: UUID
let name: String
let age: Int
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: Encodable {
enum CodingKeys: String, CodingKey {
case id
case name
case age
}
}
""",
macroSpecs: macroSpecs,
indentationWidth: .spaces(2)
)

}
}

0 comments on commit d14ca19

Please sign in to comment.