summaryrefslogtreecommitdiff
path: root/ext/B/t
diff options
context:
space:
mode:
authorNicolas R <nicolas@atoomic.org>2022-04-08 13:52:16 -0600
committerKarl Williamson <khw@cpan.org>2022-05-27 21:32:28 -0600
commit2af2513acebd4252429a3df47ef0ddde748b7ae5 (patch)
tree7659e3f00d832dad812cae433b479050a9e3928e /ext/B/t
parent00d3871069fa4c1d973663e5c3eb80ef1bc8409f (diff)
downloadperl-2af2513acebd4252429a3df47ef0ddde748b7ae5.tar.gz
Add B helpers to check if the Sv is a boolean
Bool are using PVNV. It makes it more convenient to provide these helpers at the top level for any SVs. So we can easily check if the SV is a boolean and check if it's true or false.
Diffstat (limited to 'ext/B/t')
-rw-r--r--ext/B/t/bool.t59
1 files changed, 59 insertions, 0 deletions
diff --git a/ext/B/t/bool.t b/ext/B/t/bool.t
new file mode 100644
index 0000000000..1af7dfbab0
--- /dev/null
+++ b/ext/B/t/bool.t
@@ -0,0 +1,59 @@
+#!./perl
+
+BEGIN {
+ unshift @INC, 't';
+ require Config;
+ if (($Config::Config{'extensions'} !~ /\bB\b/) ){
+ print "1..0 # Skip -- Perl configured without B module\n";
+ exit 0;
+ }
+}
+
+use strict;
+use warnings;
+
+use B;
+use Test::More;
+
+$| = 1;
+
+{
+ note "testing true";
+ my $bool = ( 1 == 1 );
+ my $sv = B::svref_2object(\$bool);
+ ok $sv->IsBOOL, "got a boolean";
+ ok $sv->TRUE_nomg, "TRUE_nomg is true";
+ ok $sv->TRUE, "TRUE is true";
+}
+
+{
+ note "testing false";
+ my $bool = ( 1 == 0 );
+ my $sv = B::svref_2object(\$bool);
+
+ ok $sv->IsBOOL, "got a boolean";
+ ok !$sv->TRUE_nomg, "TRUE_nomg is false";
+ ok !$sv->TRUE, "TRUE is false";
+}
+
+{
+ note "not a boolean";
+ my $iv = 42;
+ my $sv = B::svref_2object(\$iv);
+
+ ok !$sv->IsBOOL, "not a boolean";
+ ok $sv->TRUE_nomg, "TRUE_nomg is true";
+ ok $sv->TRUE, "TRUE is true";
+}
+
+{
+ note "not a boolean";
+ my $iv = 0;
+ my $sv = B::svref_2object(\$iv);
+
+ ok !$sv->IsBOOL, "not a boolean";
+ ok !$sv->TRUE_nomg, "TRUE_nomg is false";
+ ok !$sv->TRUE, "TRUE is false";
+}
+
+done_testing();