pr: 09
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
use prometeu_compiler::frontends::pbs::PbsFrontend;
|
||||
use prometeu_compiler::frontends::Frontend;
|
||||
use prometeu_compiler::common::files::FileManager;
|
||||
use std::fs;
|
||||
|
||||
fn check_code(code: &str) -> Result<(), String> {
|
||||
let mut file_manager = FileManager::new();
|
||||
let temp_dir = tempfile::tempdir().unwrap();
|
||||
let file_path = temp_dir.path().join("test.pbs");
|
||||
fs::write(&file_path, code).unwrap();
|
||||
|
||||
let frontend = PbsFrontend;
|
||||
match frontend.compile_to_ir(&file_path, &mut file_manager) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(bundle) => {
|
||||
let mut errors = Vec::new();
|
||||
for diag in bundle.diagnostics {
|
||||
let code = diag.code.unwrap_or_else(|| "NO_CODE".to_string());
|
||||
errors.push(format!("{}: {}", code, diag.message));
|
||||
}
|
||||
Err(errors.join(", "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_type_mismatch_let() {
|
||||
let code = "fn main() { let x: int = \"hello\"; }";
|
||||
let res = check_code(code);
|
||||
if let Err(e) = &res { println!("Error: {}", e); }
|
||||
assert!(res.is_err());
|
||||
assert!(res.unwrap_err().contains("E_TYPE_MISMATCH"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_type_mismatch_return() {
|
||||
let code = "fn main() -> int { return \"hello\"; }";
|
||||
let res = check_code(code);
|
||||
assert!(res.is_err());
|
||||
assert!(res.unwrap_err().contains("E_TYPE_MISMATCH"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_type_mismatch_call() {
|
||||
let code = "
|
||||
fn foo(a: int) {}
|
||||
fn main() {
|
||||
foo(\"hello\");
|
||||
}
|
||||
";
|
||||
let res = check_code(code);
|
||||
assert!(res.is_err());
|
||||
assert!(res.unwrap_err().contains("E_TYPE_MISMATCH"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_missing_return_path() {
|
||||
let code = "fn foo() -> int { if (true) { return 1; } }";
|
||||
let res = check_code(code);
|
||||
assert!(res.is_err());
|
||||
assert!(res.unwrap_err().contains("E_TYPE_RETURN_PATH"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_implicit_none_optional() {
|
||||
let code = "fn foo() -> optional<int> { if (true) { return some(1); } }";
|
||||
let res = check_code(code);
|
||||
if let Err(e) = &res { println!("Error: {}", e); }
|
||||
assert!(res.is_ok()); // Implicit none allowed for optional
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_valid_optional_assignment() {
|
||||
let code = "fn main() { let x: optional<int> = none; let y: optional<int> = some(10); }";
|
||||
let res = check_code(code);
|
||||
if let Err(e) = &res { println!("Error: {}", e); }
|
||||
assert!(res.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_valid_result_usage() {
|
||||
let code = "
|
||||
fn foo() -> result<int, string> {
|
||||
if (true) {
|
||||
return ok(10);
|
||||
} else {
|
||||
return err(\"error\");
|
||||
}
|
||||
}
|
||||
";
|
||||
let res = check_code(code);
|
||||
if let Err(e) = &res { println!("Error: {}", e); }
|
||||
assert!(res.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_unknown_type() {
|
||||
let code = "fn main() { let x: UnknownType = 10; }";
|
||||
let res = check_code(code);
|
||||
assert!(res.is_err());
|
||||
assert!(res.unwrap_err().contains("E_TYPE_UNKNOWN_TYPE"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_void_return_ok() {
|
||||
let code = "fn main() { return; }";
|
||||
let res = check_code(code);
|
||||
assert!(res.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_binary_op_mismatch() {
|
||||
let code = "fn main() { let x = 1 + \"hello\"; }";
|
||||
let res = check_code(code);
|
||||
assert!(res.is_err());
|
||||
assert!(res.unwrap_err().contains("E_TYPE_MISMATCH"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_struct_type_usage() {
|
||||
let code = "
|
||||
declare struct Point { x: int, y: int }
|
||||
fn foo(p: Point) {}
|
||||
fn main() {
|
||||
// Struct literals not in v0, but we can have variables of struct type
|
||||
}
|
||||
";
|
||||
let res = check_code(code);
|
||||
assert!(res.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_service_type_usage() {
|
||||
let code = "
|
||||
pub service MyService {
|
||||
fn hello(name: string) -> void
|
||||
}
|
||||
fn foo(s: MyService) {}
|
||||
";
|
||||
let res = check_code(code);
|
||||
assert!(res.is_ok());
|
||||
}
|
||||
Reference in New Issue
Block a user