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