Skip to content

Commit

Permalink
Test for the cli
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsc2 committed Aug 17, 2023
1 parent 2aa262d commit 6a9b74a
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 20 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion cairo1-run/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ cairo-lang-utils = { git = "https://github.com/starkware-libs/cairo" }
itertools = "0.11.0"
clap = { version = "4.3.10", features = ["derive"] }
thiserror = { version = "1.0.40" }
bincode.workspace = true
bincode.workspace = true
assert_matches = "1.5.0"
rstest = "0.17.0"
2 changes: 2 additions & 0 deletions cairo1-run/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
make test:
cargo test
14 changes: 14 additions & 0 deletions cairo1-run/cairo1-programs/factorial.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use core::felt252;

fn main() -> felt252 {
let n = 10;
let result = factorial(n);
result
}

fn factorial(n: felt252) -> felt252 {
match n {
0 => 1,
_ => n * factorial(n - 1),
}
}
File renamed without changes.
41 changes: 22 additions & 19 deletions cairo1-run/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,17 @@ use std::path::PathBuf;
use std::{collections::HashMap, io, path::Path};
use thiserror::Error;

#[cfg(feature = "with_mimalloc")]
use mimalloc::MiMalloc;

#[cfg(feature = "with_mimalloc")]
#[global_allocator]
static ALLOC: MiMalloc = MiMalloc;

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Args {
#[clap(value_parser, value_hint=ValueHint::FilePath)]
filename: PathBuf,

Check warning on line 44 in cairo1-run/src/main.rs

View check run for this annotation

Codecov / codecov/patch

cairo1-run/src/main.rs#L44

Added line #L44 was not covered by tests
#[clap(long = "trace_file", value_parser)]
trace_file: Option<PathBuf>,
#[structopt(long = "print_output")]
print_output: bool,
#[structopt(long = "entrypoint", default_value = "main")]
entrypoint: String,
#[structopt(long = "memory_file")]
memory_file: Option<PathBuf>,
#[clap(long = "layout", default_value = "plain", value_parser=validate_layout)]
layout: String,

Check warning on line 50 in cairo1-run/src/main.rs

View check run for this annotation

Codecov / codecov/patch

cairo1-run/src/main.rs#L50

Added line #L50 was not covered by tests
#[structopt(long = "proof_mode")]
proof_mode: bool,
#[structopt(long = "secure_run")]
secure_run: Option<bool>,
#[clap(long = "air_public_input")]
air_public_input: Option<String>,
}

fn validate_layout(value: &str) -> Result<String, String> {
Expand Down Expand Up @@ -245,8 +228,6 @@ fn run(args: impl Iterator<Item = String>) -> Result<(), Error> {
cairo_run::write_encoded_memory(&runner.relocated_memory, &mut memory_writer)?;
memory_writer.flush().unwrap();
}

Check warning on line 230 in cairo1-run/src/main.rs

View check run for this annotation

Codecov / codecov/patch

cairo1-run/src/main.rs#L230

Added line #L230 was not covered by tests
println!();
println!("Cairo1 program ran successfully");

Ok(())
}
Expand Down Expand Up @@ -277,3 +258,25 @@ fn main() -> Result<(), Error> {
other => other,
}
}

Check warning on line 260 in cairo1-run/src/main.rs

View check run for this annotation

Codecov / codecov/patch

cairo1-run/src/main.rs#L255-L260

Added lines #L255 - L260 were not covered by tests

#[cfg(test)]
mod tests {
#![allow(clippy::too_many_arguments)]
use super::*;
use assert_matches::assert_matches;
use rstest::rstest;

#[rstest]
#[case(["cairo1-run", "cairo1-programs/fibonacci.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null"].as_slice())]
fn test_run_fibonacci_ok(#[case] args: &[&str]) {
let args = args.iter().cloned().map(String::from);
assert_matches!(run(args), Ok(()));
}

#[rstest]
#[case(["cairo1-run", "cairo1-programs/factorial.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null"].as_slice())]
fn test_run_factorial_ok(#[case] args: &[&str]) {
let args = args.iter().cloned().map(String::from);
assert_matches!(run(args), Ok(()));
}
}

0 comments on commit 6a9b74a

Please sign in to comment.