-
-
Notifications
You must be signed in to change notification settings - Fork 477
Expand file tree
/
Copy pathShould-BeString.ps1
More file actions
158 lines (128 loc) · 4.74 KB
/
Copy pathShould-BeString.ps1
File metadata and controls
158 lines (128 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
function Test-StringEqual {
param (
[String]$Expected,
$Actual,
[switch]$CaseSensitive,
[switch]$IgnoreWhitespace,
[switch]$TrimWhitespace
)
if ($Actual -isnot [string]) {
return $false
}
if ($IgnoreWhitespace) {
$Expected = $Expected -replace '\s'
$Actual = $Actual -replace '\s'
}
if ($TrimWhitespace) {
$Expected = $Expected -replace '^\s+|\s+$'
$Actual = $Actual -replace '^\s+|\s+$'
}
if (-not $CaseSensitive) {
$Expected -eq $Actual
}
else {
$Expected -ceq $Actual
}
}
function Should-BeString {
<#
.SYNOPSIS
Asserts that the actual value is equal to the expected value.
.DESCRIPTION
The `Should-BeString` assertion compares the actual value to the expected value using the `-eq` operator. The `-eq` operator is case-insensitive by default, but you can make it case-sensitive by using the `-CaseSensitive` switch.
.PARAMETER Expected
The expected value.
.PARAMETER Actual
The actual value.
.PARAMETER CaseSensitive
Indicates that the comparison should be case-sensitive.
.PARAMETER IgnoreWhitespace
Indicates that the comparison should ignore whitespace.
.PARAMETER TrimWhitespace
Trims whitespace at the start and end of the string.
.PARAMETER Because
The reason why the actual value should be equal to the expected value.
.EXAMPLE
```powershell
"hello" | Should-BeString "hello"
```
This assertion will pass, because the actual value is equal to the expected value.
.EXAMPLE
```powershell
"hello" | Should-BeString "HELLO" -CaseSensitive
```
This assertion will fail, because the actual value is not equal to the expected value.
.NOTES
The `Should-BeString` assertion is the opposite of the `Should-NotBeString` assertion.
.LINK
https://pester.dev/docs/commands/Should-BeString
.LINK
https://pester.dev/docs/assertions
#>
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseProcessBlockForPipelineCommand', '')]
[CmdletBinding()]
param (
[Parameter(Position = 1, ValueFromPipeline = $true)]
$Actual,
[Parameter(Position = 0, Mandatory)]
[String]$Expected,
[String]$Because,
[switch]$CaseSensitive,
[switch]$IgnoreWhitespace,
[switch]$TrimWhitespace
)
$collectedInput = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput -UnrollInput
$Actual = $collectedInput.Actual
$stringsAreEqual = Test-StringEqual -Expected $Expected -Actual $Actual -CaseSensitive:$CaseSensitive -IgnoreWhitespace:$IgnoreWhiteSpace -TrimWhitespace:$TrimWhitespace
if (-not ($stringsAreEqual)) {
if ($Actual -is [string]) {
$Message = Get-StringDifferenceMessage -Expected $Expected -Actual $Actual -CaseSensitive:$CaseSensitive -Because $Because
}
else {
$Message = Get-AssertionMessage -Expected $Expected -Actual $Actual -Because $Because -DefaultMessage "Expected <expectedType> <expected>, but got <actualType> <actual>."
}
Invoke-AssertionFailed -Message $Message -CallerCmdlet $PSCmdlet
}
Set-AssertionPassResult
}
function Get-StringDifferenceMessage {
param (
[Parameter(Mandatory)]
[AllowEmptyString()]
[string] $Expected,
[Parameter(Mandatory)]
[AllowEmptyString()]
[string] $Actual,
[switch] $CaseSensitive,
[string] $Because
)
$maxLength = [Math]::Max($Expected.Length, $Actual.Length)
$differenceIndex = $null
for ($i = 0; $i -lt $maxLength -and ($null -eq $differenceIndex); ++$i) {
if ($CaseSensitive) {
if ($Expected[$i] -cne $Actual[$i]) { $differenceIndex = $i }
}
else {
if ($Expected[$i] -ne $Actual[$i]) { $differenceIndex = $i }
}
}
$because = if ($Because) { " because $Because," } else { "" }
$lines = @(
"Expected strings to be the same,$because but they were different."
)
if ($Expected.Length -ne $Actual.Length) {
$lines += "Expected length: $($Expected.Length)"
$lines += "Actual length: $($Actual.Length)"
}
else {
$lines += "String lengths are both $($Expected.Length)."
}
$lines += "Strings differ at index $differenceIndex."
$expectedExpanded = Expand-SpecialCharacters -InputObject $Expected
$actualExpanded = Expand-SpecialCharacters -InputObject $Actual
$prefix = "Expected: '"
$lines += "$prefix$expectedExpanded'"
$lines += "But was: '$actualExpanded'"
$lines += (' ' * ($prefix.Length - 1)) + ('-' * $differenceIndex) + '^'
$lines -join "`n"
}