Files
MDHelper/tests/word_revision_055_smoke.ps1
T

72 lines
4.0 KiB
PowerShell

param([string]$Python = (Join-Path $PSScriptRoot '..\mdhelper_env\mdhelper_env\Scripts\python.exe'))
$ErrorActionPreference='Stop'
Push-Location (Join-Path $PSScriptRoot '..')
$word=$null
try {
$folder = (@'
import sys,tempfile
from pathlib import Path
sys.path.insert(0,str(Path('tests').resolve()))
from test_style_revision_055 import *
folder=Path(tempfile.mkdtemp(prefix='MDHelper-055-Word-'))
for engine in ENGINES:
for name,source,rules in [('code',CODE_SOURCE,CODE_RULES),
('ordered',ORDERED,{'list_ordered':{'font_color':'#ff0000'},'list_bullet':{'font_color':'#0000ff'}}),
('task',TASK,{'list_task':{'font_color':'#ff0000'},'list_bullet':{'font_color':'#0000ff'}}),
('quoted',QUOTED,normalize_payload({'css':MARGINS})['rules']),
('hanging','- Outer\n\n- Second',normalize_payload({'css':MIXED})['rules']),
('invalid','Fallback',normalize_payload({'css':'p{color:red;color:rgb(nope);font-size:20pt;font:bold garbage;}'})['rules']),
('legacy','**Outer `Inner` outer**',{'strong':{'underline':True},'code':{'underline':False}})]:
export_markdown_to_docx(source,folder/f'{engine}-{name}.docx',engine=engine,style_rules=rules)
print(folder)
'@ | & $Python -B -).Trim()
if ($LASTEXITCODE -ne 0) {throw 'Fixture generation failed'}
$word=New-Object -ComObject Word.Application
$word.Visible=$false;$word.DisplayAlerts=0
foreach($engine in @('builtin','python-docx','pandoc')) {
foreach($case in @('code','ordered','task','quoted','hanging','invalid','legacy')) {
$doc=$word.Documents.Open((Join-Path $folder "$engine-$case.docx"),$false,$true)
try {
if($case -eq 'code') {
$range=$doc.Content.Duplicate;[void]$range.Find.Execute('Parent')
$range.Style.Font.Color=16711680;$range.Style.Font.Size=28
foreach($text in @('Inline','Block')) {
$range=$doc.Content.Duplicate;[void]$range.Find.Execute($text)
if($range.Font.Color -ne 16711680 -or $range.Font.Size -ne 28){throw "$engine $text does not follow ancestor: $($range.Font.Color), $($range.Font.Size)"}
}
} elseif($case -in @('ordered','task')) {
$range=$doc.Content.Duplicate;[void]$range.Find.Execute('First')
$name=if($case -eq 'ordered'){'MDHelper 有序列表'}else{'MDHelper 任务列表'}
$base=$doc.Styles.Item($name)
$base.Font.Color=65280
foreach($text in @('First','Continuation','Second')) {
$range=$doc.Content.Duplicate;[void]$range.Find.Execute($text)
if($range.Font.Color -ne 65280){throw "$engine $case $text lost editable list base: $($range.Font.Color)"}
if($text -eq 'Continuation' -and $range.ListFormat.ListString.Trim() -ne ''){throw "$engine continuation consumed a marker"}
}
} elseif($case -eq 'quoted') {
foreach($pair in @(@('Outer',36),@('Inner',72))) {
$range=$doc.Content.Duplicate;[void]$range.Find.Execute($pair[0])
if([Math]::Abs($range.ParagraphFormat.LeftIndent-$pair[1]) -gt 0.1){throw "$engine quoted $($pair[0]) indent: $($range.ParagraphFormat.LeftIndent)"}
}
} elseif($case -eq 'hanging') {
$range=$doc.Content.Duplicate;[void]$range.Find.Execute('Outer')
if($range.ParagraphFormat.FirstLineIndent -ne -12 -or $range.Font.Size -ne 20){throw "$engine inherited hanging indent resized: $($range.ParagraphFormat.FirstLineIndent), font $($range.Font.Size)"}
} elseif($case -eq 'invalid') {
$range=$doc.Content.Duplicate;[void]$range.Find.Execute('Fallback')
if($range.Font.Color -ne 255 -or $range.Font.Size -ne 20){throw "$engine invalid CSS discarded valid fallback"}
} elseif($case -eq 'legacy') {
foreach($pair in @(@('Outer',1),@('Inner',0))) {
$range=$doc.Content.Duplicate;[void]$range.Find.Execute($pair[0])
if($range.Font.Underline -ne $pair[1] -or $range.Font.Bold -ne -1){throw "$engine legacy decoration or emphasis changed"}
}
}
Write-Output "$engine $case passed"
} finally {$doc.Close(0);[void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($doc)}
}
}
} finally {
if($word){$word.Quit();[void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($word)}
Pop-Location
}