Skip to content

Commit 00e6882

Browse files
committed
Basic/String-as-Boolean
1 parent 4e5d173 commit 00e6882

File tree

7 files changed

+128
-0
lines changed

7 files changed

+128
-0
lines changed

Basic/String-as-Boolean/.test.ps1

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
task Test-2.ps1 {
3+
($1, $2, $3, $4, $5, $6 = .\Test-2.ps1)
4+
equals $1 'Operator & works'
5+
equals $2 'Dot-sourcing with | Out-Null works'
6+
equals $3 'Dot-sourcing with > $null works'
7+
equals $4 'value works as false'
8+
equals $5 'cast1 is False'
9+
assert ($6 -like 'Cannot convert value "System.String" to type "System.Boolean".*')
10+
}
11+
12+
task Test-4.ps1 {
13+
($1, $2, $3, $4, $5, $6 = .\Test-4.ps1)
14+
equals $1 'Operator & works'
15+
equals $2 'Dot-sourcing with | Out-Null works'
16+
equals $3 'Dot-sourcing with > $null works'
17+
equals $4 'value works as true'
18+
equals $5 'cast1 is True'
19+
assert ($6 -like 'Cannot convert value "System.String" to type "System.Boolean".*')
20+
}

Basic/String-as-Boolean/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# String as Boolean
2+
3+
The rule: empty strings are treated as false, not empty strings are treated as true (including strings "False").
4+
5+
The rule works fine in Boolean expressions:
6+
7+
```powershell
8+
if ($value) {...}
9+
if (!$value) {...}
10+
```
11+
12+
The rule works fine in cast expressions:
13+
14+
```powershell
15+
[bool]$value
16+
```
17+
18+
But assigning a string to a typed variable of type `[bool]` may work or fail depending on invocation:
19+
20+
```powershell
21+
[bool]$var = $value
22+
```
23+
24+
**Scripts**
25+
26+
- [Test-1.ps1](Test-1.ps1) shows how empty strings work as false
27+
- [Test-2.ps1](Test-2.ps1) calls `Test-1.ps1` and shows how assigning to `[bool]` may work or fail
28+
- [Test-3.ps1](Test-3.ps1) shows how not empty strings work as true
29+
- [Test-4.ps1](Test-4.ps1) calls `Test-3.ps1` and shows how assigning to `[bool]` may work or fail

Basic/String-as-Boolean/Test-1.ps1

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
# Empty strings work as false in Boolean expressions
3+
$value = ''
4+
if (!$value) {
5+
'value works as false'
6+
}
7+
8+
# Empty strings may be cast to [bool] as false
9+
$cast1 = [bool]$value
10+
"cast1 is $cast1"
11+
12+
# But [bool]$var = '' may work or fail depending on invocation
13+
try {
14+
[bool]$cast2 = $value
15+
"cast2 is $cast2"
16+
}
17+
catch {
18+
"$_"
19+
}

Basic/String-as-Boolean/Test-2.ps1

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
# Invocation by & works
3+
$null = & $PSScriptRoot\Test-1.ps1
4+
'Operator & works'
5+
6+
# Dot-sourcing with | Out-Null works
7+
. $PSScriptRoot\Test-1.ps1 | Out-Null
8+
'Dot-sourcing with | Out-Null works'
9+
10+
# Dot-sourcing with > $null works
11+
. $PSScriptRoot\Test-1.ps1 > $null
12+
'Dot-sourcing with > $null works'
13+
14+
# But this dot-sourcing fails
15+
try {
16+
. $PSScriptRoot\Test-1.ps1
17+
}
18+
catch {
19+
"$_"
20+
}

Basic/String-as-Boolean/Test-3.ps1

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
# Not empty strings work as true in Boolean expressions
3+
$value = 'False'
4+
if ($value) {
5+
'value works as true'
6+
}
7+
8+
# Not empty strings may be cast to [bool] as true
9+
$cast1 = [bool]$value
10+
"cast1 is $cast1"
11+
12+
# But [bool]$var = ... may work or fail depending on invocation
13+
try {
14+
[bool]$cast2 = $value
15+
"cast2 is $cast2"
16+
}
17+
catch {
18+
"$_"
19+
}

Basic/String-as-Boolean/Test-4.ps1

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
# Invocation by & works
3+
$null = & $PSScriptRoot\Test-3.ps1
4+
'Operator & works'
5+
6+
# Dot-sourcing with | Out-Null works
7+
. $PSScriptRoot\Test-3.ps1 | Out-Null
8+
'Dot-sourcing with | Out-Null works'
9+
10+
# Dot-sourcing with > $null works
11+
. $PSScriptRoot\Test-3.ps1 > $null
12+
'Dot-sourcing with > $null works'
13+
14+
# But this dot-sourcing fails
15+
try {
16+
. $PSScriptRoot\Test-3.ps1
17+
}
18+
catch {
19+
"$_"
20+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ to their directory. See also [TESTS]. Some scripts require
6262
- [Runspace pool memory leaks with `Close()`](Basic/RunspacePool)
6363
- [Statements are not expressions](Basic/Statements-are-not-expressions)
6464
- [ErrorRecord formatting may fail in the strict mode in the default host](Basic/Strict-mode-ErrorRecord-formatting)
65+
- [String as Boolean](Basic/String-as-Boolean)
6566
- [String constructor](Basic/String-constructor)
6667
- [String equality operators](Basic/String-equality-operators)
6768
- [`switch` is a looping construct](Basic/Switch-is-a-looping-construct)

0 commit comments

Comments
 (0)