Skip to content

Commit

Permalink
elf on a shelf challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
m4salah committed Dec 6, 2023
1 parent eb2ea34 commit b78b3a2
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,25 @@ async fn contest(Json(reindeers): Json<Vec<ContestReindeer>>) -> impl IntoRespon
}
}

#[derive(Debug, Serialize, Deserialize, Default, PartialEq, Eq)]
struct ElfOnShelfResult {
elf: u32,
#[serde(alias = "elf on a shelf")]
elf_on_shelf: u32,
#[serde(alias = "shelf with no elf on it")]
shelf_with_no_elf: u32,
}
async fn elf_on_shelf(elf_text: String) -> impl IntoResponse {
let shelf = elf_text.matches("shelf").count() as u32;
let elf_on_shelf = elf_text.matches("elf on a shelf").count() as u32;
let elf = elf_text.matches("elf").count() as u32;
Json(ElfOnShelfResult {
elf,
elf_on_shelf,
shelf_with_no_elf: shelf - elf_on_shelf,
})
}

#[allow(dead_code)]
fn app() -> Router {
Router::new()
Expand All @@ -105,6 +124,7 @@ fn app() -> Router {
.route("/1/*ids", get(packet_ids))
.route("/4/strength", post(sum_strength))
.route("/4/contest", post(contest))
.route("/6", post(elf_on_shelf))
}

#[shuttle_runtime::main]
Expand Down Expand Up @@ -267,4 +287,46 @@ mod tests {
.await;
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
}

#[tokio::test]
async fn elf() {
let app = app();

let client = TestClient::new(app);
let res = client
.post("/6")
.body(
"The mischievous elf peeked out from behind the toy workshop,
and another elf joined in the festive dance.
Look, there is also an elf on that shelf!",
)
.header("Content-Type", "text/plain")
.send()
.await;
assert_eq!(res.status(), StatusCode::OK);
let expected = ElfOnShelfResult {
elf: 4,
..Default::default()
};
assert_eq!(res.json::<ElfOnShelfResult>().await.elf, expected.elf);
}
#[tokio::test]
async fn elf_on_shelf() {
let app = app();

let client = TestClient::new(app);
let res = client
.post("/6")
.body("there is an elf on a shelf on an elf. there is also another shelf in Belfast.")
.header("Content-Type", "text/plain")
.send()
.await;
assert_eq!(res.status(), StatusCode::OK);
let expected = ElfOnShelfResult {
elf: 5,
shelf_with_no_elf: 1,
elf_on_shelf: 1,
};
assert_eq!(res.json::<ElfOnShelfResult>().await, expected);
}
}

0 comments on commit b78b3a2

Please sign in to comment.