Skip to content

Commit

Permalink
add test for GenerateLocaleConfigTask
Browse files Browse the repository at this point in the history
  • Loading branch information
aormsby committed Jul 3, 2024
1 parent 82c5b4a commit 2839566
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ class LocaleResourceGeneratorPluginTest extends Specification {
}

where:
variant | expectedTasks
"Debug" | taskList("Debug")
"Release" | taskList("Release")
variant || expectedTasks
"Debug" || taskList("Debug")
"Release" || taskList("Release")
}

private def taskList(String variant) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package com.mermake.locale_resource_generator.data

class Intermediates {
static File createFile(String[] input) {
File tempFile = File.createTempFile("test_tags", "txt")
tempFile.deleteOnExit()
tempFile.write(input.join("\n"))
return tempFile
}

static String[] pseudolocales = [
"ar-XB,العربية (لكنات تجريبية ثنائية الاتجاه)",
"en-XA,English (Pseudo-Accents)",
Expand Down
223 changes: 223 additions & 0 deletions src/test/groovy/com/mermake/locale_resource_generator/data/Xml.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
package com.mermake.locale_resource_generator.data

class Xml {
static String[] smallBatch = [
'<?xml version="1.0" encoding="utf-8"?>',
'<locale-config xmlns:android="http://schemas.android.com/apk/res/android">',
'<!-- English (United States) -->',
'<locale android:name="en-US"/>',
'<!-- English (Pseudo-Accents) -->',
'<locale android:name="en-XA"/>',
'<!-- 中文 (台灣) -->',
'<locale android:name="zh-TW"/>',
'</locale-config>',
]

static Set<String> pseudolocales = [
'<?xml version="1.0" encoding="utf-8"?>',
'<locale-config xmlns:android="http://schemas.android.com/apk/res/android">',
'<!-- العربية (لكنات تجريبية ثنائية الاتجاه) -->',
'<locale android:name="ar-XB"/>',
'<!-- English (Pseudo-Accents) -->',
'<locale android:name="en-XA"/>',
'</locale-config>',
]

static String[] androidSupported = [
'<?xml version="1.0" encoding="utf-8"?>',
'<locale-config xmlns:android="http://schemas.android.com/apk/res/android">',
'<!-- Afrikaans -->',
'<locale android:name="af"/>',
'<!-- Afrikaans (Suid-Afrika) -->',
'<locale android:name="af-ZA"/>',
'<!-- አማርኛ -->',
'<locale android:name="am"/>',
'<!-- አማርኛ (ኢትዮጵያ) -->',
'<locale android:name="am-ET"/>',
'<!-- العربية -->',
'<locale android:name="ar"/>',
'<!-- العربية (مصر) -->',
'<locale android:name="ar-EG"/>',
'<!-- العربية (إسرائيل) -->',
'<locale android:name="ar-IL"/>',
'<!-- অসমীয়া -->',
'<locale android:name="as"/>',
'<!-- azərbaycan -->',
'<locale android:name="az"/>',
'<!-- беларуская -->',
'<locale android:name="be"/>',
'<!-- български -->',
'<locale android:name="bg"/>',
'<!-- български (България) -->',
'<locale android:name="bg-BG"/>',
'<!-- বাংলা -->',
'<locale android:name="bn"/>',
'<!-- bosanski -->',
'<locale android:name="bs"/>',
'<!-- català -->',
'<locale android:name="ca"/>',
'<!-- català (Espanya) -->',
'<locale android:name="ca-ES"/>',
'<!-- čeština -->',
'<locale android:name="cs"/>',
'<!-- čeština (Česko) -->',
'<locale android:name="cs-CZ"/>',
'<!-- dansk -->',
'<locale android:name="da"/>',
'<!-- dansk (Danmark) -->',
'<locale android:name="da-DK"/>',
'<!-- Deutsch -->',
'<locale android:name="de"/>',
'<!-- Deutsch (Österreich) -->',
'<locale android:name="de-AT"/>',
'<!-- Deutsch (Schweiz) -->',
'<locale android:name="de-CH"/>',
'<!-- Deutsch (Deutschland) -->',
'<locale android:name="de-DE"/>',
'<!-- Deutsch (Liechtenstein) -->',
'<locale android:name="de-LI"/>',
'<!-- Ελληνικά -->',
'<locale android:name="el"/>',
'<!-- Ελληνικά (Ελλάδα) -->',
'<locale android:name="el-GR"/>',
'<!-- English (Australia) -->',
'<locale android:name="en-AU"/>',
'<!-- English (Canada) -->',
'<locale android:name="en-CA"/>',
'<!-- English (United Kingdom) -->',
'<locale android:name="en-GB"/>',
'<!-- English (Ireland) -->',
'<locale android:name="en-IE"/>',
'<!-- English (India) -->',
'<locale android:name="en-IN"/>',
'<!-- English (New Zealand) -->',
'<locale android:name="en-NZ"/>',
'<!-- English (Singapore) -->',
'<locale android:name="en-SG"/>',
'<!-- English (United States) -->',
'<locale android:name="en-US"/>',
'<!-- English (South Africa) -->',
'<locale android:name="en-ZA"/>',
'<!-- español -->',
'<locale android:name="es"/>',
'<!-- español (Colombia) -->',
'<locale android:name="es-CO"/>',
'<!-- español (Costa Rica) -->',
'<locale android:name="es-CR"/>',
'<!-- español (Ecuador) -->',
'<locale android:name="es-EC"/>',
'<!-- español (España) -->',
'<locale android:name="es-ES"/>',
'<!-- español (Guatemala) -->',
'<locale android:name="es-GT"/>',
'<!-- español (Honduras) -->',
'<locale android:name="es-HN"/>',
'<!-- español (México) -->',
'<locale android:name="es-MX"/>',
'<!-- español (Nicaragua) -->',
'<locale android:name="es-NI"/>',
'<!-- español (Panamá) -->',
'<locale android:name="es-PA"/>',
'<!-- español (Perú) -->',
'<locale android:name="es-PE"/>',
'<!-- español (El Salvador) -->',
'<locale android:name="es-SV"/>',
'<!-- español (Estados Unidos) -->',
'<locale android:name="es-US"/>',
'<!-- eesti -->',
'<locale android:name="et"/>',
'<!-- euskara -->',
'<locale android:name="eu"/>',
'<!-- فارسی -->',
'<locale android:name="fa"/>',
'<!-- فارسی (ایران) -->',
'<locale android:name="fa-IR"/>',
'<!-- suomi -->',
'<locale android:name="fi"/>',
'<!-- suomi (Suomi) -->',
'<locale android:name="fi-FI"/>',
'<!-- français -->',
'<locale android:name="fr"/>',
'<!-- français (Belgique) -->',
'<locale android:name="fr-BE"/>',
'<!-- français (Canada) -->',
'<locale android:name="fr-CA"/>',
'<!-- français (Suisse) -->',
'<locale android:name="fr-CH"/>',
'<!-- français (France) -->',
'<locale android:name="fr-FR"/>',
'<!-- galego -->',
'<locale android:name="gl"/>',
'<!-- ગુજરાતી -->',
'<locale android:name="gu"/>',
'<!-- עברית -->',
'<locale android:name="he"/>',
'<!-- हिन्दी -->',
'<locale android:name="hi"/>',
'<!-- हिन्दी (भारत) -->',
'<locale android:name="hi-IN"/>',
'<!-- hrvatski -->',
'<locale android:name="hr"/>',
'<!-- hrvatski (Hrvatska) -->',
'<locale android:name="hr-HR"/>',
'<!-- magyar -->',
'<locale android:name="hu"/>',
'<!-- magyar (Magyarország) -->',
'<locale android:name="hu-HU"/>',
'<!-- հայերեն -->',
'<locale android:name="hy"/>',
'<!-- Indonesia -->',
'<locale android:name="in"/>',
'<!-- Indonesia (Indonesia) -->',
'<locale android:name="in-ID"/>',
'<!-- íslenska -->',
'<locale android:name="is"/>',
'<!-- italiano -->',
'<locale android:name="it"/>',
'<!-- italiano (Svizzera) -->',
'<locale android:name="it-CH"/>',
'<!-- italiano (Italia) -->',
'<locale android:name="it-IT"/>',
'<!-- עברית -->',
'<locale android:name="iw"/>',
'<!-- עברית (ישראל) -->',
'<locale android:name="iw-IL"/>',
'<!-- 日本語 -->',
'<locale android:name="ja"/>',
'<!-- 日本語 (日本) -->',
'<locale android:name="ja-JP"/>',
'<!-- ქართული -->',
'<locale android:name="ka"/>',
'<!-- қазақ тілі -->',
'<locale android:name="kk"/>',
'<!-- ខ្មែរ -->',
'<locale android:name="km"/>',
'<!-- ಕನ್ನಡ -->',
'<locale android:name="kn"/>',
'<!-- 한국어 -->',
'<locale android:name="ko"/>',
'<!-- 한국어 (대한민국) -->',
'<locale android:name="ko-KR"/>',
'<!-- кыргызча -->',
'<locale android:name="ky"/>',
'<!-- ລາວ -->',
'<locale android:name="lo"/>',
'<!-- lietuvių -->',
'<locale android:name="lt"/>',
'<!-- lietuvių (Lietuva) -->',
'<locale android:name="lt-LT"/>',
'<!-- latviešu -->',
'<locale android:name="lv"/>',
'<!-- latviešu (Latvija) -->',
'<locale android:name="lv-LV"/>',
'<!-- srpski (latinica) -->',
'<locale android:name="sr-Latn"/>',
'<!-- 中文 -->',
'<locale android:name="zh"/>',
'<!-- 中文 (中国) -->',
'<locale android:name="zh-CN"/>',
'<!-- 中文 (台灣) -->',
'<locale android:name="zh-TW"/>',
'</locale-config>',
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.mermake.locale_resource_generator.tasks

import com.mermake.locale_resource_generator.data.Intermediates
import com.mermake.locale_resource_generator.data.Xml
import org.gradle.testfixtures.ProjectBuilder
import spock.lang.Specification
import spock.lang.TempDir

class GenerateLocaleConfigTaskTest extends Specification {

@TempDir
File outputDir

GenerateLocaleConfigTask task
File outputFile

def setup() {
def project = ProjectBuilder.builder().build()
task = project.tasks.register("generateLocaleConfig", GenerateLocaleConfigTask).get()

outputFile = new File(outputDir, "locale_config.xml")
task.localeConfigOutput.set(outputFile)
}

def "process locales, output correct locale config"(String[] input, String[] output) {
given: "temp intermediate local tags file"
task.languageListInput.set(Intermediates.createFile(input))

when: "task is executed, xml is generated"
task.taskAction()

then: "output file contains correctly formatted language tag data"
outputFile.readLines().eachWithIndex { String s, int i ->
assert s.trim() == output[i]
}

where:
input || output
Intermediates.smallBatch || Xml.smallBatch
Intermediates.androidSupported || Xml.androidSupported
Intermediates.pseudolocales || Xml.pseudolocales
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class GenerateSupportedLocalesTaskTest extends Specification {

def "process locales, verify properties and functions"() {
given: "temp intermediate local tags file"
task.languageListInput.set(createIntermediateFile(Intermediates.smallBatch))
task.languageListInput.set(Intermediates.createFile(Intermediates.smallBatch))

when: "task executed"
File outputFile = runActionAndGetOutput()
Expand All @@ -39,7 +39,7 @@ class GenerateSupportedLocalesTaskTest extends Specification {

def "process locales, verify invoked function results"(String[] input, String[] testArgs, Object[] output) {
given: "temp intermediate local tags file"
task.languageListInput.set(createIntermediateFile(input))
task.languageListInput.set(Intermediates.createFile(input))

when: "task is executed, class is instantiated"
File outputFile = runActionAndGetOutput()
Expand All @@ -54,15 +54,15 @@ class GenerateSupportedLocalesTaskTest extends Specification {
}

where:
input | testArgs | output
Intermediates.smallBatch | Tags.smallBatch | [Tags.smallBatch, Compiled.endonymsSmallBatch, Compiled.exonymsSmallBatch]
input | testArgs || output
Intermediates.smallBatch | Tags.smallBatch || [Tags.smallBatch, Compiled.endonymsSmallBatch, Compiled.exonymsSmallBatch]
// todo: implement after v1.1 fix for 'huge' map
// Intermediates.androidSupported | Tags.androidSupported | [Tags.androidSupported, Compiled.endonymsAndroidSupported, Compiled.exonymsAndroidSupported]
}

def "process locales, verify invoked function failures"() {
given: "temp intermediate local tags file"
task.languageListInput.set(createIntermediateFile(Intermediates.smallBatch))
task.languageListInput.set(Intermediates.createFile(Intermediates.smallBatch))

when: "task is executed, class is instantiated"
File outputFile = runActionAndGetOutput()
Expand All @@ -81,13 +81,6 @@ class GenerateSupportedLocalesTaskTest extends Specification {
thrown(NoSuchElementException)
}

private File createIntermediateFile(String[] input) {
File tempFile = File.createTempFile("test_tags", "txt")
tempFile.deleteOnExit()
tempFile.write(input.join("\n"))
return tempFile
}

private File runActionAndGetOutput() {
task.taskAction()
return new File(outputDir, "${packageName.replace('.', '/')}/${className}.kt")
Expand Down

0 comments on commit 2839566

Please sign in to comment.