]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - t/12-geo.t
3a7eef7e65a32dd702f49ac12b476d19915158f9
[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 => (11 + 2 * 5) + 2 * (10 + 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 # Polyline
64
65 my $w = Tikz->point(3, -4);
66
67 for my $closed (0, 1) {
68  my $polyline = $closed ? 'closed_polyline' : 'polyline';
69  my $cycle    = $closed ? '-- cycle '       : '';
70  my $desc     = $closed ? 'closed polyline' : 'polyline';
71
72  my $pl = eval {
73   Tikz->$polyline($o, $z);
74  };
75  is $@, '', "creating a $desc from two Tikz points doesn't croak";
76
77  check $pl, "a $desc from two Tikz points", <<"RES";
78 \\draw (0cm,0cm) -- (1cm,2cm) $cycle;
79 RES
80
81  $pl = eval {
82   Tikz->$polyline($o, $z, $w);
83  };
84  is $@, '', "creating a $desc from three Tikz points doesn't croak";
85
86  check $pl, "a $desc from three Tikz points", <<"RES";
87 \\draw (0cm,0cm) -- (1cm,2cm) -- (3cm,-4cm) $cycle;
88 RES
89
90  $pl = eval {
91   Tikz->$polyline(-1, (2-3*i), [-4, 5]);
92  };
93  is $@, '', "creating a $desc from three Tikz points doesn't croak";
94
95  check $pl, "a $desc from three Tikz points", <<"RES";
96 \\draw (-1cm,0cm) -- (2cm,-3cm) -- (-4cm,5cm) $cycle;
97 RES
98
99  $pl = eval {
100   Tikz->$polyline($o);
101  };
102  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";
103
104  $pl = eval {
105   Tikz->$polyline(qw/foo bar/);
106  };
107  like $@, failed_valid('LaTeX::TikZ::Point::Autocoerce'), "creating a $desc from two string croaks";
108 }
109
110 # Rectangle
111
112 my $r = eval {
113  Tikz->rectangle($o => $z);
114 };
115 is $@, '', 'creating a rectangle from two TikZ points doesn\'t croak';
116
117 check $r, 'a rectangle from two Tikz points', <<'RES';
118 \draw (0cm,0cm) rectangle (1cm,2cm) ;
119 RES
120
121 $r = eval {
122  Tikz->rectangle([-1,2] => 3-4*i);
123 };
124 is $@, '', 'creating a rectangle from constants doesn\'t croak';
125
126 check $r, 'a rectangle from two Tikz points', <<'RES';
127 \draw (-1cm,2cm) rectangle (3cm,-4cm) ;
128 RES
129
130 $r = eval {
131  Tikz->rectangle($z => -3);
132 };
133 is $@, '', 'creating a rectangle from a TikZ point and a constant doesn\'t croak';
134
135 check $r, 'a rectangle from a TikZ point and a constant', <<'RES';
136 \draw (1cm,2cm) rectangle (-3cm,0cm) ;
137 RES
138
139 $r = eval {
140  Tikz->rectangle($o => { width => 3, height => -4 });
141 };
142 is $@, '', 'creating a rectangle from a TikZ point and width/height doesn\'t croak';
143
144 check $r, 'a rectangle from a TikZ point and width/height', <<'RES';
145 \draw (0cm,0cm) rectangle (3cm,-4cm) ;
146 RES
147
148 $r = eval {
149  Tikz->rectangle((-1+2*i) => { width => 3, height => -4 });
150 };
151 is $@, '', 'creating a rectangle from a constant and width/height doesn\'t croak';
152
153 check $r, 'a rectangle from a constant and width/height', <<'RES';
154 \draw (-1cm,2cm) rectangle (2cm,-2cm) ;
155 RES
156
157 # Circle
158
159 my $c = eval {
160  Tikz->circle($z => 3);
161 };
162 is $@, '', 'creating a circle from a TikZ point and a constant doesn\'t croak';
163
164 check $c, 'a circle from a Tikz point and a constant', <<'RES';
165 \draw (1cm,2cm) circle (3cm) ;
166 RES
167
168 $c = eval {
169  Tikz->circle([-1,2] => 3);
170 };
171 is $@, '', 'creating a circle from an array ref and a constant doesn\'t croak';
172
173 check $c, 'a circle from an array ref and a constant', <<'RES';
174 \draw (-1cm,2cm) circle (3cm) ;
175 RES
176
177 $c = eval {
178  Tikz->circle((4-5*i) => 3);
179 };
180 is $@, '', 'creating a circle from a complex and a constant doesn\'t croak';
181
182 check $c, 'a circle from a complex and a constant', <<'RES';
183 \draw (4cm,-5cm) circle (3cm) ;
184 RES
185
186 eval {
187  Tikz->circle($o => -1);
188 };
189 like $@, qr/isn't a non-negative real number/,
190                               'creating a circle with a negative radius croaks';