From: Vincent Pit Date: Tue, 20 Jul 2010 19:50:41 +0000 (+0200) Subject: Coerce points from common types X-Git-Tag: v0.01~37 X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FLaTeX-TikZ.git;a=commitdiff_plain;h=19660696c832bcff26c87d371cccc4ffe118c782 Coerce points from common types --- diff --git a/lib/LaTeX/TikZ/Point.pm b/lib/LaTeX/TikZ/Point.pm index 3c24469..3166248 100644 --- a/lib/LaTeX/TikZ/Point.pm +++ b/lib/LaTeX/TikZ/Point.pm @@ -49,6 +49,14 @@ coerce 'LaTeX::TikZ::Point::Autocoerce' => from 'LaTeX::TikZ::Point' => via { $_ }; +coerce 'LaTeX::TikZ::Point::Autocoerce' + => from 'Num' + => via { LaTeX::TikZ::Point->new(x => $_, y => 0) }; + +coerce 'LaTeX::TikZ::Point::Autocoerce' + => from 'ArrayRef' + => via { LaTeX::TikZ::Point->new(x => $_->[0], y => $_->[1]) }; + __PACKAGE__->meta->make_immutable; =head1 AUTHOR diff --git a/lib/LaTeX/TikZ/Set/Point.pm b/lib/LaTeX/TikZ/Set/Point.pm index 58fb28c..c4749fe 100644 --- a/lib/LaTeX/TikZ/Set/Point.pm +++ b/lib/LaTeX/TikZ/Set/Point.pm @@ -39,7 +39,7 @@ sub path { use LaTeX::TikZ::Interface point => sub { shift; - __PACKAGE__->new(point => $_[0]); + __PACKAGE__->new(point => @_ > 1 ? \@_ : $_[0] ); }; __PACKAGE__->meta->make_immutable; diff --git a/t/11-point.t b/t/11-point.t index c7f53cd..9e73f15 100644 --- a/t/11-point.t +++ b/t/11-point.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 1 + 2 * 1; +use Test::More tests => 5 + 2 * 5; use Math::Complex; @@ -42,3 +42,39 @@ is $@, '', 'creating a point from a Math::Complex object doesn\'t croak'; check $p, 'a point from a Math::Complex object', <<'RES'; \draw (1cm,2cm) ; RES + +$p = eval { + Tikz->point(1-2*i); +}; +is $@, '', 'creating a point from a Math::Complex constant object doesn\'t croak'; + +check $p, 'a point from a constant Math::Complex object', <<'RES'; +\draw (1cm,-2cm) ; +RES + +$p = eval { + Tikz->point(-7); +}; +is $@, '', 'creating a point from a numish constant doesn\'t croak'; + +check $p, 'a point from a numish constant', <<'RES'; +\draw (-7cm,0cm) ; +RES + +$p = eval { + Tikz->point(5,-1); +}; +is $@, '', 'creating a point from two numish constants doesn\'t croak'; + +check $p, 'a point from two numish constants', <<'RES'; +\draw (5cm,-1cm) ; +RES + +$p = eval { + Tikz->point([-3, 2]); +}; +is $@, '', 'creating a point from an array ref doesn\'t croak'; + +check $p, 'a point from an array ref', <<'RES'; +\draw (-3cm,2cm) ; +RES