1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2024-10-27 20:34:16 +00:00

Scribunto: Fix broken test for fetchLanguageNames

This commit is contained in:
gnosygnu 2018-12-09 22:00:07 -05:00
parent 3935d10b45
commit af19381e3c
7 changed files with 46 additions and 31 deletions

View File

@ -36,10 +36,10 @@ public class Xol_name_mgr {
* @return String Language name or empty
* @since 1.20
*/
public String fetchLanguageName(String code, byte[] inLanguage, byte[] include_bry) {
public String fetchLanguageName(String code, String inLanguage, String include) {
code = String_.Lower(code);
if (include_bry == null) include_bry = fetchLanguageNamesUncached__all__key;
Ordered_hash array = fetchLanguageNames(inLanguage, include_bry);
if (include == null) include = "all";
Ordered_hash array = fetchLanguageNames(inLanguage, include);
Keyval rv = (Keyval)array.Get_by(code);
return rv == null ? "" : rv.Val_to_str_or_null();
}
@ -59,23 +59,17 @@ public class Xol_name_mgr {
private Ordered_hash lang_names_cached;
private Ordered_hash lang_files_cached;
private static final byte[] Null_bry = Bry_.new_a7("null");
public Ordered_hash fetchLanguageNames(byte[] inLanguage, byte[] include_bry) {
byte[] cacheKey = inLanguage == null ? Null_bry : inLanguage;
cacheKey = Bry_.Add(cacheKey, Byte_ascii.Colon_bry, include_bry);
public Ordered_hash fetchLanguageNames(String inLanguage, String include_str) {
if (inLanguage == null) inLanguage = "null";
String cacheKey = inLanguage + ":" + include_str;
if (languageNameCache == null)
languageNameCache = Ordered_hash_.New_bry();
languageNameCache = Ordered_hash_.New();
Ordered_hash ret = (Ordered_hash)languageNameCache.Get_by(cacheKey);
if (ret == null) {
Byte_obj_val include_byte = (Byte_obj_val)fetchLanguageNamesUncachedEnum.Get_by(include_bry);
Byte_obj_val include_byte = (Byte_obj_val)fetchLanguageNamesUncachedEnum.Get_by(include_str);
byte include = include_byte == null ? fetchLanguageNamesUncached__all : include_byte.Val();
Cldr_name_file cldr_file = cldr_loader.Load_or_null(String_.new_u8(inLanguage));
if (cldr_file == null) {
ret = Ordered_hash_.New();
languageNameCache.Add(cacheKey, ret);
return ret;
}
Cldr_name_file cldr_file = cldr_loader.Load_or_empty(inLanguage);
if (lang_names_cached == null)
lang_names_cached = name_loader.Load_as_hash();
@ -154,14 +148,13 @@ public class Xol_name_mgr {
, fetchLanguageNamesUncached__all = 1 // cldr + Names.php
, fetchLanguageNamesUncached__mwFile = 2 // *.json|*.php
;
private static final byte[] fetchLanguageNamesUncached__all__key = Bry_.new_a7("all");
private static final Hash_adp_bry fetchLanguageNamesUncachedEnum = Hash_adp_bry.cs()
.Add_str_byte("mw", fetchLanguageNamesUncached__mw)
.Add_str_byte("all", fetchLanguageNamesUncached__all)
.Add_str_byte("mwFile", fetchLanguageNamesUncached__mwFile)
private static final Hash_adp fetchLanguageNamesUncachedEnum = Hash_adp_.New()
.Add_and_more("mw" , Byte_obj_val.new_(fetchLanguageNamesUncached__mw))
.Add_and_more("all" , Byte_obj_val.new_(fetchLanguageNamesUncached__all))
.Add_and_more("mwFile", Byte_obj_val.new_(fetchLanguageNamesUncached__mwFile))
;
public static Ordered_hash fetchLanguageNamesUncached
( byte[] inLanguage, byte include
( String inLanguage, byte include
, Ordered_hash cldr_names
, Ordered_hash lang_names
, Ordered_hash lang_files
@ -190,13 +183,12 @@ public class Xol_name_mgr {
// $mwNames = $wgExtraLanguageNames + MediaWiki\Languages\Data\Names::$names;
Ordered_hash mwNames = lang_names;
int mwNames_len = mwNames.Len();
String inLanguageStr = String_.new_u8(inLanguage);
for (int i = 0; i < mwNames_len; i++) {
Keyval mw_name = (Keyval)mwNames.Get_at(i);
// # - Prefer own MediaWiki native name when not using the hook
// # - For other mwNames just add if not added through the hook
String code = mw_name.Key();
if (String_.Eq(code, inLanguageStr) || !names.Has(code)) {
if (String_.Eq(code, inLanguage) || !names.Has(code)) {
names.Add_if_dupe_use_nth(code, Keyval_.new_(code, mw_name.Val_to_str_or_empty()));
}
}

View File

@ -79,7 +79,7 @@ class Xol_name_mgr_fxt {
, Ordered_hash lang_names
, Ordered_hash lang_files
, Ordered_hash expd_langs) {
Ordered_hash actl_langs = Xol_name_mgr.fetchLanguageNamesUncached(Bry_.new_u8(inLanguage), include, cldr_names, lang_names, lang_files);
Ordered_hash actl_langs = Xol_name_mgr.fetchLanguageNamesUncached(inLanguage, include, cldr_names, lang_names, lang_files);
Gftest.Eq__ary(To_str_ary(expd_langs), To_str_ary(actl_langs));
}
private static String[] Add_suffix(String[] ary, String val_suffix) {

View File

@ -23,5 +23,7 @@ public class Cldr_name_file {
public Ordered_hash Currency_names() {return currency_names;} private final Ordered_hash currency_names = Ordered_hash_.New();
public Ordered_hash Currency_symbols() {return currency_symbols;} private final Ordered_hash currency_symbols = Ordered_hash_.New();
public Ordered_hash Country_names() {return country_names;} private final Ordered_hash country_names = Ordered_hash_.New();
public Ordered_hash Time_units() {return time_units;} private final Ordered_hash time_units = Ordered_hash_.New();
public Ordered_hash Time_units() {return time_units;} private final Ordered_hash time_units = Ordered_hash_.New();
public static final Cldr_name_file Empty = new Cldr_name_file("EMPTY");
}

View File

@ -26,7 +26,7 @@ public class Cldr_name_loader {
this.cldr_dir = cldr_dir;
}
public Cldr_name_file Load_or_null(String lang_key) {
public Cldr_name_file Load_or_empty(String lang_key) {
// normalize to lc; scrib will pass lower_case, but underlying files are Title_case
lang_key = String_.Lower(lang_key);
@ -41,13 +41,17 @@ public class Cldr_name_loader {
// get file
Io_url url = (Io_url)urls_hash.Get_by(lang_key);
if (url == null) {
files_hash.Add(lang_key, Cldr_name_file.Empty);
Gfo_usr_dlg_.Instance.Warn_many("", "", "no cldrName file exists for lang; lang=~{lang}", lang_key);
return null;
return Cldr_name_file.Empty;
}
// load json
byte[] json = Io_mgr.Instance.LoadFilBry(url);
if (Bry_.Len_eq_0(json)) {
files_hash.Add(lang_key, Cldr_name_file.Empty);
Gfo_usr_dlg_.Instance.Warn_many("", "", "json is empty; lang=~{lang}", lang_key);
return null;
return Cldr_name_file.Empty;
}
// parse, cache and return

View File

@ -31,7 +31,7 @@ class Cldr_name_loader_fxt {
Io_mgr.Instance.SaveFilStr(Io_url_.new_fil_(Dir_name + fil_name), txt);
}
public void Test__load_file_is_null(boolean expd, String lang_key) {
Cldr_name_file name_file = name_loader.Load_or_null(lang_key);
Cldr_name_file name_file = name_loader.Load_or_empty(lang_key);
Gftest.Eq__bool(expd, name_file == null);
}
}

View File

@ -131,8 +131,9 @@ public class Scrib_lib_language implements Scrib_lib {
}
public boolean FetchLanguageName(Scrib_proc_args args, Scrib_proc_rslt rslt) {
String lang_code = args.Pull_str(0);
byte[] inLanguage = args.Pull_bry(1);
String rv = core.App().Lang_mgr().Name_mgr().fetchLanguageName(lang_code, inLanguage, null);
String inLanguage = args.Cast_str_or_null(1);
String include = args.Cast_str_or(2, "mw");
String rv = core.App().Lang_mgr().Name_mgr().fetchLanguageName(lang_code, inLanguage, include);
return rslt.Init_obj(rv);
/*
byte[] lang_code = args.Pull_bry(0);

View File

@ -15,6 +15,7 @@ Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/
package gplx.xowa.xtns.scribunto.libs; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.scribunto.*;
import org.junit.*;
import gplx.langs.jsons.*;
import gplx.xowa.langs.*; import gplx.xowa.langs.msgs.*; import gplx.xowa.langs.numbers.*;
public class Scrib_lib_language_tst {
@Before public void init() {
@ -42,6 +43,21 @@ public class Scrib_lib_language_tst {
fxt.Test_scrib_proc_bool(lib, Scrib_lib_language.Invk_isValidBuiltInCode, Object_.Ary("e n"), false);
}
@Test public void FetchLanguageName() {
Io_mgr.Instance.SaveFilStr("mem/xowa/bin/any/xowa/cfg/lang/data/names.json", Json_doc.Make_str_by_apos
( "["
, " {"
, " 'code':'en'"
, " , 'name':'English'"
, " , 'note':'en_note'"
, " }"
, ", {"
, " 'code':'fr'"
, " , 'name':'Français'"
, " , 'note':'fr_note'"
, " }"
, "]"
));
fxt.Test_scrib_proc_str(lib, Scrib_lib_language.Invk_fetchLanguageName, Object_.Ary("en"), "English");
fxt.Test_scrib_proc_str(lib, Scrib_lib_language.Invk_fetchLanguageName, Object_.Ary("fr"), "Français");
fxt.Test_scrib_proc_str(lib, Scrib_lib_language.Invk_fetchLanguageName, Object_.Ary("qz"), "");