Skip to content

Commit

Permalink
Add test methods for Codable with enum
Browse files Browse the repository at this point in the history
  • Loading branch information
WendellXY committed Nov 22, 2024
1 parent c9d34e3 commit bc733c1
Showing 1 changed file with 155 additions and 0 deletions.
155 changes: 155 additions & 0 deletions Tests/CodableKitTests/CodableMacroTests+enum.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
//
// CodableMacroTests+enum.swift
// CodableKit
//
// Created by Wendell Wang on 2024/11/22.
//

import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros
import SwiftSyntaxMacrosTestSupport
import XCTest

final class CodableKitTestsForEnum: XCTestCase {
func testMacros() throws {
#if canImport(CodableKitMacros)
assertMacroExpansion(
"""
@Codable
public enum TestEnum {
case string(String)
case int(Int)
case none
}
""",
expandedSource: """
public enum TestEnum {
case string(String)
case int(Int)
case none
}
extension TestEnum: Codable {
enum CodingKeys: String, CodingKey {
case string
case int
case none
}
}
""",
macros: macros,
indentationWidth: .spaces(2)
)
#else
throw XCTSkip("macros are only supported when running tests for the host platform")
#endif
}

func testMacrosWithCodableKey() throws {
#if canImport(CodableKitMacros)
assertMacroExpansion(
"""
@Codable
public enum TestEnum {
@CodableKey("str") case string(String)
case int(Int)
@CodableKey("empty") case none
}
""",
expandedSource: """
public enum TestEnum {
case string(String)
case int(Int)
case none
}
extension TestEnum: Codable {
enum CodingKeys: String, CodingKey {
case string = "str"
case int
case none = "empty"
}
}
""",
macros: macros,
indentationWidth: .spaces(2)
)
#else
throw XCTSkip("macros are only supported when running tests for the host platform")
#endif
}

func testMacrosWithIgnoredCodableKey() throws {
#if canImport(CodableKitMacros)
assertMacroExpansion(
"""
@Codable
public enum TestEnum {
@CodableKey("str") case string(String)
case int(Int)
@CodableKey(options: .ignored) case none
}
""",
expandedSource: """
public enum TestEnum {
case string(String)
case int(Int)
case none
}
extension TestEnum: Codable {
enum CodingKeys: String, CodingKey {
case string = "str"
case int
}
}
""",
macros: macros,
indentationWidth: .spaces(2)
)
#else
throw XCTSkip("macros are only supported when running tests for the host platform")
#endif
}

func testMacrosWithIndirectCase() throws {
#if canImport(CodableKitMacros)
assertMacroExpansion(
"""
@Codable
public enum TestEnum {
@CodableKey("str") case string(String)
case int(Int)
@CodableKey("empty") case none
indirect case nestedA(TestEnum)
@CodableKey("b") indirect case nestedB(TestEnum)
}
""",
expandedSource: """
public enum TestEnum {
case string(String)
case int(Int)
case none
indirect case nestedA(TestEnum)
indirect case nestedB(TestEnum)
}
extension TestEnum: Codable {
enum CodingKeys: String, CodingKey {
case string = "str"
case int
case none = "empty"
case nestedA
case nestedB = "b"
}
}
""",
macros: macros,
indentationWidth: .spaces(2)
)
#else
throw XCTSkip("macros are only supported when running tests for the host platform")
#endif
}
}

0 comments on commit bc733c1

Please sign in to comment.