From 93eaead22a45d8568e5953a84b6497dac4f5beb5 Mon Sep 17 00:00:00 2001 From: Fabian Ebner Date: Fri, 16 Jul 2021 16:34:00 +0200 Subject: [PATCH] test for broken undef detection Signed-off-by: Fabian Ebner Signed-off-by: Wolfgang Bumiller --- perlmod-test/src/lib.rs | 3 +++ perlmod-test/src/option.rs | 19 +++++++++++++++++++ test.pl | 26 ++++++++++++++++++++++++++ test.pl.expected | 9 +++++++++ 4 files changed, 57 insertions(+) create mode 100644 perlmod-test/src/option.rs diff --git a/perlmod-test/src/lib.rs b/perlmod-test/src/lib.rs index 9935a8d..6d5c94d 100644 --- a/perlmod-test/src/lib.rs +++ b/perlmod-test/src/lib.rs @@ -9,3 +9,6 @@ mod pkginline; /// A test for blessed values. mod bless; + +/// Tests for `Option`. +mod option; diff --git a/perlmod-test/src/option.rs b/perlmod-test/src/option.rs new file mode 100644 index 0000000..a23ad3d --- /dev/null +++ b/perlmod-test/src/option.rs @@ -0,0 +1,19 @@ +#[perlmod::package(name = "RSPM::Option", lib = "perlmod_test")] +mod export { + use serde::{Deserialize, Serialize}; + + #[derive(Deserialize, Serialize)] + pub struct WithOption { + tristate: Option, + } + + #[export] + pub fn to_string(tristate: Option) -> String { + format!("{:?}", tristate) + } + + #[export] + pub fn struct_to_string(with_option: WithOption) -> String { + format!("{:?}", with_option.tristate) + } +} diff --git a/test.pl b/test.pl index 0212ff6..91958e9 100644 --- a/test.pl +++ b/test.pl @@ -5,6 +5,7 @@ use v5.28.0; use lib '.'; use RSPM::Bless; use RSPM::Foo142; +use RSPM::Option; my $v = RSPM::Bless->new("Hello"); $v->something(); @@ -40,3 +41,28 @@ my $b = RSPM::Foo142::test_serde($a); print $b; my $c = RSPM::Foo142::test_serde($b); print $c; + +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 }); diff --git a/test.pl.expected b/test.pl.expected index 996f4fa..885f3b2 100644 --- a/test.pl.expected +++ b/test.pl.expected @@ -19,3 +19,12 @@ Can I have some coffee please? Can I have some ☕ please? Can I have some ☕ please? Can I have some ☕ please? +Some(false) +Some(false) +Some(false) +Some(true) +Some(true) +Some(true) +None +None +None