test: remove vacuous diagnostic assertions
This commit is contained in:
parent
5fa9933636
commit
ea17bc8624
5 changed files with 151 additions and 28 deletions
|
|
@ -8,7 +8,9 @@
|
|||
records test attributes and traits, static skips, empty-return sites,
|
||||
prerequisite gates reached through same-file helper calls, assertion/throw
|
||||
signals (including same-file helper calls), diagnostic output signals,
|
||||
wall-clock waits, environment-variable dependencies, and source-text reads.
|
||||
high-confidence constant-truth assertions, syntactic self-comparison
|
||||
candidates, wall-clock waits, environment-variable dependencies, and
|
||||
source-text reads.
|
||||
|
||||
This is a candidate generator, not a semantic proof. In particular, an
|
||||
output-only candidate can call a failure-capable helper declared in another
|
||||
|
|
@ -182,6 +184,7 @@ function Test-DirectOutputSignal {
|
|||
})) {
|
||||
$expression = $invocation.Expression.ToString()
|
||||
if ($expression -match '(^|\.)(_?out|output|Console|Debug|Trace)\.(Write|WriteLine)$' -or
|
||||
$expression -match '(^|\.)File\.(WriteAllText|WriteAllLines|WriteAllBytes|AppendAllText|AppendAllLines)$' -or
|
||||
$expression -match '(^|\.)(WriteDiagnostic|Dump|DumpSteps|Print)(\.|$)') {
|
||||
return $true
|
||||
}
|
||||
|
|
@ -189,6 +192,60 @@ function Test-DirectOutputSignal {
|
|||
return $false
|
||||
}
|
||||
|
||||
function Get-AssertionAuditSites {
|
||||
param(
|
||||
[Parameter(Mandatory)]$Tree,
|
||||
[Parameter(Mandatory)]$Method
|
||||
)
|
||||
|
||||
$sites = [Collections.Generic.List[object]]::new()
|
||||
foreach ($invocation in @($Method.DescendantNodes() | Where-Object {
|
||||
$_.GetType().Name -eq 'InvocationExpressionSyntax'
|
||||
})) {
|
||||
$expression = $invocation.Expression.ToString()
|
||||
$arguments = @($invocation.ArgumentList.Arguments)
|
||||
if ($arguments.Count -eq 0) {
|
||||
continue
|
||||
}
|
||||
|
||||
$first = $arguments[0].Expression.ToString()
|
||||
$constantTruthKind = if ($expression -match '(^|\.)Assert\.True$' -and
|
||||
$first -ceq 'true') {
|
||||
'Assert.True(true)'
|
||||
} elseif ($expression -match '(^|\.)Assert\.False$' -and
|
||||
$first -ceq 'false') {
|
||||
'Assert.False(false)'
|
||||
} else {
|
||||
$null
|
||||
}
|
||||
if ($null -ne $constantTruthKind) {
|
||||
$sites.Add([ordered]@{
|
||||
Line = Get-NodeLine $Tree $invocation
|
||||
Category = 'ConstantTruth'
|
||||
Kind = $constantTruthKind
|
||||
Invocation = [regex]::Replace($invocation.ToString(), '\s+', ' ').Trim()
|
||||
})
|
||||
}
|
||||
|
||||
if ($arguments.Count -lt 2 -or
|
||||
$expression -notmatch '(^|\.)Assert\.(Equal|StrictEqual|Same|NotEqual|NotSame)$') {
|
||||
continue
|
||||
}
|
||||
|
||||
$second = $arguments[1].Expression.ToString()
|
||||
if ($first -cne $second) {
|
||||
continue
|
||||
}
|
||||
$sites.Add([ordered]@{
|
||||
Line = Get-NodeLine $Tree $invocation
|
||||
Category = 'SelfComparison'
|
||||
Kind = "Assert.$($expression.Split('.')[-1])"
|
||||
Invocation = [regex]::Replace($invocation.ToString(), '\s+', ' ').Trim()
|
||||
})
|
||||
}
|
||||
return @($sites)
|
||||
}
|
||||
|
||||
function Get-WaitSites {
|
||||
param(
|
||||
[Parameter(Mandatory)]$Tree,
|
||||
|
|
@ -416,6 +473,7 @@ foreach ($relativePath in $trackedFiles) {
|
|||
|
||||
$bodyText = $method.ToString()
|
||||
$waitSites = @(Get-WaitSites $tree $method)
|
||||
$assertionAuditSites = @(Get-AssertionAuditSites $tree $method)
|
||||
$environmentVariables = @([regex]::Matches(
|
||||
$bodyText,
|
||||
'GetEnvironmentVariable\s*\(\s*"([A-Za-z0-9_]+)"') |
|
||||
|
|
@ -442,6 +500,12 @@ foreach ($relativePath in $trackedFiles) {
|
|||
HasFailureSignal = $hasFailureSignal
|
||||
HasOutputSignal = $hasOutputSignal
|
||||
OutputOnlyCandidate = $hasOutputSignal -and -not $hasFailureSignal
|
||||
ConstantTruthAssertions = @($assertionAuditSites | Where-Object {
|
||||
$_.Category -eq 'ConstantTruth'
|
||||
})
|
||||
SelfComparisonAssertions = @($assertionAuditSites | Where-Object {
|
||||
$_.Category -eq 'SelfComparison'
|
||||
})
|
||||
EnvironmentVariables = $environmentVariables
|
||||
WaitSites = $waitSites
|
||||
HasThreadSleep = @($waitSites | Where-Object {
|
||||
|
|
@ -543,6 +607,16 @@ $summary = [ordered]@{
|
|||
$_.HelperPrerequisiteReturnCandidate
|
||||
}).Count
|
||||
OutputOnlyCandidates = @($orderedRecords | Where-Object { $_.OutputOnlyCandidate }).Count
|
||||
ConstantTruthAssertionMethods = @($orderedRecords | Where-Object {
|
||||
$_.ConstantTruthAssertions.Count -gt 0
|
||||
}).Count
|
||||
ConstantTruthAssertionSites = @($orderedRecords |
|
||||
ForEach-Object { $_.ConstantTruthAssertions }).Count
|
||||
SelfComparisonAssertionMethods = @($orderedRecords | Where-Object {
|
||||
$_.SelfComparisonAssertions.Count -gt 0
|
||||
}).Count
|
||||
SelfComparisonAssertionSites = @($orderedRecords |
|
||||
ForEach-Object { $_.SelfComparisonAssertions }).Count
|
||||
DiagnosticMethods = @($orderedRecords | Where-Object {
|
||||
$_.Traits -contains 'Purpose, Diagnostic'
|
||||
}).Count
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue