summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2011-07-08 23:11:34 -0500
committerJesse Luehrs <doy@tozt.net>2011-07-08 23:11:34 -0500
commit9174cec546b4dba5821ce29a408feb860f49c244 (patch)
tree10028a63e7cc2985461d0f9b4bb83c411db77020
parent6c7d5e2e4024df3b3d0f54bab14909ddfcd478c0 (diff)
downloadsmartmatch-9174cec546b4dba5821ce29a408feb860f49c244.tar.gz
smartmatch-9174cec546b4dba5821ce29a408feb860f49c244.zip
split rjbs engine out into its own dist
-rw-r--r--lib/smartmatch/engine/rjbs.pm62
-rw-r--r--t/basic.t7
-rw-r--r--t/rjbs.t154
3 files changed, 6 insertions, 217 deletions
diff --git a/lib/smartmatch/engine/rjbs.pm b/lib/smartmatch/engine/rjbs.pm
deleted file mode 100644
index da40998..0000000
--- a/lib/smartmatch/engine/rjbs.pm
+++ /dev/null
@@ -1,62 +0,0 @@
-package smartmatch::engine::rjbs;
-use strict;
-use warnings;
-
-use overload ();
-use Scalar::Util qw(blessed reftype);
-
-sub type {
- my ($thing) = @_;
-
- if (!defined($thing)) {
- return 'undef';
- }
- elsif (!ref($thing)) {
- return 'unknown non-ref';
- }
- elsif (reftype($thing) eq 'REGEXP') {
- return 'Regex';
- }
- elsif (blessed($thing)) {
- if (overload::Method($thing, '~~')) {
- return 'Overloaded';
- }
- elsif (overload::Method($thing, 'qr')) {
- return 'Regex';
- }
- else {
- return 'unknown object';
- }
- }
- elsif (reftype($thing) eq 'CODE') {
- return 'Code';
- }
- else {
- return 'unknown';
- }
-}
-
-sub match {
- my ($a, $b) = @_;
-
- if (type($b) eq 'undef') {
- return !defined($a);
- }
- elsif (type($b) eq 'Overloaded') {
- my $overload = overload::Method($b, '~~');
- return $b->$overload($a, 1);
- }
- elsif (type($b) eq 'Regex') {
- return $a =~ $b;
- }
- elsif (type($b) eq 'Code') {
- return $b->($a);
- }
- else {
- $a //= 'undef';
- $b //= 'undef';
- die "invalid smart match: $a ~~ $b";
- }
-}
-
-1;
diff --git a/t/basic.t b/t/basic.t
index 421623a..39b564c 100644
--- a/t/basic.t
+++ b/t/basic.t
@@ -12,7 +12,12 @@ ok(1 ~~ 1);
ok(1 ~~ 1);
{
- use smartmatch 'rjbs';
+ package smartmatch::engine::foo;
+ sub match { ref($_[1]) eq 'ARRAY' }
+}
+
+{
+ use smartmatch 'foo';
ok([] ~~ qr/ARRAY/);
ok(!(1 ~~ sub { 0 }));
}
diff --git a/t/rjbs.t b/t/rjbs.t
deleted file mode 100644
index b94e893..0000000
--- a/t/rjbs.t
+++ /dev/null
@@ -1,154 +0,0 @@
-#!/usr/bin/env perl
-use strict;
-use warnings;
-use Test::More;
-
-use smartmatch 'rjbs';
-
-{
- package SmartOverload;
- use overload '~~' => sub {
- no warnings 'uninitialized';
- return $_[1] eq ${ $_[0] };
- }, fallback => 1;
-}
-
-{
- package RegexOverload;
- use overload 'qr' => sub {
- return $_[0]->[0];
- }, fallback => 1;
-}
-
-{
- package StringOverload;
- use overload '""' => sub {
- return $_[0]->{val};
- }, fallback => 1;
-}
-
-sub smart { my $val = shift; bless \$val, SmartOverload:: }
-sub regex { my $val = shift; bless [qr/$val/], RegexOverload:: }
-sub string { my $val = shift; bless { val => $val }, StringOverload:: }
-
-my @tests = (
- # undef
- [ 1, undef, undef ],
- [ 0, '', undef ],
- [ 0, 0, undef ],
- [ 0, '0', undef ],
- [ 0, '0.0', undef ],
- [ 0, '0 but true', undef ],
- [ 0, 1, undef ],
- [ 0, 'x', undef ],
- [ 0, [], undef ],
- [ 0, {}, undef ],
- [ 0, sub {}, undef ],
- [ 0, smart(''), undef ],
- [ 0, regex(''), undef ],
- [ 0, string(''), undef ],
- # smart match overload
- [ 1, "smart", smart('smart') ],
- [ 1, string('smart'), smart('smart') ],
- [ 0, "SMART", smart('smart') ],
- [ 0, string('SMART'), smart('smart') ],
- [ 0, smart('smart'), smart('smart') ],
- [ 0, undef, smart('smart') ],
- [ 0, 1, smart('smart') ],
- # regex
- [ 0, undef, qr/a/ ],
- [ 1, undef, qr/a?/ ],
- [ 1, "foo", qr/f/ ],
- [ 0, "foo", qr/g/ ],
- [ 1, 1, qr/1/ ],
- [ 0, ['z'], qr/z/ ],
- [ 1, ['z'], qr/^ARRAY/ ],
- [ 0, {'y' => 'y'}, qr/y/ ],
- [ 1, {'y' => 'y'}, qr/^HASH/ ],
- [ 1, string('foo'), qr/^foo$/ ],
- [ 0, regex('foo'), qr/foo/ ],
- [ 1, regex('foo'), qr/^Regex/ ],
- [ 1, qr/foo/, qr/\(\?\^\:foo\)/ ],
- # regex overload
- [ 0, undef, regex('a') ],
- [ 1, undef, regex('a?') ],
- [ 1, "foo", regex('f') ],
- [ 0, "foo", regex('g') ],
- [ 1, 1, regex('1') ],
- [ 0, ['z'], regex('z') ],
- [ 1, ['z'], regex('^ARRAY') ],
- [ 0, {'y' => 'y'}, regex('y') ],
- [ 1, {'y' => 'y'}, regex('^HASH') ],
- [ 1, string('foo'), regex('^foo$') ],
- [ 0, regex('foo'), regex('foo') ],
- [ 1, regex('foo'), regex('^Regex') ],
- [ 1, qr/foo/, regex('\(\?\^\:foo\)') ],
- # code
- [ 1, undef, sub { 1 } ],
- [ 1, '', sub { 1 } ],
- [ 1, 0, sub { 1 } ],
- [ 1, '0', sub { 1 } ],
- [ 1, '0.0', sub { 1 } ],
- [ 1, '0 but true', sub { 1 } ],
- [ 1, 1, sub { 1 } ],
- [ 1, 'x', sub { 1 } ],
- [ 1, [], sub { 1 } ],
- [ 1, {}, sub { 1 } ],
- [ 1, sub {}, sub { 1 } ],
- [ 1, smart(''), sub { 1 } ],
- [ 1, regex(''), sub { 1 } ],
- [ 1, string(''), sub { 1 } ],
- [ 0, undef, sub { 0 } ],
- [ 0, '', sub { 0 } ],
- [ 0, 0, sub { 0 } ],
- [ 0, '0', sub { 0 } ],
- [ 0, '0.0', sub { 0 } ],
- [ 0, '0 but true', sub { 0 } ],
- [ 0, 1, sub { 0 } ],
- [ 0, 'x', sub { 0 } ],
- [ 0, [], sub { 0 } ],
- [ 0, {}, sub { 0 } ],
- [ 0, sub {}, sub { 0 } ],
- [ 0, smart(''), sub { 0 } ],
- [ 0, regex(''), sub { 0 } ],
- [ 0, string(''), sub { 0 } ],
- [ 1, ['a', 'b'], sub { ref $_[0] eq 'ARRAY' } ],
- [ 1, ['a', 'b'], sub { $_[0]->[0] eq 'a' } ],
- [ 1, string('x'), sub { $_[0] eq 'x' } ],
- [ 1, smart('x'), sub { 'x' ~~ $_[0] } ],
- [ 0, smart('x'), sub { 'y' ~~ $_[0] } ],
- # any
- [ 'die', undef, '' ],
- [ 'die', undef, 0 ],
- [ 'die', undef, '0' ],
- [ 'die', undef, '0.0' ],
- [ 'die', undef, '0 but true' ],
- [ 'die', undef, 1 ],
- [ 'die', undef, 'x' ],
- [ 'die', undef, [] ],
- [ 'die', undef, {} ],
- [ 0, undef, sub {} ],
- [ 1, undef, smart('') ],
- [ 1, undef, regex('') ],
- [ 'die', undef, string('') ],
-);
-
-for my $test (@tests) {
- # shut up warnings about undef =~ regex
- $SIG{__WARN__} = sub { } unless defined $test->[1];
-
- if ($test->[0] eq 'die') {
- ok(!eval { $test->[1] ~~ $test->[2]; 1 });
- like($@, qr/invalid smart match/);
- }
- elsif ($test->[0]) {
- ok($test->[1] ~~ $test->[2]);
- }
- else {
- ok(!($test->[1] ~~ $test->[2]));
- }
-
- delete $SIG{__WARN__};
-}
-
-done_testing;