From 620f196d4e8d42aca13eb27e76adc3174c498365 Mon Sep 17 00:00:00 2001 From: gnosygnu Date: Wed, 25 Sep 2019 23:37:04 -0400 Subject: [PATCH] Core: Add additional test classes [#553] --- .../src/gplx/core/tests/Gfo_test_err_mgr.java | 54 +++++++++ .../src/gplx/core/tests/Gfo_test_itm.java | 104 ++++++++++++++++++ .../gplx/core/tests/Gfo_test_lnr_base.java | 69 ++++++++++++ .../gplx/core/tests/Gfo_test_lnr_itm_cbk.java | 19 ++++ 100_core/src/gplx/core/tests/Gftest.java | 4 + 5 files changed, 250 insertions(+) create mode 100644 100_core/src/gplx/core/tests/Gfo_test_err_mgr.java create mode 100644 100_core/src/gplx/core/tests/Gfo_test_itm.java create mode 100644 100_core/src/gplx/core/tests/Gfo_test_lnr_base.java create mode 100644 100_core/src/gplx/core/tests/Gfo_test_lnr_itm_cbk.java diff --git a/100_core/src/gplx/core/tests/Gfo_test_err_mgr.java b/100_core/src/gplx/core/tests/Gfo_test_err_mgr.java new file mode 100644 index 000000000..20152abf2 --- /dev/null +++ b/100_core/src/gplx/core/tests/Gfo_test_err_mgr.java @@ -0,0 +1,54 @@ +/* +XOWA: the XOWA Offline Wiki Application +Copyright (C) 2012-2017 gnosygnu@gmail.com + +XOWA is licensed under the terms of the General Public License (GPL) Version 3, +or alternatively under the terms of the Apache License Version 2.0. + +You may use XOWA according to either of these licenses as is most appropriate +for your project on a case-by-case basis. + +The terms of each license can be found in the source code repository: + +GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt +Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt +*/ +package gplx.core.tests; import gplx.*; import gplx.core.*; +public class Gfo_test_err_mgr { + private final List_adp expd = List_adp_.New(); + public void Init() { + Gfo_usr_dlg_.Test__list__init(); + expd.Clear(); + } + public void Term() { + Gfo_usr_dlg_.Instance = Gfo_usr_dlg_.Noop; + } + public void Add_expd(boolean contains, String msg) { + Object[] itm = new Object[] {contains, msg}; + expd.Add(itm); + } + public void Test() { + List_adp actl = ((Gfo_usr_dlg__gui_mock)Gfo_usr_dlg_.Instance.Gui_wkr()).Warns(); + int expd_len = expd.Len(); + int actl_len = actl.Len(); + if (expd_len == 0 && actl_len == 0) {} + else if (actl_len == 0) { + Gftest.Fail("expected some errors; got zero; expd={0}", expd.To_str()); + } + else if (expd_len == 0) { + Gftest.Fail("expected zero errors; got some; actl={0}", actl.To_str()); + } + else { + for (int i = 0; i < actl_len; i++) { + String actl_err = (String)actl.Get_at(i); + Object[] expd_err_ary = (Object[])expd.Get_at(i); + if (Bool_.Cast(expd_err_ary[0])) { + Gftest.Eq__bool(true, String_.Has(actl_err, (String)expd_err_ary[1])); + } + else { + Gftest.Eq__str((String)expd_err_ary[1], (String)actl_err); + } + } + } + } +} diff --git a/100_core/src/gplx/core/tests/Gfo_test_itm.java b/100_core/src/gplx/core/tests/Gfo_test_itm.java new file mode 100644 index 000000000..1b6843578 --- /dev/null +++ b/100_core/src/gplx/core/tests/Gfo_test_itm.java @@ -0,0 +1,104 @@ +/* +XOWA: the XOWA Offline Wiki Application +Copyright (C) 2012-2017 gnosygnu@gmail.com + +XOWA is licensed under the terms of the General Public License (GPL) Version 3, +or alternatively under the terms of the Apache License Version 2.0. + +You may use XOWA according to either of these licenses as is most appropriate +for your project on a case-by-case basis. + +The terms of each license can be found in the source code repository: + +GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt +Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt +*/ +package gplx.core.tests; import gplx.*; import gplx.core.*; +import gplx.core.strings.*; +public class Gfo_test_itm { + private final boolean is_expd; + private final Ordered_hash hash = Ordered_hash_.New(); + Gfo_test_itm(boolean is_expd) { + this.is_expd = is_expd; + } + public int Len() {return hash.Len();} + public Gfo_test_itm Add(String key, Object val) {hash.Add(key, Keyval_.new_(key, val)); return this;} + private Object Get_by_val(String key) {return ((Keyval)hash.Get_by_or_fail(key)).Val();} + public String Get_str(String key) { + Object val_obj = Get_by_val(key); + if (Type_.Eq_by_obj(val_obj, byte[].class)) { + return String_.new_u8((byte[])val_obj); + } + else + return (String)val_obj; + } + public void Test_bool(String key, boolean val) { + boolean cur = Bool_.Cast(Get_by_val(key)); + boolean expd = is_expd ? cur : val; + boolean actl = is_expd ? val : cur; + Gftest.Eq__bool(expd, actl); + } + public void Test_bry(String key, byte[] val) { + byte[] cur = (byte[])Get_by_val(key); + byte[] expd = is_expd ? cur : val; + byte[] actl = is_expd ? val : cur; + Gftest.Eq__bry(expd, actl); + } + public void Test_long(String key, long val) { + long cur = Long_.cast(Get_by_val(key)); + long expd = is_expd ? cur : val; + long actl = is_expd ? val : cur; + Gftest.Eq__long(expd, actl); + } + public void Test_int(String key, int val) { + int cur = Int_.Cast(Get_by_val(key)); + int expd = is_expd ? cur : val; + int actl = is_expd ? val : cur; + Gftest.Eq__int(expd, actl); + } + public void Test_byte(String key, byte val) { + byte cur = Byte_.Cast(Get_by_val(key)); + byte expd = is_expd ? cur : val; + byte actl = is_expd ? val : cur; + Gftest.Eq__byte(expd, actl); + } + public void Test_str(String key, String val) { + String cur = String_.cast(Get_by_val(key)); + String expd = is_expd ? cur : val; + String actl = is_expd ? val : cur; + Gftest.Eq__str(expd, actl); + } + public void Test_actl(Gfo_test_itm actl) { + int expd_len = hash.Len(); + String[] expd_ary = new String[expd_len]; + String[] actl_ary = new String[expd_len]; + for (int i = 0; i < expd_len; i++) { + Keyval expd_kv = (Keyval)hash.Get_at(i); + String key = expd_kv.Key(); + expd_ary[i] = Object_.Xto_str_strict_or_null(expd_kv.Val()); + + Keyval actl_kv = (Keyval)actl.hash.Get_by(key); + actl_ary[i] = actl_kv == null ? "MISSING" : Object_.Xto_str_strict_or_null(actl_kv.Val()); + } + Gftest.Eq__ary(expd_ary, actl_ary); + } + public String To_str(boolean single_line) { + String_bldr bldr = String_bldr_.new_(); + int len = hash.Len(); + String itm_dlm = single_line ? ";" : "\n"; + for (int i = 0; i < len; i++) { + Object itm = hash.Get_at(i); + int itm_tid = Type_ids_.To_id_by_obj(itm); + if (itm_tid == Type_ids_.Id__bry) + itm = String_.new_u8((byte[])itm); + bldr.Add_obj(itm); + bldr.Add(itm_dlm); + } + return bldr.To_str(); + } + @Override public String toString() { + return this.To_str(true); + } + public static Gfo_test_itm New__actl() {return new Gfo_test_itm(false);} + public static Gfo_test_itm New__expd() {return new Gfo_test_itm(true);} +} diff --git a/100_core/src/gplx/core/tests/Gfo_test_lnr_base.java b/100_core/src/gplx/core/tests/Gfo_test_lnr_base.java new file mode 100644 index 000000000..db85d385c --- /dev/null +++ b/100_core/src/gplx/core/tests/Gfo_test_lnr_base.java @@ -0,0 +1,69 @@ +/* +XOWA: the XOWA Offline Wiki Application +Copyright (C) 2012-2017 gnosygnu@gmail.com + +XOWA is licensed under the terms of the General Public License (GPL) Version 3, +or alternatively under the terms of the Apache License Version 2.0. + +You may use XOWA according to either of these licenses as is most appropriate +for your project on a case-by-case basis. + +The terms of each license can be found in the source code repository: + +GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt +Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt +*/ +package gplx.core.tests; import gplx.*; import gplx.core.*; +import gplx.core.strings.*; +public class Gfo_test_lnr_base { + private String[] keys; + public List_adp Expd() {return expd_list;} private final List_adp expd_list = List_adp_.New(); + public List_adp Actl() {return actl_list;} private final List_adp actl_list = List_adp_.New(); + public void Clear() { + expd_list.Clear(); + actl_list.Clear(); + } + public void Add_actl_args(Object... vals) { + int len = vals.length; + Gfo_test_itm itm = Gfo_test_itm.New__actl(); + for (int i = 0; i < len; i++) { + itm.Add(keys[i], vals[i]); + } + actl_list.Add(itm); + } + public void Test() {Test(null);} + public void Test(Gfo_test_lnr_itm_cbk cbk) { + int expd_len = expd_list.Len(); + int actl_len = actl_list.Len(); + if (expd_len == 0 && actl_len == 0) {} + else if (actl_len == 0) { + Gftest.Fail("expected some itms; got zero; expd={0}", expd_list.To_str()); + } + else if (expd_len == 0) { + Gftest.Fail("expected zero itms; got some; actl={0}", actl_list.To_str()); + } + else { + for (int i = 0; i < actl_len; i++) { + Gfo_test_itm expd_itm = (Gfo_test_itm)expd_list.Get_at(i); + Gfo_test_itm actl_itm = (Gfo_test_itm)actl_list.Get_at(i); + expd_itm.Test_actl(actl_itm); + if (cbk != null) + cbk.Test_itm(i, expd_len, expd_itm, actl_itm); + } + } + } + public String To_str() { + String_bldr sb = String_bldr_.new_(); + int len = actl_list.Len(); + for (int i = 0; i < len; i++) { + Gfo_test_itm itm = (Gfo_test_itm)actl_list.Get_at(i); + sb.Add(itm.To_str(true)).Add_char_nl(); + } + return sb.To_str_and_clear(); + } + public static Gfo_test_lnr_base New__keys(String... keys) { + Gfo_test_lnr_base rv = new Gfo_test_lnr_base(); + rv.keys = keys; + return rv; + } +} diff --git a/100_core/src/gplx/core/tests/Gfo_test_lnr_itm_cbk.java b/100_core/src/gplx/core/tests/Gfo_test_lnr_itm_cbk.java new file mode 100644 index 000000000..e57779226 --- /dev/null +++ b/100_core/src/gplx/core/tests/Gfo_test_lnr_itm_cbk.java @@ -0,0 +1,19 @@ +/* +XOWA: the XOWA Offline Wiki Application +Copyright (C) 2012-2017 gnosygnu@gmail.com + +XOWA is licensed under the terms of the General Public License (GPL) Version 3, +or alternatively under the terms of the Apache License Version 2.0. + +You may use XOWA according to either of these licenses as is most appropriate +for your project on a case-by-case basis. + +The terms of each license can be found in the source code repository: + +GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt +Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt +*/ +package gplx.core.tests; import gplx.*; import gplx.core.*; +public interface Gfo_test_lnr_itm_cbk { + void Test_itm(int i, int len, Gfo_test_itm expd_itm, Gfo_test_itm actl_itm); +} diff --git a/100_core/src/gplx/core/tests/Gftest.java b/100_core/src/gplx/core/tests/Gftest.java index 891d71473..613ba3c90 100644 --- a/100_core/src/gplx/core/tests/Gftest.java +++ b/100_core/src/gplx/core/tests/Gftest.java @@ -17,6 +17,9 @@ package gplx.core.tests; import gplx.*; import gplx.core.*; import gplx.core.brys.*; public class Gftest { private static final Bry_bfr bfr = Bry_bfr_.New(); + public static void Fail(String msg_fmt, Object... msg_args) { + throw Err_.new_wo_type(String_.Format(msg_fmt, msg_args)); + } public static void Eq__ary(Object[] expd, Object[] actl, String msg_fmt, Object... msg_args) {Eq__array(Type_ids_.Id__obj, expd, actl, msg_fmt, msg_args);} public static void Eq__ary(boolean[] expd, boolean[] actl, String msg_fmt, Object... msg_args) {Eq__array(Type_ids_.Id__bool, expd, actl, msg_fmt, msg_args);} public static void Eq__ary(int[] expd, int[] actl) {Eq__array(Type_ids_.Id__int, expd, actl, "");} @@ -61,6 +64,7 @@ public class Gftest { if (actl == null) actl = Str__null; Eq__str(Object_.Xto_str_or(expd, Str__null), Object_.Xto_str_or(actl, null), Str__null); } + public static void Eq__bry(byte[] expd, String actl) {Eq__str(String_.new_u8(expd), actl, "no_msg");} public static void Eq__str(String expd, byte[] actl, String msg_fmt, Object... msg_args) {Eq__str(expd, String_.new_u8(actl), msg_fmt, msg_args);} public static void Eq__str(String expd, byte[] actl) {Eq__str(expd, String_.new_u8(actl), null);} public static void Eq__str(String expd, String actl) {Eq__str(expd, actl, null);}