#![allow(clippy::unwrap_used)]
#![allow(clippy::expect_used)]
#![allow(unused_imports)]
use super::super::*;
use crate::make_parser::ast::{MakeCondition, Span, VarFlavor};
#[test]
fn test_FUNC_SUBST_001_basic_subst() {
let makefile = "OBJS = $(subst .c,.o,main.c util.c)";
let result = parse_makefile(makefile);
assert!(
result.is_ok(),
"Should parse $(subst) function, got error: {:?}",
result.err()
);
let ast = result.unwrap();
assert_eq!(ast.items.len(), 1, "Should have 1 variable");
match &ast.items[0] {
MakeItem::Variable { name, value, .. } => {
assert_eq!(name, "OBJS", "Variable name should be OBJS");
assert_eq!(
value, "$(subst .c,.o,main.c util.c)",
"Variable value should preserve $(subst) function syntax"
);
}
other => panic!("Expected Variable item, got {:?}", other),
}
}
#[test]
fn test_FUNC_SUBST_001_subst_in_prerequisites() {
let makefile = "build: $(subst .c,.o,$(SRCS))\n\tgcc -o $@ $^";
let result = parse_makefile(makefile);
assert!(result.is_ok(), "Should parse $(subst) in prerequisites");
let ast = result.unwrap();
match &ast.items[0] {
MakeItem::Target {
name,
prerequisites,
recipe,
..
} => {
assert_eq!(name, "build");
assert_eq!(prerequisites.len(), 2);
assert_eq!(prerequisites[0], "$(subst");
assert_eq!(prerequisites[1], ".c,.o,$(SRCS))");
assert_eq!(recipe.len(), 1, "Should have 1 recipe line");
assert_eq!(recipe[0], "gcc -o $@ $^");
}
other => panic!("Expected Target, got {:?}", other),
}
}
#[test]
fn test_FUNC_SUBST_001_multiple_subst() {
let makefile = "OBJS = $(subst .c,.o,$(SRCS))\nLIBS = $(subst .a,.so,$(DEPS))";
let result = parse_makefile(makefile);
assert!(result.is_ok(), "Should parse multiple $(subst) functions");
let ast = result.unwrap();
assert_eq!(ast.items.len(), 2, "Should have 2 variables");
match &ast.items[0] {
MakeItem::Variable { name, value, .. } => {
assert_eq!(name, "OBJS");
assert_eq!(value, "$(subst .c,.o,$(SRCS))");
}
other => panic!("Expected Variable, got {:?}", other),
}
match &ast.items[1] {
MakeItem::Variable { name, value, .. } => {
assert_eq!(name, "LIBS");
assert_eq!(value, "$(subst .a,.so,$(DEPS))");
}
other => panic!("Expected Variable, got {:?}", other),
}
}
#[test]
fn test_FUNC_SUBST_001_subst_with_spaces() {
let makefile = "FILES = $(subst src/,build/,src/main.c src/util.c)";
let result = parse_makefile(makefile);
assert!(result.is_ok(), "Should parse $(subst) with spaces");
let ast = result.unwrap();
assert_eq!(ast.items.len(), 1);
match &ast.items[0] {
MakeItem::Variable { name, value, .. } => {
assert_eq!(name, "FILES");
assert_eq!(
value, "$(subst src/,build/,src/main.c src/util.c)",
"Should preserve spaces in $(subst) text argument"
);
}
other => panic!("Expected Variable, got {:?}", other),
}
}
#[test]
fn test_FUNC_SUBST_001_nested_subst() {
let makefile = "RESULT = $(subst .c,.o,$(subst src/,build/,$(SRCS)))";
let result = parse_makefile(makefile);
assert!(result.is_ok(), "Should parse nested $(subst) functions");
let ast = result.unwrap();
assert_eq!(ast.items.len(), 1);
match &ast.items[0] {
MakeItem::Variable { name, value, .. } => {
assert_eq!(name, "RESULT");
assert_eq!(
value, "$(subst .c,.o,$(subst src/,build/,$(SRCS)))",
"Should preserve nested $(subst) functions"
);
}
other => panic!("Expected Variable, got {:?}", other),
}
}
#[test]
fn test_FUNC_SUBST_001_subst_with_other_functions() {
let makefile = "OBJS = $(subst .c,.o,$(wildcard src/*.c))";
let result = parse_makefile(makefile);
assert!(
result.is_ok(),
"Should parse $(subst) with $(wildcard) function"
);
let ast = result.unwrap();
assert_eq!(ast.items.len(), 1);
match &ast.items[0] {
MakeItem::Variable { name, value, .. } => {
assert_eq!(name, "OBJS");
assert_eq!(
value, "$(subst .c,.o,$(wildcard src/*.c))",
"Should preserve $(subst) combined with other functions"
);
}
other => panic!("Expected Variable, got {:?}", other),
}
}
#[cfg(test)]
#[path = "part3_tests_prop_func.rs"]
mod tests_extracted;