-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4e5d173
commit 2b9946c
Showing
7 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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".*') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
"$_" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
"$_" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
"$_" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
"$_" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters