]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - t/20-mod.t
7a68349c197cc7adffe3869e4a24918693582554
[perl/modules/LaTeX-TikZ.git] / t / 20-mod.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 19 + 2 * 21;
7
8 use LaTeX::TikZ;
9
10 my $tikz = Tikz->formatter(
11  format => '%d',
12 );
13
14 sub check {
15  my ($set, $desc, $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  unless (ref $exp eq 'ARRAY') {
25   $exp = [ split /\n/, $exp ];
26  }
27  unshift @$exp, '\begin{tikzpicture}';
28  push    @$exp, '\end{tikzpicture}';
29
30  is_deeply $body, $exp, $desc;
31 }
32
33 my $red = eval {
34  Tikz->color('red');
35 };
36 is $@, '', 'creating a color mod doesn\'t croak';
37
38 my $foo = eval {
39  Tikz->raw('foo')
40      ->mod($red)
41 };
42 is $@, '', 'creating a modded raw set doesn\'t croak';
43
44 check $foo, 'one modded raw set', <<'RES';
45 \draw [color=red] foo ;
46 RES
47
48 sub failed_valid {
49  my ($tc) = @_;
50  qr/Validation failed for '\Q$tc\E'/;
51 }
52
53 eval {
54  Tikz->raw('fail')
55      ->mod(Tikz->raw('epic'));
56 };
57 like $@, failed_valid('LaTeX::TikZ::Mod'), 'trying to use a non LTM mod croaks';
58
59 my $width = eval {
60  Tikz->width(25);
61 };
62 is $@, '', 'creating a width mod doesn\'t croak';
63
64 eval {
65  $foo->mod($width);
66 };
67 is $@, '', 'adding another mod doesn\'t croak';
68
69 check $foo, 'one double modded raw set', <<'RES';
70 \draw [color=red,line width=4.0pt] foo ;
71 RES
72
73 eval {
74  $foo->mod($red);
75 };
76 is $@, '', 're-adding an previously set mod doesn\'t croak';
77
78 check $foo, 'one triple modded raw set (with duplicates)', <<'RES';
79 \draw [color=red,line width=4.0pt] foo ;
80 RES
81
82 my $bar = Tikz->raw('bar');
83 $foo = eval {
84  Tikz->seq(
85   Tikz->raw('foo'),
86   $bar
87  )->mod($red, $width);
88 };
89 is $@, '', 'setting two mods in a row doesn\'t croak';
90
91 check $foo, 'one triple modded sequence of raw sets', <<'RES';
92 \begin{scope} [color=red,line width=4.0pt]
93 \draw foo ;
94 \draw bar ;
95 \end{scope}
96 RES
97
98 my $nested = eval {
99  Tikz->seq(
100   Tikz->seq(Tikz->raw("foo"))
101       ->mod($red)
102  )->mod($width)
103 };
104 is $@, '', 'creating nested modded sequences doesn\'t croak';
105
106 check $nested, 'nested modded sequences', <<'RES';
107 \draw [line width=4.0pt,color=red] foo ;
108 RES
109
110 my $baz = eval {
111  Tikz->raw('baz')
112      ->mod($red);
113 };
114 is $@, '', 'creating another colored raw set doesn\'t croak';
115
116 check [ $foo, $baz ], 'mods folding 1', <<'RES';
117 \begin{scope} [color=red]
118 \begin{scope} [line width=4.0pt]
119 \draw foo ;
120 \draw bar ;
121 \end{scope}
122 \draw baz ;
123 \end{scope}
124 RES
125
126 check [ $baz, $foo ], 'mods folding 2', <<'RES';
127 \begin{scope} [color=red]
128 \draw baz ;
129 \begin{scope} [line width=4.0pt]
130 \draw foo ;
131 \draw bar ;
132 \end{scope}
133 \end{scope}
134 RES
135
136 my $qux = eval {
137  Tikz->raw('qux')
138      ->mod($width);
139 };
140 is $@, '', 'creating another raw set with modded width doesn\'t croak';
141
142 check [ $foo, $baz, $qux ], 'mods folding 3', <<'RES';
143 \begin{scope} [color=red]
144 \begin{scope} [line width=4.0pt]
145 \draw foo ;
146 \draw bar ;
147 \end{scope}
148 \draw baz ;
149 \end{scope}
150 \draw [line width=4.0pt] qux ;
151 RES
152
153 check [ $foo, $qux, $baz ], 'mods folding 4', <<'RES';
154 \begin{scope} [line width=4.0pt]
155 \begin{scope} [color=red]
156 \draw foo ;
157 \draw bar ;
158 \end{scope}
159 \draw qux ;
160 \end{scope}
161 \draw [color=red] baz ;
162 RES
163
164 check [ $baz, $foo, $qux ], 'mods folding 5', <<'RES';
165 \begin{scope} [color=red]
166 \draw baz ;
167 \begin{scope} [line width=4.0pt]
168 \draw foo ;
169 \draw bar ;
170 \end{scope}
171 \end{scope}
172 \draw [line width=4.0pt] qux ;
173 RES
174
175 check [ $baz, $qux, $foo ], 'mods folding 6', <<'RES';
176 \draw [color=red] baz ;
177 \draw [line width=4.0pt] qux ;
178 \begin{scope} [color=red,line width=4.0pt]
179 \draw foo ;
180 \draw bar ;
181 \end{scope}
182 RES
183
184 check [ $qux, $foo, $baz ], 'mods folding 7', <<'RES';
185 \begin{scope} [line width=4.0pt]
186 \draw qux ;
187 \begin{scope} [color=red]
188 \draw foo ;
189 \draw bar ;
190 \end{scope}
191 \end{scope}
192 \draw [color=red] baz ;
193 RES
194
195 check [ $qux, $baz, $foo ], 'mods folding 8', <<'RES';
196 \draw [line width=4.0pt] qux ;
197 \draw [color=red] baz ;
198 \begin{scope} [color=red,line width=4.0pt]
199 \draw foo ;
200 \draw bar ;
201 \end{scope}
202 RES
203
204 my $seq = eval {
205  Tikz->seq($foo, $qux, $baz)
206      ->mod($red);
207 };
208 is $@, '', 'creating a modded sequence set doesn\'t croak';
209
210 check $seq, 'mod covering 1', <<'RES';
211 \begin{scope} [color=red]
212 \begin{scope} [line width=4.0pt]
213 \draw foo ;
214 \draw bar ;
215 \draw qux ;
216 \end{scope}
217 \draw baz ;
218 \end{scope}
219 RES
220
221 my $seq2 = eval {
222  Tikz->seq($seq, $qux)
223      ->mod(Tikz->color('blue'));
224 };
225 is $@, '', 'creating another modded sequence set doesn\'t croak';
226
227 check $seq2, 'mod covering 2', <<'RES';
228 \begin{scope} [color=blue]
229 \begin{scope} [color=red]
230 \begin{scope} [line width=4.0pt]
231 \draw foo ;
232 \draw bar ;
233 \draw qux ;
234 \end{scope}
235 \draw baz ;
236 \end{scope}
237 \draw [line width=4.0pt] qux ;
238 \end{scope}
239 RES
240
241 eval {
242  $foo->mod(Tikz->raw_mod('raw1'));
243  $seq->mod(Tikz->raw_mod('raw2'));
244 };
245 is $@, '', 'creating and adding raw mods doesn\'t croak';
246
247 check $seq, 'mod covering 3', <<'RES';
248 \begin{scope} [color=red,raw2]
249 \begin{scope} [line width=4.0pt]
250 \begin{scope} [raw1]
251 \draw foo ;
252 \draw bar ;
253 \end{scope}
254 \draw qux ;
255 \end{scope}
256 \draw baz ;
257 \end{scope}
258 RES
259
260 eval {
261  $baz->mod(Tikz->raw_mod($_)) for qw/raw2 raw3/;
262 };
263 is $@, '', 'creating and adding another raw mod doesn\'t croak';
264
265 check $seq, 'mod covering 4', <<'RES';
266 \begin{scope} [color=red,raw2]
267 \begin{scope} [line width=4.0pt]
268 \begin{scope} [raw1]
269 \draw foo ;
270 \draw bar ;
271 \end{scope}
272 \draw qux ;
273 \end{scope}
274 \draw [raw3] baz ;
275 \end{scope}
276 RES
277
278 eval {
279  $bar->mod(Tikz->width(50));
280 };
281 is $@, '', 'creating and adding another width mod doesn\'t croak';
282
283 check $seq, 'mod covering 5', <<'RES';
284 \begin{scope} [color=red,raw2]
285 \begin{scope} [line width=4.0pt]
286 \begin{scope} [raw1]
287 \draw foo ;
288 \draw [line width=8.0pt] bar ;
289 \end{scope}
290 \draw qux ;
291 \end{scope}
292 \draw [raw3] baz ;
293 \end{scope}
294 RES
295
296 my ($fred, $fblue) = eval {
297  map Tikz->fill($_), qw/red blue/;
298 };
299 is $@, '', 'creating two fill mods doesn\'t croak';
300
301 $seq = eval {
302  Tikz->seq(
303   Tikz->raw("foo")
304       ->mod($fred)
305  )->mod($fred);
306 };
307 is $@, '', 'creating a structure with two identical fill mods doesn\'t croak';
308
309 check $seq, 'mod covering 6', <<'RES';
310 \draw [fill=red] foo ;
311 RES
312
313 $seq = eval {
314  Tikz->seq(
315   Tikz->raw("foo")
316       ->mod($fblue)
317  )->mod($fred);
318 };
319 is $@, '', 'creating a structure with two different fill mods doesn\'t croak';
320
321 check $seq, 'mod covering 7', <<'RES';
322 \draw [fill=red,fill=blue] foo ;
323 RES
324
325 $seq = eval {
326  Tikz->seq(
327   Tikz->raw("foo")
328       ->mod($red)
329  )->mod($fred);
330 };
331 is $@, '', 'creating a structure with color and fill mods doesn\'t croak';
332
333 check $seq, 'mod covering 8', <<'RES';
334 \draw [fill=red,color=red] foo ;
335 RES