]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - t/20-mod.t
256cbdd58881121400e18118690e1db9d0f26ffb
[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 * 20;
7
8 use LaTeX::TikZ;
9 use LaTeX::TikZ::Formatter;
10
11 my $tikz = LaTeX::TikZ::Formatter->new(
12  format => '%d',
13 );
14
15 sub check {
16  my ($set, $desc, $exp) = @_;
17
18  local $Test::Builder::Level = $Test::Builder::Level + 1;
19
20  my ($head, $decl, $body) = eval {
21   $tikz->render(ref $set eq 'ARRAY' ? @$set : $set);
22  };
23  is $@, '', "$desc: no error";
24
25  unless (ref $exp eq 'ARRAY') {
26   $exp = [ split /\n/, $exp ];
27  }
28  unshift @$exp, '\begin{tikzpicture}';
29  push    @$exp, '\end{tikzpicture}';
30
31  is_deeply $body, $exp, $desc;
32 }
33
34 my $red = eval {
35  Tikz->color('red');
36 };
37 is $@, '', 'creating a color mod doesn\'t croak';
38
39 my $foo = eval {
40  Tikz->raw('foo')
41      ->mod($red)
42 };
43 is $@, '', 'creating a modded raw set doesn\'t croak';
44
45 check $foo, 'one modded raw set', <<'RES';
46 \draw [color=red] foo ;
47 RES
48
49 my $width = eval {
50  Tikz->width(25);
51 };
52 is $@, '', 'creating a width mod doesn\'t croak';
53
54 eval {
55  $foo->mod($width);
56 };
57 is $@, '', 'adding another mod doesn\'t croak';
58
59 check $foo, 'one double modded raw set', <<'RES';
60 \draw [color=red,line width=4.0pt] foo ;
61 RES
62
63 eval {
64  $foo->mod($red);
65 };
66 is $@, '', 're-adding an previously set mod doesn\'t croak';
67
68 check $foo, 'one triple modded raw set (with duplicates)', <<'RES';
69 \draw [color=red,line width=4.0pt] foo ;
70 RES
71
72 my $bar = Tikz->raw('bar');
73 eval {
74  $foo->add($bar);
75 };
76 is $@, '', 'appending to a modded set doesn\'t croak';
77
78 check $foo, 'one triple modded sequence of raw sets (with duplicates)', <<'RES';
79 \begin{scope} [color=red,line width=4.0pt]
80 \draw foo ;
81 \draw bar ;
82 \end{scope}
83 RES
84
85 my $set = Tikz->raw('wut');
86
87 my $set_mod = eval {
88  $set->mod;
89 };
90 is $@,         '',     'calling empty mod out of a set doesn\'t croak';
91 is "$set_mod", "$set", 'calling empty mod out of a set returns the set';
92
93 my $new = eval {
94  $set->mod(Tikz->raw_mod('raw1'));
95 };
96 is $@, '',
97     'creating and applying a raw mod on a set in scalar context doesn\'t croak';
98 is ref($new), 'LaTeX::TikZ::Set::Mod', 'new set is of the right kind';
99 isnt  "$new", "$set", 'new set is different from the old one';
100
101 check $set, '', <<'RES';
102 \draw wut ;
103 RES
104
105 check $new, '', <<'RES';
106 \draw [raw1] wut ;
107 RES
108
109 eval {
110  $set->mod(Tikz->raw_mod('raw2'));
111  ();
112 };
113 is $@, '',
114       'creating and applying a raw mod on a set in void context doesn\'t croak';
115 is ref($new), 'LaTeX::TikZ::Set::Mod', 'new set is of the right kind';
116
117 check $set, '', <<'RES';
118 \draw [raw2] wut ;
119 RES
120
121 check $new, '', <<'RES';
122 \draw [raw1] wut ;
123 RES
124
125 my $baz = eval {
126  Tikz->raw('baz')
127      ->mod($red);
128 };
129 is $@, '', 'creating another colored raw set doesn\'t croak';
130
131 check [ $foo, $baz ], 'mods folding 1', <<'RES';
132 \begin{scope} [color=red]
133 \begin{scope} [line width=4.0pt]
134 \draw foo ;
135 \draw bar ;
136 \end{scope}
137 \draw baz ;
138 \end{scope}
139 RES
140
141 check [ $baz, $foo ], 'mods folding 2', <<'RES';
142 \begin{scope} [color=red]
143 \draw baz ;
144 \begin{scope} [line width=4.0pt]
145 \draw foo ;
146 \draw bar ;
147 \end{scope}
148 \end{scope}
149 RES
150
151 my $qux = eval {
152  Tikz->raw('qux')
153      ->mod($width);
154 };
155 is $@, '', 'creating another raw set with modded width doesn\'t croak';
156
157 check [ $foo, $baz, $qux ], 'mods folding 3', <<'RES';
158 \begin{scope} [color=red]
159 \begin{scope} [line width=4.0pt]
160 \draw foo ;
161 \draw bar ;
162 \end{scope}
163 \draw baz ;
164 \end{scope}
165 \draw [line width=4.0pt] qux ;
166 RES
167
168 check [ $foo, $qux, $baz ], 'mods folding 4', <<'RES';
169 \begin{scope} [line width=4.0pt]
170 \begin{scope} [color=red]
171 \draw foo ;
172 \draw bar ;
173 \end{scope}
174 \draw qux ;
175 \end{scope}
176 \draw [color=red] baz ;
177 RES
178
179 check [ $baz, $foo, $qux ], 'mods folding 5', <<'RES';
180 \begin{scope} [color=red]
181 \draw baz ;
182 \begin{scope} [line width=4.0pt]
183 \draw foo ;
184 \draw bar ;
185 \end{scope}
186 \end{scope}
187 \draw [line width=4.0pt] qux ;
188 RES
189
190 check [ $baz, $qux, $foo ], 'mods folding 6', <<'RES';
191 \draw [color=red] baz ;
192 \draw [line width=4.0pt] qux ;
193 \begin{scope} [color=red,line width=4.0pt]
194 \draw foo ;
195 \draw bar ;
196 \end{scope}
197 RES
198
199 check [ $qux, $foo, $baz ], 'mods folding 7', <<'RES';
200 \begin{scope} [line width=4.0pt]
201 \draw qux ;
202 \begin{scope} [color=red]
203 \draw foo ;
204 \draw bar ;
205 \end{scope}
206 \end{scope}
207 \draw [color=red] baz ;
208 RES
209
210 check [ $qux, $baz, $foo ], 'mods folding 8', <<'RES';
211 \draw [line width=4.0pt] qux ;
212 \draw [color=red] baz ;
213 \begin{scope} [color=red,line width=4.0pt]
214 \draw foo ;
215 \draw bar ;
216 \end{scope}
217 RES
218
219 my $seq = eval {
220  Tikz->seq($foo, $qux, $baz)
221      ->mod($red);
222 };
223 is $@, '', 'creating a modded sequence set doesn\'t croak';
224
225 check $seq, 'mod covering 1', <<'RES';
226 \begin{scope} [color=red]
227 \begin{scope} [line width=4.0pt]
228 \draw foo ;
229 \draw bar ;
230 \draw qux ;
231 \end{scope}
232 \draw baz ;
233 \end{scope}
234 RES
235
236 my $seq2 = eval {
237  Tikz->seq($seq, $qux)
238      ->mod(Tikz->color('blue'));
239 };
240 is $@, '', 'creating another modded sequence set doesn\'t croak';
241
242 check $seq2, 'mod covering 2', <<'RES';
243 \begin{scope} [color=blue]
244 \begin{scope} [color=red]
245 \begin{scope} [line width=4.0pt]
246 \draw foo ;
247 \draw bar ;
248 \draw qux ;
249 \end{scope}
250 \draw baz ;
251 \end{scope}
252 \draw [line width=4.0pt] qux ;
253 \end{scope}
254 RES
255
256 eval {
257  $foo->mod(Tikz->raw_mod('raw1'));
258  $seq->mod(Tikz->raw_mod('raw2'));
259 };
260 is $@, '', 'creating and adding raw mods doesn\'t croak';
261
262 check $seq, 'mod covering 3', <<'RES';
263 \begin{scope} [color=red,raw2]
264 \begin{scope} [line width=4.0pt]
265 \begin{scope} [raw1]
266 \draw foo ;
267 \draw bar ;
268 \end{scope}
269 \draw qux ;
270 \end{scope}
271 \draw baz ;
272 \end{scope}
273 RES
274
275 eval {
276  $baz->mod(Tikz->raw_mod($_)) for qw/raw2 raw3/;
277 };
278 is $@, '', 'creating and adding another raw mod doesn\'t croak';
279
280 check $seq, 'mod covering 4', <<'RES';
281 \begin{scope} [color=red,raw2]
282 \begin{scope} [line width=4.0pt]
283 \begin{scope} [raw1]
284 \draw foo ;
285 \draw bar ;
286 \end{scope}
287 \draw qux ;
288 \end{scope}
289 \draw [raw3] baz ;
290 \end{scope}
291 RES