polserver/testsuite/escript/spread/resource-tracker-functional.src
Kevin Eady 3ccf701ce9
Format testsuite scripts, pol-core support modules (#713)
* Format escript testsuite

* Manual formatting for escript testsuite
- Add format-off to places where formatter does what it should, but we don't like the results
- Manually change places where formatter outputs weird things _without_ format-off, so next format-all would show changes if not fixed

* Update .err files; add update_err.py helper script

Use the format `:line:col: message` for all .err file checks. This
allows us to change the file names without needing to edit the .err
contents.

Note that `in/inc0{1,2}.err` need have error messages between POSIX and
Windows, so their messages are a bit more "generic" than the other errs.

* Format pol testsuite

* Manual formatting for pol testsuite

* Format pol-core modules

* Manual formatting for pol-core modules

* Additional format pol-core modules

Use FormatterLineWidth=10000 since module functions must be on one line
because of the parse_modules target

* Address Discord comments

- Support multi-line module function declarations in parse_modules

* Format on df07831
2024-10-04 10:29:55 +02:00

104 lines
2.5 KiB
Text

include "module-stubs";
print( test( true ) );
print( "----" );
print( test( false ) );
function test( has_success )
return with_resources( @{
return CreateItemAtLocation( 1, 2, 3, 0xeed );
}, @{
return CreateMultiAtLocation( 1, 2, 3, "SmallHouse" );
}, with_dispose( @{
return CreateMultiAtLocation( 1, 2, 3, has_success ? "SmallHouse" : error{} );
}, @( what, item, unused multi /* from the first SmallHouse */ ) {
print( $"DisposeCustom on '{what}' but do something with item '{item}' first" );
} ), @{
return CreateNPCFromTemplate( "brigand", 1, 2, 3 );
}, @( item, multi, multi2, npc ) {
print( $"In test: item={item} multi={multi} multi2={multi2} npc={npc}" );
return 1;
} );
endfunction
function DisposeCustom( what )
print( $"DisposeCustom {what}" );
endfunction
//////////////////////////////////////////////////////
function with_dispose( resource, finalizer )
return array{ resource, finalizer };
endfunction
function with_resources( args ... )
var resources := array{};
var cleanup := @{
while ( resources.size() )
var resource_entry := resources[resources.size()];
var resource := resource_entry[1];
var cleanup_fn := resource_entry[2];
resources.shrink( resources.size() - 1 );
var call_args := resources.map( @( resource ) {
return resource[1];
} );
cleanup_fn( resource, call_args ... );
endwhile
};
foreach arg in args
if ( _arg_iter == args.size() )
var call_args := resources.map( @( resource ) {
return resource[1];
} );
var result := arg( call_args ... );
cleanup();
return result;
endif
if ( TypeOfInt( arg ) == OT_ARRAY )
var resource := ( arg[1] )();
if ( !resource )
cleanup();
return resource;
endif
var finalizer := arg[2];
resources.append( with_dispose( resource, finalizer ) );
elseif ( TypeOfInt( arg ) == OT_FUNCOBJECT )
var resource := arg();
if ( !resource )
cleanup();
return resource;
endif
var finalizer;
var type := TypeOfInt_( resource );
case ( type )
OT_ITEMREF: finalizer := @DisposeItem;
OT_MULTIREF: finalizer := @DisposeMulti;
OT_MOBILEREF: finalizer := @DisposeNpc;
default:
finalizer := @( what ) { print( $"Cannot dispose @{what}" ); };
endcase
resources.append( with_dispose( resource, finalizer ) );
endif
endforeach
endfunction