-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add e2e tests * move fs_extra to dev-dependencies * check writing to existing file * review suggestions * remove stdout eq * remove duplicated ci test
- Loading branch information
Showing
7 changed files
with
198 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"sierra_program": "wrong_program_data"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
use cairo_lang_starknet::casm_contract_class::CasmContractClass; | ||
use indoc::indoc; | ||
use snapbox::cmd::{cargo_bin, Command}; | ||
use std::fs::File; | ||
use std::path::PathBuf; | ||
use tempfile::TempDir; | ||
use test_case::test_case; | ||
|
||
#[must_use] | ||
fn runner(args: Vec<&str>, temp_dir: &TempDir) -> Command { | ||
Command::new(cargo_bin!("universal-sierra-compiler")) | ||
.current_dir(temp_dir.path()) | ||
.args(args) | ||
} | ||
|
||
#[must_use] | ||
fn temp_dir_with_sierra_file(file_name: &str) -> TempDir { | ||
let temp_dir = TempDir::new().expect("Unable to create a temporary directory"); | ||
|
||
let src_dir = PathBuf::from("tests/data"); | ||
|
||
fs_extra::file::copy( | ||
src_dir.join(file_name), | ||
temp_dir.path().join(file_name), | ||
&fs_extra::file::CopyOptions::new().overwrite(true), | ||
) | ||
.unwrap_or_else(|_| panic!("Unable to copy {file_name}")); | ||
|
||
temp_dir | ||
} | ||
|
||
fn verify_output_file(output_path: PathBuf) { | ||
let file = File::open(output_path).unwrap(); | ||
let casm_json = serde_json::from_reader(file).unwrap(); | ||
|
||
assert!(serde_json::from_value::<CasmContractClass>(casm_json).is_ok()); | ||
} | ||
|
||
#[test] | ||
fn write_to_existing_file() { | ||
let sierra_file_name = "sierra_1_4_0.json"; | ||
let casm_file_name = "casm.json"; | ||
let args = vec![ | ||
"--sierra-input-path", | ||
&sierra_file_name, | ||
"--casm-output-path", | ||
casm_file_name, | ||
]; | ||
|
||
let temp_dir = temp_dir_with_sierra_file(sierra_file_name); | ||
let _ = File::create(temp_dir.path().join(casm_file_name)).expect("Unable to create file"); | ||
|
||
let snapbox = runner(args, &temp_dir); | ||
|
||
snapbox.assert().success(); | ||
|
||
verify_output_file(temp_dir.path().join(casm_file_name)); | ||
} | ||
|
||
#[test] | ||
fn wrong_json() { | ||
let sierra_file_name = "wrong_sierra.json"; | ||
let casm_file_name = "casm.json"; | ||
let args = vec![ | ||
"--sierra-input-path", | ||
&sierra_file_name, | ||
"--casm-output-path", | ||
casm_file_name, | ||
]; | ||
|
||
let temp_dir = temp_dir_with_sierra_file(sierra_file_name); | ||
let snapbox = runner(args, &temp_dir); | ||
|
||
snapbox.assert().failure().stdout_eq(indoc! {r" | ||
[ERROR] Unable to read sierra_program. Make sure it is an array of felts | ||
"}); | ||
} | ||
|
||
#[test_case("1_4_0"; "sierra 1.4.0")] | ||
#[test_case("1_3_0"; "sierra 1.3.0")] | ||
#[test_case("1_2_0"; "sierra 1.2.0")] | ||
#[test_case("1_1_0"; "sierra 1.1.0")] | ||
#[test_case("1_0_0"; "sierra 1.0.0")] | ||
#[test_case("0_1_0"; "sierra 0.1.0")] | ||
fn test_happy_case(sierra_version: &str) { | ||
let sierra_file_name = "sierra_".to_string() + sierra_version + ".json"; | ||
let casm_file_name = "casm.json"; | ||
let args = vec![ | ||
"--sierra-input-path", | ||
&sierra_file_name, | ||
"--casm-output-path", | ||
casm_file_name, | ||
]; | ||
|
||
let temp_dir = temp_dir_with_sierra_file(&sierra_file_name); | ||
let snapbox = runner(args, &temp_dir); | ||
|
||
snapbox.assert().success(); | ||
|
||
verify_output_file(temp_dir.path().join(casm_file_name)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use std::fs::File; | ||
use test_case::test_case; | ||
use universal_sierra_compiler::compile; | ||
|
||
#[test] | ||
fn wrong_json() { | ||
let sierra_json = serde_json::json!({ | ||
"wrong": "data" | ||
}); | ||
|
||
let casm_class = compile(sierra_json); | ||
assert!(casm_class.is_err()); | ||
} | ||
|
||
#[test_case("1_4_0"; "sierra 1.4.0")] | ||
#[test_case("1_3_0"; "sierra 1.3.0")] | ||
#[test_case("1_2_0"; "sierra 1.2.0")] | ||
#[test_case("1_1_0"; "sierra 1.1.0")] | ||
#[test_case("1_0_0"; "sierra 1.0.0")] | ||
#[test_case("0_1_0"; "sierra 0.1.0")] | ||
fn compile_sierra(sierra_version: &str) { | ||
let file = File::open("tests/data/sierra_".to_string() + sierra_version + ".json").unwrap(); | ||
let sierra_json = serde_json::from_reader(file).unwrap(); | ||
|
||
let casm_class = compile(sierra_json); | ||
assert!(casm_class.is_ok()); | ||
} |