Tests/TranslationIsComplete.Tests.ps1

Describe "Test-TranslationIsComplete" {
    function Get-TestXlfContent {
        return '<?xml version="1.0" encoding="utf-8"?>
                <xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
                    <file datatype="xml" source-language="en-US" target-language="en-US" original="Test App">
                        <body>
                            <group id="body">
                                <trans-unit id="Table 1343449161 - Property 2879900210" size-unit="char" translate="yes" xml:space="preserve">
                                    <source>Test Table</source><note from="Developer" annotates="general" priority="2"></note>
                                    <note from="Xliff Generator" annotates="general" priority="3">Table Test Table - Property Caption</note>
                                </trans-unit>
                                <trans-unit id="Table 1343449161 - Field 3004954119 - Property 2879900210" size-unit="char" translate="yes" xml:space="preserve">
                                    <source>Code</source>
                                    <note from="Developer" annotates="general" priority="2"></note>
                                    <note from="Xliff Generator" annotates="general" priority="3">Table Test Table - Field Code - Property Caption</note>
                                </trans-unit>
                            </group>
                        </body>
                    </file>
                </xliff>'

      }

    function Get-IncompleteTestTranslation {
        return '<?xml version="1.0" encoding="utf-8"?>
                <xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
                    <file datatype="xml" source-language="en-US" target-language="fr-FR" original="Test App">
                        <body>
                            <group id="body">
                                <trans-unit id="Table 1343449161 - Property 2879900210" size-unit="char" translate="yes" xml:space="preserve">
                                    <source>Test Table</source><note from="Developer" annotates="general" priority="2"></note><target>Translated text</target>
                                    <note from="Xliff Generator" annotates="general" priority="3">Table Test Table - Property Caption</note>
                                </trans-unit>
                            </group>
                        </body>
                    </file>
                </xliff>'

    }

    function Get-CompleteTestTranslation {
        return '<?xml version="1.0" encoding="utf-8"?>
                <xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
                    <file datatype="xml" source-language="en-US" target-language="fr-FR" original="Test App">
                        <body>
                            <group id="body">
                                <trans-unit id="Table 1343449161 - Property 2879900210" size-unit="char" translate="yes" xml:space="preserve">
                                    <source>Test Table</source><note from="Developer" annotates="general" priority="2"></note><target>Translated text</target>
                                    <note from="Xliff Generator" annotates="general" priority="3">Table Test Table - Property Caption</note>
                                </trans-unit>
                                <trans-unit id="Table 1343449161 - Field 3004954119 - Property 2879900210" size-unit="char" translate="yes" xml:space="preserve">
                                    <source>Code</source><target>Translated text</target>
                                    <note from="Developer" annotates="general" priority="2"></note>
                                    <note from="Xliff Generator" annotates="general" priority="3">Table Test Table - Field Code - Property Caption</note>
                                </trans-unit>
                            </group>
                        </body>
                    </file>
                </xliff>'

    }

    function New-TranslationTestApp {
        param(
            # create the app with missing, partial or complete translation files
            [Parameter(Mandatory=$true)]
            [string]
            [ValidateSet('None','Partial','Complete')]
            $CreateTranslationFiles
        )
        
        New-Item -Type Directory -Path (Join-Path $TestDrive 'Translations')
        Set-Content -Value (Get-TestXlfContent) -Path (Join-Path $TestDrive (Join-Path 'Translations' 'Test.g.xlf'))
        Set-Content -Value ('{"translationSource": ".\\Translations\\Test.g.xlf", "translations": [{"country": "FR", "language": "fr"},{"country": "DE", "language": "de"}]}') -Path (Join-Path $TestDrive 'environment.json')
        switch ($CreateTranslationFiles) {
            'None' { <#do nothing#> }
            'Partial' {Set-Content -Value (Get-IncompleteTestTranslation) -Path (Join-Path (Join-Path $TestDrive 'Translations') 'fr-FR.xlf')}
            'Complete' {Set-Content -Value (Get-CompleteTestTranslation) -Path (Join-Path (Join-Path $TestDrive 'Translations') 'fr-FR.xlf')}
        }

        #in all cases also create a German translation file to test enumeration through multiple transltion files
        Set-Content -Value (Get-CompleteTestTranslation) -Path (Join-Path (Join-Path $TestDrive 'Translations') 'de-DE.xlf')
    }

    Context "App has a missing translation file" {
        New-TranslationTestApp -CreateTranslationFiles None
        $Location = Get-Location
        Set-Location $TestDrive
        It "Should error" {
            {Test-TranslationIsComplete -SourceDir $TestDrive} | should throw 'Translation file fr-FR is missing'
        }
        Set-Location $Location
    }

    Context "App has a missing translation file and error surpressed" {
        New-TranslationTestApp -CreateTranslationFiles None
        $Location = Get-Location
        Set-Location $TestDrive
        It "Should be false" {
            Test-TranslationIsComplete -SourceDir $TestDrive -SurpressError | should be $false
        }
        Set-Location $Location
    }

    Context "App has a partial translation file" {
        New-TranslationTestApp -CreateTranslationFiles Partial
        $Location = Get-Location
        Set-Location $TestDrive
        It "Should error" {
            {Test-TranslationIsComplete -SourceDir $TestDrive} | should throw 'Translation file fr-FR has missing translations'
        }
        Set-Location $Location
    }

    Context "App has a partial translation file and error supressed" {
        New-TranslationTestApp -CreateTranslationFiles Partial
        $Location = Get-Location
        Set-Location $TestDrive
        It "Should be false" {
            Test-TranslationIsComplete -SourceDir $TestDrive -SurpressError | should be $false
        }
        Set-Location $Location
    }

    Context "App has complete translation files" {
        New-TranslationTestApp -CreateTranslationFiles Complete
        $Location = Get-Location
        Set-Location $TestDrive
        It "Should be true" {
            Test-TranslationIsComplete -SourceDir $TestDrive | should be $true
        }
        Set-Location $Location
    }
}