forked from microsoft/PSRule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSRule.Within.Tests.ps1
142 lines (122 loc) · 5.43 KB
/
PSRule.Within.Tests.ps1
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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
#
# Unit tests for the Within keyword
#
[CmdletBinding()]
param (
)
BeforeAll {
# Setup error handling
$ErrorActionPreference = 'Stop';
Set-StrictMode -Version latest;
# Setup tests paths
$rootPath = $PWD;
Import-Module (Join-Path -Path $rootPath -ChildPath out/modules/PSRule) -Force;
$here = (Resolve-Path $PSScriptRoot).Path;
}
Describe 'PSRule -- Within keyword' -Tag 'Within' {
BeforeAll {
$ruleFilePath = (Join-Path -Path $here -ChildPath 'FromFile.Rule.ps1');
}
Context 'Within' {
It 'With defaults' {
$testObject = @(
[PSCustomObject]@{ Title = 'mr' }
[PSCustomObject]@{ Title = 'unknown' }
@{ Title = 'mr' }
@{ Value = @{ Title = 'Mr' } }
@{ NotTitle = 'a different property' }
)
$result = $testObject | Invoke-PSRule -Path $ruleFilePath -Name 'WithinTest' -Outcome All;
$result | Should -Not -BeNullOrEmpty;
$result.Count | Should -Be 5;
$result.RuleName | Should -BeIn 'WithinTest';
$result[0].IsSuccess() | Should -Be $True;
$result[1].IsSuccess() | Should -Be $False;
$result[2].IsSuccess() | Should -Be $True;
$result[3].IsSuccess() | Should -Be $True;
$result[4].IsSuccess() | Should -Be $False;
# Check non-string types
$testObject = @(
[PSCustomObject]@{ BooleanValue = $True; IntValue = 1; NullValue = $Null; }
[PSCustomObject]@{ BooleanValue = $False; IntValue = 100; NullValue = $Null; }
([PSCustomObject]@{ BooleanValue = $True; IntValue = 1; NullValue = $Null; } | ConvertTo-Json | ConvertFrom-Json)
[PSCustomObject]@{ BooleanValue = $Null; IntValue = $Null; NullValue = 'NotNull'; }
)
$result = $testObject | Invoke-PSRule -Path $ruleFilePath -Name 'WithinTypes' -Outcome All;
$result | Should -Not -BeNullOrEmpty;
$result.Count | Should -Be 4;
$result.RuleName | Should -BeIn 'WithinTypes';
$result[0].IsSuccess() | Should -Be $True;
$result[1].IsSuccess() | Should -Be $False;
$result[2].IsSuccess() | Should -Be $True;
$result[2].Reason | Should -BeNullOrEmpty;
$result[3].IsSuccess() | Should -Be $False;
$result[3].Reason | Should -BeLike "The field value didn't match the set.";
}
It 'With -CaseSensitive' {
$testObject = @(
[PSCustomObject]@{ Title = 'mr' }
[PSCustomObject]@{ Title = 'Mr' }
@{ Title = 'Mr' }
)
$result = $testObject | Invoke-PSRule -Path $ruleFilePath -Name 'WithinTestCaseSensitive';
$result | Should -Not -BeNullOrEmpty;
$result.Count | Should -Be 3;
$result.RuleName | Should -BeIn 'WithinTestCaseSensitive';
$result[0].IsSuccess() | Should -Be $False;
$result[1].IsSuccess() | Should -Be $True;
$result[2].IsSuccess() | Should -Be $True;
}
It 'With -Not' {
$testObject = @(
[PSCustomObject]@{ Title = 'Miss' }
[PSCustomObject]@{ Title = 'Mr' }
)
$result = $testObject | Invoke-PSRule -Path $ruleFilePath -Name 'WithinNot';
$result | Should -Not -BeNullOrEmpty;
$result.Count | Should -Be 2;
$result.RuleName | Should -BeIn 'WithinNot';
$result[0].IsSuccess() | Should -Be $True;
$result[1].IsSuccess() | Should -Be $False;
$result[1].Reason | Should -BeLike "The value '*' was within the set.";
}
It 'With -Like' {
$testObject = @(
[PSCustomObject]@{ Title = 'Miss' }
[PSCustomObject]@{ Title = 'Mr' }
[PSCustomObject]@{ Title = 'Ms' }
[PSCustomObject]@{ Title = 'Mrs' }
)
$result = $testObject | Invoke-PSRule -Path $ruleFilePath -Name 'WithinLike';
$result | Should -Not -BeNullOrEmpty;
$result.Count | Should -Be 4;
$result.RuleName | Should -BeIn 'WithinLike';
$result[0].IsSuccess() | Should -Be $True;
$result[1].IsSuccess() | Should -Be $False;
$result[2].IsSuccess() | Should -Be $True;
$result[3].IsSuccess() | Should -Be $True;
}
It 'If pre-condition' {
$testObject = @(
[PSCustomObject]@{
Name = 'TestObject1'
}
[PSCustomObject]@{
Name = 'TestObject2'
}
)
$option = New-PSRuleOption -NotProcessedWarning $False
$result = $testObject | Invoke-PSRule -Path $ruleFilePath -Name 'WithinCondition' -Outcome All -Option $option;
# Test positive cases
$filteredResult = $result | Where-Object { $_.TargetName -eq 'TestObject1' };
$filteredResult | Should -Not -BeNullOrEmpty;
$filteredResult.Outcome | Should -Be 'Pass';
# Test negative cases
$filteredResult = $result | Where-Object { $_.TargetName -eq 'TestObject2' };
$filteredResult | Should -Not -BeNullOrEmpty;
$filteredResult.Outcome | Should -Be 'None';
}
}
}