This question already has an answer here:
In C++, I have often used the following pattern to create testable functions:
MyType readfile(std::ostream& file) {
// Read file and do stuff
return MyType;
}
MyType readfile(const std::string& filename) {
std::ofstream file(filename);
return readfile(file);
}
This allows me to test the reading of the file by constructing a file in memory by using a std::stringstream
and sending it to the first overload of the function. I could read a secondary test file, but I have found this pattern to be convenient.
Is there any idiomatic way to accomplish the same thing in Rust?
If I have to create some test data, then where is the standard location to keep this data in a Rust project?
Aucun commentaire:
Enregistrer un commentaire