leo/type-inference/tests/mod.rs

82 lines
2.4 KiB
Rust
Raw Normal View History

2020-11-12 21:28:24 +03:00
// Copyright (C) 2019-2020 Aleo Systems Inc.
// This file is part of the Leo library.
// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
pub mod arrays;
pub mod circuits;
pub mod functions;
pub mod tuples;
pub mod variables;
use leo_grammar::Grammar;
use leo_type_inference::TypeInference;
2020-11-17 05:13:50 +03:00
use leo_ast::{Ast, Input, Program};
2020-11-12 21:28:24 +03:00
use leo_imports::ImportParser;
use leo_symbol_table::SymbolTable;
use std::path::PathBuf;
const TEST_PROGRAM_PATH: &str = "";
const TEST_PROGRAM_NAME: &str = "test";
/// A helper struct to test a `TypeInference` check.
pub struct TestTypeInference {
program: Program,
symbol_table: SymbolTable,
}
impl TestTypeInference {
pub fn new(program_string: &str) -> Self {
2020-11-12 21:28:24 +03:00
// Get test file path.
let file_path = PathBuf::from(TEST_PROGRAM_PATH);
// Get parser syntax tree.
let ast = Grammar::new(&file_path, program_string).unwrap();
2020-11-12 21:28:24 +03:00
// Get typed syntax tree.
2020-11-17 05:13:50 +03:00
let typed = Ast::new(TEST_PROGRAM_NAME, &ast);
2020-11-12 21:28:24 +03:00
let program = typed.into_repr();
// Create empty import parser.
let import_parser = ImportParser::default();
// Create empty input.
let input = Input::new();
// Create symbol table.
let symbol_table = SymbolTable::new(&program, &import_parser, &input).unwrap();
// Store fields for new type inference check.
Self { program, symbol_table }
}
pub fn check(self) {
TypeInference::new(&self.program, self.symbol_table).unwrap();
}
pub fn expect_error(self) {
assert!(TypeInference::new(&self.program, self.symbol_table).is_err());
}
}
#[test]
fn test_new() {
let program_string = include_str!("empty.leo");
2020-11-12 21:28:24 +03:00
let type_inference = TestTypeInference::new(program_string);
2020-11-12 21:28:24 +03:00
type_inference.check()
}