]> git.imager.perl.org - imager.git/blobdiff - t/testtools.pl
- minor cleanup of Imager::Fill
[imager.git] / t / testtools.pl
index f6125318d753bef8272d494140316e17896bfc86..e36d8073e70bf3e11db7698de5fd60ea30ac3596 100644 (file)
@@ -20,6 +20,14 @@ sub test_img {
   $img;
 }
 
+sub test_oo_img {
+  my $raw = test_img();
+  my $img = Imager->new;
+  $img->{IMG} = $raw;
+
+  $img;
+}
+
 sub skipn {
   my ($testnum, $count, $why) = @_;
   
@@ -35,13 +43,13 @@ sub skipx {
   $TESTNUM += $count;
 }
 
-sub okx {
+sub okx ($$) {
   my ($ok, $comment) = @_;
 
   return okn($TESTNUM++, $ok, $comment);
 }
 
-sub okn {
+sub okn ($$$) {
   my ($num, $ok, $comment) = @_;
 
   defined $num or confess "No \$num supplied";
@@ -74,16 +82,35 @@ sub requireokx {
   }
 }
 
+sub useokx {
+  my ($module, $comment, @imports) = @_;
+  
+  my $pack = caller;
+  eval <<EOS;
+package $pack;
+require $module;
+$module->import(\@imports);
+EOS
+  unless (okx(!$@, $comment)) {
+    my $msg = $@;
+    $msg =~ s/\n+$//;
+    $msg =~ s/\n/\n# /g;
+    print "# $msg\n";
+    return 0;
+  }
+  else {
+    return 1;
+  }
+}
+
 sub matchn($$$$) {
   my ($num, $str, $re, $comment) = @_;
 
-  my $match = $str =~ $re;
+  my $match = defined($str) && $str =~ $re;
   okn($num, $match, $comment);
   unless ($match) {
-    $str =~ s/\\/\\\\/g;
-    $str =~ s/[^\x20-\x7E]/"\\x".sprintf("%02X", ord($1))/ge;
-    print "# The string '$str'\n";
-    print "# did not match '$re'\n";
+    print "# The value: ",_sv_str($str),"\n";
+    print "# did not match: qr/$re/\n";
   }
   return $match;
 }
@@ -94,5 +121,64 @@ sub matchx($$$) {
   matchn($TESTNUM++, $str, $re, $comment);
 }
 
+sub isn ($$$$) {
+  my ($num, $left, $right, $comment) = @_;
+
+  my $match;
+  if (!defined $left && defined $right
+     || defined $left && !defined $right) {
+    $match = 0;
+  }
+  elsif (!defined $left && !defined $right) {
+    $match = 1;
+  }
+  # the right of the || produces a string of \0 if $left is a PV
+  # which is true
+  elsif (!length $left  || ($left & ~$left) ||
+        !length $right || ($right & ~$right)) {
+    $match = $left eq $right;
+  }
+  else {
+    $match = $left == $right;
+  }
+  okn($num, $match, $comment);
+  unless ($match) {
+    print "# the following two values were not equal:\n";
+    print "# value: ",_sv_str($left),"\n";
+    print "# other: ",_sv_str($right),"\n";
+  }
+
+  $match;
+}
+
+sub isx ($$$) {
+  my ($left, $right, $comment) = @_;
+
+  isn($TESTNUM++, $left, $right, $comment);
+}
+
+sub _sv_str {
+  my ($value) = @_;
+
+  if (defined $value) {
+    if (!length $value || ($value & ~$value)) {
+      $value =~ s/\\/\\\\/g;
+      $value =~ s/\r/\\r/g;
+      $value =~ s/\n/\\n/g;
+      $value =~ s/\t/\\t/g;
+      $value =~ s/\"/\\"/g;
+      $value =~ s/([^ -\x7E])/"\\x".sprintf("%02x", ord($1))/ge;
+
+      return qq!"$value"!;
+    }
+    else {
+      return $value; # a number
+    }
+  }
+  else {
+    return "undef";
+  }
+}
+
 1;