diffy

main

A tiny wrapper around the #expect macro that compares the string representation between two values and displays a diff.
andrsem/diffy

diffy logo

A tiny wrapper around the #expect macro that compares the string representation between two values and displays a diff.

Getting Started

To use Diffy in a SwiftPM project, add it as a package dependency to your Package.swift:

dependencies: [
    .package(url: "https://github.com/andrsem/diffy.git", from: "1.0.0")
]

Then, adding the Diffy module to your target dependencies:

.target(
    name: "MyTarget",
    dependencies: [
        .product(name: "Diffy", package: "Diffy")
    ]
)

Examples

diff

import Diffy

let (diff, lhs, rhs) = diff(
   """
   Lorem ipsum dolor sit amet.
   Sed do eiusmod tempor incididunt
   """,
   """
   Lorem ipsum dolor sit amet.
   Sed d eiusmod tempr incididunt
   """
)

print(diff, "", lhs, "", rhs, separator: "\n")
// 2L: "Sed do eiusmod tempor incididunt"
// 2R: "Sed d eiusmod tempr incididunt"
//
// 1 | "Lorem ipsum dolor sit amet."
// 2 | "Sed do eiusmod tempor incididunt"
//
// 1 | "Lorem ipsum dolor sit amet."
// 2 | "Sed d eiusmod tempr incididunt"

expectMatch

import Diffy
import Testing

@Test
func `match test`() {

   expectMatch("same", "same")  // Passes with no output
   expectMatch("   same", "same  ", trimLineEnds: true)  // Passes with no output
   expectMatch("   same", "same  ")
   // Expectation failed: `values differ`: 
   // 
   //   L: "   same"
   //   R: "same  "
   // 
   // LHS:
   //  1 | "   same"
   // 
   // RHS:
   //  1 | "same  "

   
   expectMatch("hello", "hullo")
   // Expectation failed: `values differ`:
   //
   //   L: "hello"
   //   R: "hullo"
   //
   // LHS:
   //  1 | "hello"
   //
   // RHS:
   //  1 | "hullo"
   
   
   expectMatch(
      """
      Lorem ipsum dolor sit amet.
      Sed do eiusmod tempor incididunt
      """,
      """
      Lorem ipsum dolor sit amet.
      Sed d eiusmod tempr incididunt
      """
   )
   // Expectation failed: `values differ`: 
   // 
   //  2L: "Sed do eiusmod tempor incididunt"
   //  2R: "Sed d eiusmod tempr incididunt"
   // 
   // LHS:
   //  1 | "Lorem ipsum dolor sit amet."
   //  2 | "Sed do eiusmod tempor incididunt"
   // 
   // RHS:
   //  1 | "Lorem ipsum dolor sit amet."
   //  2 | "Sed d eiusmod tempr incididunt"

   
   expectMatch((2, 3, 5), (1, 3, 5))
   // Expectation failed: `values differ`:
   // 
   //  2L: .0: Int = 2
   //  2R: .0: Int = 1
   // 
   // LHS:
   //  1 | (Int, Int, Int)
   //  2 |    ╰ .0: Int = 2
   //  3 |    ╰ .1: Int = 3
   //  4 |    ╰ .2: Int = 5
   // 
   // RHS:
   //  1 | (Int, Int, Int)
   //  2 |    ╰ .0: Int = 1
   //  3 |    ╰ .1: Int = 3
   //  4 |    ╰ .2: Int = 5 

}

Description

  • Swift Tools 6.2.0
View More Packages from this Author

Dependencies

  • None
Last updated: Mon Jul 13 2026 02:05:42 GMT-0900 (Hawaii-Aleutian Daylight Time)