-
Notifications
You must be signed in to change notification settings - Fork 546
macros: simplify compile-fail tests #2120
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /// To activate a doctest locally, remove ",ignore" from the code block. | ||
| /// | ||
| /// # Comma separator | ||
| /// | ||
| /// ```compile_fail,ignore | ||
| /// use macros::hashmap; | ||
| /// use std::collections::HashMap; | ||
| /// | ||
| /// // using only commas is invalid | ||
| /// let _hm: HashMap<_, _> = hashmap!('a', 1); | ||
| /// ``` | ||
| /// | ||
| /// # Double trailing commas | ||
| /// | ||
| /// ```compile_fail,ignore | ||
| /// use macros::hashmap; | ||
| /// use std::collections::HashMap; | ||
| /// | ||
| /// // a single trailing comma is okay, but two is not | ||
| /// let _hm: HashMap<_, _> = hashmap!('a' => 2, ,); | ||
| /// ``` | ||
| /// | ||
| /// # Only comma | ||
| /// | ||
| /// ```compile_fail,ignore | ||
| /// use macros::hashmap; | ||
| /// use std::collections::HashMap; | ||
| /// | ||
| /// // a single random comma is not valid | ||
| /// let _hm: HashMap<(), ()> = hashmap!(,); | ||
| /// ``` | ||
| /// | ||
| /// # Single argument | ||
| /// | ||
| /// ```compile_fail,ignore | ||
| /// use macros::hashmap; | ||
| /// use std::collections::HashMap; | ||
| /// | ||
| /// // a single argument is invalid | ||
| /// let _hm: HashMap<_, _> = hashmap!('a'); | ||
| /// ``` | ||
| /// | ||
| /// # Triple arguments | ||
| /// | ||
| /// ```compile_fail,ignore | ||
| /// use macros::hashmap; | ||
| /// use std::collections::HashMap; | ||
| /// | ||
| /// // three arguments are invalid | ||
| /// hashmap!('a' => 1, 'b'); | ||
| /// ``` | ||
| /// | ||
| /// # Only arrow | ||
| /// | ||
| /// ```compile_fail,ignore | ||
| /// use macros::hashmap; | ||
| /// use std::collections::HashMap; | ||
| /// | ||
| /// // a single random arrow is not valid | ||
| /// let _hm: HashMap<(), ()> = hashmap!(=>); | ||
| /// ``` | ||
| /// | ||
| /// # Trailing arrow | ||
| /// | ||
| /// ```compile_fail,ignore | ||
| /// use macros::hashmap; | ||
| /// use std::collections::HashMap; | ||
| /// | ||
| /// // a trailing => isn't valid either | ||
| /// hashmap!('a' => 2, =>); | ||
| /// ``` | ||
| /// | ||
| /// # Leading comma | ||
| /// | ||
| /// ```compile_fail,ignore | ||
| /// use macros::hashmap; | ||
| /// use std::collections::HashMap; | ||
| /// | ||
| /// // leading commas are not valid | ||
| /// let _hm: HashMap<_, _> = hashmap!(, 'a' => 2); | ||
| /// ``` | ||
| /// | ||
| /// # Missing comma | ||
| /// | ||
| /// ```compile_fail,ignore | ||
| /// use macros::hashmap; | ||
| /// use std::collections::HashMap; | ||
| /// | ||
| /// // Key value pairs must be separated by commas | ||
| /// let _hm: HashMap<_, _> = hashmap!('a' => 1 'b' => 2); | ||
| /// ``` | ||
| /// | ||
| /// # Missing argument | ||
| /// | ||
| /// ```compile_fail,ignore | ||
| /// use macros::hashmap; | ||
| /// use std::collections::HashMap; | ||
| /// | ||
| /// // an argument should come between each pair of commas | ||
| /// let _hm: HashMap<_, _> = hashmap!('a' => 1, , 'b' => 2); | ||
| /// ``` | ||
| /// | ||
| const _TESTS: () = (); |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically speaking, what's to prevent a user from exporting a different module as
compile_fail_testsinlib.rs?In general I think the idea is good, but I feel like it can always be cheated somehow...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's true. I feel like it should be ok. It's not like users can win something unfairly by cheating. They're just cheating themselves out of learning.
I think there would be a way to make it completely bullet proof. We could turn
lib.rsinto a "test" file, meaning students cannot modify it. It includes the doctests and amod actual_solutionor whatever.src/actual_solution.rswould be the file users can modify.But I don't want to make the exercise structure super confusing for normal users who wouldn't even think of cheating, if the risk is so low anyway.
What if we include a test in
tests/macros.rsthat reads the solution file and makes sure a normalmod compile_fail_tests;statement is present in the file ? 😅 I guess it's possible to have a string literal with that content... The next step is to parse actual Rust syntax 😄There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it's fine the way it is... 😆
It's not really a big deal, if someone cheats with creating another module then more power to them.
I also agree that we should keep users editing
lib.rs, I think cheating is better than having a whole new directory structure.