Skip to content

Commit

Permalink
mutable vars challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodesdev committed Jun 8, 2024
1 parent 11daae8 commit 303112e
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions challenges/challenges.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions challenges/mutable-variables/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "mutable-variables"
version = "0.1.0"
edition = "2021"

[dev-dependencies]
syntest = { path = "../../crates/syntest" }
9 changes: 9 additions & 0 deletions challenges/mutable-variables/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pub fn manipulate_variables() -> i32 {
let mut x = 5;
let mut y = 10;

x += 10;
y *= 2;

x + y
}
14 changes: 14 additions & 0 deletions challenges/mutable-variables/src/starter.rs
Original file line number Diff line number Diff line change
@@ -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
}
60 changes: 60 additions & 0 deletions challenges/mutable-variables/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -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.")
}
})
}
}
25 changes: 23 additions & 2 deletions crates/syntest/src/var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -227,8 +233,23 @@ impl From<Lit> 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"),
}
}
}

0 comments on commit 303112e

Please sign in to comment.