2023-06-19 23:42:47 +02:00
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
2018-09-22 03:53:29 +02:00
2023-08-11 04:46:45 +02:00
package locale // import "miniflux.app/v2/internal/locale"
2018-09-22 03:53:29 +02:00
import "testing"
func TestParserWithInvalidData ( t * testing . T ) {
2021-02-17 07:58:44 +01:00
_ , err := parseTranslationMessages ( [ ] byte ( ` { ` ) )
2018-09-22 03:53:29 +02:00
if err == nil {
t . Fatal ( ` An error should be returned when parsing invalid data ` )
}
}
func TestParser ( t * testing . T ) {
2021-02-17 07:58:44 +01:00
translations , err := parseTranslationMessages ( [ ] byte ( ` { "k": "v"} ` ) )
2018-09-22 03:53:29 +02:00
if err != nil {
t . Fatalf ( ` Unexpected parsing error: %v ` , err )
}
if translations == nil {
t . Fatal ( ` Translations should not be nil ` )
}
value , found := translations [ "k" ]
if ! found {
t . Fatal ( ` The translation should contains the defined key ` )
}
if value . ( string ) != "v" {
t . Fatal ( ` The translation key should contains the defined value ` )
}
}
2021-02-17 07:58:44 +01:00
func TestLoadCatalog ( t * testing . T ) {
if err := LoadCatalogMessages ( ) ; err != nil {
t . Fatal ( err )
}
}
func TestAllKeysHaveValue ( t * testing . T ) {
for language := range AvailableLanguages ( ) {
messages , err := loadTranslationFile ( language )
if err != nil {
t . Fatalf ( ` Unable to load translation messages for language %q ` , language )
}
if len ( messages ) == 0 {
t . Fatalf ( ` The language %q doesn't have any messages ` , language )
}
for k , v := range messages {
switch value := v . ( type ) {
case string :
if value == "" {
2024-03-01 05:20:29 +01:00
t . Errorf ( ` The key %q for the language %q has an empty string as value ` , k , language )
2021-02-17 07:58:44 +01:00
}
2024-03-01 05:20:29 +01:00
case [ ] any :
2021-02-17 07:58:44 +01:00
if len ( value ) == 0 {
2024-03-01 05:20:29 +01:00
t . Errorf ( ` The key %q for the language %q has an empty list as value ` , k , language )
2021-02-17 07:58:44 +01:00
}
}
}
}
}
func TestMissingTranslations ( t * testing . T ) {
refLang := "en_US"
references , err := loadTranslationFile ( refLang )
if err != nil {
t . Fatal ( ` Unable to parse reference language ` )
}
for language := range AvailableLanguages ( ) {
if language == refLang {
continue
}
messages , err := loadTranslationFile ( language )
if err != nil {
t . Fatalf ( ` Parsing error for language %q ` , language )
}
for key := range references {
if _ , found := messages [ key ] ; ! found {
t . Fatalf ( ` Translation key %q not found in language %q ` , key , language )
}
}
}
}
2024-03-01 05:20:29 +01:00
func TestTranslationFilePluralForms ( t * testing . T ) {
for language := range AvailableLanguages ( ) {
messages , err := loadTranslationFile ( language )
if err != nil {
t . Fatalf ( ` Unable to load translation messages for language %q ` , language )
}
for k , v := range messages {
if value , ok := v . ( [ ] any ) ; ok {
if len ( value ) != numberOfPluralFormsPerLanguage [ language ] {
t . Errorf ( ` The key %q for the language %q does not have the expected number of plurals, got %d instead of %d ` , k , language , len ( value ) , numberOfPluralFormsPerLanguage [ language ] )
}
}
}
}
}