diff --git a/Cargo.lock b/Cargo.lock index 5b36271..412d141 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -736,6 +736,13 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "mutable-variables" +version = "0.1.0" +dependencies = [ + "syntest", +] + [[package]] name = "native-tls" version = "0.2.11" diff --git a/challenges/challenges.json b/challenges/challenges.json index ad6bfc8..cd45453 100644 --- a/challenges/challenges.json +++ b/challenges/challenges.json @@ -23,6 +23,18 @@ "created_at": "2024-06-07T00:00:00Z", "updated_at": "2024-06-07T00:00:00Z" }, + { + "id": 23, + "title": "Mutable Variables", + "slug": "mutable-variables", + "short_description": "Declare and manipulate mutable variables in Rust.", + "language": "RUST", + "difficulty": "EASY", + "track": "RUST_BASICS", + "tags": [], + "created_at": "2024-06-08T00:00:00Z", + "updated_at": "2024-06-08T00:00:00Z" + }, { "id": 2, "title": "Character counting string", diff --git a/challenges/mutable-variables/Cargo.toml b/challenges/mutable-variables/Cargo.toml new file mode 100644 index 0000000..d6f9c90 --- /dev/null +++ b/challenges/mutable-variables/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "mutable-variables" +version = "0.1.0" +edition = "2021" + +[dev-dependencies] +syntest = { path = "../../crates/syntest" } diff --git a/challenges/mutable-variables/src/lib.rs b/challenges/mutable-variables/src/lib.rs new file mode 100644 index 0000000..b14afcf --- /dev/null +++ b/challenges/mutable-variables/src/lib.rs @@ -0,0 +1,9 @@ +pub fn manipulate_variables() -> i32 { + let mut x = 5; + let mut y = 10; + + x += 10; + y *= 2; + + x + y +} diff --git a/challenges/mutable-variables/src/starter.rs b/challenges/mutable-variables/src/starter.rs new file mode 100644 index 0000000..e0b33e4 --- /dev/null +++ b/challenges/mutable-variables/src/starter.rs @@ -0,0 +1,14 @@ +pub fn manipulate_variables() -> i32 { + // Steps: + // 1. Declare x as a mutable variable and assign it the value 5 + + // 2. Declare y as a mutable variable and assign it the value 10 + + // 3. Increment x by 10 + + // 4. Multiply y by 2 + + // 5. Calculate the sum of x and y + + // 6. Return the calculated sum +} diff --git a/challenges/mutable-variables/tests/tests.rs b/challenges/mutable-variables/tests/tests.rs new file mode 100644 index 0000000..554b12a --- /dev/null +++ b/challenges/mutable-variables/tests/tests.rs @@ -0,0 +1,60 @@ +#[cfg(test)] +mod tests { + use mutable_variables::*; + use syntest::{LocalVariable, Mutation, Syntest, Value}; + + #[test] + fn test_compiles() { + manipulate_variables(); + } + + #[test] + fn test_variables() { + let syntest = Syntest::from("./src/lib.rs"); + + // Expect the 2 variables to exist + syntest + .variables("manipulate_variables") + .iter() + .for_each(|var| match var { + LocalVariable::Int { .. } => match var.name() { + "x" => { + assert_eq!( + var.value(), + Value::Int(15), + "Expected x to be 15, got {}", + var.value() + ); + assert!(var.is_used(), "Expected x to be used"); + assert!(var.is_mutable(), "Expected x to be mutable"); + assert_eq!( + var.mutations(), + &vec![Mutation::new(Value::Int(5), Value::Int(15))], + "Expected variable x to have a mutation from 5 to 15.", + ); + } + "y" => { + assert_eq!( + var.value(), + Value::Int(20), + "Expected y to be 20, got {}", + var.value() + ); + assert!(var.is_used(), "Expected y to be used"); + assert!(var.is_mutable(), "Expected y to be mutable"); + assert_eq!( + var.mutations(), + &vec![Mutation::new(Value::Int(10), Value::Int(20))], + "Expected variable y to have a mutation from 10 to 20." + ); + } + _ => { + panic!("Unexpected variable {}", var.name()) + } + }, + _ => { + panic!("Expected type to be an integer.") + } + }) + } +} diff --git a/crates/syntest/src/var.rs b/crates/syntest/src/var.rs index 5dc901e..cf338ed 100644 --- a/crates/syntest/src/var.rs +++ b/crates/syntest/src/var.rs @@ -183,6 +183,12 @@ impl LocalVariable { } } +impl Display for LocalVariable { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.name()) + } +} + #[derive(Debug, PartialEq, Clone)] pub enum Value { Str(String), @@ -227,8 +233,23 @@ impl From for Value { } } -impl Display for LocalVariable { +impl Display for Value { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.name()) + match self { + Value::Str(value) => write!(f, "{}", value), + Value::Int(value) => write!(f, "{}", value), + Value::Float(value) => write!(f, "{}", value), + Value::Char(value) => write!(f, "{}", value), + Value::Bool(value) => write!(f, "{}", value), + Value::Vec(values) => { + write!(f, "[")?; + for value in values { + write!(f, "{}, ", value)?; + } + write!(f, "]") + } + Value::Closure => write!(f, "closure"), + Value::Other => write!(f, "other"), + } } }