leo/compiler/tests/function/mod.rs

114 lines
3.0 KiB
Rust
Raw Normal View History

2020-05-30 03:34:31 +03:00
use crate::{
2020-07-30 22:10:33 +03:00
assert_satisfied,
expect_compiler_error,
get_outputs,
2020-06-09 04:31:21 +03:00
parse_program,
2020-07-30 22:10:33 +03:00
parse_program_with_inputs,
2020-05-30 03:34:31 +03:00
EdwardsTestCompiler,
};
2020-07-30 22:10:33 +03:00
use leo_compiler::errors::{CompilerError, ExpressionError, FunctionError, StatementError};
2020-07-30 22:10:33 +03:00
fn expect_undefined_identifier(program: EdwardsTestCompiler) {
match expect_compiler_error(program) {
2020-06-08 09:30:39 +03:00
CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError(
ExpressionError::Error(_),
2020-06-08 09:30:39 +03:00
))) => {}
error => panic!("Expected function undefined, got {}", error),
}
}
#[test]
fn test_empty() {
2020-06-09 04:31:21 +03:00
let bytes = include_bytes!("empty.leo");
let program = parse_program(bytes).unwrap();
2020-07-30 22:10:33 +03:00
assert_satisfied(program);
}
#[test]
2020-07-30 22:10:33 +03:00
fn test_iteration() {
let bytes = include_bytes!("iteration.leo");
2020-06-09 04:31:21 +03:00
let program = parse_program(bytes).unwrap();
2020-07-30 22:10:33 +03:00
assert_satisfied(program);
}
#[test]
2020-07-30 22:10:33 +03:00
fn test_iteration_repeated() {
let bytes = include_bytes!("iteration_repeated.leo");
2020-06-09 04:31:21 +03:00
let program = parse_program(bytes).unwrap();
2020-07-30 22:10:33 +03:00
assert_satisfied(program);
}
#[test]
2020-07-30 22:10:33 +03:00
fn test_multiple_returns() {
let bytes = include_bytes!("multiple.leo");
2020-06-09 04:31:21 +03:00
let program = parse_program(bytes).unwrap();
2020-07-30 22:10:33 +03:00
assert_satisfied(program);
}
#[test]
2020-07-30 22:10:33 +03:00
fn test_multiple_returns_main() {
let program_bytes = include_bytes!("multiple_main.leo");
let inputs_bytes = include_bytes!("inputs/registers.in");
2020-06-09 04:31:21 +03:00
2020-07-30 22:10:33 +03:00
let program = parse_program_with_inputs(program_bytes, inputs_bytes).unwrap();
let expected_bytes = include_bytes!("outputs_/registers.out");
let expected = std::str::from_utf8(expected_bytes).unwrap();
let actual_bytes = get_outputs(program);
let actual = std::str::from_utf8(actual_bytes.bytes().as_slice()).unwrap();
2020-07-30 22:10:33 +03:00
assert_eq!(expected, actual);
}
#[test]
2020-07-30 22:10:33 +03:00
fn test_repeated_function_call() {
let bytes = include_bytes!("repeated.leo");
2020-06-09 04:31:21 +03:00
let program = parse_program(bytes).unwrap();
2020-07-30 22:10:33 +03:00
assert_satisfied(program);
}
#[test]
2020-07-30 22:10:33 +03:00
fn test_return() {
let bytes = include_bytes!("return.leo");
2020-06-09 04:31:21 +03:00
let program = parse_program(bytes).unwrap();
2020-07-30 22:10:33 +03:00
assert_satisfied(program);
}
#[test]
2020-07-30 22:10:33 +03:00
fn test_scope_fail() {
let bytes = include_bytes!("scope_fail.leo");
let program = parse_program(bytes).unwrap();
2020-07-30 22:10:33 +03:00
match expect_compiler_error(program) {
CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError(
ExpressionError::FunctionError(value),
))) => match *value {
FunctionError::StatementError(StatementError::ExpressionError(ExpressionError::Error(_))) => {}
error => panic!("Expected function undefined, got {}", error),
},
error => panic!("Expected function undefined, got {}", error),
}
}
2020-07-30 22:10:33 +03:00
2020-06-23 04:28:30 +03:00
#[test]
2020-07-30 22:10:33 +03:00
fn test_undefined() {
let bytes = include_bytes!("undefined.leo");
2020-06-23 04:28:30 +03:00
let program = parse_program(bytes).unwrap();
2020-07-30 22:10:33 +03:00
expect_undefined_identifier(program);
2020-06-23 04:28:30 +03:00
}
#[test]
2020-07-30 22:10:33 +03:00
fn test_value_unchanged() {
let bytes = include_bytes!("value_unchanged.leo");
2020-06-23 04:28:30 +03:00
let program = parse_program(bytes).unwrap();
2020-07-30 22:10:33 +03:00
assert_satisfied(program);
2020-06-23 04:28:30 +03:00
}