X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=t%2F30-functor.t;h=74ff18d727f489e771a7181ebdf784e03eb34204;hb=93edf9c5c04a04fbd3c8dbf04eacaf66736ff6ee;hp=86221e9e6bfd9d71a480e5c6d90141ee1faf3d86;hpb=a31ad833b19a240ea560019c24153d6115aeda88;p=perl%2Fmodules%2FLaTeX-TikZ.git diff --git a/t/30-functor.t b/t/30-functor.t index 86221e9..74ff18d 100644 --- a/t/30-functor.t +++ b/t/30-functor.t @@ -1,35 +1,19 @@ -#!perl +#!perl -T use strict; use warnings; -use Test::More tests => 5 + 2 * 5; +use Test::More tests => 11 + 2 * 8; use LaTeX::TikZ; -my $tikz = Tikz->formatter( +use lib 't/lib'; +use LaTeX::TikZ::TestHelper; + +using Tikz->formatter( format => '%d', ); -sub check { - my ($set, $desc, $exp) = @_; - - local $Test::Builder::Level = $Test::Builder::Level + 1; - - my ($head, $decl, $body) = eval { - $tikz->render(ref $set eq 'ARRAY' ? @$set : $set); - }; - is $@, '', "$desc: no error"; - - unless (ref $exp eq 'ARRAY') { - $exp = [ split /\n/, $exp ]; - } - unshift @$exp, '\begin{tikzpicture}'; - push @$exp, '\end{tikzpicture}'; - - is_deeply $body, $exp, $desc; -} - my $translate = eval { Tikz->functor( 'LaTeX::TikZ::Set::Point' => sub { @@ -53,7 +37,11 @@ my $seq = Tikz->seq( Tikz->raw('foo'), Tikz->point(2), Tikz->line(-1 => 3) - ->clip(Tikz->circle(1, 1)) + ->clip(Tikz->circle(1, 1)), + Tikz->union( + Tikz->chain(4 => '--' => [ -3, 2 ]), + Tikz->join('-|' => [ -1, 0 ], [ 0, 1 ]), + ), ) ->clip(Tikz->rectangle([0, -1] => [2, 3])); @@ -72,6 +60,7 @@ check $seq, 'the original sequence', <<'RES'; \clip (1cm,0cm) circle (1cm) ; \draw (-1cm,0cm) -- (3cm,0cm) ; \end{scope} +\draw (4cm,0cm) -- (-3cm,2cm) (-1cm,0cm) -| (0cm,1cm) ; \end{scope} RES @@ -85,12 +74,26 @@ check $seq2, 'the translated sequence', <<'RES'; \clip (0cm,1cm) circle (1cm) ; \draw (-2cm,1cm) -- (2cm,1cm) ; \end{scope} +\draw (3cm,1cm) -- (-4cm,3cm) (-2cm,1cm) -| (-1cm,2cm) ; \end{scope} RES +my $poly = Tikz->closed_polyline( + [ 0, 0 ], [ 1, 0 ], [ 1, 1 ], [ 0, 1 ] +); + +my $poly2 = eval { + $poly->$translate(Tikz->point(-1, 1)); +}; +is $@, '', 'translating a polyline doesn\'t croak'; + +check $poly2, 'the translated polyline', <<'RES'; +\draw (-1cm,1cm) -- (0cm,1cm) -- (0cm,2cm) -- (-1cm,2cm) -- cycle ; +RES + my $strip = eval { Tikz->functor( - 'LaTeX::TikZ::Mod' => sub { return }, + '+LaTeX::TikZ::Mod' => sub { return }, ); }; is $@, '', 'creating a stripper doesn\'t croak'; @@ -112,6 +115,7 @@ check $seq2, 'the original sequence', <<'RES'; \clip (0cm,1cm) circle (1cm) ; \draw (-2cm,1cm) -- (2cm,1cm) ; \end{scope} +\draw (3cm,1cm) -- (-4cm,3cm) (-2cm,1cm) -| (-1cm,2cm) ; \end{scope} RES @@ -120,9 +124,46 @@ check $seq3, 'the stripped sequence', <<'RES'; \draw foo ; \draw (1cm,1cm) ; \draw (-2cm,1cm) -- (2cm,1cm) ; +\draw (3cm,1cm) -- (-4cm,3cm) (-2cm,1cm) -| (-1cm,2cm) ; +RES + +my $special = eval { + Tikz->functor( + '+LaTeX::TikZ::Mod' => sub { die "mod\n" }, + '+LaTeX::TikZ::Set' => sub { die "set\n" }, + ); +}; +is $@, '', 'creating a special functor with + rules doesn\'t croak'; + +eval { $seq->$special }; +is $@, "set\n", 'special functor with + rules eats everything properly'; + +$special = eval { + Tikz->functor( + '+LaTeX::TikZ::Mod' => sub { die "mod\n" }, + '+LaTeX::TikZ::Set' => sub { die "set\n" }, + 'LaTeX::TikZ::Set::Point' => sub { Tikz->point(7) }, + 'LaTeX::TikZ::Set::Path' => sub { Tikz->raw('moo') }, + ); +}; +is $@, '', 'creating a special functor with + and normal rules doesn\'t croak'; + +my $res = eval { Tikz->point(3, 4)->$special }; +is $@, '', 'special functor with + and normal rules orders its rules properly'; + +check $res, 'the result of the special functor', <<'RES'; +\draw (7cm,0cm) ; RES -$tikz = eval { +$res = eval { Tikz->raw('hlagh')->$special }; +is $@, '', + 'special functor with + and normal rules orders its rules properly again'; + +check $res, 'the result of the special functor', <<'RES'; +\draw moo ; +RES + +using eval { Tikz->formatter( origin => [ -1, 1 ], ); @@ -139,6 +180,6 @@ check $seq, 'a sequence translated by an origin', <<'RES'; \clip (0cm,1cm) circle (1cm) ; \draw (-2cm,1cm) -- (2cm,1cm) ; \end{scope} +\draw (3cm,1cm) -- (-4cm,3cm) (-2cm,1cm) -| (-1cm,2cm) ; \end{scope} RES -