Skip to content

Commit

Permalink
hasher test + slight refactor for coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
hborawski committed Sep 27, 2024
1 parent dadbce3 commit fa05b8a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
17 changes: 5 additions & 12 deletions ios/core/Sources/Types/Assets/BaseAssetRegistry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,8 @@ open class BaseAssetRegistry<WrapperType>: PlayerRegistry where
public func decode(_ value: JSValue) throws -> WrapperType.AssetType {
assert(Thread.isMainThread, "decoder must be accessed from main")
typealias Shim = RegistryDecodeShim<WrapperType.AssetType>
if let context = AnyTypeDecodingContext(value) {
return try context.inject(to: decoder).decode(Shim.self, from: value).asset
}
return try decoder.decode(Shim.self, from: value).asset
let localDecoder = AnyTypeDecodingContext(value).map { $0.inject(to: decoder) } ?? decoder
return try localDecoder.decode(Shim.self, from: value).asset
}

/**
Expand All @@ -180,11 +178,8 @@ open class BaseAssetRegistry<WrapperType>: PlayerRegistry where
*/
public func decodeWrapper(_ value: JSValue) throws -> WrapperType {
assert(Thread.isMainThread, "decoder must be accessed from main")
if let context = AnyTypeDecodingContext(value) {
return try context.inject(to: decoder).decode(WrapperType.self, from: value)
}

return try decoder.decode(WrapperType.self, from: value)
let localDecoder = AnyTypeDecodingContext(value).map { $0.inject(to: decoder) } ?? decoder
return try localDecoder.decode(WrapperType.self, from: value)
}
}

Expand All @@ -202,9 +197,7 @@ extension AnyTypeDecodingContext {
guard
let obj = value.toObject(),
let data = try? JSONSerialization.data(withJSONObject: obj)
else {
return nil
}
else { return nil }
self.init(rawData: data)
}
}
Expand Down
8 changes: 5 additions & 3 deletions ios/core/Tests/Types/Generic/AnyTypeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,17 +197,18 @@ class AnyTypeTests: XCTestCase {
}

func testAnyDictionaryDataWithDeepNestedTypes() {
let string = "{\"key2\":1,\"key\":[{\"nestedKey\": \"nestedValue\"}]}"
let string = "{\"container\":{\"key2\":1,\"key\":[{\"nestedKey\": \"nestedValue\"}]}}"
guard
let data = string.data(using: .utf8),
let anyType = try? AnyTypeDecodingContext(rawData: string.data(using: .utf8)!).inject(to: JSONDecoder()).decode(AnyType.self, from: data)
else { return XCTFail("could not decode") }
switch anyType {
case .anyDictionary(let result):
let nestedArray = result["key"] as? [Any]
let container = result["container"] as? [String: Any]
let nestedArray = container?["key"] as? [Any]
let nestedDict = nestedArray?.first as? [String: Any]
XCTAssertEqual("nestedValue", nestedDict?["nestedKey"] as? String)
XCTAssertEqual(1, result["key2"] as? Double)
XCTAssertEqual(1, container?["key2"] as? Double)
default:
XCTFail("data was not dictionary")

Check warning on line 213 in ios/core/Tests/Types/Generic/AnyTypeTests.swift

View check run for this annotation

Codecov / codecov/patch

ios/core/Tests/Types/Generic/AnyTypeTests.swift#L213

Added line #L213 was not covered by tests
}
Expand Down Expand Up @@ -279,6 +280,7 @@ class AnyTypeTests: XCTestCase {
XCTAssertNotEqual(AnyType.numberArray(data: [1, 2]).hashValue, 0)
XCTAssertNotEqual(AnyType.booleanArray(data: [false, true]).hashValue, 0)
XCTAssertNotEqual(AnyType.anyDictionary(data: ["key": false, "key2": 1]).hashValue, 0)
XCTAssertNotEqual(AnyType.anyArray(data: [1, false]).hashValue, 0)
XCTAssertNotEqual(AnyType.unknownData.hashValue, 0)
}

Expand Down

0 comments on commit fa05b8a

Please sign in to comment.