]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - t/20-mod.t
Tested flattening of nested modified sets
[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 => 14 + 2 * 18;
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 $foo = eval {
74  Tikz->seq(
75   Tikz->raw('foo'),
76   $bar
77  )->mod($red, $width);
78 };
79 is $@, '', 'setting two mods in a row doesn\'t croak';
80
81 check $foo, 'one triple modded sequence of raw sets', <<'RES';
82 \begin{scope} [color=red,line width=4.0pt]
83 \draw foo ;
84 \draw bar ;
85 \end{scope}
86 RES
87
88 my $nested = eval {
89  Tikz->seq(
90   Tikz->seq(Tikz->raw("foo"))
91       ->mod($red)
92  )->mod($width)
93 };
94 is $@, '', 'creating nested modded sequences doesn\'t croak';
95
96 check $nested, 'nested modded sequences', <<'RES';
97 \draw [line width=4.0pt,color=red] foo ;
98 RES
99
100 my $baz = eval {
101  Tikz->raw('baz')
102      ->mod($red);
103 };
104 is $@, '', 'creating another colored raw set doesn\'t croak';
105
106 check [ $foo, $baz ], 'mods folding 1', <<'RES';
107 \begin{scope} [color=red]
108 \begin{scope} [line width=4.0pt]
109 \draw foo ;
110 \draw bar ;
111 \end{scope}
112 \draw baz ;
113 \end{scope}
114 RES
115
116 check [ $baz, $foo ], 'mods folding 2', <<'RES';
117 \begin{scope} [color=red]
118 \draw baz ;
119 \begin{scope} [line width=4.0pt]
120 \draw foo ;
121 \draw bar ;
122 \end{scope}
123 \end{scope}
124 RES
125
126 my $qux = eval {
127  Tikz->raw('qux')
128      ->mod($width);
129 };
130 is $@, '', 'creating another raw set with modded width doesn\'t croak';
131
132 check [ $foo, $baz, $qux ], 'mods folding 3', <<'RES';
133 \begin{scope} [color=red]
134 \begin{scope} [line width=4.0pt]
135 \draw foo ;
136 \draw bar ;
137 \end{scope}
138 \draw baz ;
139 \end{scope}
140 \draw [line width=4.0pt] qux ;
141 RES
142
143 check [ $foo, $qux, $baz ], 'mods folding 4', <<'RES';
144 \begin{scope} [line width=4.0pt]
145 \begin{scope} [color=red]
146 \draw foo ;
147 \draw bar ;
148 \end{scope}
149 \draw qux ;
150 \end{scope}
151 \draw [color=red] baz ;
152 RES
153
154 check [ $baz, $foo, $qux ], 'mods folding 5', <<'RES';
155 \begin{scope} [color=red]
156 \draw baz ;
157 \begin{scope} [line width=4.0pt]
158 \draw foo ;
159 \draw bar ;
160 \end{scope}
161 \end{scope}
162 \draw [line width=4.0pt] qux ;
163 RES
164
165 check [ $baz, $qux, $foo ], 'mods folding 6', <<'RES';
166 \draw [color=red] baz ;
167 \draw [line width=4.0pt] qux ;
168 \begin{scope} [color=red,line width=4.0pt]
169 \draw foo ;
170 \draw bar ;
171 \end{scope}
172 RES
173
174 check [ $qux, $foo, $baz ], 'mods folding 7', <<'RES';
175 \begin{scope} [line width=4.0pt]
176 \draw qux ;
177 \begin{scope} [color=red]
178 \draw foo ;
179 \draw bar ;
180 \end{scope}
181 \end{scope}
182 \draw [color=red] baz ;
183 RES
184
185 check [ $qux, $baz, $foo ], 'mods folding 8', <<'RES';
186 \draw [line width=4.0pt] qux ;
187 \draw [color=red] baz ;
188 \begin{scope} [color=red,line width=4.0pt]
189 \draw foo ;
190 \draw bar ;
191 \end{scope}
192 RES
193
194 my $seq = eval {
195  Tikz->seq($foo, $qux, $baz)
196      ->mod($red);
197 };
198 is $@, '', 'creating a modded sequence set doesn\'t croak';
199
200 check $seq, 'mod covering 1', <<'RES';
201 \begin{scope} [color=red]
202 \begin{scope} [line width=4.0pt]
203 \draw foo ;
204 \draw bar ;
205 \draw qux ;
206 \end{scope}
207 \draw baz ;
208 \end{scope}
209 RES
210
211 my $seq2 = eval {
212  Tikz->seq($seq, $qux)
213      ->mod(Tikz->color('blue'));
214 };
215 is $@, '', 'creating another modded sequence set doesn\'t croak';
216
217 check $seq2, 'mod covering 2', <<'RES';
218 \begin{scope} [color=blue]
219 \begin{scope} [color=red]
220 \begin{scope} [line width=4.0pt]
221 \draw foo ;
222 \draw bar ;
223 \draw qux ;
224 \end{scope}
225 \draw baz ;
226 \end{scope}
227 \draw [line width=4.0pt] qux ;
228 \end{scope}
229 RES
230
231 eval {
232  $foo->mod(Tikz->raw_mod('raw1'));
233  $seq->mod(Tikz->raw_mod('raw2'));
234 };
235 is $@, '', 'creating and adding raw mods doesn\'t croak';
236
237 check $seq, 'mod covering 3', <<'RES';
238 \begin{scope} [color=red,raw2]
239 \begin{scope} [line width=4.0pt]
240 \begin{scope} [raw1]
241 \draw foo ;
242 \draw bar ;
243 \end{scope}
244 \draw qux ;
245 \end{scope}
246 \draw baz ;
247 \end{scope}
248 RES
249
250 eval {
251  $baz->mod(Tikz->raw_mod($_)) for qw/raw2 raw3/;
252 };
253 is $@, '', 'creating and adding another raw mod doesn\'t croak';
254
255 check $seq, 'mod covering 4', <<'RES';
256 \begin{scope} [color=red,raw2]
257 \begin{scope} [line width=4.0pt]
258 \begin{scope} [raw1]
259 \draw foo ;
260 \draw bar ;
261 \end{scope}
262 \draw qux ;
263 \end{scope}
264 \draw [raw3] baz ;
265 \end{scope}
266 RES
267
268 eval {
269  $bar->mod(Tikz->width(50));
270 };
271 is $@, '', 'creating and adding another width mod doesn\'t croak';
272
273 check $seq, 'mod covering 4', <<'RES';
274 \begin{scope} [color=red,raw2]
275 \begin{scope} [line width=4.0pt]
276 \begin{scope} [raw1]
277 \draw foo ;
278 \draw [line width=8.0pt] bar ;
279 \end{scope}
280 \draw qux ;
281 \end{scope}
282 \draw [raw3] baz ;
283 \end{scope}
284 RES