From fe7e6e7b8486c400dee63f7a25a8846fae6775c3 Mon Sep 17 00:00:00 2001 From: John Estropia Date: Fri, 5 Apr 2019 08:29:14 +0900 Subject: [PATCH] Fix wrong VersionLock printing on Swift 5 (fixes #311) --- CoreStore.xcodeproj/project.pbxproj | 8 +++ CoreStoreTests/VersionLockTests.swift | 55 +++++++++++++++++++ ...reStore+CustomDebugStringConvertible.swift | 6 +- Sources/DynamicSchema+Convenience.swift | 7 +-- 4 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 CoreStoreTests/VersionLockTests.swift diff --git a/CoreStore.xcodeproj/project.pbxproj b/CoreStore.xcodeproj/project.pbxproj index f491cb9..be1d808 100644 --- a/CoreStore.xcodeproj/project.pbxproj +++ b/CoreStore.xcodeproj/project.pbxproj @@ -491,6 +491,9 @@ B59851491C90289D00C99590 /* NSPersistentStoreCoordinator+Setup.swift in Sources */ = {isa = PBXBuildFile; fileRef = B59AFF401C6593E400C0ABE2 /* NSPersistentStoreCoordinator+Setup.swift */; }; B598514A1C90289E00C99590 /* NSPersistentStoreCoordinator+Setup.swift in Sources */ = {isa = PBXBuildFile; fileRef = B59AFF401C6593E400C0ABE2 /* NSPersistentStoreCoordinator+Setup.swift */; }; B598514B1C90289F00C99590 /* NSPersistentStoreCoordinator+Setup.swift in Sources */ = {isa = PBXBuildFile; fileRef = B59AFF401C6593E400C0ABE2 /* NSPersistentStoreCoordinator+Setup.swift */; }; + B59A51832256C85E00CEF3C5 /* VersionLockTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B59A51822256C85E00CEF3C5 /* VersionLockTests.swift */; }; + B59A51842256C85E00CEF3C5 /* VersionLockTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B59A51822256C85E00CEF3C5 /* VersionLockTests.swift */; }; + B59A51852256C85E00CEF3C5 /* VersionLockTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B59A51822256C85E00CEF3C5 /* VersionLockTests.swift */; }; B59AFF411C6593E400C0ABE2 /* NSPersistentStoreCoordinator+Setup.swift in Sources */ = {isa = PBXBuildFile; fileRef = B59AFF401C6593E400C0ABE2 /* NSPersistentStoreCoordinator+Setup.swift */; }; B5A1DAC81F111BFA003CF369 /* KeyPath+Querying.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5A1DAC71F111BFA003CF369 /* KeyPath+Querying.swift */; }; B5A1DAC91F111BFA003CF369 /* KeyPath+Querying.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5A1DAC71F111BFA003CF369 /* KeyPath+Querying.swift */; }; @@ -858,6 +861,7 @@ B596BBAD1DD59FDB001DCDD9 /* ConvenienceTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConvenienceTests.swift; sourceTree = ""; }; B596BBB51DD5BC67001DCDD9 /* FetchableSource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FetchableSource.swift; sourceTree = ""; }; B596BBBA1DD5C39F001DCDD9 /* QueryableSource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QueryableSource.swift; sourceTree = ""; }; + B59A51822256C85E00CEF3C5 /* VersionLockTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VersionLockTests.swift; sourceTree = ""; }; B59AFF401C6593E400C0ABE2 /* NSPersistentStoreCoordinator+Setup.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSPersistentStoreCoordinator+Setup.swift"; sourceTree = ""; }; B5A1DAC71F111BFA003CF369 /* KeyPath+Querying.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "KeyPath+Querying.swift"; sourceTree = ""; }; B5A261201B64BFDB006EB6D3 /* MigrationType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MigrationType.swift; sourceTree = ""; }; @@ -1110,6 +1114,7 @@ B5489F3C1CF5EEBC008B4978 /* TestEntities */, B5489F451CF5F017008B4978 /* TransactionTests.swift */, B525577F1D029D2500E51965 /* TweakTests.swift */, + B59A51822256C85E00CEF3C5 /* VersionLockTests.swift */, B52557731D02791400E51965 /* WhereTests.swift */, ); path = CoreStoreTests; @@ -2043,6 +2048,7 @@ B5489F461CF5F017008B4978 /* TransactionTests.swift in Sources */, B52557801D029D2500E51965 /* TweakTests.swift in Sources */, B580857A1CDF808C004C2EEB /* SetupTests.swift in Sources */, + B59A51832256C85E00CEF3C5 /* VersionLockTests.swift in Sources */, B5489F4C1CF5F743008B4978 /* BaseTestCase.swift in Sources */, B5DBE2DF1C9939E100B5CEFA /* BridgingTests.m in Sources */, B57D27BE1D0BBE8200539C58 /* BaseTestDataTestCase.swift in Sources */, @@ -2237,6 +2243,7 @@ B52557711D02561A00E51965 /* SelectTests.swift in Sources */, B5489F471CF5F017008B4978 /* TransactionTests.swift in Sources */, B52557811D029D2500E51965 /* TweakTests.swift in Sources */, + B59A51842256C85E00CEF3C5 /* VersionLockTests.swift in Sources */, B5489F4D1CF5F743008B4978 /* BaseTestCase.swift in Sources */, 82BA18DC1C4BBD9C00A0916E /* Model.xcdatamodeld in Sources */, B57D27BF1D0BBE8200539C58 /* BaseTestDataTestCase.swift in Sources */, @@ -2431,6 +2438,7 @@ B5489F481CF5F017008B4978 /* TransactionTests.swift in Sources */, B52557821D029D2500E51965 /* TweakTests.swift in Sources */, B5489F4E1CF5F743008B4978 /* BaseTestCase.swift in Sources */, + B59A51852256C85E00CEF3C5 /* VersionLockTests.swift in Sources */, B5598BCC1BE2093D0092EFCE /* Model.xcdatamodeld in Sources */, B5220E261D1308C2009BC71E /* ListObserverTests.swift in Sources */, B57D27C01D0BBE8200539C58 /* BaseTestDataTestCase.swift in Sources */, diff --git a/CoreStoreTests/VersionLockTests.swift b/CoreStoreTests/VersionLockTests.swift new file mode 100644 index 0000000..8af5b47 --- /dev/null +++ b/CoreStoreTests/VersionLockTests.swift @@ -0,0 +1,55 @@ +// +// VersionLockTests.swift +// CoreStore +// +// Copyright © 2018 John Rommel Estropia +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +import XCTest + +@testable +import CoreStore + + +//MARK: - VersionLockTests + +final class VersionLockTests: XCTestCase { + + @objc + dynamic func test_ThatVersionLocksProduceCorrectHashes() { + + let versionLock: VersionLock = [ + "Animal": [0x1b59d511019695cf, 0xdeb97e86c5eff179, 0x1cfd80745646cb3, 0x4ff99416175b5b9a], + "Dog": [0xe3f0afeb109b283a, 0x29998d292938eb61, 0x6aab788333cfc2a3, 0x492ff1d295910ea7], + "Person": [0x2831cf046084d96d, 0xbe19b13ace54641, 0x635a082728b0f6f0, 0x3d4ef2dd4b74a87c] + ] + XCTAssertEqual( + versionLock.description, + """ + [ + "Animal": [0x1b59d511019695cf, 0xdeb97e86c5eff179, 0x1cfd80745646cb3, 0x4ff99416175b5b9a], + "Dog": [0xe3f0afeb109b283a, 0x29998d292938eb61, 0x6aab788333cfc2a3, 0x492ff1d295910ea7], + "Person": [0x2831cf046084d96d, 0xbe19b13ace54641, 0x635a082728b0f6f0, 0x3d4ef2dd4b74a87c] + ] + """ + ) + } +} diff --git a/Sources/CoreStore+CustomDebugStringConvertible.swift b/Sources/CoreStore+CustomDebugStringConvertible.swift index 00d923b..894932a 100644 --- a/Sources/CoreStore+CustomDebugStringConvertible.swift +++ b/Sources/CoreStore+CustomDebugStringConvertible.swift @@ -922,11 +922,11 @@ extension VersionLock: CustomStringConvertible, CustomDebugStringConvertible, Co for (index, keyValue) in self.hashesByEntityName.sorted(by: { $0.key < $1.key }).enumerated() { let data = keyValue.value - let count = data.count let bytes = data.withUnsafeBytes { (pointer) in - return (0 ..< (count / MemoryLayout.size)) - .map({ "\("0x\(String(pointer[$0], radix: 16, uppercase: false))")" }) + return pointer + .bindMemory(to: VersionLock.HashElement.self) + .map({ "\("0x\(String($0, radix: 16, uppercase: false))")" }) } string.append("\(index == 0 ? "\n" : ",\n")\"\(keyValue.key)\": [\(bytes.joined(separator: ", "))]") } diff --git a/Sources/DynamicSchema+Convenience.swift b/Sources/DynamicSchema+Convenience.swift index ada393b..f357116 100644 --- a/Sources/DynamicSchema+Convenience.swift +++ b/Sources/DynamicSchema+Convenience.swift @@ -153,11 +153,10 @@ extension DynamicSchema { valueType = Data.self if let defaultValue = (attribute.defaultValue as! Data.QueryableNativeType?).flatMap(Data.cs_fromQueryableNativeType) { - let count = defaultValue.count let bytes = defaultValue.withUnsafeBytes { (pointer) in - - return (0 ..< (count / MemoryLayout.size)) - .map({ "\("0x\(String(pointer[$0], radix: 16, uppercase: false))")" }) + return pointer + .bindMemory(to: UInt64.self) + .map({ "\("0x\(String($0, radix: 16, uppercase: false))")" }) } defaultString = ", initial: Data(bytes: [\(bytes.joined(separator: ", "))])" }