1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2026-03-02 03:49:30 +00:00

Core: Refactor base classes

This commit is contained in:
gnosygnu
2016-11-28 08:28:00 -05:00
parent 12459429b4
commit 83cf992f48
60 changed files with 172 additions and 343 deletions

View File

@@ -44,12 +44,7 @@ public class GfsCore_ {
Gfo_invk invk = Gfo_invk_.as_(rv);
Object primitive = null;
if (invk == null) { // rv is primitive; find appropriate mgr
Class<?> type = rv.getClass();
if (type == String.class) invk = String_.Gfs;
else if (Int_.TypeMatch(type)) invk = Int_.Gfs;
else if (Type_adp_.Eq(type, Bool_.Cls_ref_type)) invk = Bool_.Gfs;
else throw Err_.new_wo_type("unknown primitive", "type", Type_adp_.NameOf_type(type), "obj", Object_.Xto_str_strict_or_null_mark(rv));
primitive = rv;
throw Err_.new_wo_type("unknown primitive", "type", Type_adp_.NameOf_type(rv.getClass()), "obj", Object_.Xto_str_strict_or_null_mark(rv));
}
Object exec_rv = null;
int len = owner_msg.Subs_count();

View File

@@ -1,113 +0,0 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.langs.gfs; import gplx.*; import gplx.langs.*;
import org.junit.*;
public class GfsCore_tst {
@Before public void setup() {
core = GfsCore.new_();
core.AddObj(String_.Gfs, "String_");
core.AddObj(Int_.Gfs, "Int_");
} GfsCore core;
@Test public void Basic() { // String_.Len('abc') >> 3
tst_Msg
( msg_("String_").Subs_
( msg_("Len").Add("v", "abc"))
, 3);
}
@Test public void PrimitiveConversion() { // String_.Len('abc').Add(-3) >> 0
tst_Msg
( msg_("String_").Subs_
( msg_("Len").Add("v", "abc").Subs_
( msg_("Add").Add("operand", -3))
)
, 0);
}
// @Test public void Fail_notFound() { // String_.DoesNotExists
// tst_Err
// ( msg_("help").Add("", "String_.DoesNotExist")
// , GfsHelp.Err_Unhandled("String_", "DoesNotExist"));
// }
@Test public void Cmd() { // cmd
core.AddCmd(new GfsTest_cmd(), "testCmd");
tst_Msg
( msg_("testCmd").Add("s", "pass")
, "pass");
}
@Test public void EmptyMsg() {
tst_Msg
( msg_("")
, Gfo_invk_.Rv_unhandled);
}
// @Test public void Fail_argMissing() { // String_.Len()
// tst_String__Len_Err(msg_("Len"), GfsCtx.Err_KeyNotFound("v", "<<EMPTY>>"));
// }
// @Test public void Fail_argWrongKey() { // String_.Len(badKey='abc')
// tst_String__Len_Err(msg_("Len").Add("badKey", "abc"), GfsCtx.Err_KeyNotFound("v", "badKey;"));
// }
// @Test public void Fail_argExtraKey() { // String_.Len(v='abc' extraKey=1)
// tst_String__Len_Err(msg_("Len").Add("v", "abc").Add("extraKey", 1), GfsCtx.Err_KeyNotFound("v", "badKey;"));
// }
@Test public void Add_obj_deep() { // String_.Len(badKey='abc')
GfsCore_tst_nest obj1 = GfsCore_tst_nest.new_("1", "val1");
GfsCore_tst_nest obj1_1 = GfsCore_tst_nest.new_("1_1", "val2");
core.AddObj(obj1, "1");
core.AddDeep(obj1_1, "1", "1_1");
GfoMsg root = GfoMsg_.root_("1", "1_1", GfsCore_tst_nest.Prop2);
Object actl = core.ExecOne(GfsCtx.Instance, root);
Tfds.Eq("val2", actl);
}
void tst_String__Len_Err(GfoMsg m, Err expd) {
tst_Err(msg_("String_").Subs_(m), expd);
}
void tst_Err(GfoMsg msg, Err expd) {
GfoMsg root = msg;
GfsCtx ctx = GfsCtx.new_();
try {
core.ExecOne(ctx, root);
Tfds.Fail_expdError();
}
catch (Exception e) {
Tfds.Eq_err(expd, e);
}
}
GfoMsg msg_(String k) {return GfoMsg_.new_cast_(k);}
void tst_Msg(GfoMsg msg, Object expd) {
GfsCtx ctx = GfsCtx.new_();
Object actl = core.ExecOne(ctx, msg);
Tfds.Eq(expd, actl);
}
}
class GfsCore_tst_nest implements Gfo_invk, Gfo_invk_cmd_mgr_owner {
public Gfo_invk_cmd_mgr InvkMgr() {return invkMgr;} Gfo_invk_cmd_mgr invkMgr = Gfo_invk_cmd_mgr.new_();
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
if (ctx.Match(k, Prop1)) {return prop1;}
else if (ctx.Match(k, Prop2)) {return prop2;}
else if (ctx.Match(k, prop1)) {return this;}
else return invkMgr.Invk(ctx, ikey, k, m, this);
} public static final String Prop1 = "Prop1", Prop2 = "Prop2";
String prop1, prop2;
public static GfsCore_tst_nest new_(String prop1, String prop2) {
GfsCore_tst_nest rv = new GfsCore_tst_nest();
rv.prop1 = prop1; rv.prop2 = prop2;
return rv;
} GfsCore_tst_nest() {}
}
class GfsTest_cmd implements Gfo_invk {
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {return m.ReadStr("s");}
}

View File

@@ -20,8 +20,6 @@ import gplx.core.gfo_regys.*;
public class GfsLibIni_core implements GfsLibIni {
public void Ini(GfsCore core) {
core.AddCmd(GfsCoreHelp.new_(core), "help");
core.AddObj(String_.Gfs, "String_");
core.AddObj(Int_.Gfs, "Int_");
core.AddObj(DateAdp_.Gfs, "Date_");
core.AddObj(RandomAdp_.Gfs, "RandomAdp_");
core.AddObj(GfoTemplateFactory.Instance, "factory");
@@ -32,5 +30,5 @@ public class GfsLibIni_core implements GfsLibIni {
GfoRegy.Instance.Parsers().Add("Io_url", Io_url_.Parser);
}
public static final GfsLibIni_core Instance = new GfsLibIni_core(); GfsLibIni_core() {}
public static final GfsLibIni_core Instance = new GfsLibIni_core(); GfsLibIni_core() {}
}