]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - t/23-pattern.t
Complete patterns implementation, interface and tests
[perl/modules/LaTeX-TikZ.git] / t / 23-pattern.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 2 + 4 * 2;
7
8 use LaTeX::TikZ;
9
10 my $tikz = Tikz->formatter(
11  format => '%d',
12 );
13
14 sub check {
15  my ($set, $desc, $exp_decl, $exp) = @_;
16
17  local $Test::Builder::Level = $Test::Builder::Level + 1;
18
19  my ($head, $decl, $body) = eval {
20   $tikz->render(ref $set eq 'ARRAY' ? @$set : $set);
21  };
22  is $@, '', "$desc: no error";
23
24  is $head->[-1], '\usetikzlibrary{patterns}', "$desc: header";
25
26  unless (ref $exp_decl eq 'ARRAY') {
27   $exp_decl = [ split /\n/, $exp_decl ];
28  }
29
30  unless (ref $exp eq 'ARRAY') {
31   $exp = [ split /\n/, $exp ];
32  }
33  unshift @$exp, '\begin{tikzpicture}';
34  push    @$exp, '\end{tikzpicture}';
35
36  is_deeply $decl, $exp_decl, "$desc: declarations";
37  is_deeply $body, $exp,      "$desc: body";
38 }
39
40 my $lines = eval {
41  Tikz->raw("foo")
42      ->mod(Tikz->pattern(class => 'Lines'));
43 };
44 is $@, '', 'creating a line pattern doesn\'t croak';
45
46 check $lines, 'a line pattern', <<'DECL', <<'BODY';
47 \pgfdeclarepatternformonly{pata}{\pgfqpoint{-0.2pt}{-0.2pt}}{\pgfqpoint{0.3pt}{0.3pt}}{\pgfqpoint{0.2pt}{0.2pt}}{
48 \pgfsetlinewidth{0.2pt}
49 \pgfpathmoveto{\pgfqpoint{-0.2pt}{0.1pt}}
50 \pgfpathlineto{\pgfqpoint{0.3pt}{0.1pt}}
51 \pgfusepath{stroke}
52 }
53 DECL
54 \draw [fill,pattern=pata] foo ;
55 BODY
56
57 my $dots = eval {
58  Tikz->raw("foo")
59      ->mod(Tikz->pattern(class => 'Dots'));
60 };
61 is $@, '', 'creating a dot pattern doesn\'t croak';
62
63 check $dots, 'a dot pattern', <<'DECL', <<'BODY';
64 \pgfdeclarepatternformonly{patb}{\pgfqpoint{-0.2pt}{-0.2pt}}{\pgfqpoint{0.3pt}{0.3pt}}{\pgfqpoint{0.2pt}{0.2pt}}{
65 \pgfpathcircle{\pgfqpoint{0.1pt}{0.1pt}}{0.2pt}
66 \pgfusepath{fill}
67 }
68 DECL
69 \draw [fill,pattern=patb] foo ;
70 BODY