Initial Renamr scaffold: Tauri (Rust core + React/TS UI), naming-expression engine ported+tested
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
use renamr_core::format::ExpressionFormat;
|
||||
use renamr_core::string_bindings;
|
||||
|
||||
#[test]
|
||||
fn simple_variable_substitution() {
|
||||
let fmt = ExpressionFormat::compile("{n} - {s00e00} - {t}").unwrap();
|
||||
let bindings = string_bindings([
|
||||
("n", "Breaking Bad"),
|
||||
("s00e00", "S05E14"),
|
||||
("t", "Ozymandias"),
|
||||
]);
|
||||
assert_eq!(fmt.format(&bindings).unwrap(), "Breaking Bad - S05E14 - Ozymandias");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scripted_expression_evaluation() {
|
||||
let fmt = ExpressionFormat::compile("{n.to_upper()}").unwrap();
|
||||
let bindings = string_bindings([("n", "Breaking Bad")]);
|
||||
assert_eq!(fmt.format(&bindings).unwrap(), "BREAKING BAD");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn literal_text_passes_through() {
|
||||
let fmt = ExpressionFormat::compile("plain text, no expressions").unwrap();
|
||||
assert_eq!(fmt.format(&Default::default()).unwrap(), "plain text, no expressions");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn global_helper_function_any() {
|
||||
let fmt = ExpressionFormat::compile("{any([x, y])}").unwrap();
|
||||
let bindings = string_bindings([("y", "fallback")]);
|
||||
// `x` isn't bound at all; referencing it inside the array literal would normally
|
||||
// error, so real usage is any([x_or_default, y]) - here we just prove `any` picks
|
||||
// the first non-empty value among ones that DO evaluate.
|
||||
let bindings2 = string_bindings([("x", ""), ("y", "fallback")]);
|
||||
let _ = bindings; // silence unused warning for the simplified case above
|
||||
assert_eq!(fmt.format(&bindings2).unwrap(), "fallback");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn undefined_binding_alone_yields_error() {
|
||||
let fmt = ExpressionFormat::compile("{missing}").unwrap();
|
||||
let result = fmt.format(&Default::default());
|
||||
assert!(result.is_err(), "expected empty-result error, got {:?}", result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn undefined_binding_is_skipped_when_other_content_exists() {
|
||||
let fmt = ExpressionFormat::compile("{n}{missing}").unwrap();
|
||||
let bindings = string_bindings([("n", "Show")]);
|
||||
assert_eq!(fmt.format(&bindings).unwrap(), "Show");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nested_braces_inside_a_scriptlet_are_preserved() {
|
||||
// Rhai object map literal: #{ key: value }
|
||||
let fmt = ExpressionFormat::compile("{ (#{a: 1, b: 2}).a }").unwrap();
|
||||
assert_eq!(fmt.format(&Default::default()).unwrap(), "1");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn syntax_error_is_reported_with_the_offending_expression() {
|
||||
let err = ExpressionFormat::compile("{this is not valid rhai +++ }").unwrap_err();
|
||||
let message = err.to_string();
|
||||
assert!(message.contains("syntax error"), "message was: {message}");
|
||||
}
|
||||
Reference in New Issue
Block a user