use decy_codegen::CodeGenerator;
use decy_hir::{HirExpression, HirFunction, HirStatement, HirType};
#[test]
fn test_fread_generates_read_trait() {
let func = HirFunction::new_with_body(
"read_data".to_string(),
HirType::Void,
vec![],
vec![HirStatement::Expression(HirExpression::FunctionCall {
function: "fread".to_string(),
arguments: vec![
HirExpression::Variable("buf".to_string()),
HirExpression::IntLiteral(1),
HirExpression::IntLiteral(100),
HirExpression::Variable("file".to_string()),
],
})],
);
let codegen = CodeGenerator::new();
let code = codegen.generate_function(&func);
assert!(
code.contains("Read") || code.contains(".read("),
"Expected Read trait or .read() in:\n{}",
code
);
}
#[test]
fn test_fwrite_generates_write_trait() {
let func = HirFunction::new_with_body(
"write_data".to_string(),
HirType::Void,
vec![],
vec![HirStatement::Expression(HirExpression::FunctionCall {
function: "fwrite".to_string(),
arguments: vec![
HirExpression::Variable("data".to_string()),
HirExpression::IntLiteral(1),
HirExpression::IntLiteral(100),
HirExpression::Variable("file".to_string()),
],
})],
);
let codegen = CodeGenerator::new();
let code = codegen.generate_function(&func);
assert!(
code.contains("Write") || code.contains(".write("),
"Expected Write trait or .write() in:\n{}",
code
);
}
#[test]
fn test_fputc_generates_write() {
let func = HirFunction::new_with_body(
"write_char".to_string(),
HirType::Void,
vec![],
vec![HirStatement::Expression(HirExpression::FunctionCall {
function: "fputc".to_string(),
arguments: vec![
HirExpression::Variable("c".to_string()),
HirExpression::Variable("file".to_string()),
],
})],
);
let codegen = CodeGenerator::new();
let code = codegen.generate_function(&func);
assert!(
code.contains("Write") || code.contains(".write("),
"Expected Write trait or .write() in:\n{}",
code
);
}
#[test]
fn test_fgetc_generates_read() {
let func = HirFunction::new_with_body(
"read_char".to_string(),
HirType::Void,
vec![],
vec![HirStatement::VariableDeclaration {
name: "c".to_string(),
var_type: HirType::Int,
initializer: Some(HirExpression::FunctionCall {
function: "fgetc".to_string(),
arguments: vec![HirExpression::Variable("file".to_string())],
}),
}],
);
let codegen = CodeGenerator::new();
let code = codegen.generate_function(&func);
assert!(
code.contains("Read") || code.contains(".read("),
"Expected Read trait or .read() in:\n{}",
code
);
}
#[test]
fn test_fputs_generates_write_all() {
let func = HirFunction::new_with_body(
"write_string".to_string(),
HirType::Void,
vec![],
vec![HirStatement::Expression(HirExpression::FunctionCall {
function: "fputs".to_string(),
arguments: vec![
HirExpression::Variable("str".to_string()),
HirExpression::Variable("file".to_string()),
],
})],
);
let codegen = CodeGenerator::new();
let code = codegen.generate_function(&func);
assert!(
code.contains("Write") || code.contains(".write"),
"Expected Write trait or .write in:\n{}",
code
);
}
#[test]
fn test_getc_generates_read() {
let func = HirFunction::new_with_body(
"read_getc".to_string(),
HirType::Void,
vec![],
vec![HirStatement::VariableDeclaration {
name: "c".to_string(),
var_type: HirType::Int,
initializer: Some(HirExpression::FunctionCall {
function: "getc".to_string(),
arguments: vec![HirExpression::Variable("file".to_string())],
}),
}],
);
let codegen = CodeGenerator::new();
let code = codegen.generate_function(&func);
assert!(
code.contains("Read") || code.contains(".read("),
"Expected Read trait or .read() in:\n{}",
code
);
}
#[test]
fn test_putc_generates_write() {
let func = HirFunction::new_with_body(
"write_putc".to_string(),
HirType::Void,
vec![],
vec![HirStatement::Expression(HirExpression::FunctionCall {
function: "putc".to_string(),
arguments: vec![
HirExpression::Variable("c".to_string()),
HirExpression::Variable("file".to_string()),
],
})],
);
let codegen = CodeGenerator::new();
let code = codegen.generate_function(&func);
assert!(
code.contains("Write") || code.contains(".write("),
"Expected Write trait or .write() in:\n{}",
code
);
}