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