Add snapshot tests

This commit is contained in:
Guille Gonzalez
2026-01-10 18:01:38 +01:00
parent 00d93de457
commit 339f20a34b
8 changed files with 146 additions and 2 deletions

42
Package.resolved Normal file
View File

@@ -0,0 +1,42 @@
{
"originHash" : "f1d7267746a12d71d624c75daa345adf396e99a25ae07ee92716ae508085d85e",
"pins" : [
{
"identity" : "swift-custom-dump",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-custom-dump",
"state" : {
"revision" : "82645ec760917961cfa08c9c0c7104a57a0fa4b1",
"version" : "1.3.3"
}
},
{
"identity" : "swift-snapshot-testing",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-snapshot-testing",
"state" : {
"revision" : "a8b7c5e0ed33d8ab8887d1654d9b59f2cbad529b",
"version" : "1.18.7"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/swiftlang/swift-syntax",
"state" : {
"revision" : "4799286537280063c85a32f09884cfbca301b1a1",
"version" : "602.0.0"
}
},
{
"identity" : "xctest-dynamic-overlay",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/xctest-dynamic-overlay",
"state" : {
"revision" : "34e463e98ab8541c604af706c99bed7160f5ec70",
"version" : "1.8.1"
}
}
],
"version" : 3
}

View File

@@ -14,7 +14,9 @@ let package = Package(
products: [
.library(name: "SwiftUIMath", targets: ["SwiftUIMath"])
],
dependencies: [],
dependencies: [
.package(url: "https://github.com/pointfreeco/swift-snapshot-testing", from: "1.18.7")
],
targets: [
.target(
name: "SwiftUIMath",
@@ -23,7 +25,11 @@ let package = Package(
),
.testTarget(
name: "SwiftUIMathTests",
dependencies: ["SwiftUIMath"]
dependencies: [
"SwiftUIMath",
.product(name: "SnapshotTesting", package: "swift-snapshot-testing"),
],
exclude: ["__Snapshots__"]
),
]
)

View File

@@ -0,0 +1,96 @@
#if os(iOS)
import SnapshotTesting
import SwiftUI
import Testing
@testable import SwiftUIMath
@MainActor
struct MathTests {
private let layout = SwiftUISnapshotLayout.device(config: .iPhone8)
@Test
func displayAndInlineStyles() {
let view = VStack(alignment: .leading, spacing: 16) {
Math("\\frac{1}{2}+\\sqrt{2}+\\sum_{i=1}^{n}x_i")
.mathTypesettingStyle(.display)
.mathFont(Math.Font(name: .latinModern, size: 24))
Math("\\int_0^1 x^2\\,dx = \\frac{1}{3}")
.mathTypesettingStyle(.text)
.mathFont(Math.Font(name: .libertinus, size: 20))
}
.background(Color.guide)
.padding(.horizontal)
assertSnapshot(of: view, as: .image(layout: layout))
}
@Test
func multicolorExpressions() {
let view = VStack(alignment: .leading, spacing: 16) {
Math("\\color{#cc0000}{a}+\\color{#00aa00}{b}+\\color{#0000cc}{c}")
.mathTypesettingStyle(.text)
.mathRenderingMode(.multicolor)
.mathFont(Math.Font(name: .latinModern, size: 22))
Math("\\textcolor{#ff8800}{\\int_0^1 x^2\\,dx}=\\textcolor{#0088ff}{\\frac{1}{3}}")
.mathRenderingMode(.multicolor)
.mathFont(Math.Font(name: .libertinus, size: 20))
}
.background(Color.guide)
.padding(.horizontal)
assertSnapshot(of: view, as: .image(layout: layout))
}
@Test
func matricesAndCases() {
let view = VStack(alignment: .leading, spacing: 16) {
Math("A=\\begin{pmatrix}1&2\\\\3&4\\end{pmatrix}")
.mathTypesettingStyle(.display)
.mathFont(Math.Font(name: .asana, size: 22))
Math("\\begin{cases} x + y = 5 \\\\ 2x - y = 1 \\end{cases}")
.mathTypesettingStyle(.display)
.mathFont(Math.Font(name: .termes, size: 22))
}
.background(Color.guide)
.padding(.horizontal)
assertSnapshot(of: view, as: .image(layout: layout))
}
@Test
func largeOperatorsAndLimits() {
let view = VStack(alignment: .leading, spacing: 16) {
Math("\\lim_{n\\to\\infty}\\sum_{k=1}^{n}\\frac{1}{k^2}=\\frac{\\pi^2}{6}")
.mathTypesettingStyle(.display)
.mathFont(Math.Font(name: .xits, size: 22))
}
.background(Color.guide)
.padding(.horizontal)
assertSnapshot(of: view, as: .image(layout: layout))
}
@Test
func inlineTextWrapping() {
let view = VStack(alignment: .leading, spacing: 16) {
Math("\\text{Rappelons la conversion : 1 km équivaut à 1000 m.}")
.mathTypesettingStyle(.text)
.mathFont(Math.Font(name: .latinModern, size: 18))
}
.background(Color.guide)
.padding(.horizontal)
assertSnapshot(of: view, as: .image(layout: layout))
}
}
extension Color {
fileprivate static var guide: Color {
.accentColor.opacity(0.06)
}
}
#endif

Binary file not shown.

After

Width:  |  Height:  |  Size: 67 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 59 KiB