Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = "fallback"
}
}
"#},
indoc! {r#"
node 0
pkgname: "fallback"
"#},
);
}

#[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 = "fallback"
}
}
"#},
indoc! {r#"
node 0
pkgname: "fallback"
"#},
);
}

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