Skip to content

Commit

Permalink
constants challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodesdev committed Jun 9, 2024
1 parent a17d021 commit e0a3696
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 3 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.

18 changes: 15 additions & 3 deletions challenges/challenges.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"language": "RUST",
"difficulty": "BEGINNER",
"track": "RUST_BASICS",
"tags": [],
"tags": ["function", "string", "output"],
"created_at": "2024-04-20T00:00:00Z",
"updated_at": "2024-04-20T00:00:00Z"
},
Expand All @@ -29,12 +29,24 @@
"slug": "mutable-variables",
"short_description": "Define and modify mutable variables in Rust.",
"language": "RUST",
"difficulty": "EASY",
"difficulty": "BEGINNER",
"track": "RUST_BASICS",
"tags": [],
"tags": ["variables", "mutability"],
"created_at": "2024-06-08T00:00:00Z",
"updated_at": "2024-06-08T00:00:00Z"
},
{
"id": 24,
"title": "Constants",
"slug": "constants",
"short_description": "Learn how to define and use constants in Rust.",
"language": "RUST",
"difficulty": "BEGINNER",
"track": "RUST_BASICS",
"tags": ["constants", "definition"],
"created_at": "2024-06-09T00:00:00Z",
"updated_at": "2024-06-09T00:00:00Z"
},
{
"id": 2,
"title": "Character counting string",
Expand Down
7 changes: 7 additions & 0 deletions challenges/constants/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "constants"
version = "0.1.0"
edition = "2021"

[dev-dependencies]
syntest = { path = "../../crates/syntest" }
5 changes: 5 additions & 0 deletions challenges/constants/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const MAX_SIZE: i32 = 100;

pub fn main() -> i32 {
MAX_SIZE
}
6 changes: 6 additions & 0 deletions challenges/constants/src/starter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Define a constant MAX_SIZE with a value of 100.
// Your code here

pub fn main() -> i32 {
MAX_SIZE
}
42 changes: 42 additions & 0 deletions challenges/constants/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#[cfg(test)]
mod tests {
use constants::*;
use syntest::Syntest;

#[test]
fn compiles() {
assert_eq!(main(), 100, "Expected main() to return 100");
}

#[test]
fn test_constants() {
let syntest = Syntest::from("./src/lib.rs");

let const_to_exist = ["MAX_SIZE"];
let constants = syntest.constant.constants();

let mut success = false;
for const_name in const_to_exist.iter() {
constants.iter().for_each(|con| {
if con.ident.to_string() != *const_name {
return;
}

assert_eq!(
con.ty,
syntest::parse_quote! { i32 },
"Expected type i32 for constant MAX_SIZE"
);
assert_eq!(
con.expr,
syntest::parse_quote! { 100 },
"Expected value 100 for constant MAX_SIZE"
);

success = true;
})
}

assert!(success, "Expected constant MAX_SIZE to exist");
}
}

0 comments on commit e0a3696

Please sign in to comment.