]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - t/20-mod.t
Test mod foldings
[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 => 8 + 2 * 12;
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 $baz = eval {
86  Tikz->raw('baz')
87      ->mod($red);
88 };
89 is $@, '', 'creating another colored raw set doesn\'t croak';
90
91 check [ $foo, $baz ], 'mods folding 1', <<'RES';
92 \begin{scope} [color=red]
93 \begin{scope} [line width=4.0pt]
94 \draw foo ;
95 \draw bar ;
96 \end{scope}
97 \draw baz ;
98 \end{scope}
99 RES
100
101 check [ $baz, $foo ], 'mods folding 2', <<'RES';
102 \begin{scope} [color=red]
103 \draw baz ;
104 \begin{scope} [line width=4.0pt]
105 \draw foo ;
106 \draw bar ;
107 \end{scope}
108 \end{scope}
109 RES
110
111 my $qux = eval {
112  Tikz->raw('qux')
113      ->mod($width);
114 };
115 is $@, '', 'creating another raw set with modded width doesn\'t croak';
116
117 check [ $foo, $baz, $qux ], 'mods folding 3', <<'RES';
118 \begin{scope} [color=red]
119 \begin{scope} [line width=4.0pt]
120 \draw foo ;
121 \draw bar ;
122 \end{scope}
123 \draw baz ;
124 \end{scope}
125 \draw [line width=4.0pt] qux ;
126 RES
127
128 check [ $foo, $qux, $baz ], 'mods folding 4', <<'RES';
129 \begin{scope} [line width=4.0pt]
130 \begin{scope} [color=red]
131 \draw foo ;
132 \draw bar ;
133 \end{scope}
134 \draw qux ;
135 \end{scope}
136 \draw [color=red] baz ;
137 RES
138
139 check [ $baz, $foo, $qux ], 'mods folding 5', <<'RES';
140 \begin{scope} [color=red]
141 \draw baz ;
142 \begin{scope} [line width=4.0pt]
143 \draw foo ;
144 \draw bar ;
145 \end{scope}
146 \end{scope}
147 \draw [line width=4.0pt] qux ;
148 RES
149
150 check [ $baz, $qux, $foo ], 'mods folding 6', <<'RES';
151 \draw [color=red] baz ;
152 \draw [line width=4.0pt] qux ;
153 \begin{scope} [color=red,line width=4.0pt]
154 \draw foo ;
155 \draw bar ;
156 \end{scope}
157 RES
158
159 check [ $qux, $foo, $baz ], 'mods folding 7', <<'RES';
160 \begin{scope} [line width=4.0pt]
161 \draw qux ;
162 \begin{scope} [color=red]
163 \draw foo ;
164 \draw bar ;
165 \end{scope}
166 \end{scope}
167 \draw [color=red] baz ;
168 RES
169
170 check [ $qux, $baz, $foo ], 'mods folding 8', <<'RES';
171 \draw [line width=4.0pt] qux ;
172 \draw [color=red] baz ;
173 \begin{scope} [color=red,line width=4.0pt]
174 \draw foo ;
175 \draw bar ;
176 \end{scope}
177 RES