Skip to content

Commit

Permalink
Basic/String-as-Boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
nightroman committed Sep 4, 2024
1 parent 4e5d173 commit 2b9946c
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Basic/String-as-Boolean/.test.ps1
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".*')
}
29 changes: 29 additions & 0 deletions Basic/String-as-Boolean/README.md
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
19 changes: 19 additions & 0 deletions Basic/String-as-Boolean/Test-1.ps1
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 {
"$_"
}
20 changes: 20 additions & 0 deletions Basic/String-as-Boolean/Test-2.ps1
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 {
"$_"
}
19 changes: 19 additions & 0 deletions Basic/String-as-Boolean/Test-3.ps1
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 {
"$_"
}
20 changes: 20 additions & 0 deletions Basic/String-as-Boolean/Test-4.ps1
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 {
"$_"
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2b9946c

Please sign in to comment.