From 2b9946cdbd3af2f9f35c20b744528ff38562a519 Mon Sep 17 00:00:00 2001 From: Roman Kuzmin Date: Wed, 4 Sep 2024 05:45:49 +0000 Subject: [PATCH] Basic/String-as-Boolean --- Basic/String-as-Boolean/.test.ps1 | 20 ++++++++++++++++++++ Basic/String-as-Boolean/README.md | 29 +++++++++++++++++++++++++++++ Basic/String-as-Boolean/Test-1.ps1 | 19 +++++++++++++++++++ Basic/String-as-Boolean/Test-2.ps1 | 20 ++++++++++++++++++++ Basic/String-as-Boolean/Test-3.ps1 | 19 +++++++++++++++++++ Basic/String-as-Boolean/Test-4.ps1 | 20 ++++++++++++++++++++ README.md | 1 + 7 files changed, 128 insertions(+) create mode 100644 Basic/String-as-Boolean/.test.ps1 create mode 100644 Basic/String-as-Boolean/README.md create mode 100644 Basic/String-as-Boolean/Test-1.ps1 create mode 100644 Basic/String-as-Boolean/Test-2.ps1 create mode 100644 Basic/String-as-Boolean/Test-3.ps1 create mode 100644 Basic/String-as-Boolean/Test-4.ps1 diff --git a/Basic/String-as-Boolean/.test.ps1 b/Basic/String-as-Boolean/.test.ps1 new file mode 100644 index 0000000..27ffb11 --- /dev/null +++ b/Basic/String-as-Boolean/.test.ps1 @@ -0,0 +1,20 @@ + +task Test-2.ps1 { + ($1, $2, $3, $4, $5, $6 = .\Test-2.ps1) + equals $1 'Operator & works' + equals $2 'Dot-sourcing with | Out-Null works' + equals $3 'Dot-sourcing with > $null works' + equals $4 'value works as false' + equals $5 'cast1 is False' + assert ($6 -like 'Cannot convert value "System.String" to type "System.Boolean".*') +} + +task Test-4.ps1 { + ($1, $2, $3, $4, $5, $6 = .\Test-4.ps1) + equals $1 'Operator & works' + equals $2 'Dot-sourcing with | Out-Null works' + equals $3 'Dot-sourcing with > $null works' + equals $4 'value works as true' + equals $5 'cast1 is True' + assert ($6 -like 'Cannot convert value "System.String" to type "System.Boolean".*') +} diff --git a/Basic/String-as-Boolean/README.md b/Basic/String-as-Boolean/README.md new file mode 100644 index 0000000..fbfc30f --- /dev/null +++ b/Basic/String-as-Boolean/README.md @@ -0,0 +1,29 @@ +# String as Boolean + +The rule: empty strings are treated as false, not empty strings are treated as true (including strings "False"). + +The rule works fine in Boolean expressions: + +```powershell +if ($value) {...} +if (!$value) {...} +``` + +The rule works fine in cast expressions: + +```powershell +[bool]$value +``` + +But assigning a string to a typed variable of type `[bool]` may work or fail depending on invocation: + +```powershell +[$bool]$var = $value +``` + +**Scripts** + +- [Test-1.ps1](Test-1.ps1) shows how empty strings work as false +- [Test-2.ps1](Test-2.ps1) calls `Test-1.ps1` and shows how assigning to `[bool]` may work or fail +- [Test-3.ps1](Test-3.ps1) shows how not empty strings work as true +- [Test-4.ps1](Test-4.ps1) calls `Test-3.ps1` and shows how assigning to `[bool]` may work or fail diff --git a/Basic/String-as-Boolean/Test-1.ps1 b/Basic/String-as-Boolean/Test-1.ps1 new file mode 100644 index 0000000..7421c12 --- /dev/null +++ b/Basic/String-as-Boolean/Test-1.ps1 @@ -0,0 +1,19 @@ + +# Empty strings work as false in Boolean expressions +$value = '' +if (!$value) { + 'value works as false' +} + +# Empty strings may be cast to [bool] as false +$cast1 = [bool]$value +"cast1 is $cast1" + +# But [bool]$var = '' may work or fail depending on invocation +try { + [bool]$cast2 = $value + "cast2 is $cast2" +} +catch { + "$_" +} diff --git a/Basic/String-as-Boolean/Test-2.ps1 b/Basic/String-as-Boolean/Test-2.ps1 new file mode 100644 index 0000000..845fd33 --- /dev/null +++ b/Basic/String-as-Boolean/Test-2.ps1 @@ -0,0 +1,20 @@ + +# Invocation by & works +$null = & $PSScriptRoot\Test-1.ps1 +'Operator & works' + +# Dot-sourcing with | Out-Null works +. $PSScriptRoot\Test-1.ps1 | Out-Null +'Dot-sourcing with | Out-Null works' + +# Dot-sourcing with > $null works +. $PSScriptRoot\Test-1.ps1 > $null +'Dot-sourcing with > $null works' + +# But this dot-sourcing fails +try { + . $PSScriptRoot\Test-1.ps1 +} +catch { + "$_" +} diff --git a/Basic/String-as-Boolean/Test-3.ps1 b/Basic/String-as-Boolean/Test-3.ps1 new file mode 100644 index 0000000..7a2a51e --- /dev/null +++ b/Basic/String-as-Boolean/Test-3.ps1 @@ -0,0 +1,19 @@ + +# Not empty strings work as true in Boolean expressions +$value = 'False' +if ($value) { + 'value works as true' +} + +# Not empty strings may be cast to [bool] as true +$cast1 = [bool]$value +"cast1 is $cast1" + +# But [bool]$var = ... may work or fail depending on invocation +try { + [bool]$cast2 = $value + "cast2 is $cast2" +} +catch { + "$_" +} diff --git a/Basic/String-as-Boolean/Test-4.ps1 b/Basic/String-as-Boolean/Test-4.ps1 new file mode 100644 index 0000000..1af72e3 --- /dev/null +++ b/Basic/String-as-Boolean/Test-4.ps1 @@ -0,0 +1,20 @@ + +# Invocation by & works +$null = & $PSScriptRoot\Test-3.ps1 +'Operator & works' + +# Dot-sourcing with | Out-Null works +. $PSScriptRoot\Test-3.ps1 | Out-Null +'Dot-sourcing with | Out-Null works' + +# Dot-sourcing with > $null works +. $PSScriptRoot\Test-3.ps1 > $null +'Dot-sourcing with > $null works' + +# But this dot-sourcing fails +try { + . $PSScriptRoot\Test-3.ps1 +} +catch { + "$_" +} diff --git a/README.md b/README.md index 4d412e0..9d2e066 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,7 @@ to their directory. See also [TESTS]. Some scripts require - [Runspace pool memory leaks with `Close()`](Basic/RunspacePool) - [Statements are not expressions](Basic/Statements-are-not-expressions) - [ErrorRecord formatting may fail in the strict mode in the default host](Basic/Strict-mode-ErrorRecord-formatting) +- [String as Boolean](Basic/String-as-Boolean) - [String constructor](Basic/String-constructor) - [String equality operators](Basic/String-equality-operators) - [`switch` is a looping construct](Basic/Switch-is-a-looping-construct)