From f5eff1711de851002fc52d65876d2f751dc574e3 Mon Sep 17 00:00:00 2001 From: Jeffrey Stuhr <110697945+fadwen@users.noreply.github.com> Date: Tue, 24 Nov 2026 14:47:46 -0800 Subject: [PATCH] refactor(helpers): move the assignment filter's evaluator out The third part of #65. The four functions nested in Test-IntuneAssignmentFilter are private functions in files of their own, advanced functions with full help and output types: - Get-VersionPart -> Get-IslVersionPart - Compare-Version -> Compare-IslFilterVersion - Test-Clause -> Test-IslFilterClause, with the device's facts as -Facts where it read them from the command - Test-Node -> Test-IslFilterNode, with -Facts as well The clause keeps being changed in place, so that the result of the command still shows what each clause saw. Each new function has a test file of its own; the command's tests are unchanged. Refs #65. --- CHANGELOG.md | 4 +- Private/Compare-IslFilterVersion.ps1 | 51 +++++++++++++ Private/Get-IslVersionPart.ps1 | 40 ++++++++++ Private/Test-IslFilterClause.ps1 | 71 +++++++++++++++++ Private/Test-IslFilterNode.ps1 | 54 +++++++++++++ Public/Test-IntuneAssignmentFilter.ps1 | 70 +---------------- .../Compare-IslFilterVersion.Tests.ps1 | 42 ++++++++++ .../Unit/Private/Get-IslVersionPart.Tests.ps1 | 44 +++++++++++ .../Private/Test-IslFilterClause.Tests.ps1 | 76 +++++++++++++++++++ .../Unit/Private/Test-IslFilterNode.Tests.ps1 | 55 ++++++++++++++ 10 files changed, 436 insertions(+), 71 deletions(-) create mode 100644 Private/Compare-IslFilterVersion.ps1 create mode 100644 Private/Get-IslVersionPart.ps1 create mode 100644 Private/Test-IslFilterClause.ps1 create mode 100644 Private/Test-IslFilterNode.ps1 create mode 100644 Tests/Unit/Private/Compare-IslFilterVersion.Tests.ps1 create mode 100644 Tests/Unit/Private/Get-IslVersionPart.Tests.ps1 create mode 100644 Tests/Unit/Private/Test-IslFilterClause.Tests.ps1 create mode 100644 Tests/Unit/Private/Test-IslFilterNode.Tests.ps1 diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d2b884..9961ab7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,8 +43,8 @@ entries are carried over from the manifest's release notes. ### Changed - The helper functions nested in `Test-IntuneScript`, `Test-IntuneWin32Requirement`, - `Export-IntuneFindingSarif`, `Export-IntuneAgentDiagnostic`, `Get-IslSetting`, - `Get-IslAgentLogEvent` and `Invoke-IslGraphRequest` are private functions of their own, each an + `Test-IntuneAssignmentFilter`, `Export-IntuneFindingSarif`, `Export-IntuneAgentDiagnostic`, + `Get-IslSetting`, `Get-IslAgentLogEvent` and `Invoke-IslGraphRequest` are private functions of their own, each an advanced function with help and tests (#65). Nothing changes for a caller, except that the error of a settings value that is not allowed now names `ConvertTo-IslSettingsObject` after `SettingsInvalid` in its `FullyQualifiedErrorId`. diff --git a/Private/Compare-IslFilterVersion.ps1 b/Private/Compare-IslFilterVersion.ps1 new file mode 100644 index 0000000..60b45eb --- /dev/null +++ b/Private/Compare-IslFilterVersion.ps1 @@ -0,0 +1,51 @@ +function Compare-IslFilterVersion { + <# + .SYNOPSIS + Compares two version texts the way the assignment filter evaluator ordered them. + + .DESCRIPTION + Both texts are read as four numbers by Get-IslVersionPart and compared part by part, so + '10.0' equals '10.0.0.0' and '10.0.9' is less than '10.0.10'. When either text is not a + version there is no order, and the function returns nothing; the filter's gt, ge, lt and le + then do not match. + + .PARAMETER Actual + The value the device has. + + .PARAMETER Expected + The value of the rule. + + .EXAMPLE + Compare-IslFilterVersion -Actual '10.0.26100' -Expected '10.0.22631' + + Returns 1: the device's version is the greater. + + .INPUTS + None. + + .OUTPUTS + System.Int32. -1, 0 or 1, or nothing when a side is not a version. + #> + [CmdletBinding()] + [OutputType([int])] + param( + [Parameter(Mandatory)] + [AllowEmptyString()] + [string]$Actual, + + [Parameter(Mandatory)] + [AllowEmptyString()] + [string]$Expected + ) + + $left = @(Get-IslVersionPart -Text $Actual) + $right = @(Get-IslVersionPart -Text $Expected) + if ($left.Count -ne 4 -or $right.Count -ne 4) { + return + } + for ($index = 0; $index -lt 4; $index++) { + if ($left[$index] -lt $right[$index]) { return -1 } + if ($left[$index] -gt $right[$index]) { return 1 } + } + 0 +} diff --git a/Private/Get-IslVersionPart.ps1 b/Private/Get-IslVersionPart.ps1 new file mode 100644 index 0000000..7cf7dd5 --- /dev/null +++ b/Private/Get-IslVersionPart.ps1 @@ -0,0 +1,40 @@ +function Get-IslVersionPart { + <# + .SYNOPSIS + Splits a version text into four numbers, the way an assignment filter compares versions. + + .DESCRIPTION + A text of one to four numbers separated by dots, with white space around it allowed, gives + four numbers, the parts that are missing as 0: '10.0.26100' gives 10, 0, 26100, 0. Any other + text is not a version, and gives nothing. + + .PARAMETER Text + The text to read. + + .EXAMPLE + Get-IslVersionPart -Text '10.0.26100.4202' + + Returns 10, 0, 26100 and 4202. + + .INPUTS + None. + + .OUTPUTS + System.Int64. Four numbers, or nothing when the text is not a version. + #> + [CmdletBinding()] + [OutputType([long])] + param( + [Parameter(Mandatory)] + [AllowEmptyString()] + [string]$Text + ) + + if ($Text -notmatch '^\s*\d+(\.\d+){0,3}\s*$') { + return + } + $parts = [System.Collections.Generic.List[long]]::new() + foreach ($part in ($Text.Trim() -split '\.')) { $parts.Add([long]$part) } + while ($parts.Count -lt 4) { $parts.Add(0) } + $parts.ToArray() +} diff --git a/Private/Test-IslFilterClause.ps1 b/Private/Test-IslFilterClause.ps1 new file mode 100644 index 0000000..dc3bcff --- /dev/null +++ b/Private/Test-IslFilterClause.ps1 @@ -0,0 +1,71 @@ +function Test-IslFilterClause { + <# + .SYNOPSIS + Tells whether one clause of an assignment filter rule matches the facts of a device. + + .DESCRIPTION + The clause is one comparison that ConvertFrom-IslFilterRule read, such as + (device.osVersion -ge "10.0.22000"). Its property is looked up in the facts; a property the + device does not have compares as an empty text. Text comparisons with eq, ne, in and notIn + are those of PowerShell, which ignore letter case, and startsWith, contains and notContains + ignore it as well. A clause of the Version kind compares with Compare-IslFilterVersion for + eq, ne, gt, ge, lt and le; when one of its sides is not a version, eq does not match, ne + does, and gt, ge, lt and le do not. + + The clause is changed in place: Actual is set to the device's value, or $null, and Matched + to the result, so that the result object of Test-IntuneAssignmentFilter shows what each + clause saw. + + .PARAMETER Clause + The clause, an IntuneScriptLab.FilterClause with Property, Operator, Value and Kind. + + .PARAMETER Facts + The device's facts, by property name, compared without regard to letter case. + + .EXAMPLE + Test-IslFilterClause -Clause $parsed.Clauses[0] -Facts $facts + + $true when the device's value satisfies the first clause of the rule. + + .INPUTS + None. + + .OUTPUTS + System.Boolean. + #> + [CmdletBinding()] + [OutputType([bool])] + param( + [Parameter(Mandatory)] + [PSTypeName('IntuneScriptLab.FilterClause')] + [psobject]$Clause, + + [Parameter(Mandatory)] + [AllowEmptyCollection()] + [System.Collections.Generic.Dictionary[string, object]]$Facts + ) + + $raw = if ($Facts.ContainsKey($Clause.Property)) { $Facts[$Clause.Property] } else { $null } + $actual = if ($null -eq $raw) { '' } else { "$raw" } + $Clause.Actual = if ($null -eq $raw) { $null } else { "$raw" } + $values = @(foreach ($item in @($Clause.Value)) { if ($null -ne $item) { "$item".Trim() } }) + $expected = if ($values.Count) { $values[0] } else { '' } + $ignoreCase = [System.StringComparison]::OrdinalIgnoreCase + $order = $null + if ($Clause.Kind -eq 'Version') { $order = Compare-IslFilterVersion -Actual $actual -Expected $expected } + $matched = switch ($Clause.Operator) { + 'eq' { if ($Clause.Kind -eq 'Version') { $order -eq 0 } else { $actual -eq $expected } } + 'ne' { if ($Clause.Kind -eq 'Version') { $order -ne 0 } else { $actual -ne $expected } } + 'in' { $values -contains $actual } + 'notIn' { $values -notcontains $actual } + 'startsWith' { $actual.StartsWith($expected, $ignoreCase) } + 'contains' { $actual.IndexOf($expected, $ignoreCase) -ge 0 } + 'notContains' { $actual.IndexOf($expected, $ignoreCase) -lt 0 } + 'gt' { $null -ne $order -and $order -gt 0 } + 'ge' { $null -ne $order -and $order -ge 0 } + 'lt' { $null -ne $order -and $order -lt 0 } + 'le' { $null -ne $order -and $order -le 0 } + } + $Clause.Matched = [bool]$matched + $Clause.Matched +} diff --git a/Private/Test-IslFilterNode.ps1 b/Private/Test-IslFilterNode.ps1 new file mode 100644 index 0000000..97233a3 --- /dev/null +++ b/Private/Test-IslFilterNode.ps1 @@ -0,0 +1,54 @@ +function Test-IslFilterNode { + <# + .SYNOPSIS + Tells whether a node of a parsed assignment filter rule matches the facts of a device. + + .DESCRIPTION + The node is one element of the tree that ConvertFrom-IslFilterRule builds: a Clause, or an + And or an Or of a Left and a Right node. A clause is tested with Test-IslFilterClause. Both + sides of And and Or are always tested, without stopping at the first that decides, so that + every clause of the rule records what it saw. + + .PARAMETER Node + The node, a hashtable with Type, and Clause or Left and Right. + + .PARAMETER Facts + The device's facts, by property name, compared without regard to letter case. + + .EXAMPLE + Test-IslFilterNode -Node $parsed.Tree -Facts $facts + + $true when the whole rule matches the device. + + .INPUTS + None. + + .OUTPUTS + System.Boolean. + #> + [CmdletBinding()] + [OutputType([bool])] + param( + [Parameter(Mandatory)] + [hashtable]$Node, + + [Parameter(Mandatory)] + [AllowEmptyCollection()] + [System.Collections.Generic.Dictionary[string, object]]$Facts + ) + + switch ($Node['Type']) { + 'Clause' { Test-IslFilterClause -Clause $Node['Clause'] -Facts $Facts } + 'And' { + # Both sides run so every clause reports what it saw + $left = Test-IslFilterNode -Node $Node['Left'] -Facts $Facts + $right = Test-IslFilterNode -Node $Node['Right'] -Facts $Facts + $left -and $right + } + 'Or' { + $left = Test-IslFilterNode -Node $Node['Left'] -Facts $Facts + $right = Test-IslFilterNode -Node $Node['Right'] -Facts $Facts + $left -or $right + } + } +} diff --git a/Public/Test-IntuneAssignmentFilter.ps1 b/Public/Test-IntuneAssignmentFilter.ps1 index 5d1d6c2..d4c2ed6 100644 --- a/Public/Test-IntuneAssignmentFilter.ps1 +++ b/Public/Test-IntuneAssignmentFilter.ps1 @@ -43,74 +43,6 @@ function Test-IntuneAssignmentFilter { $view = [ordered]@{} foreach ($key in @($facts.Keys | Sort-Object)) { $view[$key] = $facts[$key] } $deviceView = [pscustomobject]$view - - function Get-VersionPart { - # Four numeric parts, missing ones as 0; $null when the text is not a version - param([string]$Text) - if ($Text -notmatch '^\s*\d+(\.\d+){0,3}\s*$') { return $null } - $parts = [System.Collections.Generic.List[long]]::new() - foreach ($part in ($Text.Trim() -split '\.')) { $parts.Add([long]$part) } - while ($parts.Count -lt 4) { $parts.Add(0) } - $parts.ToArray() - } - - function Compare-Version { - # -1, 0 or 1 the way the filter evaluator ordered versions; $null when a side is not one - param([string]$Actual, [string]$Expected) - $left = Get-VersionPart -Text $Actual - $right = Get-VersionPart -Text $Expected - if ($null -eq $left -or $null -eq $right) { return $null } - for ($index = 0; $index -lt 4; $index++) { - if ($left[$index] -lt $right[$index]) { return -1 } - if ($left[$index] -gt $right[$index]) { return 1 } - } - 0 - } - - function Test-Clause { - param($Clause) - $raw = if ($facts.ContainsKey($Clause.Property)) { $facts[$Clause.Property] } else { $null } - $actual = if ($null -eq $raw) { '' } else { "$raw" } - $Clause.Actual = if ($null -eq $raw) { $null } else { "$raw" } - $values = @(foreach ($item in @($Clause.Value)) { if ($null -ne $item) { "$item".Trim() } }) - $expected = if ($values.Count) { $values[0] } else { '' } - $ignoreCase = [System.StringComparison]::OrdinalIgnoreCase - $order = $null - if ($Clause.Kind -eq 'Version') { $order = Compare-Version -Actual $actual -Expected $expected } - $matched = switch ($Clause.Operator) { - 'eq' { if ($Clause.Kind -eq 'Version') { $order -eq 0 } else { $actual -eq $expected } } - 'ne' { if ($Clause.Kind -eq 'Version') { $order -ne 0 } else { $actual -ne $expected } } - 'in' { $values -contains $actual } - 'notIn' { $values -notcontains $actual } - 'startsWith' { $actual.StartsWith($expected, $ignoreCase) } - 'contains' { $actual.IndexOf($expected, $ignoreCase) -ge 0 } - 'notContains' { $actual.IndexOf($expected, $ignoreCase) -lt 0 } - 'gt' { $null -ne $order -and $order -gt 0 } - 'ge' { $null -ne $order -and $order -ge 0 } - 'lt' { $null -ne $order -and $order -lt 0 } - 'le' { $null -ne $order -and $order -le 0 } - } - $Clause.Matched = [bool]$matched - $Clause.Matched - } - - function Test-Node { - param($Node) - switch ($Node.Type) { - 'Clause' { Test-Clause -Clause $Node.Clause } - 'And' { - # Both sides run so every clause reports what it saw - $left = Test-Node -Node $Node.Left - $right = Test-Node -Node $Node.Right - $left -and $right - } - 'Or' { - $left = Test-Node -Node $Node.Left - $right = Test-Node -Node $Node.Right - $left -or $right - } - } - } } process { @@ -139,7 +71,7 @@ function Test-IntuneAssignmentFilter { $reason = "The rule is valid: $clauseCount clause(s), $(@($parsed.Warnings).Count) warning(s)" } else { - $matched = [bool](Test-Node -Node $parsed.Tree) + $matched = [bool](Test-IslFilterNode -Node $parsed.Tree -Facts $facts) $applicable = if ($Mode -eq 'Include') { $matched } else { -not $matched } $reason = if ($Mode -eq 'Include' -and $matched) { 'Included: the rule matches this device, so the assignment applies' diff --git a/Tests/Unit/Private/Compare-IslFilterVersion.Tests.ps1 b/Tests/Unit/Private/Compare-IslFilterVersion.Tests.ps1 new file mode 100644 index 0000000..4cda7dc --- /dev/null +++ b/Tests/Unit/Private/Compare-IslFilterVersion.Tests.ps1 @@ -0,0 +1,42 @@ +#Requires -Modules @{ ModuleName = 'Pester'; ModuleVersion = '6.2.0' } + +<# + The order of two versions in an assignment filter. +#> + +BeforeAll { + $script:ModuleRoot = Split-Path -Parent (Split-Path -Parent (Split-Path -Parent $PSScriptRoot)) + if (-not (Get-Module -Name IntuneScriptLab)) { + Import-Module (Join-Path $script:ModuleRoot 'IntuneScriptLab.psd1') + } + + function script:Compare-Pair { + param([string]$Actual, [string]$Expected) + InModuleScope IntuneScriptLab -Parameters @{ Actual = $Actual; Expected = $Expected } { + @(Compare-IslFilterVersion -Actual $Actual -Expected $Expected) + } + } +} + +Describe 'Compare-IslFilterVersion' -Tag 'Unit', 'Private' { + + It 'orders against as ' -ForEach @( + @{ Actual = '10.0.26100'; Expected = '10.0.22631'; Order = 1 } + @{ Actual = '10.0.9'; Expected = '10.0.10'; Order = -1 } + @{ Actual = '10.0'; Expected = '10.0.0.0'; Order = 0 } + @{ Actual = '10.0.26100.4202'; Expected = '10.0.26100.4203'; Order = -1 } + ) { + $result = Compare-Pair -Actual $Actual -Expected $Expected + + $result | Should-BeCollection -Count 1 + $result[0] | Should-Be $Order + } + + It 'gives no order when ' -ForEach @( + @{ Case = 'the device value is not a version'; Actual = 'unknown'; Expected = '10.0' } + @{ Case = 'the rule value is not a version'; Actual = '10.0'; Expected = 'Windows 11' } + @{ Case = 'the device has no value'; Actual = ''; Expected = '10.0' } + ) { + Compare-Pair -Actual $Actual -Expected $Expected | Should-BeCollection @() + } +} diff --git a/Tests/Unit/Private/Get-IslVersionPart.Tests.ps1 b/Tests/Unit/Private/Get-IslVersionPart.Tests.ps1 new file mode 100644 index 0000000..f4c6423 --- /dev/null +++ b/Tests/Unit/Private/Get-IslVersionPart.Tests.ps1 @@ -0,0 +1,44 @@ +#Requires -Modules @{ ModuleName = 'Pester'; ModuleVersion = '6.2.0' } + +<# + A version text as the four numbers an assignment filter compares. +#> + +BeforeAll { + $script:ModuleRoot = Split-Path -Parent (Split-Path -Parent (Split-Path -Parent $PSScriptRoot)) + if (-not (Get-Module -Name IntuneScriptLab)) { + Import-Module (Join-Path $script:ModuleRoot 'IntuneScriptLab.psd1') + } + + function script:Get-Part { + param([string]$Text) + InModuleScope IntuneScriptLab -Parameters @{ Text = $Text } { @(Get-IslVersionPart -Text $Text) } + } +} + +Describe 'Get-IslVersionPart' -Tag 'Unit', 'Private' { + + It 'reads as ' -ForEach @( + @{ Text = '10'; Expected = @(10, 0, 0, 0) } + @{ Text = '10.0.26100'; Expected = @(10, 0, 26100, 0) } + @{ Text = '10.0.26100.4202'; Expected = @(10, 0, 26100, 4202) } + @{ Text = ' 6.3 '; Expected = @(6, 3, 0, 0) } + ) { + $parts = Get-Part -Text $Text + + $parts | Should-BeCollection -Count 4 + for ($index = 0; $index -lt 4; $index++) { + $parts[$index] | Should-Be ([long]$Expected[$index]) + } + } + + It 'gives nothing for ' -ForEach @( + @{ Text = '' } + @{ Text = 'Windows 11' } + @{ Text = '1.2.3.4.5' } + @{ Text = '10.0.' } + @{ Text = '-1.0' } + ) { + Get-Part -Text $Text | Should-BeCollection @() + } +} diff --git a/Tests/Unit/Private/Test-IslFilterClause.Tests.ps1 b/Tests/Unit/Private/Test-IslFilterClause.Tests.ps1 new file mode 100644 index 0000000..ae9bf61 --- /dev/null +++ b/Tests/Unit/Private/Test-IslFilterClause.Tests.ps1 @@ -0,0 +1,76 @@ +#Requires -Modules @{ ModuleName = 'Pester'; ModuleVersion = '6.2.0' } + +<# + One clause of an assignment filter against the facts of a device. The clauses are built by + the module's own parser, as Test-IntuneAssignmentFilter builds them. +#> + +BeforeAll { + $script:ModuleRoot = Split-Path -Parent (Split-Path -Parent (Split-Path -Parent $PSScriptRoot)) + if (-not (Get-Module -Name IntuneScriptLab)) { + Import-Module (Join-Path $script:ModuleRoot 'IntuneScriptLab.psd1') + } + + # The first clause of a rule, its verdict, and the clause afterwards + function script:Test-Rule { + param([string]$Rule, [hashtable]$Device) + InModuleScope IntuneScriptLab -Parameters @{ Rule = $Rule; Device = $Device } { + $facts = [System.Collections.Generic.Dictionary[string, object]]::new( + [System.StringComparer]::OrdinalIgnoreCase) + foreach ($key in $Device.Keys) { $facts[$key] = $Device[$key] } + $clause = @((ConvertFrom-IslFilterRule -Rule $Rule).Clauses)[0] + $matched = Test-IslFilterClause -Clause $clause -Facts $facts + [pscustomobject]@{ Matched = $matched; Clause = $clause } + } + } + + $script:Device = @{ + deviceName = 'LAB-PC-01'; operatingSystemVersion = '10.0.26100.4202' + manufacturer = 'Microsoft Corporation'; operatingSystemSKU = 'Enterprise' + } +} + +Describe 'Test-IslFilterClause' -Tag 'Unit', 'Private' { + + Context 'Parameter Validation' { + It 'takes only a filter clause' { + $command = InModuleScope IntuneScriptLab { Get-Command -Name Test-IslFilterClause } + $command | Should-HaveParameter -ParameterName Clause -Mandatory + $command | Should-HaveParameter -ParameterName Facts -Mandatory + $typeAttribute = [System.Management.Automation.PSTypeNameAttribute] + $clauseType = @($command.Parameters['Clause'].Attributes | Where-Object { $_ -is $typeAttribute }) + $clauseType[0].PSTypeName | Should-Be 'IntuneScriptLab.FilterClause' + } + } + + Context 'Core Functionality' { + It 'gives for ' -ForEach @( + @{ Rule = '(device.deviceName -eq "lab-pc-01")'; Expected = $true } + @{ Rule = '(device.deviceName -ne "LAB-PC-01")'; Expected = $false } + @{ Rule = '(device.deviceName -startsWith "lab-")'; Expected = $true } + @{ Rule = '(device.manufacturer -contains "microsoft")'; Expected = $true } + @{ Rule = '(device.manufacturer -notContains "Dell")'; Expected = $true } + @{ Rule = '(device.operatingSystemSKU -in ["Education", "Enterprise"])'; Expected = $true } + @{ Rule = '(device.operatingSystemSKU -notIn ["Education", "Enterprise"])'; Expected = $false } + @{ Rule = '(device.operatingSystemVersion -ge 10.0.22000.1)'; Expected = $true } + @{ Rule = '(device.operatingSystemVersion -lt 10.0.22000.1)'; Expected = $false } + @{ Rule = '(device.operatingSystemVersion -eq 10.0.26100.4202)'; Expected = $true } + ) { + (Test-Rule -Rule $Rule -Device $script:Device).Matched | Should-Be $Expected + } + + It 'records on the clause what the device had and whether it matched' { + $result = Test-Rule -Rule '(device.deviceName -eq "OTHER")' -Device $script:Device + + $result.Clause.Actual | Should-Be 'LAB-PC-01' + $result.Clause.Matched | Should-BeFalse + } + + It 'compares a property the device does not have as an empty text, and records no value' { + $result = Test-Rule -Rule '(device.deviceName -eq "LAB-PC-01")' -Device @{} + + $result.Matched | Should-BeFalse + $result.Clause.Actual | Should-BeNull + } + } +} diff --git a/Tests/Unit/Private/Test-IslFilterNode.Tests.ps1 b/Tests/Unit/Private/Test-IslFilterNode.Tests.ps1 new file mode 100644 index 0000000..aa27676 --- /dev/null +++ b/Tests/Unit/Private/Test-IslFilterNode.Tests.ps1 @@ -0,0 +1,55 @@ +#Requires -Modules @{ ModuleName = 'Pester'; ModuleVersion = '6.2.0' } + +<# + The tree of a parsed assignment filter rule against the facts of a device. +#> + +BeforeAll { + $script:ModuleRoot = Split-Path -Parent (Split-Path -Parent (Split-Path -Parent $PSScriptRoot)) + if (-not (Get-Module -Name IntuneScriptLab)) { + Import-Module (Join-Path $script:ModuleRoot 'IntuneScriptLab.psd1') + } + + # The verdict of a whole rule, and the Matched value of each of its clauses + function script:Test-Tree { + param([string]$Rule) + InModuleScope IntuneScriptLab -Parameters @{ Rule = $Rule } { + $facts = [System.Collections.Generic.Dictionary[string, object]]::new( + [System.StringComparer]::OrdinalIgnoreCase) + $facts['deviceName'] = 'LAB-PC-01' + $facts['manufacturer'] = 'Microsoft Corporation' + $parsed = ConvertFrom-IslFilterRule -Rule $Rule + $matched = Test-IslFilterNode -Node $parsed.Tree -Facts $facts + [pscustomobject]@{ Matched = $matched; Clauses = @($parsed.Clauses | ForEach-Object { $_.Matched }) } + } + } +} + +Describe 'Test-IslFilterNode' -Tag 'Unit', 'Private' { + + It 'gives for ' -ForEach @( + @{ Rule = '(device.deviceName -eq "LAB-PC-01")'; Expected = $true } + @{ Rule = '(device.deviceName -eq "LAB-PC-01") and (device.manufacturer -eq "Dell")'; Expected = $false } + @{ Rule = '(device.deviceName -eq "X") or (device.manufacturer -startsWith "Micro")'; Expected = $true } + @{ Rule = '(device.deviceName -eq "X") or (device.manufacturer -eq "Dell")'; Expected = $false } + ) { + (Test-Tree -Rule $Rule).Matched | Should-Be $Expected + } + + It 'tests both sides of and, so every clause records what it saw' { + $result = Test-Tree -Rule '(device.deviceName -eq "X") and (device.manufacturer -startsWith "Microsoft")' + + $result.Matched | Should-BeFalse + $result.Clauses | Should-BeCollection -Count 2 + $result.Clauses[0] | Should-BeFalse + $result.Clauses[1] | Should-BeTrue + } + + It 'tests both sides of or as well' { + $result = Test-Tree -Rule '(device.deviceName -eq "LAB-PC-01") or (device.manufacturer -eq "Dell")' + + $result.Matched | Should-BeTrue + $result.Clauses | Should-BeCollection -Count 2 + $result.Clauses[1] | Should-BeFalse + } +}