Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuawright11 committed Dec 11, 2023
1 parent 40e9958 commit 8f49dec
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions PapyrusPlugin/Tests/APIMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,60 @@ final class APIMacroTests: XCTestCase {
"""
}
}

func testJSON() {
assertMacro(["API": APIMacro.self]) {
"""
struct User {
let id: Int
let name: String
}
@API
@KeyMapping(.snakeCase)
@JSON(encoder: .foo, decoder: .bar)
protocol MyService {
@POST("users")
func getUser(name: String) async throws -> User
}
"""
} expansion: {
"""
struct User {
let id: Int
let name: String
}
@KeyMapping(.snakeCase)
@JSON(encoder: .foo, decoder: .bar)
protocol MyService {
@POST("users")
func getUser(name: String) async throws -> User
}
struct MyServiceAPI: MyService {
private let provider: PapyrusCore.Provider
init(provider: PapyrusCore.Provider) {
self.provider = provider
}
func getUser(name: String) async throws -> User {
var req = builder(method: "POST", path: "users")
req.addField("name", value: name)
let res = try await provider.request(req)
try res.validate()
return try res.decode(User.self, using: req.responseDecoder)
}
private func builder(method: String, path: String) -> RequestBuilder {
var req = provider.newBuilder(method: method, path: path)
req.keyMapping = .snakeCase
req.requestEncoder = .json(.foo)
req.responseDecoder = .json(.bar)
return req
}
}
"""
}
}
}

0 comments on commit 8f49dec

Please sign in to comment.