mirror of
https://github.com/notohh/rustlings.git
synced 2024-11-21 21:42:23 -05:00
Fix tests
This commit is contained in:
parent
bee62c89de
commit
9831cbb139
5 changed files with 39 additions and 37 deletions
|
@ -10,18 +10,18 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct Exercise {
|
struct ExerciseInfo {
|
||||||
name: String,
|
name: String,
|
||||||
path: String,
|
dir: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct InfoToml {
|
struct InfoFile {
|
||||||
exercises: Vec<Exercise>,
|
exercises: Vec<ExerciseInfo>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let exercises = toml_edit::de::from_str::<InfoToml>(
|
let exercise_infos = toml_edit::de::from_str::<InfoFile>(
|
||||||
&fs::read_to_string("info.toml").context("Failed to read `info.toml`")?,
|
&fs::read_to_string("info.toml").context("Failed to read `info.toml`")?,
|
||||||
)
|
)
|
||||||
.context("Failed to deserialize `info.toml`")?
|
.context("Failed to deserialize `info.toml`")?
|
||||||
|
@ -36,12 +36,16 @@ fn main() -> Result<()> {
|
||||||
bin = [\n",
|
bin = [\n",
|
||||||
);
|
);
|
||||||
|
|
||||||
for exercise in exercises {
|
for exercise_info in exercise_infos {
|
||||||
buf.extend_from_slice(b" { name = \"");
|
buf.extend_from_slice(b" { name = \"");
|
||||||
buf.extend_from_slice(exercise.name.as_bytes());
|
buf.extend_from_slice(exercise_info.name.as_bytes());
|
||||||
buf.extend_from_slice(b"\", path = \"../");
|
buf.extend_from_slice(b"\", path = \"../exercises/");
|
||||||
buf.extend_from_slice(exercise.path.as_bytes());
|
if let Some(dir) = &exercise_info.dir {
|
||||||
buf.extend_from_slice(b"\" },\n");
|
buf.extend_from_slice(dir.as_bytes());
|
||||||
|
buf.extend_from_slice(b"/");
|
||||||
|
}
|
||||||
|
buf.extend_from_slice(exercise_info.name.as_bytes());
|
||||||
|
buf.extend_from_slice(b".rs\" },\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
buf.extend_from_slice(
|
buf.extend_from_slice(
|
||||||
|
|
|
@ -5,34 +5,39 @@ use serde::Deserialize;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct Exercise {
|
struct ExerciseInfo {
|
||||||
name: String,
|
name: String,
|
||||||
path: String,
|
dir: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct InfoToml {
|
struct InfoFile {
|
||||||
exercises: Vec<Exercise>,
|
exercises: Vec<ExerciseInfo>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn dev_cargo_bins() {
|
fn dev_cargo_bins() {
|
||||||
let content = fs::read_to_string("dev/Cargo.toml").unwrap();
|
let cargo_toml = fs::read_to_string("dev/Cargo.toml").unwrap();
|
||||||
|
|
||||||
let exercises = toml_edit::de::from_str::<InfoToml>(&fs::read_to_string("info.toml").unwrap())
|
let exercise_infos =
|
||||||
.unwrap()
|
toml_edit::de::from_str::<InfoFile>(&fs::read_to_string("info.toml").unwrap())
|
||||||
.exercises;
|
.unwrap()
|
||||||
|
.exercises;
|
||||||
|
|
||||||
let mut start_ind = 0;
|
let mut start_ind = 0;
|
||||||
for exercise in exercises {
|
for exercise_info in exercise_infos {
|
||||||
let name_start = start_ind + content[start_ind..].find('"').unwrap() + 1;
|
let name_start = start_ind + cargo_toml[start_ind..].find('"').unwrap() + 1;
|
||||||
let name_end = name_start + content[name_start..].find('"').unwrap();
|
let name_end = name_start + cargo_toml[name_start..].find('"').unwrap();
|
||||||
assert_eq!(exercise.name, &content[name_start..name_end]);
|
assert_eq!(exercise_info.name, &cargo_toml[name_start..name_end]);
|
||||||
|
|
||||||
// +3 to skip `../` at the begeinning of the path.
|
let path_start = name_end + cargo_toml[name_end + 1..].find('"').unwrap() + 2;
|
||||||
let path_start = name_end + content[name_end + 1..].find('"').unwrap() + 5;
|
let path_end = path_start + cargo_toml[path_start..].find('"').unwrap();
|
||||||
let path_end = path_start + content[path_start..].find('"').unwrap();
|
let expected_path = if let Some(dir) = exercise_info.dir {
|
||||||
assert_eq!(exercise.path, &content[path_start..path_end]);
|
format!("../exercises/{dir}/{}.rs", exercise_info.name)
|
||||||
|
} else {
|
||||||
|
format!("../exercises/{}.rs", exercise_info.name)
|
||||||
|
};
|
||||||
|
assert_eq!(expected_path, &cargo_toml[path_start..path_end]);
|
||||||
|
|
||||||
start_ind = path_end + 1;
|
start_ind = path_end + 1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,9 @@
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "compFailure"
|
name = "compFailure"
|
||||||
path = "exercises/compFailure.rs"
|
mode = "run"
|
||||||
mode = "compile"
|
|
||||||
hint = ""
|
hint = ""
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "testFailure"
|
name = "testFailure"
|
||||||
path = "exercises/testFailure.rs"
|
|
||||||
mode = "test"
|
mode = "test"
|
||||||
hint = "Hello!"
|
hint = "Hello!"
|
||||||
|
|
|
@ -1,17 +1,14 @@
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "pending_exercise"
|
name = "pending_exercise"
|
||||||
path = "exercises/pending_exercise.rs"
|
mode = "run"
|
||||||
mode = "compile"
|
|
||||||
hint = """"""
|
hint = """"""
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "pending_test_exercise"
|
name = "pending_test_exercise"
|
||||||
path = "exercises/pending_test_exercise.rs"
|
|
||||||
mode = "test"
|
mode = "test"
|
||||||
hint = """"""
|
hint = """"""
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "finished_exercise"
|
name = "finished_exercise"
|
||||||
path = "exercises/finished_exercise.rs"
|
mode = "run"
|
||||||
mode = "compile"
|
|
||||||
hint = """"""
|
hint = """"""
|
||||||
|
|
|
@ -1,11 +1,9 @@
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "compSuccess"
|
name = "compSuccess"
|
||||||
path = "exercises/compSuccess.rs"
|
mode = "run"
|
||||||
mode = "compile"
|
|
||||||
hint = """"""
|
hint = """"""
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "testSuccess"
|
name = "testSuccess"
|
||||||
path = "exercises/testSuccess.rs"
|
|
||||||
mode = "test"
|
mode = "test"
|
||||||
hint = """"""
|
hint = """"""
|
||||||
|
|
Loading…
Reference in a new issue