Skip to content

Strengthen test assertions across the suite#883

Open
jpurnell wants to merge 2 commits into
twostraws:mainfrom
jpurnell:test/quality-fixes
Open

Strengthen test assertions across the suite#883
jpurnell wants to merge 2 commits into
twostraws:mainfrom
jpurnell:test/quality-fixes

Conversation

@jpurnell
Copy link
Copy Markdown
Contributor

Summary

Small housekeeping pass over the existing test suite to tighten up assertions:

  • Float comparisons: Replaced exact == on floating-point values with tolerance-based checks (abs(a - b) < 1e-6) in AnimatableData, Animation, Transition, and Percentage tests
  • Force-try removal: Replaced try! with try? in the shared HTML test helpers (String-TestingHTML.swift), so a bad regex pattern returns nil instead of crashing the test runner
  • Stronger nil checks: Converted #expect(x != nil) to let x = try #require(x) where the unwrapped value is used by later assertions — gives a clear failure message and stops the test early instead of cascading nil-related failures
  • Deterministic test data: Replaced Int.random(), Double.random(), and Bool.random() with fixed values so tests are reproducible across runs

No new tests, no production code changes. All 1,177 existing tests continue to pass.

Test plan

  • swift test passes (1,177 tests, 214 suites)
  • No changes to any file under Sources/

jpurnell added 2 commits May 20, 2026 14:23
… unseeded random

Resolves all errors and warnings from the test-quality auditor across
19 existing test files. No new tests added, no production code changed.

- Replace exact == on floating-point values with abs(a - b) < 1e-6
- Replace try! with try? in test HTML helpers
- Replace weak assertions (#expect != nil) with #require() for fail-fast
- Replace unseeded random with deterministic test data
Replace fixed test values with a SplitMix64-based seeded generator
so tests remain deterministic while exercising realistic random inputs.
Copy link
Copy Markdown
Collaborator

@MrSkwiggs MrSkwiggs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contributions 👍 A lot of these are quite nice, though some I believe are not entirely warranted. I've left comments below, please have a look 🙏

Comment on lines -188 to +200
var isPrimitive: Bool = Bool.random()
var isPrimitive: Bool
var body: some HTML { self }
func render() -> String { "<div id=\"\(id)\"></div>" }
}

let mockHTMLElements = [
MockHTML(id: "a"),
MockHTML(id: "b"),
MockHTML(id: "c")
]
let mockHTMLElements: [MockHTML] = {
var rng = SeededRandomNumberGenerator(seed: 99)
return [
MockHTML(id: "a", isPrimitive: rng.nextBool()),
MockHTML(id: "b", isPrimitive: rng.nextBool()),
MockHTML(id: "c", isPrimitive: rng.nextBool())
]
}()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely convinced adding seeded RNG here is really necessary

func defaultDuration() async throws {
let data = AnimatableData(.opacity, value: "0")
#expect(data.duration == 0.35)
#expect(abs(data.duration - 0.35) < 1e-6)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd much prefer if we wrote an overload or helper function that did this for us in a more controlled manner. Right now this is very repetitive and easy to forget adding in future tests.

Comment on lines -131 to +130
#expect(text != nil)
_ = try #require(element.as(Text.self))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this exact scenario where we don't actually need the non-optional value, #require does not offer any benefit over checking for optionality with #expect(text != nil). In fact I would argue these specific changes make the test cases ever so slightly less readable

let hoverID = try #require(firstHoverAnimationID(in: output))

#expect(hoverID != nil)
_ = hoverID
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe this is a good change; we're now silently dismissing a test expectation


/// A deterministic random number generator using SplitMix64.
/// Use in tests to get reproducible "random" values across runs.
struct SeededRandomNumberGenerator: RandomNumberGenerator {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, not convinced we need this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants