Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test format #28412

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
34 changes: 4 additions & 30 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ features = [ "derive", "rc" ]
version = "1.0"
features = [ "preserve_order" ]

[workspace.dependencies.toml]
version = "0.8"
features = [ "preserve_order" ]

[workspace.dependencies.tracing]
version = "0.1"

Expand Down Expand Up @@ -202,7 +206,7 @@ features = [ "circuit", "console" ]
version = "0.9.1"

[dependencies.toml]
version = "0.8"
workspace = true

[dependencies.tracing]
version = "0.1"
Expand Down
4 changes: 2 additions & 2 deletions compiler/compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ workspace = true
[dev-dependencies.serde]
workspace = true

[dev-dependencies.serde_yaml]
version = "0.8.25"
[dev-dependencies.toml]
workspace = true

[dev-dependencies.tempfile]
version = "3.13"
Expand Down
7 changes: 3 additions & 4 deletions compiler/compiler/tests/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use utilities::{
get_build_options,
get_cwd_option,
hash_asts,
hash_content,
hash_symbol_tables,
parse_program,
};
Expand All @@ -43,9 +42,9 @@ use snarkvm::console::prelude::*;
use indexmap::IndexMap;
use leo_span::Symbol;
use regex::Regex;
use serde_yaml::Value;
use snarkvm::{prelude::Process, synthesizer::program::ProgramCore};
use std::{fs, path::Path, rc::Rc};
use toml::Value;

struct CompileNamespace;

Expand Down Expand Up @@ -169,7 +168,7 @@ fn run_test(test: Test, handler: &Handler, buf: &BufferEmitter) -> Result<Value,
destructured_ast,
inlined_ast,
dce_ast,
bytecode: hash_content(&bytecode),
bytecode,
errors: buf.0.take().to_string(),
warnings: buf.1.take().to_string(),
};
Expand All @@ -180,7 +179,7 @@ fn run_test(test: Test, handler: &Handler, buf: &BufferEmitter) -> Result<Value,
let compile_outputs = CompileOutputs { compile };
all_outputs.push(compile_outputs);
}
Ok(serde_yaml::to_value(all_outputs).expect("serialization failed"))
Ok(Value::try_from(all_outputs).expect("serialization failed"))
}

struct TestRunner;
Expand Down
32 changes: 13 additions & 19 deletions compiler/compiler/tests/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use utilities::{
get_build_options,
get_cwd_option,
hash_asts,
hash_content,
hash_symbol_tables,
parse_program,
};
Expand All @@ -49,12 +48,12 @@ use leo_errors::LeoError;
use leo_span::Symbol;
use regex::Regex;
use serde::{Deserialize, Serialize};
use serde_yaml::Value;
use snarkvm::{
prelude::store::{ConsensusStore, helpers::memory::ConsensusMemory},
synthesizer::program::ProgramCore,
};
use std::{fs, panic::AssertUnwindSafe, path::Path, rc::Rc};
use toml::Value;

// TODO: Evaluate namespace.
struct ExecuteNamespace;
Expand Down Expand Up @@ -89,8 +88,8 @@ fn run_test(test: Test, handler: &Handler, buf: &BufferEmitter) -> Result<Value,
let cwd = get_cwd_option(&test);

// Initialize an rng.
let rng = &mut match test.config.extra.get("seed").map(|seed| seed.as_u64()) {
Some(Some(seed)) => TestRng::from_seed(seed),
let rng = &mut match test.config.extra.get("seed").map(|seed| seed.as_integer()) {
Some(Some(seed)) => TestRng::from_seed(seed as u64),
_ => TestRng::from_seed(1234567890),
};

Expand Down Expand Up @@ -213,7 +212,7 @@ fn run_test(test: Test, handler: &Handler, buf: &BufferEmitter) -> Result<Value,
destructured_ast,
inlined_ast,
dce_ast,
bytecode: hash_content(&bytecode),
bytecode,
errors: buf.0.take().to_string(),
warnings: buf.1.take().to_string(),
};
Expand All @@ -222,31 +221,26 @@ fn run_test(test: Test, handler: &Handler, buf: &BufferEmitter) -> Result<Value,
}

// Extract the cases from the test config.
let all_cases = test
.config
.extra
.get("cases")
.expect("An `Execute` config must have a `cases` field.")
.as_sequence()
.unwrap();
let all_cases =
test.config.extra.get("cases").expect("An `Execute` config must have a `cases` field.").as_array().unwrap();

// Initialize storage for the execution outputs.
let mut execute = Vec::with_capacity(all_cases.len());

// Run each test case for each function.
for case in all_cases {
let case = case.as_mapping().unwrap();
let program_name = case.get(&Value::from("program")).expect("expected program name").as_str().unwrap();
let function_name = case.get(&Value::from("function")).expect("expected function name").as_str().unwrap();
let case = case.as_table().unwrap();
let program_name = case.get("program").expect("expected program name").as_str().unwrap();
let function_name = case.get("function").expect("expected function name").as_str().unwrap();
let inputs: Vec<_> = case
.get(&Value::from("input"))
.get("input")
.unwrap()
.as_sequence()
.as_array()
.unwrap()
.iter()
.map(|input| console::program::Value::<CurrentNetwork>::from_str(input.as_str().unwrap()).unwrap())
.collect();
let private_key = match case.get(&Value::from("private_key")) {
let private_key = match case.get("private_key") {
Some(private_key) => {
PrivateKey::from_str(private_key.as_str().expect("expected string for private key"))
.expect("unable to parse private key")
Expand Down Expand Up @@ -325,7 +319,7 @@ fn run_test(test: Test, handler: &Handler, buf: &BufferEmitter) -> Result<Value,
let combined_output = CompileAndExecuteOutputs { compile, execute };
outputs.push(combined_output);
}
Ok(serde_yaml::to_value(outputs).expect("serialization failed"))
Ok(Value::try_from(outputs).expect("serialization failed"))
}

struct TestRunner;
Expand Down
6 changes: 3 additions & 3 deletions compiler/compiler/tests/utilities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,19 @@ pub fn get_build_options(test_config: &TestConfig) -> Vec<BuildOptions> {
Some(configs) => {
// Parse the sequence of compiler configurations.
configs
.as_sequence()
.as_array()
.unwrap()
.iter()
.map(|config| {
let config = config.as_mapping().expect("Expected the compiler configuration to be a mapping.");
let config = config.as_table().expect("Expected the compiler configuration to be a mapping.");
assert_eq!(
config.len(),
1,
"A compiler configuration must have exactly one key-value pair. e.g. `dce_enabled`: true"
);
BuildOptions {
dce_enabled: config
.get(&serde_yaml::Value::String("dce_enabled".to_string()))
.get("dce_enabled")
.expect("Expected key `dce_enabled`")
.as_bool()
.expect("Expected value to be a boolean."),
Expand Down
4 changes: 2 additions & 2 deletions compiler/parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ path = "../../tests/test-framework"
[dev-dependencies.serde_json]
workspace = true

[dev-dependencies.serde_yaml]
version = "0.8"
[dev-dependencies.toml]
workspace = true

[features]
default = [ ]
Expand Down
18 changes: 9 additions & 9 deletions compiler/parser/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ use leo_test_framework::{
runner::{Namespace, ParseType, Runner},
};
use serde::Serialize;
use serde_yaml::Value;
use tokenizer::Token;
use toml::Value;

type CurrentNetwork = snarkvm::prelude::MainnetV0;

Expand Down Expand Up @@ -87,8 +87,8 @@ fn all_are_comments(tokens: &[SpannedToken]) -> bool {
tokens.iter().all(|x| matches!(x.token, Token::CommentLine(_) | Token::CommentBlock(_)))
}

fn yaml_or_fail<T: Serialize>(value: T) -> Value {
serde_yaml::to_value(value).expect("serialization failed")
fn toml_or_fail<T: Serialize>(value: T) -> Value {
Value::try_from(value).expect("serialization failed")
}

struct ParseExpressionNamespace;
Expand All @@ -102,9 +102,9 @@ impl Namespace for ParseExpressionNamespace {
create_session_if_not_set_then(|s| {
let tokenizer = tokenize(test, s)?;
if all_are_comments(&tokenizer) {
return Ok(yaml_or_fail(""));
return Ok(toml_or_fail(""));
}
with_handler(tokenizer, |p| p.parse_expression()).map(yaml_or_fail)
with_handler(tokenizer, |p| p.parse_expression()).map(toml_or_fail)
})
}
}
Expand All @@ -120,9 +120,9 @@ impl Namespace for ParseStatementNamespace {
create_session_if_not_set_then(|s| {
let tokenizer = tokenize(test, s)?;
if all_are_comments(&tokenizer) {
return Ok(yaml_or_fail(Statement::dummy(Span::default(), NodeID::default())));
return Ok(toml_or_fail(Statement::dummy(Span::default(), NodeID::default())));
}
with_handler(tokenizer, |p| p.parse_statement()).map(yaml_or_fail)
with_handler(tokenizer, |p| p.parse_statement()).map(toml_or_fail)
})
}
}
Expand All @@ -135,7 +135,7 @@ impl Namespace for ParseNamespace {
}

fn run_test(&self, test: Test) -> Result<Value, String> {
create_session_if_not_set_then(|s| with_handler(tokenize(test, s)?, |p| p.parse_program()).map(yaml_or_fail))
create_session_if_not_set_then(|s| with_handler(tokenize(test, s)?, |p| p.parse_program()).map(toml_or_fail))
}
}

Expand Down Expand Up @@ -199,7 +199,7 @@ impl Namespace for SerializeNamespace {
remove_key_from_json(&mut json, "span");
json = normalize_json_value(json);

Ok(serde_json::from_value::<serde_yaml::Value>(json).expect("failed serialization"))
Ok(serde_json::from_value::<toml::Value>(json).expect("failed serialization"))
})
}
}
Expand Down
18 changes: 4 additions & 14 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ incorrect) Leo code. What makes Leo file a test is a block comment at the top of

```
/*
namespace: Parse
expectation: Pass
namespace = "Parse"
expectation = "Pass"
*/

struct X {
Expand All @@ -25,7 +25,7 @@ struct X {
}
```

This comment contains YAML structure with set of mandatory and optional fields.
This comment is a TOML document with some mandatory and optional fields.

## Test Expectations

Expand All @@ -43,17 +43,7 @@ generated instead. A PR should contain changes to expectations as well as to tes

## Test Configuration

Here is the list of all possible configuration options for compiler and parser tests.

### namespace

```yaml
- Mandatory: yes
- Namespace: all
- Values: ...
```

Several values are supported, but they vary depending on the directory you are in.
There are several different values for the `namespace` test option.

Parser Directory namespaces:

Expand Down
Loading
Loading