Skip to content

Commit

Permalink
Allow optional globals be omitted.
Browse files Browse the repository at this point in the history
  • Loading branch information
bm-w committed Jun 12, 2024
1 parent 1448489 commit 64e7539
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,17 @@ impl File {
for global in &self.globals {
match globals.get(&global.name) {
None => {
if let Some(default) = &global.default {
let default_or_null = global
.default
.as_ref()
.map(|default| default.to_string().into())
.or_else(|| {
(global.quantifier == CaptureQuantifier::ZeroOrOne)
.then_some(Value::Null)
});
if let Some(default_or_null) = default_or_null {
globals
.add(global.name.clone(), default.to_string().into())
.add(global.name.clone(), default_or_null)
.map_err(|_| {
ExecutionError::DuplicateVariable(format!(
"global variable {} already defined",
Expand Down
22 changes: 22 additions & 0 deletions tests/it/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,28 @@ fn can_omit_global_variable_with_default() {
);
}

#[test]
fn can_omit_optional_global_variable() {
check_execution(
"pass",
indoc! {r#"
global pkgname?
(module)
{
node n
if (is-null pkgname) {
attr (n) pkgname = "none"
}
}
"#},
indoc! {r#"
node 0
pkgname: "none"
"#},
);
}

#[test]
fn cannot_omit_global_variable() {
fail_execution(
Expand Down
22 changes: 22 additions & 0 deletions tests/it/lazy_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,28 @@ fn can_omit_global_variable_with_default() {
);
}

#[test]
fn can_omit_optional_global_variable() {
check_execution(
"pass",
indoc! {r#"
global pkgname?
(module)
{
node n
if (is-null pkgname) {
attr (n) pkgname = "none"
}
}
"#},
indoc! {r#"
node 0
pkgname: "none"
"#},
);
}

#[test]
fn cannot_omit_global_variable() {
fail_execution(
Expand Down

0 comments on commit 64e7539

Please # to comment.