|
| 1 | +# Pester tests for ILVerify helper functions defined in ilverify.ps1. |
| 2 | +# Compatible with Pester 3.x+ (ships with Windows PowerShell). |
| 3 | +# Run with: Invoke-Pester ./tests/ILVerify/ilverify.Tests.ps1 |
| 4 | + |
| 5 | +# Extract and load only the function definitions from ilverify.ps1 |
| 6 | +# without executing the build/verification logic. |
| 7 | +$scriptContent = Get-Content "$PSScriptRoot/ilverify.ps1" -Raw |
| 8 | +$ast = [System.Management.Automation.Language.Parser]::ParseInput($scriptContent, [ref]$null, [ref]$null) |
| 9 | +$functions = $ast.FindAll({ $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] }, $false) |
| 10 | +foreach ($fn in $functions) { |
| 11 | + Invoke-Expression $fn.Extent.Text |
| 12 | +} |
| 13 | + |
| 14 | +Describe "Normalize-IlverifyOutputLine" { |
| 15 | + It "removes closure suffixes" { |
| 16 | + Normalize-IlverifyOutputLine 'Foo+clo@924-516::Invoke()' | Should Be 'Foo+clo::Invoke()' |
| 17 | + } |
| 18 | + |
| 19 | + It "removes function suffixes with line numbers" { |
| 20 | + Normalize-IlverifyOutputLine 'parseOption@269::Bar()' | Should Be 'parseOption::Bar()' |
| 21 | + } |
| 22 | + |
| 23 | + It "removes 'at line NNNN'" { |
| 24 | + Normalize-IlverifyOutputLine 'something at line 1234 rest' | Should Be 'something rest' |
| 25 | + } |
| 26 | + |
| 27 | + It "removes pipe stage patterns" { |
| 28 | + Normalize-IlverifyOutputLine 'Foo+Pipe #1 stage #1 at line 1782@1782::Invoke()' | Should Be 'Foo+::Invoke()' |
| 29 | + } |
| 30 | + |
| 31 | + It "collapses multiple spaces" { |
| 32 | + Normalize-IlverifyOutputLine 'a b c' | Should Be 'a b c' |
| 33 | + } |
| 34 | + |
| 35 | + It "trims leading and trailing whitespace" { |
| 36 | + Normalize-IlverifyOutputLine ' hello world ' | Should Be 'hello world' |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +Describe "Remove-IlverifyOffsets" { |
| 41 | + It "strips a single offset from an error line" { |
| 42 | + $line = "[IL]: Error [StackByRef]: : Foo::Bar(int32)][offset 0x0000001E][found Native Int] Expected ByRef." |
| 43 | + $result = Remove-IlverifyOffsets @($line) |
| 44 | + $result | Should Be "[IL]: Error [StackByRef]: : Foo::Bar(int32)][found Native Int] Expected ByRef." |
| 45 | + } |
| 46 | + |
| 47 | + It "handles uppercase and lowercase hex digits" { |
| 48 | + $result = Remove-IlverifyOffsets @("prefix [offset 0xABcd0012] suffix") |
| 49 | + $result | Should Be "prefix suffix" |
| 50 | + } |
| 51 | + |
| 52 | + It "trims trailing whitespace" { |
| 53 | + $result = Remove-IlverifyOffsets @("some text ") |
| 54 | + $result | Should Be "some text" |
| 55 | + } |
| 56 | + |
| 57 | + It "returns empty array for empty input" { |
| 58 | + $result = @(Remove-IlverifyOffsets @()) |
| 59 | + $result.Count | Should Be 0 |
| 60 | + } |
| 61 | + |
| 62 | + It "processes multiple lines" { |
| 63 | + $lines = @( |
| 64 | + "[IL]: Error [X]: : A::M()][offset 0x00000011][found Y] Msg1.", |
| 65 | + "[IL]: Error [X]: : B::N()][offset 0x00000022][found Z] Msg2." |
| 66 | + ) |
| 67 | + $result = Remove-IlverifyOffsets $lines |
| 68 | + $result.Count | Should Be 2 |
| 69 | + $result[0] | Should Be "[IL]: Error [X]: : A::M()][found Y] Msg1." |
| 70 | + $result[1] | Should Be "[IL]: Error [X]: : B::N()][found Z] Msg2." |
| 71 | + } |
| 72 | + |
| 73 | + It "passes through lines without offsets unchanged" { |
| 74 | + $result = Remove-IlverifyOffsets @("no offsets here") |
| 75 | + $result | Should Be "no offsets here" |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +Describe "Soft comparison (offset-tolerant)" { |
| 80 | + It "matches when only IL offsets differ" { |
| 81 | + $output = @( |
| 82 | + "[IL]: Error [StackByRef]: : Foo::Bar()][offset 0x0000001E][found Native Int] Expected ByRef.", |
| 83 | + "[IL]: Error [ReturnPtrToStack]: : Baz::Qux()][offset 0x00000070] Return type is ByRef." |
| 84 | + ) |
| 85 | + $baseline = @( |
| 86 | + "[IL]: Error [StackByRef]: : Foo::Bar()][offset 0x0000001A][found Native Int] Expected ByRef.", |
| 87 | + "[IL]: Error [ReturnPtrToStack]: : Baz::Qux()][offset 0x00000064] Return type is ByRef." |
| 88 | + ) |
| 89 | + $cmp = Compare-Object (Remove-IlverifyOffsets $output) (Remove-IlverifyOffsets $baseline) |
| 90 | + $cmp | Should BeNullOrEmpty |
| 91 | + } |
| 92 | + |
| 93 | + It "detects real differences even when offsets also differ" { |
| 94 | + $output = @( |
| 95 | + "[IL]: Error [StackByRef]: : Foo::Bar()][offset 0x0000001E][found Native Int] Expected ByRef.", |
| 96 | + "[IL]: Error [NEW_ERROR]: : New::Method()][offset 0x00000099][found X] New error." |
| 97 | + ) |
| 98 | + $baseline = @( |
| 99 | + "[IL]: Error [StackByRef]: : Foo::Bar()][offset 0x0000001A][found Native Int] Expected ByRef.", |
| 100 | + "[IL]: Error [ReturnPtrToStack]: : Baz::Qux()][offset 0x00000064] Return type is ByRef." |
| 101 | + ) |
| 102 | + $cmp = Compare-Object (Remove-IlverifyOffsets $output) (Remove-IlverifyOffsets $baseline) |
| 103 | + $cmp | Should Not BeNullOrEmpty |
| 104 | + } |
| 105 | + |
| 106 | + It "detects added errors" { |
| 107 | + $output = @( |
| 108 | + "[IL]: Error [X]: : Foo::A()][offset 0x00000011] Msg.", |
| 109 | + "[IL]: Error [X]: : Foo::B()][offset 0x00000022] Msg.", |
| 110 | + "[IL]: Error [X]: : Foo::C()][offset 0x00000033] New." |
| 111 | + ) |
| 112 | + $baseline = @( |
| 113 | + "[IL]: Error [X]: : Foo::A()][offset 0x00000011] Msg.", |
| 114 | + "[IL]: Error [X]: : Foo::B()][offset 0x00000022] Msg." |
| 115 | + ) |
| 116 | + $cmp = Compare-Object (Remove-IlverifyOffsets $output) (Remove-IlverifyOffsets $baseline) |
| 117 | + $cmp | Should Not BeNullOrEmpty |
| 118 | + } |
| 119 | + |
| 120 | + It "detects removed errors" { |
| 121 | + $output = @( |
| 122 | + "[IL]: Error [X]: : Foo::A()][offset 0x00000011] Msg." |
| 123 | + ) |
| 124 | + $baseline = @( |
| 125 | + "[IL]: Error [X]: : Foo::A()][offset 0x00000011] Msg.", |
| 126 | + "[IL]: Error [X]: : Foo::B()][offset 0x00000022] Msg." |
| 127 | + ) |
| 128 | + $cmp = Compare-Object (Remove-IlverifyOffsets $output) (Remove-IlverifyOffsets $baseline) |
| 129 | + $cmp | Should Not BeNullOrEmpty |
| 130 | + } |
| 131 | + |
| 132 | + It "handles trailing whitespace differences between output and baseline" { |
| 133 | + $output = @("[IL]: Error [X]: : Foo::A()][offset 0x00000011] Msg. ") |
| 134 | + $baseline = @("[IL]: Error [X]: : Foo::A()][offset 0x000000FF] Msg.") |
| 135 | + $cmp = Compare-Object (Remove-IlverifyOffsets $output) (Remove-IlverifyOffsets $baseline) |
| 136 | + $cmp | Should BeNullOrEmpty |
| 137 | + } |
| 138 | +} |
0 commit comments