]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - t/12-geo.t
Introduce LaTeX::TikZ::Set::Arrow
[perl/modules/LaTeX-TikZ.git] / t / 12-geo.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => (16 + 2 * 5) + 2 * (13 + 2 * 3);
7
8 use Math::Complex;
9
10 use LaTeX::TikZ;
11
12 my $tikz = Tikz->formatter(
13  format => '%d',
14 );
15
16 sub check {
17  my ($set, $desc, $exp) = @_;
18
19  local $Test::Builder::Level = $Test::Builder::Level + 1;
20
21  my ($head, $decl, $body) = eval {
22   $tikz->render(ref $set eq 'ARRAY' ? @$set : $set);
23  };
24  is $@, '', "$desc: no error";
25
26  unless (ref $exp eq 'ARRAY') {
27   $exp = [ split /\n/, $exp ];
28  }
29  unshift @$exp, '\begin{tikzpicture}';
30  push    @$exp, '\end{tikzpicture}';
31
32  is_deeply $body, $exp, $desc;
33 }
34
35 sub failed_valid {
36  my ($tc) = @_;
37  qr/Validation failed for '\Q$tc\E'/;
38 }
39
40 my $o = Tikz->point(0);
41 my $z = Tikz->point(1+2*i);
42
43 # Line
44
45 my $l = eval {
46  Tikz->line($o => $z);
47 };
48 is $@, '', 'creating a line from two TikZ points doesn\'t croak';
49
50 check $l, 'a line from two Tikz points', <<'RES';
51 \draw (0cm,0cm) -- (1cm,2cm) ;
52 RES
53
54 $l = eval {
55  Tikz->line([-1,2] => 3-4*i);
56 };
57 is $@, '', 'creating a line from constants doesn\'t croak';
58
59 check $l, 'a line from two Tikz points', <<'RES';
60 \draw (-1cm,2cm) -- (3cm,-4cm) ;
61 RES
62
63 # Arrow
64
65 my $ar = eval {
66  Tikz->arrow($o, 1);
67 };
68 is $@, '', 'creating an arrow from two points doesn\'t croak';
69
70 check $ar, 'an arrow from two points', <<'RES';
71 \draw [->] (0cm,0cm) -- (1cm,0cm) ;
72 RES
73
74 $ar = eval {
75  Tikz->arrow(2, dir => -i());
76 };
77 is $@, '', 'creating an arrow from a point and a direction doesn\'t croak';
78
79 check $ar, 'an arrow from a point and a direction', <<'RES';
80 \draw [->] (2cm,0cm) -- (2cm,-1cm) ;
81 RES
82
83 # Polyline
84
85 my $w = Tikz->point(3, -4);
86
87 for my $closed (0, 1) {
88  my $polyline = $closed ? 'closed_polyline' : 'polyline';
89  my $cycle    = $closed ? '-- cycle '       : '';
90  my $desc     = $closed ? 'closed polyline' : 'polyline';
91
92  my $pl = eval {
93   Tikz->$polyline($o, $z);
94  };
95  is $@, '', "creating a $desc from two Tikz points doesn't croak";
96
97  check $pl, "a $desc from two Tikz points", <<"RES";
98 \\draw (0cm,0cm) -- (1cm,2cm) $cycle;
99 RES
100
101  $pl = eval {
102   Tikz->$polyline($o, $z, $w);
103  };
104  is $@, '', "creating a $desc from three Tikz points doesn't croak";
105
106  check $pl, "a $desc from three Tikz points", <<"RES";
107 \\draw (0cm,0cm) -- (1cm,2cm) -- (3cm,-4cm) $cycle;
108 RES
109
110  $pl = eval {
111   Tikz->$polyline(-1, (2-3*i), [-4, 5]);
112  };
113  is $@, '', "creating a $desc from three Tikz points doesn't croak";
114
115  check $pl, "a $desc from three Tikz points", <<"RES";
116 \\draw (-1cm,0cm) -- (2cm,-3cm) -- (-4cm,5cm) $cycle;
117 RES
118
119  $pl = eval {
120   Tikz->$polyline($o);
121  };
122  like $@, qr/at least two LaTeX::TikZ::Set::Point objects are needed in order to build a polyline/, "creating a $desc from only one Tikz point croaks";
123
124  $pl = eval {
125   Tikz->$polyline(qw/foo bar/);
126  };
127  like $@, failed_valid('LaTeX::TikZ::Point::Autocoerce'), "creating a $desc from two string croaks";
128 }
129
130 # Rectangle
131
132 my $r = eval {
133  Tikz->rectangle($o => $z);
134 };
135 is $@, '', 'creating a rectangle from two TikZ points doesn\'t croak';
136
137 check $r, 'a rectangle from two Tikz points', <<'RES';
138 \draw (0cm,0cm) rectangle (1cm,2cm) ;
139 RES
140
141 $r = eval {
142  Tikz->rectangle([-1,2] => 3-4*i);
143 };
144 is $@, '', 'creating a rectangle from constants doesn\'t croak';
145
146 check $r, 'a rectangle from two Tikz points', <<'RES';
147 \draw (-1cm,2cm) rectangle (3cm,-4cm) ;
148 RES
149
150 $r = eval {
151  Tikz->rectangle($z => -3);
152 };
153 is $@, '', 'creating a rectangle from a TikZ point and a constant doesn\'t croak';
154
155 check $r, 'a rectangle from a TikZ point and a constant', <<'RES';
156 \draw (1cm,2cm) rectangle (-3cm,0cm) ;
157 RES
158
159 $r = eval {
160  Tikz->rectangle($o => { width => 3, height => -4 });
161 };
162 is $@, '', 'creating a rectangle from a TikZ point and width/height doesn\'t croak';
163
164 check $r, 'a rectangle from a TikZ point and width/height', <<'RES';
165 \draw (0cm,0cm) rectangle (3cm,-4cm) ;
166 RES
167
168 $r = eval {
169  Tikz->rectangle((-1+2*i) => { width => 3, height => -4 });
170 };
171 is $@, '', 'creating a rectangle from a constant and width/height doesn\'t croak';
172
173 check $r, 'a rectangle from a constant and width/height', <<'RES';
174 \draw (-1cm,2cm) rectangle (2cm,-2cm) ;
175 RES
176
177 # Circle
178
179 my $c = eval {
180  Tikz->circle($z => 3);
181 };
182 is $@, '', 'creating a circle from a TikZ point and a constant doesn\'t croak';
183
184 check $c, 'a circle from a Tikz point and a constant', <<'RES';
185 \draw (1cm,2cm) circle (3cm) ;
186 RES
187
188 $c = eval {
189  Tikz->circle([-1,2] => 3);
190 };
191 is $@, '', 'creating a circle from an array ref and a constant doesn\'t croak';
192
193 check $c, 'a circle from an array ref and a constant', <<'RES';
194 \draw (-1cm,2cm) circle (3cm) ;
195 RES
196
197 $c = eval {
198  Tikz->circle((4-5*i) => 3);
199 };
200 is $@, '', 'creating a circle from a complex and a constant doesn\'t croak';
201
202 check $c, 'a circle from a complex and a constant', <<'RES';
203 \draw (4cm,-5cm) circle (3cm) ;
204 RES
205
206 eval {
207  Tikz->circle($o => -1);
208 };
209 like $@, qr/isn't a non-negative real number/,
210                               'creating a circle with a negative radius croaks';
211
212 # Arc
213
214 $tikz = Tikz->formatter(
215  format => "%.03f"
216 );
217
218 my $arc = eval {
219  Tikz->arc(1, i, $o);
220 };
221 is $@, '', 'creating a simple arc doesn\'t croak';
222
223 check $arc, 'simple arc', <<'RES';
224 \begin{scope}
225 \clip (0.969cm,0.000cm) -- (1.085cm,0.000cm) -- (1.032cm,0.335cm) -- (0.878cm,0.638cm) -- (0.638cm,0.878cm) -- (0.335cm,1.032cm) -- (0.000cm,1.085cm) -- (0.000cm,0.969cm) -- cycle ;
226 \draw (0.000cm,0.000cm) circle (1.000cm) ;
227 \end{scope}
228 RES
229
230 eval {
231  Tikz->arc(0, 1);
232 };
233 my $err = quotemeta 'Tikz->arc($first_point, $second_point, $center)';
234 like $@, qr/^$err/, 'creating an arc from only two points croaks';
235
236 eval {
237  Tikz->arc(0, 1, i);
238 };
239 like $@, qr/The two first points aren't on a circle of center the last/,
240          'creating an arc with two points not on a circle of center c croaks';