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