forked from pester/Pester
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShould-NotBeString.ps1
More file actions
79 lines (61 loc) · 2.88 KB
/
Copy pathShould-NotBeString.ps1
File metadata and controls
79 lines (61 loc) · 2.88 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
function Get-StringNotEqualDefaultFailureMessage ([String]$Expected, $Actual) {
"Expected the strings to be different but they were the same '$Expected'."
}
function Should-NotBeString {
<#
.SYNOPSIS
Asserts that the actual value is not equal to the expected value.
.DESCRIPTION
The `Should-NotBeString` assertion compares the actual value to the expected value using the `-ne` operator. The `-ne` 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 Because
The reason why the actual value should not be equal to the expected value.
.EXAMPLE
```powershell
"hello" | Should-NotBeString "HELLO"
```
This assertion will pass, because the actual value is not equal to the expected value.
.EXAMPLE
```powershell
"hello" | Should-NotBeString "hello" -CaseSensitive
```
This assertion will fail, because the actual value is equal to the expected value.
.NOTES
The `Should-NotBeString` assertion is the opposite of the `Should-BeString` assertion.
.LINK
https://pester.dev/docs/commands/Should-NotBeString
.LINK
https://pester.dev/docs/assertions
#>
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseProcessBlockForPipelineCommand', '')]
param (
[Parameter(Position = 1, ValueFromPipeline = $true)]
$Actual,
[Parameter(Position = 0)]
[String]$Expected,
[String]$Because,
[switch]$CaseSensitive,
[switch]$IgnoreWhitespace
)
$collectedInput = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput -UnrollInput
$Actual = $collectedInput.Actual
if ($Actual -isnot [string]) {
throw [ArgumentException]"Actual is expected to be string, to avoid confusing behavior that -ne operator exhibits with collections. To assert on collections use Should-Any, Should-All or some other collection assertion."
}
if (Test-StringEqual -Expected $Expected -Actual $Actual -CaseSensitive:$CaseSensitive -IgnoreWhitespace:$IgnoreWhiteSpace) {
if (-not $CustomMessage) {
$formattedMessage = Get-StringNotEqualDefaultFailureMessage -Expected $Expected -Actual $Actual
}
else {
$formattedMessage = Get-CustomFailureMessage -Expected $Expected -Actual $Actual -Because $Because
}
throw [Pester.Factory]::CreateShouldErrorRecord($formattedMessage, $MyInvocation.ScriptName, $MyInvocation.ScriptLineNumber, $MyInvocation.Line.TrimEnd([System.Environment]::NewLine), $true)
}
}