util/generate: Use the same template for all generations
Resolves:
@@ -69,16 +69,12 @@ fn parse_case(
// Generate test suite using the canonical data
fn generate_tests_from_canonical_data(
exercise_name: &str,
- exercise_path: &Path,
+ tests_path: &Path,
canonical_data: &JsonValue,
use_maplit: bool,
) -> Result<()> {
exercise::update_cargo_toml_version(exercise_name, canonical_data)?;
- let tests_path = exercise_path
- .join("tests")
- .join(format!("{}.rs", exercise_name));
-
let tests_content = exercise::get_tests_content(exercise_name)?;
let updated_tests_content = format!(
@@ -211,21 +207,9 @@ pub fn generate_exercise(exercise_name: &str, use_maplit: bool) -> Result<()> {
fs::create_dir(exercise_path.join("tests"))?;
- let mut test_file = File::create(
- exercise_path
+ let test_file_name = exercise_path
.join("tests")
- .join(format!("{}.rs", exercise_name)),
- )?;
-
- if use_maplit {
- test_file.write_all(b"#[macro_use]\nextern crate maplit;\n")?;
- }
-
- test_file
- .write_all(&format!("extern crate {};\n", exercise_name.replace("-", "_")).into_bytes())?;
-
- test_file
- .write_all(&format!("use {}::*;\n\n", exercise_name.replace("-", "_")).into_bytes())?;
+ .join(format!("{}.rs", exercise_name));
fs::write(exercise_path.join("example.rs"), EXAMPLE_RS_CONTENT)?;
@@ -235,7 +219,7 @@ pub fn generate_exercise(exercise_name: &str, use_maplit: bool) -> Result<()> {
generate_tests_from_canonical_data(
&exercise_name,
- &exercise_path,
+ &test_file_name,
&canonical_data,
use_maplit,
)?;
@@ -246,7 +230,33 @@ pub fn generate_exercise(exercise_name: &str, use_maplit: bool) -> Result<()> {
&exercise_name
);
- test_file.write_all(b"// Add your tests here\n")?;
+ let mut test_file = File::create(test_file_name)?;
+
+ test_file.write_all(
+ &format!(
+ "//! Tests for {exercise_name} \n\
+ //! \n\
+ //! Generated by [utility][utility]\n\
+ //! \n\
+ //! [utility]: https://github.com/exercism/rust/tree/master/util/exercise\n\
+ \n",
+ exercise_name = exercise_name,
+ )
+ .into_bytes(),
+ )?;
+ if use_maplit {
+ test_file.write_all(b"#[macro_use]\nextern crate maplit;\n")?;
+ }
+
+ test_file.write_all(
+ &format!(
+ "extern crate {escaped_exercise_name};\n\
+ use {escaped_exercise_name}::*;\n\
+ \n",
+ escaped_exercise_name = exercise_name.replace("-", "_"),
+ )
+ .into_bytes(),
+ )?;
}
}
