Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the minimal number of escapes when updating tests #28

Merged
merged 4 commits into from
Jan 4, 2022

Conversation

ecstatic-morse
Copy link
Contributor

Currently, expect-test uses a raw string with at least one hash when blessing tests. This is a bit verbose for single-line output that doesn't contain any quotes or escaped characters.

This PR parses each patch to find the simplest string literal that can represent it losslessly, and uses that instead. For now, multiline patches continue to use raw string literals (with the minimum number of hashes), although I think they could use regular ones.

@matklad
Copy link
Member

matklad commented Jan 3, 2022

Yeah, we intentionally didn't pick the simplest literal -- the benefit of using r#" everywhere is consistency. But I also agree that this is unfortunate for single-line expects. I think the logic we want here is:

  • If the expect is single-line, we do expect!["foo"]
  • if expect is multi-line, we do expect![[r#"foo"#]]

@ecstatic-morse
Copy link
Contributor Author

Updated version uses StrLitKind::Raw(1 /*hash*/) instead of StrLitKind::Raw(0) for patches containing newlines or backslash escapes.

Copy link
Member

@matklad matklad left a comment

Choose a reason for hiding this comment

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

bors r+

@bors
Copy link
Contributor

bors bot commented Jan 4, 2022

Build succeeded:

@bors bors bot merged commit 1b035b3 into rust-analyzer:master Jan 4, 2022
bors added a commit to rust-lang/chalk that referenced this pull request Feb 13, 2022
Allow tests to be updated automatically

Uses the `expect-test` crate (thanks for the recommendation `@lnicola!)` to implement snapshot testing. `expect-test` is a bit more flexible than `insta`, so it seems like a good fit. This gives nicer diffs as well, since we don't strip whitespace prior to the comparison.

Setting `UPDATE_EXPECT=1` before running `cargo test` will update all tests automatically.

## Blockers
- [x] Omit empty lists from `Solution`'s `Display` impl.
  * `chalk` currently compares prefixes to make things less noisy, but that seems error prone anyways.
  * Harder than it seems at first, since you need to unintern to check whether a list is empty.
- [x] Handle recursive/SLG comparisons better.
  * Many tests use `yields`, which compares the results of the two solvers against a known output. Unfortunately, the test macro will duplicate the `expect` invocation, so if neither output matches the expected one, `expect-test` will overwrite it *twice*, almost assuredly corrupting the test file.
  * There's many ways to fix this. ~~I've hacked around it here by adding a method to `Expect` (which is not yet upstream).~~ Most robust is to collect the various engine results first and only compare them against the output if they agree. *This is implemented now*.
- [x] rust-analyzer/expect-test#20

## Nice to haves
- [x] Don't use raw strings with a hash unconditionally when blessing (rust-analyzer/expect-test#28).
- [x] Shorter alias for `expect![[`? It's currently hard-coded into the `expect-test` crate (rust-analyzer/expect-test#26).
- [ ] Remove newline escapes from existing tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants