SPB Git

Repository Public

CodeDiffTests.swift 6.8 KB 153 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Astra9// File             : Tests/AstraCoreTests/CodeDiffTests.swift10// Module           : AstraCoreTests11// Repository       : https://git.spboucher.ai/orix-astra12// Created          : 2026-11-2213// Last Modified    : 2026-11-2214//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author           : Simon-Pierre Boucher20// Position         : Professor of Finance21// Institution      : Université du Québec en Outaouais (UQO)22// Department       : Département des sciences administratives23// Organization     : SP Boucher24//25// Website          : https://spboucher.ai26// Git              : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General          : info@spboucher.ai33// Professional     : contact@spboucher.ai34// Direct           : simon@spboucher.ai35// Research         : research@spboucher.ai36// Development      : dev@spboucher.ai37// Support          : support@spboucher.ai38// Security/Admin   : admin@spboucher.ai39// Notifications    : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification   : Public66// Distribution     : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License          : Proprietary, all rights reserved (see license file)80// License File     : LICENSE81// Maintainer       : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import Foundation89import Testing90@testable import AstraCore9192@Suite struct CodeDiffTests {93    @Test func identicalTextHasNoDifferences() {94        let code = "func a() {\n    return 1\n}\n"95        let lines = CodeDiff.lines(old: code, new: code)96        #expect(lines.allSatisfy { $0.kind == .same } && lines.count == 3)97        #expect(CodeDiff.summary(lines).isIdentical)98        #expect(CodeDiff.summary(lines).label == "No differences")99    }100101    @Test func aChangedLineIsARemovalAndAnAddition() {102        let lines = CodeDiff.lines(old: "let a = 1\nlet b = 2\nprint(a + b)", new: "let a = 1\nlet b = 3\nprint(a + b)")103        #expect(lines.map(\.kind) == [.same, .removed, .added, .same])104        #expect(lines[1].text == "let b = 2" && lines[1].oldNumber == 2 && lines[1].newNumber == nil)105        #expect(lines[2].text == "let b = 3" && lines[2].oldNumber == nil && lines[2].newNumber == 2)106        #expect(lines[3].oldNumber == 3 && lines[3].newNumber == 3)107        let s = CodeDiff.summary(lines)108        #expect(s.added == 1 && s.removed == 1 && s.unchanged == 2)109    }110111    @Test func insertionsAndDeletionsKeepLineNumbersAligned() {112        let old = "a\nb\nc\nd", new = "a\nx\nb\nd\ne"113        let lines = CodeDiff.lines(old: old, new: new)114        // rebuilding each side from the diff must give back the inputs exactly115        #expect(lines.filter { $0.kind != .added }.map(\.text).joined(separator: "\n") == old)116        #expect(lines.filter { $0.kind != .removed }.map(\.text).joined(separator: "\n") == new)117        #expect(lines.compactMap(\.oldNumber) == [1, 2, 3, 4])118        #expect(lines.compactMap(\.newNumber) == [1, 2, 3, 4, 5])119    }120121    @Test func trailingWhitespaceAndLineEndingsAreNotDifferences() {122        let lines = CodeDiff.lines(old: "x = 1  \r\ny = 2\r\n", new: "x = 1\ny = 2")123        #expect(CodeDiff.summary(lines).isIdentical)124    }125126    @Test func emptySides() {127        #expect(CodeDiff.lines(old: "", new: "").isEmpty)128        #expect(CodeDiff.lines(old: "", new: "a\nb").map(\.kind) == [.added, .added])129        #expect(CodeDiff.lines(old: "a\nb", new: "").map(\.kind) == [.removed, .removed])130    }131132    @Test func roundTripsOnRandomEdits() {133        var rng = SystemRandomNumberGenerator()134        for _ in 0..<60 {135            let base = (0..<Int.random(in: 0...40, using: &rng)).map { _ in "line \(Int.random(in: 0...12, using: &rng))" }136            var edited = base137            for _ in 0..<Int.random(in: 0...8, using: &rng) {138                if !edited.isEmpty, Bool.random(using: &rng) { edited.remove(at: Int.random(in: 0..<edited.count, using: &rng)) }139                else { edited.insert("new \(Int.random(in: 0...99, using: &rng))", at: Int.random(in: 0...edited.count, using: &rng)) }140            }141            let lines = CodeDiff.lines(old: base.joined(separator: "\n"), new: edited.joined(separator: "\n"))142            #expect(lines.filter { $0.kind != .added }.map(\.text) == base)143            #expect(lines.filter { $0.kind != .removed }.map(\.text) == edited)144        }145    }146147    @Test func veryLargeInputsFallBackWithoutHanging() {148        let old = (0..<(CodeDiff.lineLimit + 5)).map { "o\($0)" }.joined(separator: "\n")149        let lines = CodeDiff.lines(old: old, new: "n1\nn2")150        #expect(CodeDiff.summary(lines).removed == CodeDiff.lineLimit + 5 && CodeDiff.summary(lines).added == 2)151    }152}153
Files main
Menu

Theme