|
| 1 | +if (-not (Get-Module ILAssembler -ea Ignore)) { |
| 2 | + $projectBase = $PSScriptRoot | Split-Path |
| 3 | + $psd1File = (Get-ChildItem $projectBase/Release/ILAssembler/*/ILAssembler.psd1).FullName |
| 4 | + |
| 5 | + Import-Module $psd1File -Global |
| 6 | +} |
| 7 | + |
| 8 | +function GetBodyAsShouldOperator { |
| 9 | + param( |
| 10 | + [Parameter(ValueFromPipeline)] |
| 11 | + [System.Delegate] $Delegate |
| 12 | + ) |
| 13 | + process { |
| 14 | + $bytes = $Delegate | GetResolver | GetResolverField m_code |
| 15 | + '| Should -HaveBody {0}' -f ( |
| 16 | + $( |
| 17 | + if ($bytes.Length -gt 1) { '('} |
| 18 | + $bytes.ForEach{ '0x{0:X2}' -f $PSItem } -join ', ' |
| 19 | + if ($bytes.Length -gt 1) { ')'} |
| 20 | + ) -join '') |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +function GetResolver { |
| 25 | + param( |
| 26 | + [Parameter(ValueFromPipeline)] |
| 27 | + [Delegate] $Delegate |
| 28 | + ) |
| 29 | + process { |
| 30 | + $dynamicMethod = $Delegate.Method.GetType(). |
| 31 | + GetField('m_owner', 60). |
| 32 | + GetValue($Delegate.Method) |
| 33 | + |
| 34 | + $dynamicMethod.GetType(). |
| 35 | + GetField('m_resolver', 60). |
| 36 | + GetValue($dynamicMethod) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +function GetResolverField { |
| 41 | + param( |
| 42 | + [string] $FieldName, |
| 43 | + |
| 44 | + [Parameter(ValueFromPipeline)] |
| 45 | + [object] $Resolver |
| 46 | + ) |
| 47 | + process { |
| 48 | + ,$Resolver.GetType(). |
| 49 | + GetField($FieldName, 60). |
| 50 | + GetValue($Resolver) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +function ShouldHaveBody { |
| 55 | + param( |
| 56 | + $ActualValue, |
| 57 | + $ExpectedValue |
| 58 | + ) |
| 59 | + end { |
| 60 | + |
| 61 | + function GetFailureMessage { |
| 62 | + param([string] $Reason) |
| 63 | + $failureMessage = "Expected method body to match, but $Reason." + |
| 64 | + [System.Environment]::NewLine + [System.Environment]::NewLine + |
| 65 | + 'Expected: {0}' + [System.Environment]::NewLine + |
| 66 | + 'Actual: {1}' |
| 67 | + |
| 68 | + return $failureMessage -f ( |
| 69 | + ($ExpectedValue.ForEach{ '0x{0:X2}' -f $PSItem } -join ', '), |
| 70 | + ($code.ForEach{ '0x{0:X2}' -f $PSItem } -join ', ')) |
| 71 | + } |
| 72 | + |
| 73 | + $ExpectedValue = [byte[]]$ExpectedValue |
| 74 | + $code = [byte[]]($ActualValue | GetResolver | GetResolverField m_code) |
| 75 | + |
| 76 | + if ($ExpectedValue.Length -ne $code.Length) { |
| 77 | + return [PSCustomObject]@{ |
| 78 | + Succeeded = $false |
| 79 | + FailureMessage = GetFailureMessage 'the byte count did not match' |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + for ($i = 0; $i -lt $code.Length; $i++) { |
| 84 | + if ($ExpectedValue[$i] -ne $code[$i]) { |
| 85 | + return [PSCustomObject]@{ |
| 86 | + Succeeded = $false |
| 87 | + FailureMessage = GetFailureMessage "the byte at offset $i did not match" |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return [PSCustomObject]@{ |
| 93 | + Succeeded = $true |
| 94 | + FailureMessage = [string]::Empty |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +$pesterModule = Get-Module Pester -ErrorAction Ignore |
| 100 | +if (-not $pesterModule) { |
| 101 | + $pesterModule = Import-Module Pester -ErrorAction Stop -PassThru -Global |
| 102 | +} |
| 103 | + |
| 104 | +if (-not (& $pesterModule { $AssertionOperators.ContainsKey('HaveBody') })) { |
| 105 | + Add-ShouldOperator -Name HaveBody -Test $function:ShouldHaveBody |
| 106 | +} |
0 commit comments