XOMW: Add Assert [#632]

staging
gnosygnu 4 years ago
parent 49f8d4b000
commit 43866163b9

@ -19,6 +19,7 @@ import gplx.Err;
// REF.PHP:https://www.php.net/manual/en/class.exception.php
public class XophpException extends Err {
public XophpException() {this("", 0, null);}
public XophpException(String message) {this(message, 0, null);}
public XophpException(String message, int code, XophpException previous) {
super(true, "", "", message);

@ -13,7 +13,11 @@ 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.*;
package gplx.xowa.mediawiki;
import gplx.Err;
import gplx.String_;
public class XophpInvalidArgumentException extends Err {
public XophpInvalidArgumentException(String fmt, Object... args) {
super(true, "", "", String_.Format(fmt, args));

@ -0,0 +1,25 @@
/*
XOWA: the XOWA Offline Wiki Application
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.
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;
// REF.PHP:https://www.php.net/manual/en/class.logicexception.php
public class XophpLogicException extends XophpException {
public XophpLogicException() {}
public XophpLogicException(String message) {super(message);}
public XophpLogicException(String message, int code, XophpException previous) {
super(message, code, previous);
}
}

@ -15,6 +15,7 @@ Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/
package gplx.xowa.mediawiki;
public class XophpRuntimeException extends XophpException {
public XophpRuntimeException() {}
public XophpRuntimeException(String message) {super(message);}
public XophpRuntimeException(String message, int code, XophpException previous) {
super(message, code, previous);

@ -53,4 +53,5 @@ public class XophpType_ {
}
public static Class<?> get_class(Object o) {return o.getClass();}
public static String To_str(Class c) {return c.getCanonicalName();}
}

@ -19,10 +19,7 @@ import gplx.xowa.mediawiki.XophpArray;
import gplx.xowa.mediawiki.XophpCallback;
import gplx.xowa.mediawiki.XophpObject_;
import gplx.xowa.mediawiki.XophpType_;
/*
XOTODO:
* XomwAssert: /vendor/wikimedia/Assert/src
*/
// MW.SRC:1.33.1
/**
* ServiceContainer provides a generic service to manage named services using
@ -132,7 +129,7 @@ public class XomwServiceContainer implements XomwDestructibleService {
* instantiator functions.
*/
public void applyWiring(XophpArray $serviceInstantiators) {
// Assert::parameterElementType('callable', $serviceInstantiators, '$serviceInstantiators');
// XomwAssert.parameterElementType(XophpCallback.class, $serviceInstantiators, "serviceInstantiators");
// foreach ($serviceInstantiators as $name => $instantiator) {
// this.defineService($name, $instantiator);

@ -0,0 +1,226 @@
/*
XOWA: the XOWA Offline Wiki Application
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.
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.vendor.wikimedia.Assert.src;
import gplx.xowa.mediawiki.XophpArray;
import gplx.xowa.mediawiki.XophpObject_;
import gplx.xowa.mediawiki.XophpType;
import gplx.xowa.mediawiki.XophpType_;
// MW.SRC:1.33.1
/**
* Assert provides functions for assorting preconditions (such as parameter types) and
* postconditions. It is intended as a safer alternative to PHP's assert() function.
*
* Note that assertions evaluate expressions and add function calls, so using assertions
* may have a negative impact on performance when used in performance hotspots. The idea
* if this class is to have a neat tool for assertions if and when they are needed.
* It is not recommended to place assertions all over the code indiscriminately.
*
* For more information, see the the README file.
*
* @license MIT
* @author Daniel Kinzler
* @copyright Wikimedia Deutschland e.V.
*/
public class XomwAssert {
/**
* Checks a precondition, that is, throws a PreconditionException if condition is false.
* For checking call parameters, use Assert::parameter() instead.
*
* This is provided for completeness, most preconditions should be covered by
* Assert::parameter() and related assertions.
*
* @see parameter()
*
* @note This is intended mostly for checking preconditions in constructors and setters,
* or before using parameters in complex computations.
* Checking preconditions in every function call is not recommended, since it may have a
* negative impact on performance.
*
* @param bool condition
* @param string description The message to include in the exception if the condition fails.
*
* @throws PreconditionException if condition is not true.
*/
public static void precondition(boolean condition, String description) {
if (!condition) {
throw new XomwPreconditionException("Precondition failed: {0}", description);
}
}
/**
* Checks a parameter, that is, throws a ParameterAssertionException if condition is false.
* This is similar to Assert::precondition().
*
* @note This is intended for checking parameters in constructors and setters.
* Checking parameters in every function call is not recommended, since it may have a
* negative impact on performance.
*
* @param bool condition
* @param string name The name of the parameter that was checked.
* @param string description The message to include in the exception if the condition fails.
*
* @throws ParameterAssertionException if condition is not true.
*/
public static void parameter(boolean condition, String name, String description) {
if (!condition) {
throw new XomwParameterAssertionException(name, description);
}
}
/**
* Checks an parameter's type, that is, throws a InvalidArgumentException if condition is false.
* This is really a special case of Assert::precondition().
*
* @note This is intended for checking parameters in constructors and setters.
* Checking parameters in every function call is not recommended, since it may have a
* negative impact on performance.
*
* @note If possible, type hints should be used instead of calling this function.
* It is intended for cases where type hints to not work, e.g. for checking primitive types.
*
* @param string type The parameter's expected type. Can be the name of a native type or a
* class or interface. If multiple types are allowed, they can be given separated by
* a pipe character ("|").
* @param mixed value The parameter's actual value.
* @param string name The name of the parameter that was checked.
*
* @throws ParameterTypeException if value is not of type (or, for objects, is not an
* instance of) type.
*/
public static void parameterType(Class type, Object value, String name) {
// if (!hasType(value, XophpArray.explode('|', type))) {
if (!hasType(value, type)) {
throw new XomwParameterTypeException(name, XophpType_.To_str(type));
}
}
/**
* Checks the type of all elements of an parameter, assuming the parameter is an array,
* that is, throws a ParameterElementTypeException if value
*
* @note This is intended for checking parameters in constructors and setters.
* Checking parameters in every function call is not recommended, since it may have a
* negative impact on performance.
*
* @param string type The elements' expected type. Can be the name of a native type or a
* class or interface. If multiple types are allowed, they can be given separated by
* a pipe character ("|").
* @param mixed value The parameter's actual value. If this is not an array,
* a ParameterTypeException is raised.
* @param string name The name of the parameter that was checked.
*
* @throws ParameterTypeException If value is not an array.
* @throws ParameterElementTypeException If an element of value is not of type
* (or, for objects, is not an instance of) type.
*/
public static void parameterElementType(Class type, XophpArray<Class> value, String name) {
// parameterType(XophpArray.class, valueObj, name);
// allowedTypes = explode('|', type);
for (Object element : value) {
if (!hasType(element, type)) {
throw new XomwParameterElementTypeException(name, XophpType_.To_str(type));
}
}
}
/**
* Checks a postcondition, that is, throws a PostconditionException if condition is false.
* This is very similar Assert::invariant() but is intended for use only after a computation
* is complete.
*
* @note This is intended for sanity-checks in the implementation of complex algorithms.
* Note however that it should not be used in performance hotspots, since evaluating
* condition and calling postcondition() costs time.
*
* @param bool condition
* @param string description The message to include in the exception if the condition fails.
*
* @throws PostconditionException
*/
public static void postcondition(boolean condition, String description) {
if (!condition) {
throw new XomwPostconditionException("Postcondition failed: {0}", description);
}
}
/**
* Checks an invariant, that is, throws a InvariantException if condition is false.
* This is very similar Assert::postcondition() but is intended for use throughout the code.
*
* @note This is intended for sanity-checks in the implementation of complex algorithms.
* Note however that it should not be used in performance hotspots, since evaluating
* condition and calling postcondition() costs time.
*
* @param bool condition
* @param string description The message to include in the exception if the condition fails.
*
* @throws InvariantException
*/
public static void invariant(boolean condition, String description) {
if (!condition) {
throw new XomwInvariantException("Invariant failed: {0}", description);
}
}
/**
* @param mixed value
* @param array allowedTypes
*
* @return bool
*/
private static boolean hasType(Object value, Class allowedTypes) {
// Apply strtolower because gettype returns "NULL" for null values.
//type = strtolower(gettype(value));
//
//if (in_array(type, allowedTypes)) {
// return true;
//}
//
//if (is_callable(value) && in_array('callable', allowedTypes)) {
// return true;
//}
if (XophpObject_.is_object(value) && isInstanceOf(value, allowedTypes)) {
return true;
}
return false;
}
/**
* @param mixed value
* @param array allowedTypes
*
* @return bool
*/
private static boolean isInstanceOf(Object value, Class type) {return XophpType_.instance_of(value, type);}
private static boolean isInstanceOf(Object value, XophpArray<Class> allowedTypes) {
for (Class type : allowedTypes) {
if (XophpType_.instance_of(value, type)) {
return true;
}
}
return false;
}
}

@ -0,0 +1,31 @@
/*
XOWA: the XOWA Offline Wiki Application
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.
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.vendor.wikimedia.Assert.src;
// MW.SRC:1.33.1
/**
* Marker interface for exceptions thrown by Assert. Since the exceptions thrown by Assert
* use different standard exceptions as base classes, the marker interface is needed to be
* able to catch them all at once.
*
* @license MIT
* @author Daniel Kinzler
* @copyright Wikimedia Deutschland e.V.
*/
public interface XomwAssertionException {
}

@ -0,0 +1,34 @@
/*
XOWA: the XOWA Offline Wiki Application
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.
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.vendor.wikimedia.Assert.src;
import gplx.String_;
import gplx.xowa.mediawiki.XophpLogicException;
// MW.SRC:1.33.1
/**
* Exception indicating that an invariant assertion failed.
* This generally means an error in the internal logic of a function, or a serious problem
* in the runtime environment.
*
* @license MIT
* @author Daniel Kinzler
* @copyright Wikimedia Deutschland e.V.
*/
public class XomwInvariantException extends XophpLogicException implements XomwAssertionException {
public XomwInvariantException(String fmt, Object... args) {super(String_.Format(fmt, args));}
}

@ -0,0 +1,63 @@
/*
XOWA: the XOWA Offline Wiki Application
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.
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.vendor.wikimedia.Assert.src;
import gplx.xowa.mediawiki.XophpInvalidArgumentException;
// MW.SRC:1.33.1
/**
* Exception indicating that an parameter assertion failed.
* This generally means a disagreement between the caller and the implementation of a function.
*
* @license MIT
* @author Daniel Kinzler
* @copyright Wikimedia Deutschland e.V.
*/
public class XomwParameterAssertionException extends XophpInvalidArgumentException implements XomwAssertionException {
/**
* @var string
*/
private String parameterName;
/**
* @param string $parameterName
* @param string $description
*
* @throws ParameterTypeException
*/
public XomwParameterAssertionException(String parameterName, String description) {
//if (!is_string($parameterName)) {
// throw new ParameterTypeException("parameterName", "string");
//}
//
//if (!is_string($description)) {
// throw new ParameterTypeException("description", "string");
//}
super("Bad value for parameter {0}:{1}", parameterName, description);
this.parameterName = parameterName;
}
/**
* @return string
*/
public String getParameterName() {
return this.parameterName;
}
}

@ -0,0 +1,58 @@
/*
XOWA: the XOWA Offline Wiki Application
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.
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.vendor.wikimedia.Assert.src;
import gplx.String_;
// MW.SRC:1.33.1
/**
* Exception indicating that a parameter element type assertion failed.
* This generally means a disagreement between the caller and the implementation of a function.
*
* @license MIT
* @author Daniel Kinzler
* @copyright Wikimedia Deutschland e.V.
*/
public class XomwParameterElementTypeException extends XomwParameterAssertionException implements XomwAssertionException {
/**
* @var string
*/
private String elementType;
/**
* @param string $parameterName
* @param string $elementType
*
* @throws ParameterTypeException
*/
public XomwParameterElementTypeException(String parameterName, String elementType) {
//if ( !is_string( $elementType ) ) {
// throw new ParameterTypeException( 'elementType', 'string' );
//}
super(parameterName, String_.Format("all elements must be {0}", elementType));
this.elementType = elementType;
}
/**
* @return string
*/
public String getElementType() {
return this.elementType;
}
}

@ -0,0 +1,58 @@
/*
XOWA: the XOWA Offline Wiki Application
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.
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.vendor.wikimedia.Assert.src;
import gplx.String_;
// MW.SRC:1.33.1
/**
* Exception indicating that a parameter type assertion failed.
* This generally means a disagreement between the caller and the implementation of a function.
*
* @license MIT
* @author Daniel Kinzler
* @copyright Wikimedia Deutschland e.V.
*/
public class XomwParameterTypeException extends XomwParameterAssertionException implements XomwAssertionException {
/**
* @var string
*/
private String parameterType;
/**
* @param string $parameterName
* @param string $parameterType
*
* @throws ParameterTypeException
*/
public XomwParameterTypeException(String parameterName, String parameterType) {
//if ( !is_string( $parameterType ) ) {
// throw new ParameterTypeException( 'parameterType', 'string' );
//}
super(parameterName, String_.Format("must be a {0}", parameterType));
this.parameterType = parameterType;
}
/**
* @return string
*/
public String getParameterType() {
return this.parameterType;
}
}

@ -0,0 +1,34 @@
/*
XOWA: the XOWA Offline Wiki Application
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.
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.vendor.wikimedia.Assert.src;
import gplx.String_;
import gplx.xowa.mediawiki.XophpLogicException;
// MW.SRC:1.33.1
/**
* Exception indicating that a postcondition assertion failed.
* This generally means an error in the internal logic of a function, or a serious problem
* in the runtime environment.
*
* @license MIT
* @author Daniel Kinzler
* @copyright Wikimedia Deutschland e.V.
*/
public class XomwPostconditionException extends XophpLogicException implements XomwAssertionException {
public XomwPostconditionException(String fmt, Object... args) {super(String_.Format(fmt, args));}
}

@ -0,0 +1,33 @@
/*
XOWA: the XOWA Offline Wiki Application
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.
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.vendor.wikimedia.Assert.src;
import gplx.String_;
import gplx.xowa.mediawiki.XophpRuntimeException;
// MW.SRC:1.33.1
/**
* Exception indicating that an precondition assertion failed.
* This generally means a disagreement between the caller and the implementation of a function.
*
* @license MIT
* @author Daniel Kinzler
* @copyright Wikimedia Deutschland e.V.
*/
public class XomwPreconditionException extends XophpRuntimeException implements XomwAssertionException {
public XomwPreconditionException(String fmt, Object... args) {super(String_.Format(fmt, args));}
}
Loading…
Cancel
Save