Files
MDHelper/tests/word_revision_054_smoke.ps1
T

89 lines
5.7 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_054 import *
folder=Path(tempfile.mkdtemp(prefix='MDHelper-054-Word-'))
for engine in ENGINES:
for name,source,rules in [('code',CODE_SOURCE,CODE_RULES),('continuation',CONTINUATION_SOURCE,{}),('nested',NESTED_SOURCE,NESTED_RULES),('background',TABLE,BACKGROUND_RULES),
('inner-quote','- Item\n\n > Quote\n\n- Second',{'blockquote':{'font_color':'#ff0000'},'list_bullet':{'font_color':'#0000ff'}}),
('container-quote','> - First\n>\n> - Second',normalize_payload({'css':'blockquote {color:red;} li {color:blue;}'})['rules']),
('decoration','**Outer *Inner* outer**',{'strong':{'underline':True,'strike':True},'emphasis':{'underline':False}}),
('invalid','Fallback',normalize_payload({'css':'p {color:red;font-size:20pt;font-style:italic;color:#gggggg;font-size:"10pt";font-style:italic normal;font:garbage;}'})['rules']),
('cell-fill',TABLE,normalize_payload({'css':'table {--mdhelper-table-banded-rows:true;} td {background-color:#ffeecc;}'})['rules']),
('detached',TABLE,{'table':{'table_banded_rows':True},'_metadata':{'table_head':{'based_on':''},'table_cell':{'based_on':''}}}),
('context-code',CODE_SOURCE,{**CODE_RULES,'paragraph':{'font_color':'#008000'},'inline_code':{'font_color':'inherit'}})]:
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','continuation','nested','background','inner-quote','container-quote','decoration','invalid','cell-fill','detached','context-code')) {
$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('Block')
$base=$range.Style.BaseStyle
$doc.Styles.Item($base).Font.Color=16711680
foreach($text in @('Inline','Block')) {
$range=$doc.Content.Duplicate;[void]$range.Find.Execute($text)
if($range.Font.Color -ne 16711680){throw "$engine $text does not follow code base: $($range.Font.Color)"}
}
} elseif($case -eq 'continuation') {
foreach($pair in @(@('First','1.'),@('Continuation',''),@('Second','2.'))) {
$range=$doc.Content.Duplicate;[void]$range.Find.Execute($pair[0])
if($range.ListFormat.ListString.Trim() -ne $pair[1]){throw "$engine $($pair[0]) wrong numbering: $($range.ListFormat.ListString)"}
}
} elseif($case -eq 'nested') {
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 $($pair[0]) wrong indent: $($range.ParagraphFormat.LeftIndent)"}
}
} elseif($case -eq 'background') {
$table=$doc.Tables.Item(1)
$table.Style.Table.Condition(0).Shading.BackgroundPatternColor=65535
if($table.Cell(1,1).Shading.BackgroundPatternColor -ne 65535){throw "$engine table style cannot change header"}
foreach($row in @(1,2)) {
$range=$table.Cell($row,1).Range.Duplicate;$range.End=$range.End-1
if($range.ParagraphFormat.Shading.BackgroundPatternColor -ne -16777216){throw "$engine inherited paragraph shading remains"}
}
} elseif($case -eq 'inner-quote') {
$range=$doc.Content.Duplicate;[void]$range.Find.Execute('Quote')
if($range.Font.Color -ne 255 -or $range.ListFormat.ListString.Trim() -ne ''){throw "$engine lost inner quote style or added a bullet"}
$range.Style.Font.Color=65280
if($range.Font.Color -ne 65280){throw "$engine quote style edit blocked"}
} elseif($case -eq 'container-quote') {
$range=$doc.Content.Duplicate;[void]$range.Find.Execute('First')
if($range.Font.Color -ne 16711680){throw "$engine inherited container color overrides list color"}
} elseif($case -eq 'decoration') {
$range=$doc.Content.Duplicate;[void]$range.Find.Execute('Inner')
if($range.Font.Underline -ne 0 -or $range.Font.StrikeThrough -ne -1 -or $range.Font.Bold -ne -1 -or $range.Font.Italic -ne -1){throw "$engine nested decoration or protected emphasis changed"}
} 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 -or $range.Font.Italic -ne -1){throw "$engine invalid CSS erased valid typography"}
} elseif($case -eq 'cell-fill') {
foreach($row in @(2,3)) {if($doc.Tables.Item(1).Cell($row,1).Shading.BackgroundPatternColor -ne 13430527){throw "$engine explicit cell background lost"}}
} elseif($case -eq 'detached') {
$table=$doc.Tables.Item(1)
if($table.Cell(1,1).Shading.BackgroundPatternColor -ne 16381425 -or $table.Cell(3,1).Shading.BackgroundPatternColor -ne 16579320){throw "$engine detached text lost table decorations"}
} elseif($case -eq 'context-code') {
$range=$doc.Content.Duplicate;[void]$range.Find.Execute('Inline')
if($range.Font.Color -ne 32768){throw "$engine inline code cannot use surrounding color"}
}
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
}