XOMW: More XomwHooks implementation [#632]

staging
gnosygnu 4 years ago
parent 0c3cb1ba3d
commit a6b128422e

@ -1,18 +1,18 @@
/*
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
*/
/*
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.xowa.mediawiki; import gplx.*; import gplx.xowa.*;
import gplx.core.brys.*;
// NOTE: Object-representation of PHP Array; REF.PHP: https://www.php.net/manual/en/language.types.array.php

@ -247,4 +247,29 @@ public class XophpArray_ {
}
return false;
}
// REF.PHP: https://www.php.net/manual/en/function.array-shift.php
// Returns the shifted value, or NULL if array is empty or is not an array.
public static Object array_shift(XophpArray array) {
if (array == null) {
return null;
}
int len = array.Len();
if (len == 0) {
return null;
}
XophpArrayItm[] itms = array.To_ary();
array.Clear();
int idx = 0;
for (int i = 1; i < len; i++) {
XophpArrayItm itm = itms[i];
if (itm.Key_is_int()) {
array.Add(idx++, itm.Val());
}
else {
array.Add(itm.Key(), itm.Val());
}
}
return itms[0].Val();
}
}

@ -19,9 +19,8 @@ package gplx.xowa.mediawiki.includes;
import gplx.xowa.mediawiki.*;
/*
TODO:
* $wgHooks
* array_shift
* Closure?
* class XophpClosure: https://www.php.net/manual/en/class.closure.php
* array_filter: https://www.php.net/manual/en/function.array-filter.php
*/
/**
@ -78,9 +77,7 @@ public class XomwHooks {
* @return bool True if the hook has a function registered to it
*/
public static boolean isRegistered(String name) {
// global $wgHooks;
// return !XophpArray_.empty($wgHooks[$name]) || !XophpArray_.empty(handlers[$name]);
return false;
return !XophpObject_.empty_obj(XomwDefaultSettings.wgHooks.Get_by(name)) || !XophpObject_.empty_obj(handlers.Get_by(name));
}
/**
@ -93,17 +90,14 @@ public class XomwHooks {
* @return array
*/
public static XophpArray getHandlers(String name) {
// global $wgHooks;
if (!isRegistered(name)) {
return XophpArray.New();
// } else if (!XophpArray_.isset(handlers, name)) {
// return $wgHooks[name];
// } else if (!isset($wgHooks[name])) {
// return handlers[name];
} else if (!XophpArray_.isset(handlers, name)) {
return XomwDefaultSettings.wgHooks.Get_by_ary(name);
} else if (!XophpArray_.isset(XomwDefaultSettings.wgHooks, name)) {
return handlers.Get_by_ary(name);
} else {
// return XophpArray_.array_merge(handlers.Get_by_ary(name), $wgHooks[name]);
return XophpArray_.array_merge(handlers.Get_by_ary(name));
return XophpArray_.array_merge(handlers.Get_by_ary(name), XomwDefaultSettings.wgHooks.Get_by_ary(name));
}
}

@ -1,6 +1,6 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com
Copyright (C) 2012-2020 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.
@ -13,273 +13,330 @@ 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.xowa.mediawiki; import gplx.*; import gplx.xowa.*;
import org.junit.*; import gplx.core.tests.*;
public class XophpArray__tst { // REF:https://www.php.net/manual/en/function.array-merge.php
private final XophpArray__fxt fxt = new XophpArray__fxt();
@Test public void array_merge__basic() {
XophpArray ary1 = fxt.Make().Add("key1", "val1").Add("a");
XophpArray ary2 = fxt.Make().Add("key2", "val2").Add("b");
fxt.Test__eq
( fxt.Make().Add("key1", "val1").Add("a").Add("key2", "val2").Add("b")
, XophpArray_.array_merge(ary1, ary2));
}
@Test public void array_merge__same_key() {
XophpArray ary1 = fxt.Make().Add("key", "val1");
XophpArray ary2 = fxt.Make().Add("key", "val2");
fxt.Test__eq
( fxt.Make().Add("key", "val2")
, XophpArray_.array_merge(ary1, ary2));
}
@Test public void array_merge__same_idx() {
XophpArray ary1 = fxt.Make().Add(0, "a");
XophpArray ary2 = fxt.Make().Add(0, "b");
fxt.Test__eq
( fxt.Make().Add(0, "a").Add(1, "b")
, XophpArray_.array_merge(ary1, ary2));
}
@Test public void array_merge__renumber() {
XophpArray ary1 = fxt.Make().Add(3, "a");
XophpArray ary2 = fxt.Make().Add(2, "b");
fxt.Test__eq
( fxt.Make().Add(0, "a").Add(1, "b")
, XophpArray_.array_merge(ary1, ary2));
}
@Test public void array_merge__example_1() {
XophpArray ary1 = fxt.Make().Add("color", "red").Add_many(2, 4);
XophpArray ary2 = fxt.Make().Add_many("a", "b").Add("color", "green").Add("shape", "trapezoid").Add(4);
fxt.Test__eq
( fxt.Make().Add("color", "green").Add_many(2, 4, "a", "b").Add("shape", "trapezoid").Add(4)
, XophpArray_.array_merge(ary1, ary2));
}
@Test public void array_merge__example_2() {
XophpArray ary1 = fxt.Make();
XophpArray ary2 = fxt.Make().Add(1, "data");
fxt.Test__eq
( fxt.Make().Add(0, "data")
, XophpArray_.array_merge(ary1, ary2));
}
@Test public void array_add() {
XophpArray ary1 = fxt.Make().Add(0, "zero_a").Add(2, "two_a").Add(3, "three_a");
XophpArray ary2 = fxt.Make().Add(1, "one_b").Add(3, "three_b").Add(4, "four_b");
fxt.Test__eq
( fxt.Make().Add(0, "zero_a").Add(2, "two_a").Add(3, "three_a").Add(1, "one_b").Add(4, "four_b")
, XophpArray_.array_add(ary1, ary2));
}
@Test public void array_splice__bgn_is_positive() {
XophpArray src = fxt.Make().Add_many("a", "b", "c", "d");
XophpArray del = XophpArray_.array_splice(src, 1);
fxt.Test__eq
( fxt.Make().Add_many("a")
, src
);
fxt.Test__eq
( fxt.Make().Add_many("b", "c", "d")
, del
);
}
@Test public void array_splice__bgn_is_positive_large() {
XophpArray src = fxt.Make().Add_many("a", "b", "c", "d");
XophpArray del = XophpArray_.array_splice(src, 99);
fxt.Test__eq
( fxt.Make().Add_many("a", "b", "c", "d")
, src
);
fxt.Test__eq
( fxt.Make().Add_many()
, del
);
}
@Test public void array_splice__bgn_is_negative() {
XophpArray src = fxt.Make().Add_many("a", "b", "c", "d");
XophpArray del = XophpArray_.array_splice(src, -3);
fxt.Test__eq
( fxt.Make().Add_many("a")
, src
);
fxt.Test__eq
( fxt.Make().Add_many("b", "c", "d")
, del
);
}
@Test public void array_splice__bgn_is_negative_large() {
XophpArray src = fxt.Make().Add_many("a", "b", "c", "d");
XophpArray del = XophpArray_.array_splice(src, -99);
fxt.Test__eq
( fxt.Make()
, src
);
fxt.Test__eq
( fxt.Make().Add_many("a", "b", "c", "d")
, del
);
}
@Test public void array_splice__len_is_positive() {
XophpArray src = fxt.Make().Add_many("a", "b", "c", "d");
XophpArray del = XophpArray_.array_splice(src, 1, 2);
fxt.Test__eq
( fxt.Make().Add_many("a", "d")
, src
);
fxt.Test__eq
( fxt.Make().Add_many("b", "c")
, del
);
}
@Test public void array_splice__len_is_positive_large() {
XophpArray src = fxt.Make().Add_many("a", "b", "c", "d");
XophpArray del = XophpArray_.array_splice(src, 1, 99);
fxt.Test__eq
( fxt.Make().Add_many("a")
, src
);
fxt.Test__eq
( fxt.Make().Add_many("b", "c", "d")
, del
);
}
@Test public void array_splice__len_is_negative() {
XophpArray src = fxt.Make().Add_many("a", "b", "c", "d");
XophpArray del = XophpArray_.array_splice(src, 1, -2);
fxt.Test__eq
( fxt.Make().Add_many("a", "c", "d")
, src
);
fxt.Test__eq
( fxt.Make().Add_many("b")
, del
);
}
@Test public void array_splice__len_is_negative_large() {
XophpArray src = fxt.Make().Add_many("a", "b", "c", "d");
XophpArray del = XophpArray_.array_splice(src, 1, -99);
fxt.Test__eq
( fxt.Make().Add_many("a", "b", "c", "d")
, src
);
fxt.Test__eq
( fxt.Make()
, del
);
}
@Test public void array_splice__repl() {
XophpArray src = fxt.Make().Add(0, "a").Add(1, "b").Add(2, "c").Add(3, "d");
XophpArray del = XophpArray_.array_splice(src, 1, 2, fxt.Make().Add(0, "x"));
fxt.Test__eq
( fxt.Make().Add(0, "a").Add(1, "x").Add(2, "d")
, src
);
fxt.Test__eq
( fxt.Make().Add(0, "b").Add(1, "c")
, del
);
}
@Test public void array_splice__example_1a() {
XophpArray src = fxt.Make().Add_many("red", "green", "blue", "yellow");
XophpArray del = XophpArray_.array_splice(src, 2);
fxt.Test__eq
( fxt.Make().Add_many("red", "green")
, src
);
fxt.Test__eq
( fxt.Make().Add_many("blue", "yellow")
, del
);
}
@Test public void array_splice__example_1b() {
XophpArray src = fxt.Make().Add_many("red", "green", "blue", "yellow");
XophpArray del = XophpArray_.array_splice(src, 1, -1);
fxt.Test__eq
( fxt.Make().Add_many("red", "yellow")
, src
);
fxt.Test__eq
( fxt.Make().Add_many("green", "blue")
, del
);
}
@Test public void array_splice__example_1c() {
XophpArray src = fxt.Make().Add_many("red", "green", "blue", "yellow");
XophpArray del = XophpArray_.array_splice(src, 1, 4, XophpArray.New("orange"));
fxt.Test__eq
( fxt.Make().Add_many("red", "orange")
, src
);
fxt.Test__eq
( fxt.Make().Add_many("green", "blue", "yellow")
, del
);
}
@Test public void array_splice__example_1d() {
XophpArray src = fxt.Make().Add_many("red", "green", "blue", "yellow");
XophpArray del = XophpArray_.array_splice(src, -1, 1, XophpArray.New("black", "maroon"));
fxt.Test__eq
( fxt.Make().Add_many("red", "green", "blue", "black", "maroon")
, src
);
fxt.Test__eq
( fxt.Make().Add_many("yellow")
, del
);
}
@Test public void values() {
XophpArray orig = fxt.Make().Add("size", "XL").Add("color", "gold");
fxt.Test__eq
( fxt.Make().Add(0, "XL").Add(1, "gold")
, orig.values()
);
}
@Test public void array_map() {
XophpArray orig = fxt.Make().Add_many("a", "b", "c");
fxt.Test__eq
( fxt.Make().Add_many("A", "B", "C")
, XophpArray_.array_map(XophpString_.Callback_owner, "strtoupper", orig)
);
}
@Test public void array_flip__basic() {
XophpArray orig = fxt.Make().Add_many("oranges", "apples", "pears");
fxt.Test__eq
( fxt.Make().Add("oranges", 0).Add("apples", 1).Add("pears", 2)
, XophpArray_.array_flip(orig)
);
}
@Test public void array_flip__collision() {
XophpArray orig = fxt.Make().Add("a", 1).Add("b", 1).Add("c", 2);
fxt.Test__eq
( fxt.Make().Add("1", "b").Add("2", "c")
, XophpArray_.array_flip(orig)
);
}
@Test public void implode() {
XophpArray orig = fxt.Make().Add_many("a", "b", "c");
Gftest.Eq__str
( "a b c"
, XophpArray_.implode(" ", orig)
);
}
@Test public void in_array() {
// PHP samples
XophpArray array;
// Example #1
array = XophpArray.New("Mac", "NT", "Irix", "Linux");
Gftest.Eq__bool(Bool_.Y, XophpArray_.in_array("Irix", array));
Gftest.Eq__bool(Bool_.N, XophpArray_.in_array("mac" , array));
// Example #2
array = XophpArray.New(12.4d, 1.13d);
Gftest.Eq__bool(Bool_.N, XophpArray_.in_array("12.4", array, true));
Gftest.Eq__bool(Bool_.Y, XophpArray_.in_array( 1.13d, array, true));
// Example #3
array = XophpArray.New(XophpArray.New('p', 'h'), XophpArray.New('p', 'r'), 'o');
Gftest.Eq__bool(Bool_.Y, XophpArray_.in_array(XophpArray.New('p', 'h'), array));
Gftest.Eq__bool(Bool_.N, XophpArray_.in_array(XophpArray.New('f', 'i'), array));
Gftest.Eq__bool(Bool_.Y, XophpArray_.in_array('o', array));
}
}
class XophpArray__fxt {
public XophpArray Make() {return new XophpArray();}
public void Test__eq(XophpArray expd, XophpArray actl) {
Gftest.Eq__str(expd.To_str(), actl.To_str());
}
}
package gplx.xowa.mediawiki;
import gplx.Bool_;
import gplx.core.tests.Gftest;
import org.junit.Test;
public class XophpArray__tst {
private final XophpArray__fxt fxt = new XophpArray__fxt();
// REF.PHP:https://www.php.net/manual/en/function.array-merge.php
@Test public void array_merge__basic() {
XophpArray ary1 = fxt.Make().Add("key1", "val1").Add("a");
XophpArray ary2 = fxt.Make().Add("key2", "val2").Add("b");
fxt.Test__eq
( fxt.Make().Add("key1", "val1").Add("a").Add("key2", "val2").Add("b")
, XophpArray_.array_merge(ary1, ary2));
}
@Test public void array_merge__same_key() {
XophpArray ary1 = fxt.Make().Add("key", "val1");
XophpArray ary2 = fxt.Make().Add("key", "val2");
fxt.Test__eq
( fxt.Make().Add("key", "val2")
, XophpArray_.array_merge(ary1, ary2));
}
@Test public void array_merge__same_idx() {
XophpArray ary1 = fxt.Make().Add(0, "a");
XophpArray ary2 = fxt.Make().Add(0, "b");
fxt.Test__eq
( fxt.Make().Add(0, "a").Add(1, "b")
, XophpArray_.array_merge(ary1, ary2));
}
@Test public void array_merge__renumber() {
XophpArray ary1 = fxt.Make().Add(3, "a");
XophpArray ary2 = fxt.Make().Add(2, "b");
fxt.Test__eq
( fxt.Make().Add(0, "a").Add(1, "b")
, XophpArray_.array_merge(ary1, ary2));
}
@Test public void array_merge__example_1() {
XophpArray ary1 = fxt.Make().Add("color", "red").Add_many(2, 4);
XophpArray ary2 = fxt.Make().Add_many("a", "b").Add("color", "green").Add("shape", "trapezoid").Add(4);
fxt.Test__eq
( fxt.Make().Add("color", "green").Add_many(2, 4, "a", "b").Add("shape", "trapezoid").Add(4)
, XophpArray_.array_merge(ary1, ary2));
}
@Test public void array_merge__example_2() {
XophpArray ary1 = fxt.Make();
XophpArray ary2 = fxt.Make().Add(1, "data");
fxt.Test__eq
( fxt.Make().Add(0, "data")
, XophpArray_.array_merge(ary1, ary2));
}
@Test public void array_add() {
XophpArray ary1 = fxt.Make().Add(0, "zero_a").Add(2, "two_a").Add(3, "three_a");
XophpArray ary2 = fxt.Make().Add(1, "one_b").Add(3, "three_b").Add(4, "four_b");
fxt.Test__eq
( fxt.Make().Add(0, "zero_a").Add(2, "two_a").Add(3, "three_a").Add(1, "one_b").Add(4, "four_b")
, XophpArray_.array_add(ary1, ary2));
}
@Test public void array_splice__bgn_is_positive() {
XophpArray src = fxt.Make().Add_many("a", "b", "c", "d");
XophpArray del = XophpArray_.array_splice(src, 1);
fxt.Test__eq
( fxt.Make().Add_many("a")
, src
);
fxt.Test__eq
( fxt.Make().Add_many("b", "c", "d")
, del
);
}
@Test public void array_splice__bgn_is_positive_large() {
XophpArray src = fxt.Make().Add_many("a", "b", "c", "d");
XophpArray del = XophpArray_.array_splice(src, 99);
fxt.Test__eq
( fxt.Make().Add_many("a", "b", "c", "d")
, src
);
fxt.Test__eq
( fxt.Make().Add_many()
, del
);
}
@Test public void array_splice__bgn_is_negative() {
XophpArray src = fxt.Make().Add_many("a", "b", "c", "d");
XophpArray del = XophpArray_.array_splice(src, -3);
fxt.Test__eq
( fxt.Make().Add_many("a")
, src
);
fxt.Test__eq
( fxt.Make().Add_many("b", "c", "d")
, del
);
}
@Test public void array_splice__bgn_is_negative_large() {
XophpArray src = fxt.Make().Add_many("a", "b", "c", "d");
XophpArray del = XophpArray_.array_splice(src, -99);
fxt.Test__eq
( fxt.Make()
, src
);
fxt.Test__eq
( fxt.Make().Add_many("a", "b", "c", "d")
, del
);
}
@Test public void array_splice__len_is_positive() {
XophpArray src = fxt.Make().Add_many("a", "b", "c", "d");
XophpArray del = XophpArray_.array_splice(src, 1, 2);
fxt.Test__eq
( fxt.Make().Add_many("a", "d")
, src
);
fxt.Test__eq
( fxt.Make().Add_many("b", "c")
, del
);
}
@Test public void array_splice__len_is_positive_large() {
XophpArray src = fxt.Make().Add_many("a", "b", "c", "d");
XophpArray del = XophpArray_.array_splice(src, 1, 99);
fxt.Test__eq
( fxt.Make().Add_many("a")
, src
);
fxt.Test__eq
( fxt.Make().Add_many("b", "c", "d")
, del
);
}
@Test public void array_splice__len_is_negative() {
XophpArray src = fxt.Make().Add_many("a", "b", "c", "d");
XophpArray del = XophpArray_.array_splice(src, 1, -2);
fxt.Test__eq
( fxt.Make().Add_many("a", "c", "d")
, src
);
fxt.Test__eq
( fxt.Make().Add_many("b")
, del
);
}
@Test public void array_splice__len_is_negative_large() {
XophpArray src = fxt.Make().Add_many("a", "b", "c", "d");
XophpArray del = XophpArray_.array_splice(src, 1, -99);
fxt.Test__eq
( fxt.Make().Add_many("a", "b", "c", "d")
, src
);
fxt.Test__eq
( fxt.Make()
, del
);
}
@Test public void array_splice__repl() {
XophpArray src = fxt.Make().Add(0, "a").Add(1, "b").Add(2, "c").Add(3, "d");
XophpArray del = XophpArray_.array_splice(src, 1, 2, fxt.Make().Add(0, "x"));
fxt.Test__eq
( fxt.Make().Add(0, "a").Add(1, "x").Add(2, "d")
, src
);
fxt.Test__eq
( fxt.Make().Add(0, "b").Add(1, "c")
, del
);
}
@Test public void array_splice__example_1a() {
XophpArray src = fxt.Make().Add_many("red", "green", "blue", "yellow");
XophpArray del = XophpArray_.array_splice(src, 2);
fxt.Test__eq
( fxt.Make().Add_many("red", "green")
, src
);
fxt.Test__eq
( fxt.Make().Add_many("blue", "yellow")
, del
);
}
@Test public void array_splice__example_1b() {
XophpArray src = fxt.Make().Add_many("red", "green", "blue", "yellow");
XophpArray del = XophpArray_.array_splice(src, 1, -1);
fxt.Test__eq
( fxt.Make().Add_many("red", "yellow")
, src
);
fxt.Test__eq
( fxt.Make().Add_many("green", "blue")
, del
);
}
@Test public void array_splice__example_1c() {
XophpArray src = fxt.Make().Add_many("red", "green", "blue", "yellow");
XophpArray del = XophpArray_.array_splice(src, 1, 4, XophpArray.New("orange"));
fxt.Test__eq
( fxt.Make().Add_many("red", "orange")
, src
);
fxt.Test__eq
( fxt.Make().Add_many("green", "blue", "yellow")
, del
);
}
@Test public void array_splice__example_1d() {
XophpArray src = fxt.Make().Add_many("red", "green", "blue", "yellow");
XophpArray del = XophpArray_.array_splice(src, -1, 1, XophpArray.New("black", "maroon"));
fxt.Test__eq
( fxt.Make().Add_many("red", "green", "blue", "black", "maroon")
, src
);
fxt.Test__eq
( fxt.Make().Add_many("yellow")
, del
);
}
@Test public void values() {
XophpArray orig = fxt.Make().Add("size", "XL").Add("color", "gold");
fxt.Test__eq
( fxt.Make().Add(0, "XL").Add(1, "gold")
, orig.values()
);
}
@Test public void array_map() {
XophpArray orig = fxt.Make().Add_many("a", "b", "c");
fxt.Test__eq
( fxt.Make().Add_many("A", "B", "C")
, XophpArray_.array_map(XophpString_.Callback_owner, "strtoupper", orig)
);
}
@Test public void array_flip__basic() {
XophpArray orig = fxt.Make().Add_many("oranges", "apples", "pears");
fxt.Test__eq
( fxt.Make().Add("oranges", 0).Add("apples", 1).Add("pears", 2)
, XophpArray_.array_flip(orig)
);
}
@Test public void array_flip__collision() {
XophpArray orig = fxt.Make().Add("a", 1).Add("b", 1).Add("c", 2);
fxt.Test__eq
( fxt.Make().Add("1", "b").Add("2", "c")
, XophpArray_.array_flip(orig)
);
}
@Test public void implode() {
XophpArray orig = fxt.Make().Add_many("a", "b", "c");
Gftest.Eq__str
( "a b c"
, XophpArray_.implode(" ", orig)
);
}
@Test public void in_array() {
// PHP samples
XophpArray array;
// Example #1
array = XophpArray.New("Mac", "NT", "Irix", "Linux");
Gftest.Eq__bool(Bool_.Y, XophpArray_.in_array("Irix", array));
Gftest.Eq__bool(Bool_.N, XophpArray_.in_array("mac" , array));
// Example #2
array = XophpArray.New(12.4d, 1.13d);
Gftest.Eq__bool(Bool_.N, XophpArray_.in_array("12.4", array, true));
Gftest.Eq__bool(Bool_.Y, XophpArray_.in_array( 1.13d, array, true));
// Example #3
array = XophpArray.New(XophpArray.New('p', 'h'), XophpArray.New('p', 'r'), 'o');
Gftest.Eq__bool(Bool_.Y, XophpArray_.in_array(XophpArray.New('p', 'h'), array));
Gftest.Eq__bool(Bool_.N, XophpArray_.in_array(XophpArray.New('f', 'i'), array));
Gftest.Eq__bool(Bool_.Y, XophpArray_.in_array('o', array));
}
@Test public void array_shift() {
XophpArray array;
String shifted;
// key is int
array = XophpArray.New("a", "b");
shifted = (String)XophpArray_.array_shift(array);
Gftest.Eq__str("a", shifted);
fxt.Test__eq
( XophpArray.New().Add(0, "b")
, array
);
// key is str and int
array = XophpArray.New().Add("a", "a").Add(2, "b").Add(5, "c");
shifted = (String)XophpArray_.array_shift(array);
Gftest.Eq__str("a", shifted);
fxt.Test__eq
( XophpArray.New().Add(0, "b").Add(1, "c")
, array
);
// empty
array = XophpArray.New();
shifted = (String)XophpArray_.array_shift(array);
Gftest.Eq__bool_y(shifted == null);
fxt.Test__eq
( XophpArray.New()
, array
);
// null
array = null;
shifted = (String)XophpArray_.array_shift(array);
Gftest.Eq__bool_y(shifted == null);
Gftest.Eq__bool_y(array == null);
// PHP samples
// Example #1
array = XophpArray.New("orange", "banana", "apple", "strawberry");
shifted = (String)XophpArray_.array_shift(array);
Gftest.Eq__str("orange", shifted);
fxt.Test__eq
( XophpArray.New().Add(0, "banana").Add(1, "apple").Add(2, "strawberry")
, array
);
}
}
class XophpArray__fxt {
public XophpArray Make() {return new XophpArray();}
public void Test__eq(XophpArray expd, XophpArray actl) {
Gftest.Eq__str(expd.To_str(), actl.To_str());
}
}

@ -13,172 +13,172 @@ 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.xowa.mediawiki; import gplx.*; import gplx.xowa.*;
import org.junit.*; import gplx.core.tests.*;
public class XophpArray_tst { // REF: http://php.net/manual/en/language.types.array.php
private final XophpArray_fxt fxt = new XophpArray_fxt();
@Test public void array__kvs() {
// $array = array("foo" => "bar", "bar" => "foo",);
fxt.Test__array
( XophpArray.New()
. Add("foo", "bar")
. Add("bar", "foo")
, XophpArrayItm.New_str("foo", "bar")
, XophpArrayItm.New_str("bar", "foo")
);
}
@Test public void array__casting() {
// $array = array(1 => "a", "1" => "b", 1.5 => "c", true => "d",);
fxt.Test__array
( XophpArray.New()
. Add(1 , "a")
. Add("1" , "b")
. Add(1.5 , "c")
. Add(true, "d")
, XophpArrayItm.New_int(1, "d"));
}
@Test public void array__mixed() {
// $array = array("foo" => "bar", "bar" => "foo", 100 => -100, -100 => 100);
fxt.Test__array
( XophpArray.New()
. Add("foo", "bar")
. Add("bar", "foo")
. Add(100, -100)
. Add(-100, 100)
, XophpArrayItm.New_str("foo", "bar")
, XophpArrayItm.New_str("bar", "foo")
, XophpArrayItm.New_int(100, -100)
, XophpArrayItm.New_int(-100, 100)
);
}
@Test public void array__objs() {
// $array = array("foo", "bar", "hello", "world");
fxt.Test__array
( XophpArray.New()
. Add("foo")
. Add("bar")
. Add("hello")
. Add("world")
, XophpArrayItm.New_int(0, "foo")
, XophpArrayItm.New_int(1, "bar")
, XophpArrayItm.New_int(2, "hello")
, XophpArrayItm.New_int(3, "world")
);
}
@Test public void array__unkeyed() {
// $array = array("a", "b", 6 => "c", "d");
fxt.Test__array
( XophpArray.New()
. Add("a")
. Add("b")
. Add(6, "c")
. Add("d")
, XophpArrayItm.New_int(0, "a")
, XophpArrayItm.New_int(1, "b")
, XophpArrayItm.New_int(6, "c")
, XophpArrayItm.New_int(7, "d")
);
}
@Test public void array__multidimensional() {
/*
$array = array(
"foo" => "bar",
42 => 24,
"multi" => array(
"dimensional" => array(
"array" => "foo"
)
)
);
*/
fxt.Test__array
( XophpArray.New()
. Add("foo" , "bar")
. Add(42 , 24)
. Add("multi" , XophpArray.New()
. Add("dimensional", XophpArray.New()
. Add("array", "foo")
))
, XophpArrayItm.New_str("foo", "bar")
, XophpArrayItm.New_int(42, "24")
, XophpArrayItm.New_str("multi", XophpArray.New()
. Add("dimensional", XophpArray.New()
. Add("array", "foo")
))
);
}
@Test public void array__unset() {
XophpArray ary = XophpArray.New();
ary.Add(0, "a").Add(1, "b");
// delete all
ary.unset(0);
ary.unset(1);
fxt.Test__array(ary);
// add new and assert idx is 2
ary.Add("c");
fxt.Test__array(ary, XophpArrayItm.New_int(2, "c"));
}
@Test public void Pop() {
XophpArray ary = XophpArray.New();
ary.Add(0, "a").Add(1, "b").Add(2, "c");
// pop all
fxt.Test__Pop(ary, "c");
fxt.Test__Pop(ary, "b");
fxt.Test__Pop(ary, "a");
fxt.Test__Count(ary, 0);
fxt.Test__Pop(ary, null);
}
@Test public void Itm_str_concat_end() {
XophpArray ary = XophpArray.New();
ary.Add(0, "a").Add(1, "b").Add(2, "c");
// pop all
fxt.Test__Itm_str_concat_end(ary, "a0", 0, "0");
fxt.Test__Itm_str_concat_end(ary, "b1", 1, "1");
fxt.Test__Itm_str_concat_end(ary, "c2", 2, "2");
}
@Test public void Clone() {
XophpArray ary = XophpArray.New();
ary.Add(0, "a").Add(1, "b").Add(2, "c");
fxt.Test__Eq(ary, ary.Clone());
}
@Test public void Get_by() {
XophpArray ary = XophpArray.New();
ary.Add("0", "a").Add("1", "b").Add("2", "c");
fxt.Test__Get_by(ary, "0", "a");
fxt.Test__Get_by(ary, "missing", null);
}
}
class XophpArray_fxt {
public void Test__Count(XophpArray ary, int expd) {
Gftest.Eq__int(expd, ary.count());
}
public void Test__array(XophpArray ary, XophpArrayItm... expd) {
XophpArrayItm[] actl = ary.To_ary();
Gftest.Eq__ary(expd, actl);
}
public void Test__unset(XophpArray ary, int idx, XophpArrayItm... expd) {
XophpArrayItm[] actl = ary.To_ary();
Gftest.Eq__ary(expd, actl);
}
public void Test__Pop(XophpArray ary, String expd) {
String actl = (String)ary.pop();
Gftest.Eq__str(expd, actl);
}
public void Test__Itm_str_concat_end(XophpArray ary, String expd, int idx, String v) {
ary.Itm_str_concat_end(idx, v);
String actl = ary.Get_at_str(idx);
Gftest.Eq__str(expd, actl);
}
public void Test__Eq(XophpArray lhs, XophpArray rhs) {
Gftest.Eq__ary(lhs.To_ary(), rhs.To_ary());
}
public void Test__Get_by(XophpArray ary, String key, Object expd) {
Gftest.Eq__obj_or_null(expd, ary.Get_by(key));
}
}
package gplx.xowa.mediawiki; import gplx.*; import gplx.xowa.*;
import org.junit.*; import gplx.core.tests.*;
public class XophpArray_tst { // REF: http://php.net/manual/en/language.types.array.php
private final XophpArray_fxt fxt = new XophpArray_fxt();
@Test public void array__kvs() {
// $array = array("foo" => "bar", "bar" => "foo",);
fxt.Test__array
( XophpArray.New()
. Add("foo", "bar")
. Add("bar", "foo")
, XophpArrayItm.New_str("foo", "bar")
, XophpArrayItm.New_str("bar", "foo")
);
}
@Test public void array__casting() {
// $array = array(1 => "a", "1" => "b", 1.5 => "c", true => "d",);
fxt.Test__array
( XophpArray.New()
. Add(1 , "a")
. Add("1" , "b")
. Add(1.5 , "c")
. Add(true, "d")
, XophpArrayItm.New_int(1, "d"));
}
@Test public void array__mixed() {
// $array = array("foo" => "bar", "bar" => "foo", 100 => -100, -100 => 100);
fxt.Test__array
( XophpArray.New()
. Add("foo", "bar")
. Add("bar", "foo")
. Add(100, -100)
. Add(-100, 100)
, XophpArrayItm.New_str("foo", "bar")
, XophpArrayItm.New_str("bar", "foo")
, XophpArrayItm.New_int(100, -100)
, XophpArrayItm.New_int(-100, 100)
);
}
@Test public void array__objs() {
// $array = array("foo", "bar", "hello", "world");
fxt.Test__array
( XophpArray.New()
. Add("foo")
. Add("bar")
. Add("hello")
. Add("world")
, XophpArrayItm.New_int(0, "foo")
, XophpArrayItm.New_int(1, "bar")
, XophpArrayItm.New_int(2, "hello")
, XophpArrayItm.New_int(3, "world")
);
}
@Test public void array__unkeyed() {
// $array = array("a", "b", 6 => "c", "d");
fxt.Test__array
( XophpArray.New()
. Add("a")
. Add("b")
. Add(6, "c")
. Add("d")
, XophpArrayItm.New_int(0, "a")
, XophpArrayItm.New_int(1, "b")
, XophpArrayItm.New_int(6, "c")
, XophpArrayItm.New_int(7, "d")
);
}
@Test public void array__multidimensional() {
/*
$array = array(
"foo" => "bar",
42 => 24,
"multi" => array(
"dimensional" => array(
"array" => "foo"
)
)
);
*/
fxt.Test__array
( XophpArray.New()
. Add("foo" , "bar")
. Add(42 , 24)
. Add("multi" , XophpArray.New()
. Add("dimensional", XophpArray.New()
. Add("array", "foo")
))
, XophpArrayItm.New_str("foo", "bar")
, XophpArrayItm.New_int(42, "24")
, XophpArrayItm.New_str("multi", XophpArray.New()
. Add("dimensional", XophpArray.New()
. Add("array", "foo")
))
);
}
@Test public void array__unset() {
XophpArray ary = XophpArray.New();
ary.Add(0, "a").Add(1, "b");
// delete all
ary.unset(0);
ary.unset(1);
fxt.Test__array(ary);
// add new and assert idx is 2
ary.Add("c");
fxt.Test__array(ary, XophpArrayItm.New_int(2, "c"));
}
@Test public void Pop() {
XophpArray ary = XophpArray.New();
ary.Add(0, "a").Add(1, "b").Add(2, "c");
// pop all
fxt.Test__Pop(ary, "c");
fxt.Test__Pop(ary, "b");
fxt.Test__Pop(ary, "a");
fxt.Test__Count(ary, 0);
fxt.Test__Pop(ary, null);
}
@Test public void Itm_str_concat_end() {
XophpArray ary = XophpArray.New();
ary.Add(0, "a").Add(1, "b").Add(2, "c");
// pop all
fxt.Test__Itm_str_concat_end(ary, "a0", 0, "0");
fxt.Test__Itm_str_concat_end(ary, "b1", 1, "1");
fxt.Test__Itm_str_concat_end(ary, "c2", 2, "2");
}
@Test public void Clone() {
XophpArray ary = XophpArray.New();
ary.Add(0, "a").Add(1, "b").Add(2, "c");
fxt.Test__Eq(ary, ary.Clone());
}
@Test public void Get_by() {
XophpArray ary = XophpArray.New();
ary.Add("0", "a").Add("1", "b").Add("2", "c");
fxt.Test__Get_by(ary, "0", "a");
fxt.Test__Get_by(ary, "missing", null);
}
}
class XophpArray_fxt {
public void Test__Count(XophpArray ary, int expd) {
Gftest.Eq__int(expd, ary.count());
}
public void Test__array(XophpArray ary, XophpArrayItm... expd) {
XophpArrayItm[] actl = ary.To_ary();
Gftest.Eq__ary(expd, actl);
}
public void Test__unset(XophpArray ary, int idx, XophpArrayItm... expd) {
XophpArrayItm[] actl = ary.To_ary();
Gftest.Eq__ary(expd, actl);
}
public void Test__Pop(XophpArray ary, String expd) {
String actl = (String)ary.pop();
Gftest.Eq__str(expd, actl);
}
public void Test__Itm_str_concat_end(XophpArray ary, String expd, int idx, String v) {
ary.Itm_str_concat_end(idx, v);
String actl = ary.Get_at_str(idx);
Gftest.Eq__str(expd, actl);
}
public void Test__Eq(XophpArray lhs, XophpArray rhs) {
Gftest.Eq__ary(lhs.To_ary(), rhs.To_ary());
}
public void Test__Get_by(XophpArray ary, String key, Object expd) {
Gftest.Eq__obj_or_null(expd, ary.Get_by(key));
}
}
Loading…
Cancel
Save