#![cfg(any(
not(any(
feature = "parser_tests",
feature = "analyzer_tests",
feature = "codegen_tests",
feature = "interpreter_tests",
feature = "conformance_tests",
feature = "integration_tests",
)),
feature = "codegen_tests",
))]
#![allow(clippy::needless_borrow)]
use windjammer::codegen::rust::expression_helpers::{is_const_evaluable, is_reference_expression};
use windjammer::parser::ast::builders::*;
use windjammer::parser::UnaryOp;
use windjammer::test_utils::*;
fn alloc_var(name: &str) -> &'static windjammer::parser::Expression<'static> {
test_alloc_expr(expr_var(name))
}
fn alloc_int(n: i64) -> &'static windjammer::parser::Expression<'static> {
test_alloc_expr(expr_int(n))
}
fn alloc_string(s: &str) -> &'static windjammer::parser::Expression<'static> {
test_alloc_expr(expr_string(s))
}
fn alloc_bool(b: bool) -> &'static windjammer::parser::Expression<'static> {
test_alloc_expr(expr_bool(b))
}
fn alloc_unary(
op: windjammer::parser::UnaryOp,
operand: &'static windjammer::parser::Expression<'static>,
) -> &'static windjammer::parser::Expression<'static> {
test_alloc_expr(expr_unary(op, operand))
}
fn alloc_not(
operand: &'static windjammer::parser::Expression<'static>,
) -> &'static windjammer::parser::Expression<'static> {
test_alloc_expr(expr_not(operand))
}
fn alloc_neg(
operand: &'static windjammer::parser::Expression<'static>,
) -> &'static windjammer::parser::Expression<'static> {
test_alloc_expr(expr_neg(operand))
}
fn alloc_add(
left: &'static windjammer::parser::Expression<'static>,
right: &'static windjammer::parser::Expression<'static>,
) -> &'static windjammer::parser::Expression<'static> {
test_alloc_expr(expr_add(left, right))
}
#[test]
fn test_is_reference_expression_ref() {
let expr = alloc_unary(UnaryOp::Ref, alloc_var("x"));
assert!(is_reference_expression(expr));
}
#[test]
fn test_is_reference_expression_mut_ref() {
let expr = alloc_unary(UnaryOp::MutRef, alloc_var("x"));
assert!(is_reference_expression(expr));
}
#[test]
fn test_is_reference_expression_not() {
let expr = alloc_not(alloc_var("x"));
assert!(!is_reference_expression(expr));
}
#[test]
fn test_is_reference_expression_identifier() {
let expr = alloc_var("x");
assert!(!is_reference_expression(expr));
}
#[test]
fn test_is_reference_expression_literal() {
let expr = alloc_int(42);
assert!(!is_reference_expression(expr));
}
#[test]
fn test_is_const_evaluable_literal_int() {
let expr = alloc_int(42);
assert!(is_const_evaluable(&expr));
}
#[test]
fn test_is_const_evaluable_literal_string() {
let expr = alloc_string("hello");
assert!(is_const_evaluable(&expr));
}
#[test]
fn test_is_const_evaluable_literal_bool() {
let expr = alloc_bool(true);
assert!(is_const_evaluable(&expr));
}
#[test]
fn test_is_const_evaluable_binary_const() {
let expr = alloc_add(alloc_int(1), alloc_int(2));
assert!(is_const_evaluable(&expr));
}
#[test]
fn test_is_const_evaluable_binary_non_const() {
let expr = alloc_add(alloc_var("x"), alloc_int(2));
assert!(!is_const_evaluable(&expr));
}
#[test]
fn test_is_const_evaluable_unary_const() {
let expr = alloc_neg(alloc_int(5));
assert!(is_const_evaluable(&expr));
}
#[test]
fn test_is_const_evaluable_unary_non_const() {
let expr = alloc_neg(alloc_var("x"));
assert!(!is_const_evaluable(&expr));
}
#[test]
fn test_is_const_evaluable_identifier() {
let expr = alloc_var("x");
assert!(!is_const_evaluable(&expr));
}
#[test]
fn test_is_const_evaluable_complex() {
let expr = expr_mul(alloc_add(alloc_int(1), alloc_int(2)), alloc_int(3));
assert!(is_const_evaluable(&expr));
}