Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 months ago.
Improve this question
I'm trying to find a way how to document my tests which might be rather long to fully explain the context and desired behavior(up to 10-12 words).
I'm more used to BDD style specs with nested contexts and verbose expectations but standard [test] attribute is fine as well.
My question is: can this happy-path snippet below be somehow rewritten for better readability?
#[test]
fn test_when_user_this_and_than_it_does_something_special() {
// ...
}
I was looking for something like #[test(name="plain text test case description")](to avoid heave snake_case naming) but without much success. Or perhaps there is a crate to mitigate this issue?
Rust does not have any kind of test naming separate from the function names. In order to include information about the purpose of the test, I would suggest that you write a concise name and documentation containing the rest of the words:
/// When the user does this and that, the special thing should happen.
#[test]
fn this_that_then_special() {
// ...
}
You can also, if you want, put the comment inside the block using the inner doc-comment syntax //!:
#[test]
fn this_that_then_special() {
//! When the user does this and that, the special thing should happen.
// ...
}
Documentation for tests doesn't show up in generated documentation files, of course, so there isn't a whole lot of point to using the specific syntax, but it is a standard syntax for attaching an explanation to any item.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 months ago.
Improve this question
Same question as Is there any negative performance implication to using local functions in Rust? but for struct.
fn bla() -> PublicStruct {
MyHiddenStruct {bla:String};
let m = MyHiddenStruct{bla: "aa".to_string()};
// some work...
m.into()
}
Edit: Indeed I tried a little benchmark with criterion
innerouterstruct/outer struct
time: [901.16 ps 905.16 ps 910.44 ps]
innerouterstruct/inner struct
time: [901.10 ps 903.89 ps 907.31 ps]
No. The only thing this affects is the visibility of the struct (and its name). Codegen is not affected at all.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I'm working on a large rust project. It's split into multiple workspaces, but occasionally I come across enormous files containing multiple modules. I wonder if there are any good reasons not to split (at least) each module into a separate file, other than readability?
Crates are compilation units so there's no downside wrt. optimization opportunities when you split up large files into smaller files / modules.
You could write your complete crate in a single lib.rs or main.rs but that makes working with the code a pain. With visibility rules, you can control the internal API surface of your crate through modules and enforce going through certain APIs instead of accessing internals of structures / private functions.
Splitting up your code into submodules can make your life easier when dealing with conditional compilation through feature gates if you encapsulate all code relevant to a given feature in a module of its own. That way you don't have to add a feature gate to every line / function pertaining to that feature but just put the attribute on the module declaration.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
If you have an instance of std::path::Path and want to write a text File to that path, what is the best way? I can call .display() to get a string and then use File I/O methods that take strings as file names, but is there a preferred way that involves something related to Path, PathBuf, etc? The .display() method is said to be lossy due to encoding issues so I would prefer a means that did not sacrifice generality.
You can use std::fs::write. The function has following signature, so it will work with Path objects too.
pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> Result<()>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
If a global variable is being cloned (as implemented within the standard library) while being written to will it create a data race?
Cloning data involves reading it. Writing to data involves, well, writing it.
We can safely access data in the following ways
Any number of threads can read data at a given moment, or
One thread can write to data at a given moment, provided no one else is reading or writing
Neither of these conditions applies (we're reading for the clone and we're writing at the same time). Therefore, yes, it's a data race.
As pointed out in the comments, Rust forbids data races. You can't so much as look at a global variable in Rust without an unsafe block, since it's never safe to do so, by Rust's rules. But if you wrap your code in unsafe and don't provide additional protection then yes, this is a data race.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
`
Blockquote
How to write test class to test action classes using mockito?
Are you using JUnit? Think about having one test method for each requirement that your class has to meet, or each particular scenario of a requirement. Once you've planned out what each test method will be, work out
what you need to set up in each test method,
what the actual call to your class will look like within the test method,
what you need to verify at the end of the test method.
Use Mockito when any of your test methods are likely to use functionality outside of the class that you're actually trying to test. Mocking is all about removing the behaviour of other classes (collaborator classes) from what happens during your test. Once you've identified what these other classes are, and what behaviour you want from them, then you can start using Mockito.
But you really need to plan out your tests first. Mockito is not a silver bullet that will test things for you.
I'm sorry, but I really can't give a more detailed answer than this, unless you would care to add some detail to your question. Right now, your question is remarkably vague.