From: Vincent Pit Date: Fri, 26 Nov 2010 13:21:16 +0000 (+0100) Subject: Factor the check() test function in an helper test module X-Git-Tag: rt87282~24 X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FLaTeX-TikZ.git;a=commitdiff_plain;h=1ef0d773dfe4f90087af6a39b06061eaadb44a65 Factor the check() test function in an helper test module --- diff --git a/MANIFEST b/MANIFEST index ba92b57..a717992 100644 --- a/MANIFEST +++ b/MANIFEST @@ -53,4 +53,5 @@ t/91-pod.t t/92-pod-coverage.t t/95-portability-files.t t/99-kwalitee.t +t/lib/LaTeX/TikZ/TestHelper.pm t/lib/LaTeX/TikZ/TestX/FromTestY.pm diff --git a/t/10-set.t b/t/10-set.t index 6ff94ef..5ec0e33 100644 --- a/t/10-set.t +++ b/t/10-set.t @@ -7,29 +7,13 @@ use Test::More tests => 19 + 2 * 14; 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 $foo = eval { Tikz->raw('foo'); }; diff --git a/t/11-point.t b/t/11-point.t index 0ceee6c..a8481ec 100644 --- a/t/11-point.t +++ b/t/11-point.t @@ -9,29 +9,13 @@ use Math::Complex; 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 $z = Math::Complex->make(1, 2); my $p = eval { diff --git a/t/12-geo.t b/t/12-geo.t index f36d002..c1caa3e 100644 --- a/t/12-geo.t +++ b/t/12-geo.t @@ -9,29 +9,13 @@ use Math::Complex; 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; -} - sub failed_valid { my ($tc) = @_; qr/Validation failed for '\Q$tc\E'/; @@ -211,7 +195,7 @@ like $@, failed_valid('__ANON__'), # Arc -$tikz = Tikz->formatter( +using Tikz->formatter( format => "%.03f" ); diff --git a/t/20-mod.t b/t/20-mod.t index 878a72b..ae9cc77 100644 --- a/t/20-mod.t +++ b/t/20-mod.t @@ -7,29 +7,13 @@ use Test::More tests => 20 + 2 * 22; 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 $red = eval { Tikz->color('red'); }; diff --git a/t/21-layer.t b/t/21-layer.t index 1087d5c..a4990b5 100644 --- a/t/21-layer.t +++ b/t/21-layer.t @@ -7,25 +7,19 @@ use Test::More tests => 9 + 3 * 10; use LaTeX::TikZ; -my $tikz = Tikz->formatter( +use lib 't/lib'; +use LaTeX::TikZ::TestHelper; + +using Tikz->formatter( format => '%d', ); -sub check { +sub check_layers { my ($set, $desc, $exp, $layers) = @_; 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}'; + my ($head, $decl, $body) = check($set, $desc, $exp); my $exp_decl = [ map("\\pgfdeclarelayer{$_}", @$layers), @@ -33,7 +27,6 @@ sub check { ]; is_deeply $decl, $exp_decl, "$desc: declarations"; - is_deeply $body, $exp, "$desc: body"; } my $middle = eval { @@ -64,7 +57,7 @@ my $foo = eval { }; is $@, '', 'creating a layered raw set doesn\'t croak'; -check $foo, 'one layered raw set', <<'RES', [ 'middle' ]; +check_layers $foo, 'one layered raw set', <<'RES', [ 'middle' ]; \begin{pgfonlayer}{middle} \draw foo ; \end{pgfonlayer} @@ -78,7 +71,8 @@ is $@, '', 'creating another layered raw set doesn\'t croak'; my $seq = Tikz->seq($foo, $bar); -check $seq, 'a sequence of two layered raw sets', <<'RES', [ qw/middle top/ ]; +check_layers $seq, 'a sequence of two layered raw sets', + <<'RES', [ qw/middle top/ ]; \begin{pgfonlayer}{middle} \draw foo ; \end{pgfonlayer} @@ -108,7 +102,7 @@ my $res = eval { is $@, '', 'calling an empty ->layer onto a sequence doesn\'t croak'; is "$res", "$seq", 'empty ->layer returns the object itself'; -check $seq, 'a layered sequence', <<'RES', [ qw/bottom middle top/ ]; +check_layers $seq, 'a layered sequence', <<'RES', [ qw/bottom middle top/ ]; \begin{pgfonlayer}{bottom} \begin{pgfonlayer}{middle} \draw foo ; @@ -125,7 +119,8 @@ $seq->add($baz); my $red = Tikz->color('red'); $seq->mod($red); -check $seq, 'mods folding with layers 1', <<'RES', [ qw/bottom middle top/ ]; +check_layers $seq, 'mods folding with layers 1', + <<'RES', [ qw/bottom middle top/ ]; \begin{pgfonlayer}{bottom} \begin{scope} [color=red] \begin{pgfonlayer}{middle} @@ -141,7 +136,8 @@ RES $baz->mod($top); -check $seq, 'mods folding with layers 2', <<'RES', [ qw/bottom middle top/ ]; +check_layers $seq, 'mods folding with layers 2', + <<'RES', [ qw/bottom middle top/ ]; \begin{pgfonlayer}{bottom} \begin{scope} [color=red] \begin{pgfonlayer}{middle} @@ -160,7 +156,7 @@ RES my $seq2 = Tikz->seq($bar, $baz, $foo) ->mod($red); -check $seq2, 'mods folding with layers 3', <<'RES', [ qw/middle top/ ]; +check_layers $seq2, 'mods folding with layers 3', <<'RES', [ qw/middle top/ ]; \begin{scope} [color=red] \begin{pgfonlayer}{top} \begin{scope} [color=red] @@ -178,7 +174,7 @@ my $qux = Tikz->raw('qux'); $seq2 = Tikz->seq($qux, $foo) ->mod($red); -check $seq2, 'mods folding with layers 4', <<'RES', [ 'middle' ]; +check_layers $seq2, 'mods folding with layers 4', <<'RES', [ 'middle' ]; \begin{scope} [color=red] \draw qux ; \begin{pgfonlayer}{middle} @@ -190,7 +186,7 @@ RES my $seq3 = Tikz->seq($seq2, $bar) ->mod($red); -check $seq3, 'mods folding with layers 5', <<'RES', [ qw/middle top/ ]; +check_layers $seq3, 'mods folding with layers 5', <<'RES', [ qw/middle top/ ]; \begin{scope} [color=red] \draw qux ; \begin{pgfonlayer}{middle} @@ -205,7 +201,7 @@ RES $seq3 = Tikz->seq($bar, $seq2) ->mod($red); -check $seq3, 'mods folding with layers 6', <<'RES', [ qw/middle top/ ]; +check_layers $seq3, 'mods folding with layers 6', <<'RES', [ qw/middle top/ ]; \begin{scope} [color=red] \begin{pgfonlayer}{top} \draw [color=red] bar ; @@ -220,7 +216,7 @@ RES my $blue = Tikz->color('blue'); $qux->mod($blue); -check $seq2, 'mods folding with layers 7', <<'RES', [ 'middle' ]; +check_layers $seq2, 'mods folding with layers 7', <<'RES', [ 'middle' ]; \begin{scope} [color=red] \draw [color=blue] qux ; \begin{pgfonlayer}{middle} diff --git a/t/22-clip.t b/t/22-clip.t index 0b0ac32..029dc5b 100644 --- a/t/22-clip.t +++ b/t/22-clip.t @@ -7,29 +7,13 @@ use Test::More tests => 8 + 2 * 16; 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 $clip1 = Tikz->raw('clip1'); my $clip2 = Tikz->raw('clip2'); diff --git a/t/23-pattern.t b/t/23-pattern.t index c592ad0..0370322 100644 --- a/t/23-pattern.t +++ b/t/23-pattern.t @@ -7,19 +7,19 @@ use Test::More tests => 5 + 4 * 5; use LaTeX::TikZ; -my $tikz = Tikz->formatter( +use lib 't/lib'; +use LaTeX::TikZ::TestHelper; + +using Tikz->formatter( format => '%d', ); -sub check { +sub check_patterns { my ($set, $desc, $exp_decl, $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"; + my ($head, $decl, $body) = check($set, $desc, $exp); is $head->[-1], '\usetikzlibrary{patterns}', "$desc: header"; @@ -27,14 +27,7 @@ sub check { $exp_decl = [ split /\n/, $exp_decl ]; } - unless (ref $exp eq 'ARRAY') { - $exp = [ split /\n/, $exp ]; - } - unshift @$exp, '\begin{tikzpicture}'; - push @$exp, '\end{tikzpicture}'; - is_deeply $decl, $exp_decl, "$desc: declarations"; - is_deeply $body, $exp, "$desc: body"; } my $lines = eval { @@ -43,7 +36,7 @@ my $lines = eval { }; is $@, '', 'creating a line pattern doesn\'t croak'; -check $lines, 'a line pattern', <<'DECL', <<'BODY'; +check_patterns $lines, 'a line pattern', <<'DECL', <<'BODY'; \pgfdeclarepatternformonly{pata}{\pgfqpoint{-0.2pt}{-0.2pt}}{\pgfqpoint{1.8pt}{1.8pt}}{\pgfqpoint{1.6pt}{1.6pt}}{ \pgfsetlinewidth{0.2pt} \pgfpathmoveto{\pgfqpoint{-0.2pt}{0.8pt}} @@ -60,7 +53,7 @@ my $dots = eval { }; is $@, '', 'creating a dot pattern doesn\'t croak'; -check $dots, 'a dot pattern', <<'DECL', <<'BODY'; +check_patterns $dots, 'a dot pattern', <<'DECL', <<'BODY'; \pgfdeclarepatternformonly{patb}{\pgfqpoint{-0.2pt}{-0.2pt}}{\pgfqpoint{1.8pt}{1.8pt}}{\pgfqpoint{1.6pt}{1.6pt}}{ \pgfpathcircle{\pgfqpoint{0.8pt}{0.8pt}}{0.2pt} \pgfusepath{fill} @@ -80,7 +73,7 @@ my $seq = eval { }; is $@, '', 'creating a sequence with two identic patterns doesn\'t croak'; -check $seq, 'a sequence with two identic patterns', <<'DECL', <<'BODY'; +check_patterns $seq, 'a sequence with two identic patterns', <<'DECL', <<'BODY'; \pgfdeclarepatternformonly{pata}{\pgfqpoint{-0.2pt}{-0.2pt}}{\pgfqpoint{1.8pt}{1.8pt}}{\pgfqpoint{1.6pt}{1.6pt}}{ \pgfsetlinewidth{0.2pt} \pgfpathmoveto{\pgfqpoint{-0.2pt}{0.8pt}} @@ -100,7 +93,8 @@ $seq = eval { is $@, '', 'creating a sequence with two orthogonal line patterns doesn\'t croak'; -check $seq, 'a sequence with two orthogonal line patterns', <<'DECL', <<'BODY'; +check_patterns $seq, 'a sequence with two orthogonal line patterns', + <<'DECL', <<'BODY'; \pgfdeclarepatternformonly{patc}{\pgfqpoint{-0.2pt}{-0.2pt}}{\pgfqpoint{1.8pt}{1.8pt}}{\pgfqpoint{1.6pt}{1.6pt}}{ \pgfsetlinewidth{0.2pt} \pgfpathmoveto{\pgfqpoint{0.8pt}{-0.2pt}} @@ -125,7 +119,8 @@ $seq = eval { }; is $@, '', 'creating a sequence with two different patterns doesn\'t croak'; -check $seq, 'a sequence with two different patterns', <<'DECL', <<'BODY'; +check_patterns $seq, 'a sequence with two different patterns', + <<'DECL', <<'BODY'; \pgfdeclarepatternformonly{patb}{\pgfqpoint{-0.2pt}{-0.2pt}}{\pgfqpoint{1.8pt}{1.8pt}}{\pgfqpoint{1.6pt}{1.6pt}}{ \pgfpathcircle{\pgfqpoint{0.8pt}{0.8pt}}{0.2pt} \pgfusepath{fill} diff --git a/t/30-functor.t b/t/30-functor.t index bdade4b..64d7d4b 100644 --- a/t/30-functor.t +++ b/t/30-functor.t @@ -7,29 +7,13 @@ use Test::More tests => 10 + 2 * 7; 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 { @@ -158,7 +142,7 @@ check $res, 'the result of the special functor', <<'RES'; \draw moo ; RES -$tikz = eval { +using eval { Tikz->formatter( origin => [ -1, 1 ], ); diff --git a/t/lib/LaTeX/TikZ/TestHelper.pm b/t/lib/LaTeX/TikZ/TestHelper.pm new file mode 100644 index 0000000..deb1dff --- /dev/null +++ b/t/lib/LaTeX/TikZ/TestHelper.pm @@ -0,0 +1,43 @@ +package LaTeX::TikZ::TestHelper; + +use strict; +use warnings; + +use Test::More (); + +use Any::Moose 'Exporter'; + +any_moose('Exporter')->setup_import_methods( + as_is => [ qw ], +); + +my $tikz; + +sub using { + $tikz = $_[0] if defined $_[0]; + + return $tikz; +} + +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); + }; + Test::More::is($@, '', "$desc: no error"); + + unless (ref $exp eq 'ARRAY') { + $exp = [ split /\n/, $exp ]; + } + unshift @$exp, '\begin{tikzpicture}'; + push @$exp, '\end{tikzpicture}'; + + Test::More::is_deeply($body, $exp, "$desc: body"); + + return $head, $decl, $body; +} + +1;