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

Allow users to rebind expect! #26

Merged
merged 3 commits into from
Jan 3, 2022
Merged

Conversation

ecstatic-morse
Copy link
Contributor

Currently, expect-test checks for the string expect![[ when updating test cases. As a result, setting UPDATE_EXPECT=1 will cause a panic if expect is imported under a different name.

We have the column of the macro invocation for each Expect. Use that as a starting point instead.

@ecstatic-morse
Copy link
Contributor Author

ecstatic-morse commented Dec 26, 2021

This doesn't test that UPDATE_EXPECT actually works, yet (I've been testing it locally with chalk). It seems like an integration test would be best for this, as opposed to the unit tests I've been writing, but I'll wait to see what you think of this change in general.

@matklad
Copy link
Member

matklad commented Dec 27, 2021

👍 to general idea of allowing renaming

Yeah, I like this approach more! I am a bit surprised that we look for first [ and not the second one, but it probably is fine. I'd also maybe try to write this in a simpler way, like line[column..].find("[[") but I guess that doesn't work, as column is in unicode charactres.

Actually, what unit the column is in? This seems to not be documented.

src/lib.rs Outdated
//!
//! ```
//! use expect_test::expect;
//! use expect_test::expect as ex; // Note the renaming
Copy link
Member

Choose a reason for hiding this comment

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

Let's not add this to the docs: this is a niche thing you shouldn't generally be doing. Documenting this helps for few people that do need this, but harms everyone else. Docs are like code -- the fewer lines you spend, the better!

@ecstatic-morse
Copy link
Contributor Author

Actually, what unit the column is in? This seems to not be documented.

I filed rust-lang/rust#92301 while implementing this. I believe it's code points.

I'd also maybe try to write this in a simpler way,

Part of the complexity is because I split this out from #27. You could do find("[[") on top of char_indices if there's no plan to support the usual set of macro delimiters, but I'd like to make it work.

@matklad
Copy link
Member

matklad commented Jan 3, 2022

bors r+

@bors
Copy link
Contributor

bors bot commented Jan 3, 2022

Build succeeded:

@bors bors bot merged commit 8ad37b7 into rust-analyzer:master Jan 3, 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.
bors bot added a commit that referenced this pull request Mar 4, 2022
27: Support single delimiter version of `expect![]` r=matklad a=ecstatic-morse

Pairs of braces are no longer necessary that we parse string literals. Allow users to omit the inner `[]`.

This represents a significant change to the public API, and you might have other tooling that depends on the paired braces, so I understand if you don't want it upstream. I think it reads a bit nicer, though.

Depends on #26 (although it could be separated), and is similarly lacking in integration tests for now.

Co-authored-by: Dylan MacKenzie <ecstaticmorse@gmail.com>
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