From eb3ccd9057ca566ffef3161a311eec86747f748f Mon Sep 17 00:00:00 2001 From: Jason Morley Date: Mon, 24 Feb 2025 20:01:03 -1000 Subject: [PATCH] fix: Sort additions and deletions together (#45) --- Sources/ReporterCore/Model/Change.swift | 46 +++++++++++++++++++++++ Sources/ReporterCore/Model/Changes.swift | 23 ++++-------- Sources/ReporterCore/Model/Snapshot.swift | 4 +- Sources/ReporterCore/Reporter.swift | 11 +++--- 4 files changed, 63 insertions(+), 21 deletions(-) create mode 100644 Sources/ReporterCore/Model/Change.swift diff --git a/Sources/ReporterCore/Model/Change.swift b/Sources/ReporterCore/Model/Change.swift new file mode 100644 index 0000000..95297de --- /dev/null +++ b/Sources/ReporterCore/Model/Change.swift @@ -0,0 +1,46 @@ +// Copyright (c) 2024-2025 Jason Morley +// +// 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 Foundation + +public struct Change { + + enum Kind { + case addition + case deletion + } + + let kind: Kind + let source: Item + let destination: Item? + + let isAddition: Bool + let isDeletion: Bool + + init(kind: Kind, source: Item, destination: Item? = nil) { + self.kind = kind + self.source = source + self.destination = destination + + self.isAddition = kind == .addition + self.isDeletion = kind == .deletion + } + +} diff --git a/Sources/ReporterCore/Model/Changes.swift b/Sources/ReporterCore/Model/Changes.swift index e2fb066..0367df2 100644 --- a/Sources/ReporterCore/Model/Changes.swift +++ b/Sources/ReporterCore/Model/Changes.swift @@ -22,30 +22,23 @@ import Foundation public struct Changes: CustomStringConvertible { - public let additions: [Item] - public let deletions: [Item] + public let changes: [Change] public let isEmpty: Bool public var description: String { return ( - "\(additions.count) additions\n" + - additions - .map { " \($0.path)\n" } - .joined() + - "\(deletions.count) deletions\n" + - deletions - .map { " \($0.path)\n" } + "\(changes.count) changes\n" + + changes + .map { " \($0.source.path)\n" } .joined() ) } - public init(additions: [Item], deletions: [Item]) { - self.additions = additions - .sorted { $0.path.localizedStandardCompare($1.path) == .orderedAscending } - self.deletions = deletions - .sorted { $0.path.localizedStandardCompare($1.path) == .orderedAscending } - self.isEmpty = additions.isEmpty && deletions.isEmpty + public init(changes: [Change]) { + self.changes = changes + .sorted { $0.source.path.localizedStandardCompare($1.source.path) == .orderedAscending } + self.isEmpty = changes.isEmpty } } diff --git a/Sources/ReporterCore/Model/Snapshot.swift b/Sources/ReporterCore/Model/Snapshot.swift index a39365b..940b2fb 100644 --- a/Sources/ReporterCore/Model/Snapshot.swift +++ b/Sources/ReporterCore/Model/Snapshot.swift @@ -36,8 +36,10 @@ public struct Snapshot: Codable { public func changes(from initialState: Snapshot) -> Changes { let additions = items.subtracting(initialState.items) + .map { Change(kind: .addition, source: $0) } let deletions = initialState.items.subtracting(items) - return Changes(additions: Array(additions), deletions: Array(deletions)) + .map { Change(kind: .deletion, source: $0) } + return Changes(changes: Array(additions) + Array(deletions)) } } diff --git a/Sources/ReporterCore/Reporter.swift b/Sources/ReporterCore/Reporter.swift index e3873be..b133196 100644 --- a/Sources/ReporterCore/Reporter.swift +++ b/Sources/ReporterCore/Reporter.swift @@ -284,11 +284,12 @@ public class Reporter { {% if item.changes.isEmpty %}{% else %} {% endif %}