Skip to content

Commit b9c1ab1

Browse files
Compare effective scalar tags in formatter corpus
Resolve every formatter scalar tag canonically while retaining explicit and non-specific tag state and ignoring presentation style. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 73e1e32 commit b9c1ab1

2 files changed

Lines changed: 95 additions & 7 deletions

File tree

tests/Conformance.Tests.ps1

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,67 @@ Describe 'Released yaml-test-suite corpus accounting' {
249249
$nonSpecificEvents | Should -Contain '=VAL|nonSpecificTag=true|value=value'
250250
}
251251

252+
It 'distinguishes implicit scalar tags without treating style as semantic' {
253+
foreach ($mutation in @(
254+
@{ Plain = 'true'; Quoted = '"true"'; Tag = 'tag:yaml.org,2002:bool' }
255+
@{ Plain = '42'; Quoted = '"42"'; Tag = 'tag:yaml.org,2002:int' }
256+
@{ Plain = '1.5'; Quoted = '"1.5"'; Tag = 'tag:yaml.org,2002:float' }
257+
@{ Plain = '.inf'; Quoted = '".inf"'; Tag = 'tag:yaml.org,2002:float' }
258+
@{ Plain = 'null'; Quoted = '"null"'; Tag = 'tag:yaml.org,2002:null' }
259+
)) {
260+
$plain = Invoke-InYamlModule -ScriptBlock $readYamlSuiteRepresentation `
261+
-Arguments @($mutation.Plain)
262+
$quoted = Invoke-InYamlModule -ScriptBlock $readYamlSuiteRepresentation `
263+
-Arguments @($mutation.Quoted)
264+
$plainEvents = ConvertTo-YamlSuiteActualEvent -Documents $plain.Value `
265+
-IncludeEffectiveScalarTags
266+
$quotedEvents = ConvertTo-YamlSuiteActualEvent -Documents $quoted.Value `
267+
-IncludeEffectiveScalarTags
268+
269+
(Compare-YamlSuiteCanonicalList -Left $plainEvents -Right $quotedEvents) |
270+
Should -BeFalse
271+
$plainEvents | Should -Contain (
272+
'=VAL|tag={0}|value={1}' -f $mutation.Tag, $mutation.Plain
273+
)
274+
$quotedEvents | Should -Contain (
275+
'=VAL|tag=tag:yaml.org,2002:str|value={0}' -f $mutation.Plain
276+
)
277+
}
278+
279+
$singleQuoted = Invoke-InYamlModule -ScriptBlock $readYamlSuiteRepresentation `
280+
-Arguments @("'value'")
281+
$doubleQuoted = Invoke-InYamlModule -ScriptBlock $readYamlSuiteRepresentation `
282+
-Arguments @('"value"')
283+
$singleEvents = ConvertTo-YamlSuiteActualEvent -Documents $singleQuoted.Value `
284+
-IncludeEffectiveScalarTags
285+
$doubleEvents = ConvertTo-YamlSuiteActualEvent -Documents $doubleQuoted.Value `
286+
-IncludeEffectiveScalarTags
287+
288+
(Compare-YamlSuiteCanonicalList -Left $singleEvents -Right $doubleEvents) |
289+
Should -BeTrue
290+
291+
$explicit = Invoke-InYamlModule -ScriptBlock $readYamlSuiteRepresentation `
292+
-Arguments @('!!str value')
293+
$unknown = Invoke-InYamlModule -ScriptBlock $readYamlSuiteRepresentation `
294+
-Arguments @('!local value')
295+
$nonSpecific = Invoke-InYamlModule -ScriptBlock $readYamlSuiteRepresentation `
296+
-Arguments @('! value')
297+
$explicitEvents = ConvertTo-YamlSuiteActualEvent -Documents $explicit.Value `
298+
-IncludeEffectiveScalarTags
299+
$unknownEvents = ConvertTo-YamlSuiteActualEvent -Documents $unknown.Value `
300+
-IncludeEffectiveScalarTags
301+
$nonSpecificEvents = ConvertTo-YamlSuiteActualEvent -Documents $nonSpecific.Value `
302+
-IncludeEffectiveScalarTags
303+
304+
$explicitEvents |
305+
Should -Contain '=VAL|tag=tag:yaml.org,2002:str|explicitTag=true|value=value'
306+
$unknownEvents | Should -Contain '=VAL|tag=!local|explicitTag=true|value=value'
307+
$nonSpecificEvents |
308+
Should -Contain '=VAL|tag=tag:yaml.org,2002:str|nonSpecificTag=true|value=value'
309+
(Compare-YamlSuiteCanonicalList -Left $explicitEvents -Right $doubleEvents) |
310+
Should -BeFalse
311+
}
312+
252313
It 'detects altered ordered-map and alias semantics in out.yaml' {
253314
$mutatedSuitePath = Join-Path $TestDrive 'mutated-out-cases'
254315
$null = New-Item -Path $mutatedSuitePath -ItemType Directory -Force

tests/tools/Invoke-YamlTestSuite.ps1

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -632,12 +632,29 @@ function ConvertFrom-YamlSuiteEventText {
632632
[string[]] $canonical.ToArray()
633633
}
634634

635+
function Get-YamlSuiteScalarEffectiveTag {
636+
[OutputType([string])]
637+
param (
638+
[Parameter(Mandatory)]
639+
[pscustomobject] $Node
640+
)
641+
642+
Invoke-InYamlModule -ScriptBlock {
643+
param ([pscustomobject] $ScalarNode)
644+
645+
$resolved = Resolve-YamlScalar -Node $ScalarNode
646+
Get-YamlEffectiveTag -Node $ScalarNode -Value $resolved.Value
647+
} -Arguments @($Node)
648+
}
649+
635650
function ConvertTo-YamlSuiteActualEvent {
636651
[OutputType([string[]])]
637652
param (
638653
[Parameter(Mandatory)]
639654
[AllowEmptyCollection()]
640-
[object[]] $Documents
655+
[object[]] $Documents,
656+
657+
[switch] $IncludeEffectiveScalarTags
641658
)
642659

643660
function ConvertTo-YamlSuiteEventEscapedText {
@@ -688,10 +705,19 @@ function ConvertTo-YamlSuiteActualEvent {
688705
if ($node.Kind -eq 'Scalar') {
689706
$parts = [System.Collections.Generic.List[string]]::new()
690707
$parts.Add('=VAL')
691-
if ([string]::IsNullOrEmpty($node.Tag) -and $node.HasUnknownTag) {
692-
$parts.Add('nonSpecificTag=true')
693-
} elseif ($node.Tag) {
694-
$parts.Add("tag=$($node.Tag)")
708+
if ($IncludeEffectiveScalarTags) {
709+
$parts.Add("tag=$(Get-YamlSuiteScalarEffectiveTag -Node $node)")
710+
if (-not [string]::IsNullOrEmpty($node.Tag)) {
711+
$parts.Add('explicitTag=true')
712+
} elseif ($node.HasUnknownTag) {
713+
$parts.Add('nonSpecificTag=true')
714+
}
715+
} else {
716+
if ([string]::IsNullOrEmpty($node.Tag) -and $node.HasUnknownTag) {
717+
$parts.Add('nonSpecificTag=true')
718+
} elseif ($node.Tag) {
719+
$parts.Add("tag=$($node.Tag)")
720+
}
695721
}
696722
if ($node.Anchor) {
697723
$parts.Add("anchor=$(
@@ -1274,9 +1300,10 @@ foreach ($inputFile in $inputFiles) {
12741300
} else {
12751301
$formattedRepresentation = Invoke-InYamlModule `
12761302
-ScriptBlock $readYamlSuiteRepresentation -Arguments @($formatterText)
1277-
$originalEvents = ConvertTo-YamlSuiteActualEvent -Documents $representation.Value
1303+
$originalEvents = ConvertTo-YamlSuiteActualEvent -Documents $representation.Value `
1304+
-IncludeEffectiveScalarTags
12781305
$formattedEvents = ConvertTo-YamlSuiteActualEvent `
1279-
-Documents $formattedRepresentation.Value
1306+
-Documents $formattedRepresentation.Value -IncludeEffectiveScalarTags
12801307
$formatterExpected = $originalEvents -join "`n"
12811308
$formatterActual = $formattedEvents -join "`n"
12821309
if (-not (

0 commit comments

Comments
 (0)