-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: only ignore computed properties with getter in decoding/encoding (…
…#11) * fix: only ignore computed properties with getter in decoding/encoding - fixes issue #10 * wip: update method doc
- Loading branch information
1 parent
0cc623f
commit a6bc3a2
Showing
7 changed files
with
268 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
Tests/MetaCodableTests/CodableMacroGroupedVariableTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
import XCTest | ||
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
import SwiftSyntaxMacros | ||
import SwiftSyntaxMacrosTestSupport | ||
@testable import CodableMacroPlugin | ||
|
||
class CodableMacroGroupedVariableTests: XCTestCase { | ||
func testWithoutAnyCustomization() throws { | ||
assertMacroExpansion( | ||
""" | ||
@Codable | ||
struct SomeCodable { | ||
let one, two, three: String | ||
} | ||
""", | ||
expandedSource: | ||
""" | ||
struct SomeCodable { | ||
let one, two, three: String | ||
init(one: String, two: String, three: String) { | ||
self.one = one | ||
self.two = two | ||
self.three = three | ||
} | ||
init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
self.one = try container.decode(String.self, forKey: CodingKeys.one) | ||
self.two = try container.decode(String.self, forKey: CodingKeys.two) | ||
self.three = try container.decode(String.self, forKey: CodingKeys.three) | ||
} | ||
func encode(to encoder: Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
try container.encode(self.one, forKey: CodingKeys.one) | ||
try container.encode(self.two, forKey: CodingKeys.two) | ||
try container.encode(self.three, forKey: CodingKeys.three) | ||
} | ||
enum CodingKeys: String, CodingKey { | ||
case one = "one" | ||
case two = "two" | ||
case three = "three" | ||
} | ||
} | ||
extension SomeCodable: Codable { | ||
} | ||
""", | ||
macros: ["Codable": CodableMacro.self] | ||
) | ||
} | ||
|
||
func testWithSomeInitialized() throws { | ||
assertMacroExpansion( | ||
""" | ||
@Codable | ||
struct SomeCodable { | ||
let one, two: String, three: String = "" | ||
} | ||
""", | ||
expandedSource: | ||
""" | ||
struct SomeCodable { | ||
let one, two: String, three: String = "" | ||
init(one: String, two: String) { | ||
self.one = one | ||
self.two = two | ||
} | ||
init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
self.one = try container.decode(String.self, forKey: CodingKeys.one) | ||
self.two = try container.decode(String.self, forKey: CodingKeys.two) | ||
} | ||
func encode(to encoder: Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
try container.encode(self.one, forKey: CodingKeys.one) | ||
try container.encode(self.two, forKey: CodingKeys.two) | ||
} | ||
enum CodingKeys: String, CodingKey { | ||
case one = "one" | ||
case two = "two" | ||
} | ||
} | ||
extension SomeCodable: Codable { | ||
} | ||
""", | ||
macros: ["Codable": CodableMacro.self] | ||
) | ||
} | ||
|
||
func testMixedTypes() throws { | ||
assertMacroExpansion( | ||
""" | ||
@Codable | ||
struct SomeCodable { | ||
let one, two: String, three: Int | ||
} | ||
""", | ||
expandedSource: | ||
""" | ||
struct SomeCodable { | ||
let one, two: String, three: Int | ||
init(one: String, two: String, three: Int) { | ||
self.one = one | ||
self.two = two | ||
self.three = three | ||
} | ||
init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
self.one = try container.decode(String.self, forKey: CodingKeys.one) | ||
self.two = try container.decode(String.self, forKey: CodingKeys.two) | ||
self.three = try container.decode(Int.self, forKey: CodingKeys.three) | ||
} | ||
func encode(to encoder: Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
try container.encode(self.one, forKey: CodingKeys.one) | ||
try container.encode(self.two, forKey: CodingKeys.two) | ||
try container.encode(self.three, forKey: CodingKeys.three) | ||
} | ||
enum CodingKeys: String, CodingKey { | ||
case one = "one" | ||
case two = "two" | ||
case three = "three" | ||
} | ||
} | ||
extension SomeCodable: Codable { | ||
} | ||
""", | ||
macros: ["Codable": CodableMacro.self] | ||
) | ||
} | ||
|
||
func testMixedTypesWithSomeInitialized() throws { | ||
assertMacroExpansion( | ||
""" | ||
@Codable | ||
struct SomeCodable { | ||
let one: String, two: String = "", three: Int | ||
} | ||
""", | ||
expandedSource: | ||
""" | ||
struct SomeCodable { | ||
let one: String, two: String = "", three: Int | ||
init(one: String, three: Int) { | ||
self.one = one | ||
self.three = three | ||
} | ||
init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
self.one = try container.decode(String.self, forKey: CodingKeys.one) | ||
self.three = try container.decode(Int.self, forKey: CodingKeys.three) | ||
} | ||
func encode(to encoder: Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
try container.encode(self.one, forKey: CodingKeys.one) | ||
try container.encode(self.three, forKey: CodingKeys.three) | ||
} | ||
enum CodingKeys: String, CodingKey { | ||
case one = "one" | ||
case three = "three" | ||
} | ||
} | ||
extension SomeCodable: Codable { | ||
} | ||
""", | ||
macros: ["Codable": CodableMacro.self] | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import XCTest | ||
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
import SwiftSyntaxMacros | ||
import SwiftSyntaxMacrosTestSupport | ||
@testable import CodableMacroPlugin | ||
|
||
final class CodableMacroTests: XCTestCase { | ||
func testWithoutAnyCustomization() throws { | ||
assertMacroExpansion( | ||
""" | ||
@Codable | ||
struct SomeCodable { | ||
let value: String | ||
} | ||
""", | ||
expandedSource: | ||
""" | ||
struct SomeCodable { | ||
let value: String | ||
init(value: String) { | ||
self.value = value | ||
} | ||
init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
self.value = try container.decode(String.self, forKey: CodingKeys.value) | ||
} | ||
func encode(to encoder: Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
try container.encode(self.value, forKey: CodingKeys.value) | ||
} | ||
enum CodingKeys: String, CodingKey { | ||
case value = "value" | ||
} | ||
} | ||
extension SomeCodable: Codable { | ||
} | ||
""", | ||
macros: ["Codable": CodableMacro.self] | ||
) | ||
} | ||
|
||
func testCustomKey() throws { | ||
assertMacroExpansion( | ||
""" | ||
@Codable | ||
struct SomeCodable { | ||
@CodablePath("customKey") | ||
let value: String | ||
} | ||
""", | ||
expandedSource: | ||
""" | ||
struct SomeCodable { | ||
let value: String | ||
init(value: String) { | ||
self.value = value | ||
} | ||
init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
self.value = try container.decode(String.self, forKey: CodingKeys.value) | ||
} | ||
func encode(to encoder: Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
try container.encode(self.value, forKey: CodingKeys.value) | ||
} | ||
enum CodingKeys: String, CodingKey { | ||
case value = "customKey" | ||
} | ||
} | ||
extension SomeCodable: Codable { | ||
} | ||
""", | ||
macros: ["Codable": CodableMacro.self] | ||
) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters