Skip to content

Conversation

@grynspan
Copy link
Contributor

@grynspan grynspan commented Apr 19, 2024

This PR enables expression expansion for expressions of the forms !x, !f(), !y.g(), etc. such that the value of the expressions they negate are available at runtime to inspect in a test failure. For example, consider this test:

func f() -> Int { 123 }
func g() -> Int { 123 }

@Test func myTest() {
  #expect(!(f() == g())) // ♦️ Expectation failed: !(f() == g())
}

(i.e., testing that the result of f() is not equal to the result of g(), but using !(==) instead of the more typical !=.)

This is a trivial/contrived example where we can tell at a glance that the values equal 123, of course. Still, previously this test would (correctly) fail, but would not be able to provide the return values of f() and g(). With this change, the following test issue is recorded:

 #expect(!(f() == g())) // ♦️ Expectation failed: !((f() → 123) == (g() → 123) → true)

This increasing the amount of diagnostic information available.

This PR also makes adjustments to the Expression type to improve the information we log during test runs. In particular, when --verbose is passed to swift test, we will capture type information about more parts of a failed expectation/expression.

Checklist:

  • Code and documentation should follow the style of the Style Guide.
  • If public symbols are renamed or modified, DocC references should be updated.

This PR enables expression expansion for expressions of the forms `!x`, `!f()`,
`!y.g()`, etc. such that the value of the expressions they negate are available
at runtime to inspect in a test failure. For example, consider this test:

```swift
func f() -> Int { 123 }
func g() -> Int { 123 }

@test func myTest() {
  #expect(!(f() == g())) // ♦️ Expectation failed: !(f() == g())
}
```

(i.e., testing that the result of `f()` is not equal to the result of `g()`, but
using `!(==)` instead of the more typical `!=`.)

This is a trivial/contrived example where we can tell at a glance that the
values equal `123`, of course. Still, previously this test would (correctly)
fail, but would not be able to provide the return values of `f()` and `g()`.
With this change, the following test issue is recorded:

```swift
 #expect(!(f() == g())) // ♦️ Expectation failed: !((f() → 123) == (g() → 123) → true)
```

This increasing the amount of diagnostic information available.

This PR also makes adjustments to the `Expression` type to improve the
information we log during test runs. In particular, when `--verbose` is passed
to `swift test`, we will capture type information about more parts of a failed
expectation/expression.
@grynspan
Copy link
Contributor Author

@swift-ci please test

Copy link
Contributor

@briancroom briancroom left a comment

Choose a reason for hiding this comment

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

Very nice, this is a great enhancement.

Copy link
Contributor

@stmontgomery stmontgomery left a comment

Choose a reason for hiding this comment

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

Nice, thanks!

@grynspan
Copy link
Contributor Author

@swift-ci please test

@grynspan
Copy link
Contributor Author

@swift-ci please test

@grynspan
Copy link
Contributor Author

@swift-ci please test Windows

@grynspan grynspan merged commit 41fe485 into main Apr 19, 2024
@grynspan grynspan deleted the jgrynspan/expand-negations branch April 19, 2024 20:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants