Skip to content

Commit 76542e1

Browse files
committed
General cleanup and introduced tests
1 parent cbd15e4 commit 76542e1

File tree

7 files changed

+54
-29
lines changed

7 files changed

+54
-29
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/target
2+
/input_examples/**
23
.env

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ check:
88
@echo "Checking..."
99
@cargo check
1010

11+
test:
12+
@echo "Running tests..."
13+
@cargo test
14+
1115
build_dev:
1216
@echo "Building debug..."
1317
@cargo build

src/days/_2.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ impl _2 {
4949
let mut result = 0;
5050

5151
for line_collection in self.collection.iter() {
52+
// TODO: Fix the bug where is_valid_line_collection calls itself with a None p_invalid_number_index, correct the allowed mutation count and move the subtract with overflow check to the method call
5253
result += self.is_valid_line_collection(line_collection, None, 3);
5354
}
5455

src/days/template.rs

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ pub fn init() -> Result<Template, ()> {
66
let mut template = Template {};
77

88
match utils::http::request(0) {
9-
Ok(_) => (),
10-
Err(()) => {
11-
return Err(());
12-
}
9+
Ok(string) => template.parse_input(&input_string),
10+
Err(()) => Err(()),
1311
}
14-
Ok(template)
1512
}
1613

1714
impl Template {
15+
fn parse_input(mut self, string: &String) -> Result<Template, ()> {
16+
Ok(self)
17+
}
18+
1819
fn part1(&self) -> String {
1920
"No Result".to_string()
2021
}
@@ -27,3 +28,35 @@ impl Template {
2728
vec![self.part1(), self.part2()]
2829
}
2930
}
31+
32+
#[cfg(test)]
33+
mod tests {
34+
use super::Template;
35+
36+
pub fn test_init() -> Result<Template, ()> {
37+
let mut template = Template {};
38+
39+
match fs::read_to_string("input_examples/template.txt") {
40+
Ok(input_string) => template.parse_input(&input_string),
41+
Err(err) => {
42+
eprintln!(
43+
"An error occurred while loading the example input: {}",
44+
&err
45+
);
46+
Err(())
47+
}
48+
}
49+
}
50+
51+
#[test]
52+
fn part1_test() {
53+
let template = test_init().unwrap();
54+
assert_eq!("Part 1 Example Input Result".to_string(), template.part1())
55+
}
56+
57+
#[test]
58+
fn part2_test() {
59+
let mut template = test_init().unwrap();
60+
assert_eq!("Part 2 Example Input Result".to_string(), template.part2())
61+
}
62+
}

src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
// Add tests using the example data
1+
// TODO: Add tests for days 1-4 using the example input
22

33
use std::process::ExitCode;
44

55
mod days;
66
mod utils;
77

88
fn main() -> ExitCode {
9-
if let Err(exitcode) = utils::env::load_session_token() {
9+
if let Err(exitcode) = utils::env::load_dotenv() {
1010
eprintln!("Exiting the program");
1111
return ExitCode::from(exitcode);
1212
}
1313

14-
// TODO: Allow giving a range
14+
// TODO: Allow giving a range of days
1515
let all_results = days::results();
1616

1717
let mut day: u8 = 1;

src/utils/env.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,15 @@
11
use std::{env, fs};
22

3-
pub fn load_session_token() -> Result<(), u8> {
3+
pub fn load_dotenv() -> Result<(), u8> {
44
println!("Loading all environment variables from the dotenv file...");
55

6-
let p_dotenv_vec = fs::read(".env");
7-
8-
let dotenv_vec;
9-
match p_dotenv_vec {
10-
Ok(r_dotenv_vec) => dotenv_vec = r_dotenv_vec,
11-
Err(err) => {
12-
eprintln!("Failed to load the dotenv file, error: {}", &err);
13-
return Err(0x1);
14-
}
15-
}
16-
17-
let p_dotenv_string = String::from_utf8(dotenv_vec);
6+
let p_dotenv_string = fs::read_to_string(".env");
187

198
let dotenv_string;
209
match p_dotenv_string {
2110
Ok(r_dotenv_string) => dotenv_string = r_dotenv_string,
2211
Err(err) => {
23-
eprintln!(
24-
"Failed to parse the dotenv vec<u8> into a String, error: {}",
25-
&err
26-
);
12+
eprintln!("Failed to load the dotenv file, error: {}", &err);
2713
return Err(0x1);
2814
}
2915
}

src/utils/http.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// TODO: Replace the Curl subprocess with a basic HTTP/2 request using std::io::net
22
// TODO: Prevent early fetching
3-
// TODO: Cache the response
3+
// TODO: Cache the response and use the instead cache if it is available
44

55
use std::{env, process::Command};
66

@@ -17,8 +17,8 @@ pub fn request(day: u8) -> Result<String, ()> {
1717
.output();
1818

1919
match p_request {
20-
Ok(p_input) => match String::from_utf8(p_input.stdout) {
21-
Ok(input) => match input.as_str() {
20+
Ok(p_input_string) => match String::from_utf8(p_input_string.stdout) {
21+
Ok(input_string) => match input_string.as_str() {
2222
"Puzzle inputs differ by user. Please log in to get your puzzle input.\n" => {
2323
eprintln!("An error occurred while requesting the input: the provided session token is invalid");
2424
Err(())
@@ -29,7 +29,7 @@ pub fn request(day: u8) -> Result<String, ()> {
2929
}
3030
_ => {
3131
println!("Successfully requested the input");
32-
Ok(input)
32+
Ok(input_string)
3333
}
3434
}
3535
Err(err) => {

0 commit comments

Comments
 (0)