leo/compiler/tests/function/mod.rs

112 lines
2.9 KiB
Rust
Raw Normal View History

2020-05-30 03:34:31 +03:00
use crate::{
2020-06-08 09:30:39 +03:00
get_error,
get_output,
integers::u32::output_one,
2020-06-09 04:31:21 +03:00
parse_program,
2020-06-08 09:30:39 +03:00
EdwardsConstrainedValue,
2020-05-30 03:34:31 +03:00
EdwardsTestCompiler,
};
use leo_compiler::{
errors::{CompilerError, ExpressionError, FunctionError, StatementError},
ConstrainedValue,
};
2020-06-02 04:35:43 +03:00
use snarkos_models::gadgets::utilities::boolean::Boolean;
2020-05-30 03:34:31 +03:00
pub(crate) fn output_empty(program: EdwardsTestCompiler) {
let output = get_output(program);
2020-06-08 09:30:39 +03:00
assert_eq!(EdwardsConstrainedValue::Return(vec![]).to_string(), output.to_string());
}
// (true, false)
2020-05-30 03:34:31 +03:00
pub(crate) fn output_multiple(program: EdwardsTestCompiler) {
let output = get_output(program);
assert_eq!(
2020-05-30 03:34:31 +03:00
EdwardsConstrainedValue::Return(vec![
ConstrainedValue::Boolean(Boolean::Constant(true)),
ConstrainedValue::Boolean(Boolean::Constant(false))
2020-05-30 03:34:31 +03:00
])
.to_string(),
output.to_string()
)
}
2020-05-30 03:34:31 +03:00
fn fail_undefined_identifier(program: EdwardsTestCompiler) {
match get_error(program) {
2020-06-08 09:30:39 +03:00
CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError(
ExpressionError::UndefinedIdentifier(_),
))) => {}
error => panic!("Expected function undefined, got {}", error),
}
}
// Inline function call
#[test]
fn test_empty() {
2020-06-09 04:31:21 +03:00
let bytes = include_bytes!("empty.leo");
let program = parse_program(bytes).unwrap();
output_empty(program);
}
#[test]
fn test_return() {
2020-06-09 04:31:21 +03:00
let bytes = include_bytes!("return.leo");
let program = parse_program(bytes).unwrap();
output_one(program);
}
#[test]
fn test_undefined() {
2020-06-09 04:31:21 +03:00
let bytes = include_bytes!("undefined.leo");
let program = parse_program(bytes).unwrap();
fail_undefined_identifier(program);
}
// Function scope
#[test]
fn test_global_scope_fail() {
2020-06-09 04:31:21 +03:00
let bytes = include_bytes!("scope_fail.leo");
let program = parse_program(bytes).unwrap();
match get_error(program) {
2020-06-08 09:30:39 +03:00
CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError(
ExpressionError::FunctionError(value),
))) => match *value {
FunctionError::StatementError(StatementError::ExpressionError(ExpressionError::UndefinedIdentifier(_))) => {
}
error => panic!("Expected function undefined, got {}", error),
},
error => panic!("Expected function undefined, got {}", error),
}
}
#[test]
fn test_value_unchanged() {
2020-06-09 04:31:21 +03:00
let bytes = include_bytes!("value_unchanged.leo");
let program = parse_program(bytes).unwrap();
output_one(program);
}
// Multiple returns
#[test]
fn test_multiple_returns() {
2020-06-09 04:31:21 +03:00
let bytes = include_bytes!("multiple.leo");
let program = parse_program(bytes).unwrap();
output_multiple(program);
}
#[test]
fn test_multiple_returns_main() {
2020-06-09 04:31:21 +03:00
let bytes = include_bytes!("multiple_main.leo");
let program = parse_program(bytes).unwrap();
output_multiple(program);
}