Skip to content

Commit

Permalink
Fix JSValueDecoder (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
j-f1 authored Apr 9, 2022
1 parent 85e1a11 commit 7b0e0d2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
22 changes: 22 additions & 0 deletions IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -804,5 +804,27 @@ try test("Hashable Conformance") {
try expectEqual(firstHash, secondHash)
}

struct AnimalStruct: Decodable {
let name: String
let age: Int
let isCat: Bool
}

try test("JSValueDecoder") {
let Animal = JSObject.global.Animal.function!
let tama = try Animal.throws.new("Tama", 3, true)
let decoder = JSValueDecoder()
let decodedTama = try decoder.decode(AnimalStruct.self, from: tama.jsValue)

try expectEqual(decodedTama.name, tama.name.string)
try expectEqual(decodedTama.name, "Tama")

try expectEqual(decodedTama.age, tama.age.number.map(Int.init))
try expectEqual(decodedTama.age, 3)

try expectEqual(decodedTama.isCat, tama.isCat.boolean)
try expectEqual(decodedTama.isCat, true)
}


Expectation.wait(expectations)
2 changes: 1 addition & 1 deletion Sources/JavaScriptKit/JSValueDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private struct _Decoder: Decoder {
}

private enum Object {
static let ref = JSObject.global.Object.object!
static let ref = JSObject.global.Object.function!
static func keys(_ object: JSObject) -> [String] {
let keys = ref.keys!(object).array!
return keys.map { $0.string! }
Expand Down

0 comments on commit 7b0e0d2

Please sign in to comment.