]> git.proxmox.com Git - perlmod.git/blame - test.pl
experimental direct substr support
[perlmod.git] / test.pl
CommitLineData
e95a2c0a
WB
1#!/usr/bin/env perl
2
3use v5.28.0;
4
1521415b
WB
5use POSIX ();
6
1e46bfbe
WB
7# The nasty ones:
8use Storable;
9use Clone;
10
e95a2c0a
WB
11use lib '.';
12use RSPM::Bless;
61143f5d 13use RSPM::Foo142;
93eaead2 14use RSPM::Option;
1521415b 15use RSPM::Magic;
e95a2c0a 16
0864428b 17STDOUT->autoflush;
1521415b
WB
18# Let's combine stderr and stdout:
19POSIX::dup2(fileno(STDOUT), fileno(STDERR));
0864428b 20
e95a2c0a
WB
21my $v = RSPM::Bless->new("Hello");
22$v->something();
245de6dd 23$v->something_nonraw();
e95a2c0a
WB
24my ($a, $b, $c) = $v->multi_return();
25say "Got ($a, $b, $c)";
26my @ret = $v->multi_return();
27say "Got: ".scalar(@ret)." values: @ret";
28
29$v->another(54);
61143f5d
WB
30
31my $param = { a => 1 };
32my $s = "Hello You";
b5c53f3d 33print "These should be called with a valid substr:\n";
61143f5d
WB
34RSPM::Foo142::test(substr($s, 3, 3));
35RSPM::Foo142::teststr(substr($s, 3, 3));
b5c53f3d 36print "Parameter exists: " . (exists($param->{x}) ? "YES" : "NO") . "\n";
61143f5d 37RSPM::Foo142::test($param->{x});
b5c53f3d 38print "Was auto-vivified: " . (exists($param->{x}) ? "YES" : "NO") . "\n";
61143f5d 39RSPM::Foo142::teststr($param->{x});
fa780264 40
83ac604e
WB
41my $a = "Can I have some coffee please?\n";
42print $a;
43my $b = RSPM::Foo142::test_serde($a);
44print $b;
45my $c = RSPM::Foo142::test_serde($b);
46print $c;
47
48use utf8;
49binmode STDOUT, ':utf8';
50my $a = "Can I have some ☕ please?\n";
51print $a;
52my $b = RSPM::Foo142::test_serde($a);
53print $b;
54my $c = RSPM::Foo142::test_serde($b);
55print $c;
93eaead2
FE
56
57sub to_string {
58 my ($param) = @_;
59
60 my $state = $param->{tristate};
61 $state = int($state) if defined($state);
62
63 my $a;
64 if (defined($state)) {
65 $a = $state ? "Some(true)" : "Some(false)";
66 } else {
67 $a = "None";
68 }
69
70 my $b = RSPM::Option::to_string($state);
71 my $c = RSPM::Option::struct_to_string({ 'tristate' => $state });
72
73 print "$a\n";
74 print "$b\n";
75 print "$c\n";
76}
77
78to_string({ 'tristate' => '0' });
79to_string({ 'tristate' => '1' });
80to_string({ 'tristate' => undef });
6ccbcbec
WB
81
82my $ref1 = { x => "x was stored" };
83my $ref2 = RSPM::Foo142::test_refs({ copied => "copied string", reference => $ref1 });
84print($ref1->{x}, "\n");
85$ref2->{x} = "x was changed";
86print($ref1->{x}, "\n");
1521415b
WB
87
88my $magic = RSPM::Magic->new('magic test');
89$magic->call();
90
1e46bfbe
WB
91sub test_unsafe_clone($) {
92 my ($bad) = @_;
93 eval { $bad->call() };
94 if (!$@) {
95 die "cloned object not properly detected!\n";
96 } elsif ($@ ne "value blessed into RSPM::Magic did not contain its declared magic pointer\n") {
97 die "cloned object error message changed to: [$@]\n";
98 }
99 undef $bad;
100 print("unsafe dclone dropped\n");
1521415b 101}
1e46bfbe
WB
102
103print("Testing unsafe dclone\n");
104test_unsafe_clone(Storable::dclone($magic));
105
106print("Testing unsafe clone\n");
107test_unsafe_clone(Clone::clone($magic));
0044cdce 108undef $magic;
8932eaeb
WB
109
110print("Testing enum deserialization\n");
111my $ra = RSPM::Foo142::test_enums("something");
112die "unexpected result from test_enums: $ra\n" if $ra ne 'result-a';
0044cdce
WB
113
114print("Testing optional parameters\n");
115RSPM::Foo142::test_trailing_optional(1, 99);
116RSPM::Foo142::test_trailing_optional(2, undef);
117RSPM::Foo142::test_trailing_optional(3);
a43bf932
WB
118
119print("Substring test\n");
120my $orig = "OneTwoThree";
121my $sub = RSPM::Foo142::test_substr_return($orig);
122print("[$orig] [$sub]\n");