Files
MDHelper/tests/word_table_smoke.ps1

50 lines
3.1 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 {
$testDirectory = (@'
import json, tempfile, sys
from pathlib import Path
sys.path.insert(0, str(Path('tests').resolve()))
from test_table_presentation import SOURCE, RULE
from mdhelper.docx_exporter import export_markdown_to_docx
from mdhelper.style_catalog import style_name
folder = Path(tempfile.mkdtemp(prefix='MDHelper-Word-Tables-'))
for engine in ('builtin', 'python-docx', 'pandoc'):
for custom in (False, True):
rules = {'table': RULE, 'table_head': {'background_color': '#ddeeff'}} if custom else {}
export_markdown_to_docx(SOURCE, folder / f'{engine}-{int(custom)}.docx', engine=engine, style_rules=rules)
(folder / 'names.json').write_text(json.dumps({'table': 'MDHelper ' + style_name('table')}), encoding='utf-8')
print(folder)
'@ | & $Python -B -).Trim()
if ($LASTEXITCODE -ne 0) { throw 'Fixture generation failed.' }
$names = Get-Content -LiteralPath (Join-Path $testDirectory 'names.json') -Raw -Encoding UTF8 | ConvertFrom-Json
$word = New-Object -ComObject Word.Application
$word.Visible = $false
$word.DisplayAlerts = 0
foreach ($engine in @('builtin', 'python-docx', 'pandoc')) {
foreach ($custom in @(0, 1)) {
$doc = $word.Documents.Open((Join-Path $testDirectory "$engine-$custom.docx"), $false, $true)
try {
$table = $doc.Tables.Item(1)
if (-not $table.AllowAutoFit) { throw "$engine disabled automatic column sizing." }
$header = if ($custom) {0} else {-1}
if ($table.Rows.Item(1).HeadingFormat -ne $header) { throw "$engine header repetition differs from settings." }
if ([bool]$table.Rows.AllowBreakAcrossPages -ne [bool]$custom) { throw "$engine row splitting differs from settings." }
if ($custom -and $table.Borders.Item(-6).LineStyle -ne 0) { throw "$engine horizontal layout retains vertical borders." }
$style = $doc.Styles.Item($names.table)
$expectedHeader = if ($custom) {16772829} else {16381425}
if ($table.Cell(1,1).Shading.BackgroundPatternColor -ne $expectedHeader) { throw "$engine lost header background: $($table.Cell(1,1).Shading.BackgroundPatternColor)." }
if ($custom -and $table.Cell(3,1).Shading.BackgroundPatternColor -ne 16446442) { throw "$engine lost alternating row background: $($table.Cell(3,1).Shading.BackgroundPatternColor)." }
$style.Table.Condition(0).Shading.BackgroundPatternColor = 255
if ($table.Cell(1,1).Shading.BackgroundPatternColor -ne 255) { throw "$engine direct formatting blocks table style edits." }
Write-Output "$engine case $custom passed: layout, pagination, backgrounds and shared style edits."
} finally { $doc.Close(0); [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($doc) }
}
}
} finally {
if ($word) { $word.Quit(); [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($word) }
Pop-Location
}