-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7e1a7ce
commit ccdac27
Showing
11 changed files
with
350 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
if (-not (Get-Module ILAssembler -ea Ignore)) { | ||
$projectBase = $PSScriptRoot | Split-Path | ||
$psd1File = (Get-ChildItem $projectBase/Release/ILAssembler/*/ILAssembler.psd1).FullName | ||
|
||
Import-Module $psd1File -Global | ||
} | ||
|
||
function GetBodyAsShouldOperator { | ||
param( | ||
[Parameter(ValueFromPipeline)] | ||
[System.Delegate] $Delegate | ||
) | ||
process { | ||
$bytes = $Delegate | GetResolver | GetResolverField m_code | ||
'| Should -HaveBody {0}' -f ( | ||
$( | ||
if ($bytes.Length -gt 1) { '('} | ||
$bytes.ForEach{ '0x{0:X2}' -f $PSItem } -join ', ' | ||
if ($bytes.Length -gt 1) { ')'} | ||
) -join '') | ||
} | ||
} | ||
|
||
function GetResolver { | ||
param( | ||
[Parameter(ValueFromPipeline)] | ||
[Delegate] $Delegate | ||
) | ||
process { | ||
$dynamicMethod = $Delegate.Method.GetType(). | ||
GetField('m_owner', 60). | ||
GetValue($Delegate.Method) | ||
|
||
$dynamicMethod.GetType(). | ||
GetField('m_resolver', 60). | ||
GetValue($dynamicMethod) | ||
} | ||
} | ||
|
||
function GetResolverField { | ||
param( | ||
[string] $FieldName, | ||
|
||
[Parameter(ValueFromPipeline)] | ||
[object] $Resolver | ||
) | ||
process { | ||
,$Resolver.GetType(). | ||
GetField($FieldName, 60). | ||
GetValue($Resolver) | ||
} | ||
} | ||
|
||
function ShouldHaveBody { | ||
param( | ||
$ActualValue, | ||
$ExpectedValue | ||
) | ||
end { | ||
|
||
function GetFailureMessage { | ||
param([string] $Reason) | ||
$failureMessage = "Expected method body to match, but $Reason." + | ||
[System.Environment]::NewLine + [System.Environment]::NewLine + | ||
'Expected: {0}' + [System.Environment]::NewLine + | ||
'Actual: {1}' | ||
|
||
return $failureMessage -f ( | ||
($ExpectedValue.ForEach{ '0x{0:X2}' -f $PSItem } -join ', '), | ||
($code.ForEach{ '0x{0:X2}' -f $PSItem } -join ', ')) | ||
} | ||
|
||
$ExpectedValue = [byte[]]$ExpectedValue | ||
$code = [byte[]]($ActualValue | GetResolver | GetResolverField m_code) | ||
|
||
if ($ExpectedValue.Length -ne $code.Length) { | ||
return [PSCustomObject]@{ | ||
Succeeded = $false | ||
FailureMessage = GetFailureMessage 'the byte count did not match' | ||
} | ||
} | ||
|
||
for ($i = 0; $i -lt $code.Length; $i++) { | ||
if ($ExpectedValue[$i] -ne $code[$i]) { | ||
return [PSCustomObject]@{ | ||
Succeeded = $false | ||
FailureMessage = GetFailureMessage "the byte at offset $i did not match" | ||
} | ||
} | ||
} | ||
|
||
return [PSCustomObject]@{ | ||
Succeeded = $true | ||
FailureMessage = [string]::Empty | ||
} | ||
} | ||
} | ||
|
||
$pesterModule = Get-Module Pester -ErrorAction Ignore | ||
if (-not $pesterModule) { | ||
$pesterModule = Import-Module Pester -ErrorAction Stop -PassThru -Global | ||
} | ||
|
||
if (-not (& $pesterModule { $AssertionOperators.ContainsKey('HaveBody') })) { | ||
Add-ShouldOperator -Name HaveBody -Test $function:ShouldHaveBody | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
Describe 'Delegate signatures' { | ||
BeforeAll { | ||
. "$($PSCommandPath | Split-Path)/Common.ps1" | ||
} | ||
|
||
It 'Array expression for no parameters' { | ||
$d = il { [int]@() } { | ||
ldc.i4.0 | ||
ret | ||
} | ||
|
||
$d | Should -BeOfType ([Func[int]]) | ||
} | ||
|
||
It 'Parameters in paren expression' { | ||
$d = il { [int]([int], [string]) } { | ||
ldc.i4.0 | ||
ret | ||
} | ||
|
||
$d | Should -BeOfType ([Func[int, string, int]]) | ||
} | ||
|
||
It 'Parameters in array expression' { | ||
$d = il { [int]@([object], [string]) } { | ||
ldc.i4.0 | ||
ret | ||
} | ||
|
||
$d | Should -BeOfType ([Func[object, string, int]]) | ||
} | ||
|
||
It 'Named parameters' { | ||
$d = il { [int]([object] $instance, [string] $name) } { | ||
ldc.i4.0 | ||
ret | ||
} | ||
|
||
$d | Should -BeOfType ([Func[object, string, int]]) | ||
} | ||
|
||
It 'Invoke member expression syntax' { | ||
$d = il { [int] $_._([object] $instance, [string] $name) } { | ||
ldc.i4.0 | ||
ret | ||
} | ||
|
||
$d | Should -BeOfType ([Func[object, string, int]]) | ||
} | ||
|
||
It 'Explicit delegate type' { | ||
$d = il ([Action[int]]) { | ||
ret | ||
} | ||
|
||
$d | Should -BeOfType ([Action[int]]) | ||
} | ||
|
||
It 'Explicit non-standard delegate type' { | ||
$d = New-IlDelegate ([System.ResolveEventHandler]) { | ||
ret | ||
} | ||
|
||
$d | Should -BeOfType ([System.ResolveEventHandler]) | ||
$method = $d.GetType().GetMethod('Invoke') | ||
$method.ReturnType | Should -Be ([System.Reflection.Assembly]) | ||
$method.GetParameters().ParameterType | Should -Be ([object], [System.ResolveEventArgs]) | ||
} | ||
|
||
It 'Generates delegate type when parameters cannot be generics' { | ||
$d = il { [bool]([int], [ref] [string], [int+]) } { | ||
ldc.i4.0 | ||
ret | ||
} | ||
|
||
$method = $d.GetType().GetMethod('Invoke') | ||
$method.ReturnType | Should -Be ([bool]) | ||
$method.GetParameters().ParameterType | Should -Be ( | ||
[int], | ||
[string].MakeByRefType(), | ||
[int].MakePointerType()) | ||
} | ||
} |
Oops, something went wrong.