From: Vincent Pit Date: Wed, 21 Jul 2010 12:47:03 +0000 (+0200) Subject: Introduce LaTeX::TikZ::Set::Polyline X-Git-Tag: v0.01~30 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=080eb1e263eb7c3700a9ebfd96522eaa534a0b6c;p=perl%2Fmodules%2FLaTeX-TikZ.git Introduce LaTeX::TikZ::Set::Polyline --- diff --git a/MANIFEST b/MANIFEST index 8cda4e2..a9a373a 100644 --- a/MANIFEST +++ b/MANIFEST @@ -28,6 +28,7 @@ lib/LaTeX/TikZ/Set/Mutable.pm lib/LaTeX/TikZ/Set/Op.pm lib/LaTeX/TikZ/Set/Path.pm lib/LaTeX/TikZ/Set/Point.pm +lib/LaTeX/TikZ/Set/Polyline.pm lib/LaTeX/TikZ/Set/Raw.pm lib/LaTeX/TikZ/Set/Rectangle.pm lib/LaTeX/TikZ/Set/Sequence.pm diff --git a/lib/LaTeX/TikZ/Interface.pm b/lib/LaTeX/TikZ/Interface.pm index 91342ab..6496b06 100644 --- a/lib/LaTeX/TikZ/Interface.pm +++ b/lib/LaTeX/TikZ/Interface.pm @@ -63,6 +63,7 @@ sub load { require LaTeX::TikZ::Set::Point; # point require LaTeX::TikZ::Set::Line; # line + require LaTeX::TikZ::Set::Polyline; # polyline, closed_polyline require LaTeX::TikZ::Set::Rectangle; # rectangle require LaTeX::TikZ::Set::Circle; # circle diff --git a/lib/LaTeX/TikZ/Set/Point.pm b/lib/LaTeX/TikZ/Set/Point.pm index 0a4911f..409db5f 100644 --- a/lib/LaTeX/TikZ/Set/Point.pm +++ b/lib/LaTeX/TikZ/Set/Point.pm @@ -33,6 +33,10 @@ coerce 'LaTeX::TikZ::Set::Point' => from 'Any' => via { __PACKAGE__->new(point => $_) }; +coerce 'LaTeX::TikZ::Point::Autocoerce' + => from 'LaTeX::TikZ::Set::Point' + => via { $_->point }; + sub path { my ($set, $tikz) = @_; diff --git a/lib/LaTeX/TikZ/Set/Polyline.pm b/lib/LaTeX/TikZ/Set/Polyline.pm new file mode 100644 index 0000000..ef339e4 --- /dev/null +++ b/lib/LaTeX/TikZ/Set/Polyline.pm @@ -0,0 +1,96 @@ +package LaTeX::TikZ::Set::Polyline; + +use strict; +use warnings; + +=head1 NAME + +LaTeX::TikZ::Set::Polyline - A set object representing a line. + +=head1 VERSION + +Version 0.01 + +=cut + +our $VERSION = '0.01'; + +use LaTeX::TikZ::Set::Point; + +use Any::Moose; +use Any::Moose 'Util::TypeConstraints'; + +with 'LaTeX::TikZ::Set::Op'; + +subtype 'LaTeX::TikZ::Set::Polyline::Vertices' + => as 'ArrayRef[LaTeX::TikZ::Set::Point]' + => where { @$_ >= 2 } + => message { 'at least two LaTeX::TikZ::Set::Point objects are needed in order to build a polyline' }; + +coerce 'LaTeX::TikZ::Set::Polyline::Vertices' + => from 'ArrayRef[Any]' + => via { [ map LaTeX::TikZ::Set::Point->new(point => $_), @$_ ] }; + +has '_points' => ( + is => 'ro', + isa => 'LaTeX::TikZ::Set::Polyline::Vertices', + init_arg => 'points', + required => 1, + coerce => 1, +); + +sub points { @{$_[0]->_points} } + +has 'closed' => ( + is => 'ro', + isa => 'Bool', + default => 0, +); + +sub path { + my $set = shift; + + join ' -- ', map($_->path(@_), $set->points), + ($set->closed ? 'cycle' : ()); +} + +use LaTeX::TikZ::Interface polyline => sub { + shift; + + __PACKAGE__->new(points => \@_); +}; + +use LaTeX::TikZ::Interface closed_polyline => sub { + shift; + + __PACKAGE__->new(points => \@_, closed => 1); +}; + +__PACKAGE__->meta->make_immutable; + +=head1 AUTHOR + +Vincent Pit, C<< >>, L. + +You can contact me by mail or on C (vincent). + +=head1 BUGS + +Please report any bugs or feature requests to C, or through the web interface at L. +I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. + +=head1 SUPPORT + +You can find documentation for this module with the perldoc command. + + perldoc LaTeX::TikZ + +=head1 COPYRIGHT & LICENSE + +Copyright 2010 Vincent Pit, all rights reserved. + +This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. + +=cut + +1; # End of LaTeX::TikZ::Set::Polyline diff --git a/t/00-load.t b/t/00-load.t index 4eac5ce..33cfe89 100644 --- a/t/00-load.t +++ b/t/00-load.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 29; +use Test::More tests => 30; BEGIN { use_ok( 'LaTeX::TikZ' ); @@ -31,6 +31,7 @@ BEGIN { use_ok( 'LaTeX::TikZ::Set::Op' ); use_ok( 'LaTeX::TikZ::Set::Path' ); use_ok( 'LaTeX::TikZ::Set::Point' ); + use_ok( 'LaTeX::TikZ::Set::Polyline' ); use_ok( 'LaTeX::TikZ::Set::Raw' ); use_ok( 'LaTeX::TikZ::Set::Rectangle' ); use_ok( 'LaTeX::TikZ::Set::Sequence' ); diff --git a/t/01-api.t b/t/01-api.t index 7a29a36..dc15124 100644 --- a/t/01-api.t +++ b/t/01-api.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 5 + 13 + 12; +use Test::More tests => 5 + 15 + 12; use LaTeX::TikZ; @@ -31,7 +31,7 @@ is(prototype('Tikz'), '', 'main::Tikz is actually a constant'); my @methods = qw/ raw path seq - point line rectangle circle + point line polyline closed_polyline rectangle circle raw_mod clip layer width color fill diff --git a/t/12-geo.t b/t/12-geo.t index ab56eaa..3a7eef7 100644 --- a/t/12-geo.t +++ b/t/12-geo.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 11 + 2 * 10; +use Test::More tests => (11 + 2 * 5) + 2 * (10 + 2 * 3); use Math::Complex; @@ -32,6 +32,11 @@ sub check { is_deeply $body, $exp, $desc; } +sub failed_valid { + my ($tc) = @_; + qr/Validation failed for '\Q$tc\E'/; +} + my $o = Tikz->point(0); my $z = Tikz->point(1+2*i); @@ -55,6 +60,53 @@ check $l, 'a line from two Tikz points', <<'RES'; \draw (-1cm,2cm) -- (3cm,-4cm) ; RES +# Polyline + +my $w = Tikz->point(3, -4); + +for my $closed (0, 1) { + my $polyline = $closed ? 'closed_polyline' : 'polyline'; + my $cycle = $closed ? '-- cycle ' : ''; + my $desc = $closed ? 'closed polyline' : 'polyline'; + + my $pl = eval { + Tikz->$polyline($o, $z); + }; + is $@, '', "creating a $desc from two Tikz points doesn't croak"; + + check $pl, "a $desc from two Tikz points", <<"RES"; +\\draw (0cm,0cm) -- (1cm,2cm) $cycle; +RES + + $pl = eval { + Tikz->$polyline($o, $z, $w); + }; + is $@, '', "creating a $desc from three Tikz points doesn't croak"; + + check $pl, "a $desc from three Tikz points", <<"RES"; +\\draw (0cm,0cm) -- (1cm,2cm) -- (3cm,-4cm) $cycle; +RES + + $pl = eval { + Tikz->$polyline(-1, (2-3*i), [-4, 5]); + }; + is $@, '', "creating a $desc from three Tikz points doesn't croak"; + + check $pl, "a $desc from three Tikz points", <<"RES"; +\\draw (-1cm,0cm) -- (2cm,-3cm) -- (-4cm,5cm) $cycle; +RES + + $pl = eval { + Tikz->$polyline($o); + }; + like $@, qr/at least two LaTeX::TikZ::Set::Point objects are needed in order to build a polyline/, "creating a $desc from only one Tikz point croaks"; + + $pl = eval { + Tikz->$polyline(qw/foo bar/); + }; + like $@, failed_valid('LaTeX::TikZ::Point::Autocoerce'), "creating a $desc from two string croaks"; +} + # Rectangle my $r = eval {