]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - t/23-pattern.t
5f69bd0f22290156244f9ba0809d4f4cf6ceabe9
[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 => 5 + 4 * 5;
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
71
72 my ($lines_mod) = $lines->mods;
73 my ($dots_mod)  = $dots->mods;
74
75 my $seq = eval {
76  Tikz->seq(
77   Tikz->raw('foo')
78       ->mod($lines_mod)
79  )->mod($lines_mod);
80 };
81 is $@, '', 'creating a sequence with two identic patterns doesn\'t croak';
82
83 check $seq, 'a sequence with two identic patterns', <<'DECL', <<'BODY';
84 \pgfdeclarepatternformonly{pata}{\pgfqpoint{-0.2pt}{-0.2pt}}{\pgfqpoint{0.3pt}{0.3pt}}{\pgfqpoint{0.2pt}{0.2pt}}{
85 \pgfsetlinewidth{0.2pt}
86 \pgfpathmoveto{\pgfqpoint{-0.2pt}{0.1pt}}
87 \pgfpathlineto{\pgfqpoint{0.3pt}{0.1pt}}
88 \pgfusepath{stroke}
89 }
90 DECL
91 \draw [fill,pattern=pata] foo ;
92 BODY
93
94 $seq = eval {
95  Tikz->seq(
96   Tikz->raw('foo')
97       ->mod($lines_mod)
98  )->mod(Tikz->pattern(class => 'Lines', direction => 'vertical'));
99 };
100 is $@, '',
101          'creating a sequence with two orthogonal line patterns doesn\'t croak';
102
103 check $seq, 'a sequence with two orthogonal line patterns', <<'DECL', <<'BODY';
104 \pgfdeclarepatternformonly{patc}{\pgfqpoint{-0.2pt}{-0.2pt}}{\pgfqpoint{0.3pt}{0.3pt}}{\pgfqpoint{0.2pt}{0.2pt}}{
105 \pgfsetlinewidth{0.2pt}
106 \pgfpathmoveto{\pgfqpoint{0.1pt}{-0.2pt}}
107 \pgfpathlineto{\pgfqpoint{0.1pt}{0.3pt}}
108 \pgfusepath{stroke}
109 }
110 \pgfdeclarepatternformonly{pata}{\pgfqpoint{-0.2pt}{-0.2pt}}{\pgfqpoint{0.3pt}{0.3pt}}{\pgfqpoint{0.2pt}{0.2pt}}{
111 \pgfsetlinewidth{0.2pt}
112 \pgfpathmoveto{\pgfqpoint{-0.2pt}{0.1pt}}
113 \pgfpathlineto{\pgfqpoint{0.3pt}{0.1pt}}
114 \pgfusepath{stroke}
115 }
116 DECL
117 \draw [fill,pattern=patc,pattern=pata] foo ;
118 BODY
119
120 $seq = eval {
121  Tikz->seq(
122   Tikz->raw('foo')
123       ->mod($lines_mod)
124  )->mod($dots_mod);
125 };
126 is $@, '', 'creating a sequence with two different patterns doesn\'t croak';
127
128 check $seq, 'a sequence with two different patterns', <<'DECL', <<'BODY';
129 \pgfdeclarepatternformonly{patb}{\pgfqpoint{-0.2pt}{-0.2pt}}{\pgfqpoint{0.3pt}{0.3pt}}{\pgfqpoint{0.2pt}{0.2pt}}{
130 \pgfpathcircle{\pgfqpoint{0.1pt}{0.1pt}}{0.2pt}
131 \pgfusepath{fill}
132 }
133 \pgfdeclarepatternformonly{pata}{\pgfqpoint{-0.2pt}{-0.2pt}}{\pgfqpoint{0.3pt}{0.3pt}}{\pgfqpoint{0.2pt}{0.2pt}}{
134 \pgfsetlinewidth{0.2pt}
135 \pgfpathmoveto{\pgfqpoint{-0.2pt}{0.1pt}}
136 \pgfpathlineto{\pgfqpoint{0.3pt}{0.1pt}}
137 \pgfusepath{stroke}
138 }
139 DECL
140 \draw [fill,pattern=patb,pattern=pata] foo ;
141 BODY