Rework tests.php

Previously this relied on getting all known classes/functions/etc
when it was loaded, and then again after the PHP module being
tested was loaded.  This approach no longer works now we've
stopped loading modules using dl(), so use ReflectionExtension
instead to get information about a specific extension.

This is likely also faster than wading through lists including
everything predefined by PHP.
This commit is contained in:
Olly Betts 2021-05-04 15:26:45 +12:00
commit 0d028d8942
8 changed files with 36 additions and 85 deletions

View file

@ -4,7 +4,7 @@ require "tests.php";
check::functions(array('test_a','test_b'));
check::classes(array('arrays_global','SimpleStruct','Material'));
check::globals(array('array_c','array_sc','array_uc','array_s','array_us','array_i','array_ui','array_l','array_ul','array_ll','array_f','array_d','array_struct','array_structpointers','array_ipointers','array_enum','array_enumpointers','array_const_i','beginstring_fix44a','beginstring_fix44b','beginstring_fix44c','beginstring_fix44d','beginstring_fix44e','beginstring_fix44f','chitmat','hitmat_val','hitmat'));
heck::globals(array('array_c','array_sc','array_uc','array_s','array_us','array_i','array_ui','array_l','array_ul','array_ll','array_f','array_d','array_struct','array_structpointers','array_ipointers','array_enum','array_enumpointers','array_const_i','BeginString_FIX44a','BeginString_FIX44b','BeginString_FIX44c','BeginString_FIX44d','BeginString_FIX44e','BeginString_FIX44f','chitMat','hitMat_val','hitMat'));
// The size of array_c is 2, but the last byte is \0, so we can only store a
// single byte string in it.

View file

@ -4,7 +4,7 @@ require "tests.php";
check::functions(array('fn_taking_arrays','get_2d_array',));
check::classes(array('arrays_global_twodim','SimpleStruct','Material'));
check::globals(array('array_c','array_sc','array_uc','array_s','array_us','array_i','array_ui','array_l','array_ul','array_ll','array_f','array_d','array_struct','array_structpointers','array_ipointers','array_enum','array_enumpointers','array_const_i','chitmat','hitmat_val','hitmat'));
check::globals(array('array_c','array_sc','array_uc','array_s','array_us','array_i','array_ui','array_l','array_ul','array_ll','array_f','array_d','array_struct','array_structpointers','array_ipointers','array_enum','array_enumpointers','array_const_i','chitMat','hitMat_val','hitMat'));
$a1=array(10,11,12,13);
$a2=array(14,15,16,17);

View file

@ -3,7 +3,7 @@
require "tests.php";
// New functions
check::functions(array('deletefoo','getstatus','launder','resetstatus'));
check::functions(array('deleteFoo','getStatus','launder','resetStatus'));
// New classes
check::classes(array('director_finalizer','Foo'));
// No new vars

View file

@ -3,14 +3,14 @@
require "tests.php";
// Check functions.
check::functions(array('new_intarray','delete_intarray','intarray_getitem','intarray_setitem','new_abarray','delete_abarray','abarray_getitem','abarray_setitem','sum_array'));
check::functions(array('new_intArray','delete_intArray','intArray_getitem','intArray_setitem','new_ABArray','delete_ABArray','ABArray_getitem','ABArray_setitem','sum_Array'));
// Check classes.
// NB An "li_carrays_cpp" class is created as a mock namespace.
check::classes(array('li_carrays_cpp','doubleArray','AB','XY','XYArray','shortArray'));
// Check global variables.
check::globals(array('globalxyarray','globalabarray'));
check::globals(array('globalXYArray','globalABArray'));
$d = new doubleArray(10);

View file

@ -3,14 +3,14 @@
require "tests.php";
// Check functions.
check::functions(array('new_intarray','delete_intarray','intarray_getitem','intarray_setitem','new_abarray','delete_abarray','abarray_getitem','abarray_setitem','sum_array'));
check::functions(array('new_intArray','delete_intArray','intArray_getitem','intArray_setitem','new_ABArray','delete_ABArray','ABArray_getitem','ABArray_setitem','sum_Array'));
// Check classes.
// NB An "li_carrays" class is created as a mock namespace.
check::classes(array('li_carrays','doubleArray','AB','XY','XYArray','shortArray'));
// Check global variables.
check::globals(array('globalxyarray','globalabarray'));
check::globals(array('globalXYArray','globalABArray'));
$d = new doubleArray(10);

View file

@ -1,77 +1,24 @@
<?php
$_original_functions=get_defined_functions();
$_original_globals=1;
$_original_classes=get_declared_classes();
$_original_globals=array_keys($GLOBALS);
class check {
// do we have true global vars or just GETSET functions?
// Used to filter out get/set global functions to fake vars...
const GETSET = 1;
static function get_extra_classes($ref=FALSE) {
static $extra;
global $_original_classes;
if ($ref===FALSE) $f=$_original_classes;
if (! is_array($extra)) {
$df=array_flip(get_declared_classes());
foreach($_original_classes as $class) unset($df[$class]);
// Filter out pointer wrappers such as SWIG/_p_int.
foreach(array_keys($df) as $class) {
if (preg_match('/^SWIG\\\\/', $class)) unset($df[$class]);
}
$extra=array_keys($df);
}
return $extra;
}
private static $_extension = null;
static function get_extra_functions($ref=FALSE,$gs=false) {
static $extra;
static $extrags; // for get/setters
global $_original_functions;
if ($ref===FALSE) $f=$_original_functions;
if (! is_array($extra) || $gs) {
$extra=array();
$extrags=array();
$df=get_defined_functions();
$df=array_flip($df['internal']);
foreach($_original_functions['internal'] as $func) unset($df[$func]);
// Now chop out any get/set accessors
foreach(array_keys($df) as $func)
if ((self::GETSET && preg_match('/_[gs]et$/', $func)))
$extrags[]=$func;
else $extra[]=$func;
// $extra=array_keys($df);
// This is called automatically at the end of this file.
static function init() {
foreach(get_included_files() as $f) {
$module_name = preg_filter('/.*\/([^\/]+)_runme\.php$/', '\1', $f);
if ($module_name !== null) break;
}
if ($module_name === null) {
print("Failed to determine module name from get_included_files()\n");
exit(1);
}
if ($gs) return $extrags;
return $extra;
}
static function get_extra_globals($ref=FALSE) {
static $extra;
global $_original_globals;
if (! is_array($extra)) {
if (self::GETSET) {
$_extra=array();
foreach(check::get_extra_functions(false,1) as $global) {
if (preg_match('/^(.*)_[sg]et$/', $global, $match))
$_extra[$match[1]] = 1;
}
$extra=array_keys($_extra);
} else {
if ($ref===FALSE) $ref=$_original_globals;
if (! is_array($extra)) {
$df=array_flip(array_keys($GLOBALS));
foreach($_original_globals as $func) unset($df[$func]);
// MASK xxxx_LOADED__ variables
foreach(array_keys($df) as $func)
if (preg_match('/_LOADED__$/', $func)) unset($df[$func]);
$extra=array_keys($df);
}
}
}
return $extra;
self::$_extension = new ReflectionExtension($module_name);
}
static function classname($string,$object) {
@ -139,7 +86,8 @@ class check {
if (! is_array($classes)) $classes=array($classes);
$message=array();
$missing=array();
$extra=array_flip(check::get_extra_classes());
$extra = array_flip(array_filter(self::$_extension->getClassNames(),
function ($e) { return !preg_match('/^SWIG\\\\/', $e); }));
foreach($classes as $class) {
if (! class_exists($class)) $missing[]=$class;
else unset($extra[$class]);
@ -155,15 +103,16 @@ class check {
if (! is_array($functions)) $functions=array($functions);
$message=array();
$missing=array();
$extra=array_flip(check::get_extra_functions());
$extra = self::$_extension->getFunctions();
foreach ($functions as $func) {
if (! function_exists($func)) $missing[]=$func;
else unset($extra[$func]);
}
$extra = array_filter(array_keys($extra),
function ($e) { return !preg_match('/_[gs]et$/', $e); });
if ($missing) $message[]=sprintf("Functions missing: %s",join(",",$missing));
if ($message) return check::fail(join("\n ",$message));
if ($extra) $message[]=sprintf("These extra functions are defined: %s",join(",",array_keys($extra)));
if ($extra) $message[]=sprintf("These extra functions are defined: %s",join(",",$extra));
if ($message) return check::warn(join("\n ",$message));
return TRUE;
}
@ -172,19 +121,19 @@ class check {
if (! is_array($globals)) $globals=array($globals);
$message=array();
$missing=array();
$extra=array_flip(check::get_extra_globals());
$extra = self::$_extension->getFunctions();
foreach ($globals as $glob) {
if (self::GETSET) {
if (! function_exists($glob . "_get") && ! function_exists($glob . "_set")) $missing[]=$glob;
else unset($extra[$glob]);
} else {
if (! isset($GLOBALS[$glob])) $missing[]=$glob;
else unset($extra[$glob]);
if (! function_exists($glob . "_get") && ! function_exists($glob . "_set")) $missing[]=$glob;
else {
unset($extra[$glob . "_get"]);
unset($extra[$glob . "_set"]);
}
}
$extra = array_filter(array_keys($extra),
function ($e) { return preg_match('/_[gs]et$/', $e); });
if ($missing) $message[]=sprintf("Globals missing: %s",join(",",$missing));
if ($message) return check::fail(join("\n ",$message));
if ($extra) $message[]=sprintf("These extra globals are defined: %s",join(",",array_keys($extra)));
if ($extra) $message[]=sprintf("These extra globals are defined: %s",join(",",$extra));
if ($message) return check::warn(join("\n ",$message));
return TRUE;
@ -226,3 +175,5 @@ class check {
# print $_SERVER[argv][0]." ok\n";
}
}
check::init();

View file

@ -3,7 +3,7 @@
require "tests.php";
check::classes(array("valuewrapper_base","Base","Interface_BP"));
check::functions("make_interface_bp");
check::functions("make_Interface_BP");
$ibp=valuewrapper_base::make_interface_bp();
check::classname("interface_bp",$ibp);

View file

@ -2,7 +2,7 @@
require "tests.php";
check::functions(array('guint16_swap_le_be_constant', 'maximum'));
check::functions(array('GUINT16_SWAP_LE_BE_CONSTANT', 'maximum'));
check::equal(maximum(2.3, 2.4), 2.4, "maximum() doesn't work");
check::equal(guint16_swap_le_be_constant(0x1234), 0x3412, "GUINT16_SWAP_LE_BE_CONSTANT() doesn't work");