Add predefined constants (#2054)

This commit is contained in:
Casey Rodarmor
2024-05-18 16:12:11 -07:00
committed by GitHub
parent f3eebb74d8
commit 6907847a77
14 changed files with 128 additions and 31 deletions

45
tests/constants.rs Normal file
View File

@@ -0,0 +1,45 @@
use super::*;
#[test]
fn constants_are_defined() {
assert_eval_eq("HEX", "0123456789abcdef");
}
#[test]
fn constants_are_defined_in_recipe_bodies() {
Test::new()
.justfile(
"
@foo:
echo {{HEX}}
",
)
.stdout("0123456789abcdef\n")
.run();
}
#[test]
fn constants_are_defined_in_recipe_parameters() {
Test::new()
.justfile(
"
@foo hex=HEX:
echo {{hex}}
",
)
.stdout("0123456789abcdef\n")
.run();
}
#[test]
fn constants_can_be_redefined() {
Test::new()
.justfile(
"
HEX := 'foo'
",
)
.args(["--evaluate", "HEX"])
.stdout("foo")
.run();
}

View File

@@ -436,15 +436,6 @@ fn semver_matches() {
.run();
}
fn assert_eval_eq(expression: &str, result: &str) {
Test::new()
.justfile(format!("x := {expression}"))
.args(["--evaluate", "x"])
.stdout(result)
.unindent_stdout(false)
.run();
}
#[test]
fn trim_end_matches() {
assert_eval_eq("trim_end_matches('foo', 'o')", "f");

View File

@@ -3,7 +3,7 @@ pub(crate) use {
assert_stdout::assert_stdout,
assert_success::assert_success,
tempdir::tempdir,
test::{Output, Test},
test::{assert_eval_eq, Output, Test},
},
cradle::input::Input,
executable_path::executable_path,
@@ -46,6 +46,7 @@ mod command;
mod completions;
mod conditional;
mod confirm;
mod constants;
mod delimiters;
mod directories;
mod dotenv;

View File

@@ -312,3 +312,12 @@ fn test_round_trip(tmpdir: &Path) {
assert_eq!(reparsed, dumped, "reparse mismatch");
}
pub fn assert_eval_eq(expression: &str, result: &str) {
Test::new()
.justfile(format!("x := {expression}"))
.args(["--evaluate", "x"])
.stdout(result)
.unindent_stdout(false)
.run();
}