Skip to content

Commit

Permalink
add support for convenient testing
Browse files Browse the repository at this point in the history
  • Loading branch information
bsamseth committed Dec 2, 2023
1 parent 8eff63c commit d131d08
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ yes, this too tries to cache everything it gets from Advent of Code to spare the

```rust
use aocd::*;
// or, if you prefer: use aocd::prelude::*;

#[aocd(2022, 1)]
fn main() {
Expand All @@ -29,14 +30,29 @@ fn main() {
}
```

### Testing

If you want to use a smaller test input while debugging, this can be done by just adding the file name at the top.
In this case, the `input!` macro simply reads the file, and `submit!` just prints the result without submitting to AoC.

```rust
use aocd::*;

#[aocd(2022, 1, "test.txt")]
fn main() {
...
}
```


## Quickstart

You need to provide your AoC session token in order for this crate to get your personal puzzle input and to be able to
submit answers for you. This is a cookie which is set when you login to AoC. You can find it with your browser
inspector. See [this issue](https://github.com/wimglenn/advent-of-code/issues/1) for a how-to. You can provide it to
`aocd` using any of the following alternatives:

```bash
``` bash
# Alt 1 (this way doesn't require any environment variables to be set):
mkdir -p ~/.config/aocd
echo "your session cookie here" > ~/.config/aocd/token
Expand Down
31 changes: 29 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct Aocd {
url: String,
session_token: String,
cache: cache::Cache,
test_file: Option<String>,
}

impl Aocd {
Expand All @@ -18,10 +19,21 @@ impl Aocd {
/// Requires a valid session cookie from adventofcode.com to be in a file named `~/.config/aocd/token`
/// It will also require write access to `~/.cache/aocd` to cache puzzle inputs and answers.
///
/// Alternatively, if a test file is provided, the client will just be a thin wrapper using the
/// file as input and simply printing answers to stdout.
///
/// # Examples
/// ```
/// use aocd::Aocd;
///
/// let client = Aocd::new(2020, 1, None);
/// let test_client = Aocd::new(2020, 1, Some("test_input.txt"));
/// ```
///
/// # Panics
/// Panics if the session cookie is not found or the cache could not be successfully setup/initialized.
#[must_use]
pub fn new(year: u16, day: u8) -> Self {
pub fn new(year: u16, day: u8, test_file: Option<&str>) -> Self {
let session_token = find_aoc_token();
let cache = cache::Cache::new(year, day, &session_token)
.expect("Should be able to create cache for aocd");
Expand All @@ -37,6 +49,7 @@ impl Aocd {
url,
session_token,
cache,
test_file: test_file.map(|s| s.to_string()),
}
}

Expand All @@ -48,6 +61,14 @@ impl Aocd {
/// Panics if the Advent of Code server responds with an error.
#[must_use]
pub fn get_input(&self) -> String {
if let Some(test_file) = &self.test_file {
return std::fs::read_to_string(test_file)
.expect("Failed to read test file")
.trim_end_matches('\n')
.trim_end_matches('\r')
.to_string();
}

if let Ok(input) = self.cache.get_input() {
return input;
}
Expand All @@ -74,6 +95,12 @@ impl Aocd {
/// Panics if the Advent of Code server responds to the submission with an error.
pub fn submit(&self, part: u8, answer: impl Display) {
let answer = answer.to_string();

if self.test_file.is_some() {
println!("🕵️ Part {part} test result: {answer} 🕵️");
return;
}

// First check if we have already cached a _correct_ answer for this puzzle.
if let Ok(correct_answer) = self.cache.get_correct_answer(part) {
if correct_answer == answer {
Expand Down Expand Up @@ -264,7 +291,7 @@ mod tests {
("AOC_CACHE_DIR", Some(cache_path.to_str().unwrap())),
],
move || {
let client = Aocd::new(self.year, self.day);
let client = Aocd::new(self.year, self.day, None);
if let Some(input) = &self.input {
let url = format!("/{}/day/{}/input", client.year, client.day);
let m = mock("GET", url.as_str())
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![doc = include_str!("../README.md")]

mod cache;
mod client;

Expand Down

0 comments on commit d131d08

Please sign in to comment.