diff --git a/Sources/Automerge/Codable/Encoding/AutomergeEncoderImpl.swift b/Sources/Automerge/Codable/Encoding/AutomergeEncoderImpl.swift index fbb2a870..281df072 100644 --- a/Sources/Automerge/Codable/Encoding/AutomergeEncoderImpl.swift +++ b/Sources/Automerge/Codable/Encoding/AutomergeEncoderImpl.swift @@ -53,7 +53,6 @@ final class AutomergeEncoderImpl { if codingPath.map({ AnyCodingKey($0) }).starts(with: prefix) { switch containerType { case .Key: - precondition(!mapKeysWritten.isEmpty) // Remove keys that exist in this objectId that weren't // written during encode. (clean up 'dead' keys from maps) let extraAutomergeKeys = document.keys(obj: objectIdForContainer) diff --git a/Tests/AutomergeTests/CodableTests/AutomergeDictionaryEncodeDecodeTests.swift b/Tests/AutomergeTests/CodableTests/AutomergeDictionaryEncodeDecodeTests.swift index fe019816..811ed28e 100644 --- a/Tests/AutomergeTests/CodableTests/AutomergeDictionaryEncodeDecodeTests.swift +++ b/Tests/AutomergeTests/CodableTests/AutomergeDictionaryEncodeDecodeTests.swift @@ -19,20 +19,47 @@ final class AutomergeDictionaryEncodeDecodeTests: XCTestCase { } let encoder = AutomergeEncoder(doc: doc) - let decoder = AutomergeDecoder(doc: doc) var wrapper = Wrapper() wrapper.exampleDictionary[1] = "one" wrapper.exampleDictionary[2] = "two" - XCTExpectFailure("Automerge encoder can't encode keys other than strings.") - try encoder.encode(wrapper) + XCTAssertThrowsError(try encoder.encode(wrapper)) + // Automerge throws a DocError because it only supports + // Strings as keys for dictionaries. + // DocError(inner: AutomergeUniffi.DocError.WrongObjectType(message: "WrongObjectType")) + } + + func testEmptyDictionaryEncode() throws { + // example of Dictionary with keys other than a string + + struct Wrapper: Codable, Equatable { + var exampleDictionary: [String: Int] = [:] + } + + let encoder = AutomergeEncoder(doc: doc) + let decoder = AutomergeDecoder(doc: doc) + let wrapper = Wrapper() + try encoder.encode(wrapper) let replica = try decoder.decode(Wrapper.self) XCTAssertEqual(replica, wrapper) } - func testEmptyDictionaryEncode() throws { + func testEmptyDictionaryEncodeAtRoot() throws { + let exampleDictionary: [String: Int] = [:] + + let encoder = AutomergeEncoder(doc: doc) + let decoder = AutomergeDecoder(doc: doc) + + // this fails when you encode an empty dictionary + try encoder.encode(exampleDictionary) + + let replica = try decoder.decode([String: Int].self) + XCTAssertEqual(replica, exampleDictionary) + } + + func testWorkingDictionaryEncode() throws { var exampleDictionary: [String: Int] = [:] exampleDictionary["one"] = 1