diff --git a/Tests/CodableToTypeScriptTests/Generate/GenerateCustomTypeConverterTests.swift b/Tests/CodableToTypeScriptTests/Generate/GenerateCustomTypeConverterTests.swift new file mode 100644 index 0000000..d312aec --- /dev/null +++ b/Tests/CodableToTypeScriptTests/Generate/GenerateCustomTypeConverterTests.swift @@ -0,0 +1,106 @@ +import XCTest +import CodableToTypeScript +import SwiftTypeReader +import TypeScriptAST + +final class GenerateCustomTypeConverterTests: GenerateTestCaseBase { + /* + Use TypeMap instead of custom TypeConverter as much as possible + */ + struct CustomConverter: TypeConverter { + var generator: CodeGenerator + var swiftType: any SType + + func name(for target: GenerationTarget) throws -> String { + switch target { + case .entity: return "Custom" + case .json: return try `default`.name(for: .json) + } + } + + func typeDecl(for target: GenerationTarget) throws -> TSTypeDecl? { + return nil + } + + func hasDecode() throws -> Bool { + return true + } + + func decodeName() throws -> String { + return "Custom_decode" + } + + func decodeDecl() throws -> TSFunctionDecl? { + return nil + } + + func hasEncode() throws -> Bool { + return true + } + + func encodeName() throws -> String { + return "Custom_encode" + } + + func encodeDecl() throws -> TSFunctionDecl? { + return nil + } + } + + func testCustomTypeConverter() throws { + let typeConverterProvider = TypeConverterProvider { (gen, type) in + let repr = type.toTypeRepr(containsModule: false) + if let ident = repr.asIdent, + let element = ident.elements.last, + element.name == "Custom" + { + return CustomConverter(generator: gen, swiftType: type) + } + return nil + } + + try assertGenerate( + source: """ +struct S { + var a: Custom + var b: [Custom] + var c: [[Custom]] +} +""", + typeConverterProvider: typeConverterProvider, + expecteds: [""" +export type S = { + a: Custom; + b: Custom[]; + c: Custom[][]; +}; +""", """ +export type S_JSON = { + a: Custom_JSON; + b: Custom_JSON[]; + c: Custom_JSON[][]; +}; +""", """ +export function S_decode(json: S_JSON): S { + return { + a: Custom_decode(json.a), + b: Array_decode(json.b, Custom_decode), + c: Array_decode(json.c, (json: Custom_JSON[]): Custom[] => { + return Array_decode(json, Custom_decode); + }) + }; +} +""", """ +export function S_encode(entity: S): S_JSON { + return { + a: Custom_encode(entity.a), + b: Array_encode(entity.b, Custom_encode), + c: Array_encode(entity.c, (entity: Custom[]): Custom_JSON[] => { + return Array_encode(entity, Custom_encode); + }) + }; +} +""" + ] + ) + }} diff --git a/Tests/CodableToTypeScriptTests/Generate/GenerateCustomTypeTests.swift b/Tests/CodableToTypeScriptTests/Generate/GenerateCustomTypeMapTests.swift similarity index 71% rename from Tests/CodableToTypeScriptTests/Generate/GenerateCustomTypeTests.swift rename to Tests/CodableToTypeScriptTests/Generate/GenerateCustomTypeMapTests.swift index 982237d..8495270 100644 --- a/Tests/CodableToTypeScriptTests/Generate/GenerateCustomTypeTests.swift +++ b/Tests/CodableToTypeScriptTests/Generate/GenerateCustomTypeMapTests.swift @@ -3,7 +3,7 @@ import CodableToTypeScript import SwiftTypeReader import TypeScriptAST -final class GenerateCustomTypeTests: GenerateTestCaseBase { +final class GenerateCustomTypeMapTests: GenerateTestCaseBase { func testCustomName() throws { var typeMap = TypeMap.default typeMap.table["URL"] = .init(name: "string") @@ -252,105 +252,6 @@ export type S = { ) } - - struct DateConverter: TypeConverter { - var generator: CodeGenerator - var swiftType: any SType - - func name(for target: GenerationTarget) throws -> String { - switch target { - case .entity: return "Date" - case .json: return try `default`.name(for: .json) - } - } - - func typeDecl(for target: GenerationTarget) throws -> TSTypeDecl? { - return nil - } - - func hasDecode() throws -> Bool { - return true - } - - func decodeName() throws -> String { - return "Date_decode" - } - - func decodeDecl() throws -> TSFunctionDecl? { - return nil - } - - func hasEncode() throws -> Bool { - return true - } - - func encodeName() throws -> String { - return "Date_encode" - } - - func encodeDecl() throws -> TSFunctionDecl? { - return nil - } - } - - func testCustomTypeConverter() throws { - let typeConverterProvider = TypeConverterProvider { (gen, type) in - let repr = type.toTypeRepr(containsModule: false) - if let ident = repr.asIdent, - let element = ident.elements.last, - element.name == "Date" - { - return DateConverter(generator: gen, swiftType: type) - } - return nil - } - - try assertGenerate( - source: """ -struct S { - var a: Date - var b: [Date] - var c: [[Date]] -} -""", - typeConverterProvider: typeConverterProvider, - expecteds: [""" -export type S = { - a: Date; - b: Date[]; - c: Date[][]; -}; -""", """ -export type S_JSON = { - a: Date_JSON; - b: Date_JSON[]; - c: Date_JSON[][]; -}; -""", """ -export function S_decode(json: S_JSON): S { - return { - a: Date_decode(json.a), - b: Array_decode(json.b, Date_decode), - c: Array_decode(json.c, (json: Date_JSON[]): Date[] => { - return Array_decode(json, Date_decode); - }) - }; -} -""", """ -export function S_encode(entity: S): S_JSON { - return { - a: Date_encode(entity.a), - b: Array_encode(entity.b, Date_encode), - c: Array_encode(entity.c, (entity: Date[]): Date_JSON[] => { - return Array_encode(entity, Date_encode); - }) - }; -} -""" - ] - ) - } - func testMapUserType() throws { var typeMap = TypeMap() typeMap.table["S"] = .init(name: "V")