2021-02-16 12:42:10 +03:00
#!/usr/bin/env perl
use v5 .28 .0 ;
2021-10-27 12:37:45 +03:00
use POSIX ( ) ;
2021-10-27 16:42:19 +03:00
# The nasty ones:
use Storable ;
use Clone ;
2021-02-16 12:42:10 +03:00
use lib '.' ;
use RSPM::Bless ;
2021-06-08 12:28:01 +03:00
use RSPM::Foo142 ;
2021-07-16 17:34:00 +03:00
use RSPM::Option ;
2021-10-27 12:37:45 +03:00
use RSPM::Magic ;
2021-02-16 12:42:10 +03:00
2021-07-19 10:37:08 +03:00
STDOUT - > autoflush ;
2021-10-27 12:37:45 +03:00
# Let's combine stderr and stdout:
POSIX:: dup2 ( fileno ( STDOUT ) , fileno ( STDERR ) ) ;
2021-07-19 10:37:08 +03:00
2021-02-16 12:42:10 +03:00
my $ v = RSPM::Bless - > new ( "Hello" ) ;
$ v - > something ( ) ;
2021-10-14 14:40:06 +03:00
$ v - > something_nonraw ( ) ;
2021-02-16 12:42:10 +03:00
my ( $ a , $ b , $ c ) = $ v - > multi_return ( ) ;
say "Got ($a, $b, $c)" ;
my @ ret = $ v - > multi_return ( ) ;
say "Got: " . scalar ( @ ret ) . " values: @ret" ;
$ v - > another ( 54 ) ;
2021-06-08 12:28:01 +03:00
my $ param = { a = > 1 } ;
my $ s = "Hello You" ;
2021-06-10 11:14:04 +03:00
print "These should be called with a valid substr:\n" ;
2021-06-08 12:28:01 +03:00
RSPM::Foo142:: test ( substr ( $ s , 3 , 3 ) ) ;
RSPM::Foo142:: teststr ( substr ( $ s , 3 , 3 ) ) ;
2021-06-10 11:14:04 +03:00
print "Parameter exists: " . ( exists ( $ param - > { x } ) ? "YES" : "NO" ) . "\n" ;
2021-06-08 12:28:01 +03:00
RSPM::Foo142:: test ( $ param - > { x } ) ;
2021-06-10 11:14:04 +03:00
print "Was auto-vivified: " . ( exists ( $ param - > { x } ) ? "YES" : "NO" ) . "\n" ;
2021-06-08 12:28:01 +03:00
RSPM::Foo142:: teststr ( $ param - > { x } ) ;
2021-06-23 11:58:31 +03:00
2021-07-02 12:32:30 +03:00
my $ a = "Can I have some coffee please?\n" ;
print $ a ;
my $ b = RSPM::Foo142:: test_serde ( $ a ) ;
print $ b ;
my $ c = RSPM::Foo142:: test_serde ( $ b ) ;
print $ c ;
use utf8 ;
binmode STDOUT , ':utf8' ;
my $ a = "Can I have some ☕ please?\n" ;
print $ a ;
my $ b = RSPM::Foo142:: test_serde ( $ a ) ;
print $ b ;
my $ c = RSPM::Foo142:: test_serde ( $ b ) ;
print $ c ;
2021-07-16 17:34:00 +03:00
sub to_string {
my ( $ param ) = @ _ ;
my $ state = $ param - > { tristate } ;
$ state = int ( $ state ) if defined ( $ state ) ;
my $ a ;
if ( defined ( $ state ) ) {
$ a = $ state ? "Some(true)" : "Some(false)" ;
} else {
$ a = "None" ;
}
my $ b = RSPM::Option:: to_string ( $ state ) ;
my $ c = RSPM::Option:: struct_to_string ( { 'tristate' = > $ state } ) ;
print "$a\n" ;
print "$b\n" ;
print "$c\n" ;
}
to_string ( { 'tristate' = > '0' } ) ;
to_string ( { 'tristate' = > '1' } ) ;
to_string ( { 'tristate' = > undef } ) ;
2021-10-14 16:13:32 +03:00
my $ ref1 = { x = > "x was stored" } ;
my $ ref2 = RSPM::Foo142:: test_refs ( { copied = > "copied string" , reference = > $ ref1 } ) ;
print ( $ ref1 - > { x } , "\n" ) ;
$ ref2 - > { x } = "x was changed" ;
print ( $ ref1 - > { x } , "\n" ) ;
2021-10-27 12:37:45 +03:00
my $ magic = RSPM::Magic - > new ( 'magic test' ) ;
$ magic - > call ( ) ;
2021-10-27 16:42:19 +03:00
sub test_unsafe_clone ($) {
my ( $ bad ) = @ _ ;
eval { $ bad - > call ( ) } ;
if ( ! $@ ) {
die "cloned object not properly detected!\n" ;
} elsif ( $@ ne "value blessed into RSPM::Magic did not contain its declared magic pointer\n" ) {
die "cloned object error message changed to: [$@]\n" ;
}
undef $ bad ;
print ( "unsafe dclone dropped\n" ) ;
2021-10-27 12:37:45 +03:00
}
2021-10-27 16:42:19 +03:00
print ( "Testing unsafe dclone\n" ) ;
test_unsafe_clone ( Storable:: dclone ( $ magic ) ) ;
print ( "Testing unsafe clone\n" ) ;
test_unsafe_clone ( Clone:: clone ( $ magic ) ) ;
2021-11-25 12:17:33 +03:00
undef $ magic ;
2021-11-05 12:14:19 +03:00
print ( "Testing enum deserialization\n" ) ;
my $ ra = RSPM::Foo142:: test_enums ( "something" ) ;
die "unexpected result from test_enums: $ra\n" if $ ra ne 'result-a' ;
2021-11-25 12:17:33 +03:00
print ( "Testing optional parameters\n" ) ;
RSPM::Foo142:: test_trailing_optional ( 1 , 99 ) ;
RSPM::Foo142:: test_trailing_optional ( 2 , undef ) ;
RSPM::Foo142:: test_trailing_optional ( 3 ) ;
2022-02-22 17:21:41 +03:00
print ( "Substring test\n" ) ;
my $ orig = "OneTwoThree" ;
my $ sub = RSPM::Foo142:: test_substr_return ( $ orig ) ;
print ( "[$orig] [$sub]\n" ) ;
2023-05-02 10:38:39 +03:00
my $ ok = RSPM::Foo142:: test_deserialized_error ( 0 ) ;
2023-05-02 15:31:47 +03:00
die "test_deserialized_error failed to set errno value\n" if $! != 77 ;
2023-05-02 10:38:39 +03:00
die "test_deserialized_error failed to return a value\n" if $ ok ne 'worked' ;
$ ok = eval { RSPM::Foo142:: test_deserialized_error ( 1 ) } ;
die "test_deserialized_error error case returned a value\n" if defined $ ok ;
my $ err = $@ ;
die "test_deserialized_error error is not a hash\n" if ref ( $ err ) ne 'HASH' ;
die "structured error has invalid fields\n" if join ( ',' , sort ( keys ( %$ err ) ) ) ne 'a,b' ;
print ( 'error type: { a: ' , $ err - > { a } , ', b: ' , $ err - > { b } , " }\n" ) ;