]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - t/12-geo.t
ab56eaac3049175cb7777e7c4c529a33fe2ff3e8
[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 * 10;
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 my $o = Tikz->point(0);
36 my $z = Tikz->point(1+2*i);
37
38 # Line
39
40 my $l = eval {
41  Tikz->line($o => $z);
42 };
43 is $@, '', 'creating a line from two TikZ points doesn\'t croak';
44
45 check $l, 'a line from two Tikz points', <<'RES';
46 \draw (0cm,0cm) -- (1cm,2cm) ;
47 RES
48
49 $l = eval {
50  Tikz->line([-1,2] => 3-4*i);
51 };
52 is $@, '', 'creating a line from constants doesn\'t croak';
53
54 check $l, 'a line from two Tikz points', <<'RES';
55 \draw (-1cm,2cm) -- (3cm,-4cm) ;
56 RES
57
58 # Rectangle
59
60 my $r = eval {
61  Tikz->rectangle($o => $z);
62 };
63 is $@, '', 'creating a rectangle from two TikZ points doesn\'t croak';
64
65 check $r, 'a rectangle from two Tikz points', <<'RES';
66 \draw (0cm,0cm) rectangle (1cm,2cm) ;
67 RES
68
69 $r = eval {
70  Tikz->rectangle([-1,2] => 3-4*i);
71 };
72 is $@, '', 'creating a rectangle from constants doesn\'t croak';
73
74 check $r, 'a rectangle from two Tikz points', <<'RES';
75 \draw (-1cm,2cm) rectangle (3cm,-4cm) ;
76 RES
77
78 $r = eval {
79  Tikz->rectangle($z => -3);
80 };
81 is $@, '', 'creating a rectangle from a TikZ point and a constant doesn\'t croak';
82
83 check $r, 'a rectangle from a TikZ point and a constant', <<'RES';
84 \draw (1cm,2cm) rectangle (-3cm,0cm) ;
85 RES
86
87 $r = eval {
88  Tikz->rectangle($o => { width => 3, height => -4 });
89 };
90 is $@, '', 'creating a rectangle from a TikZ point and width/height doesn\'t croak';
91
92 check $r, 'a rectangle from a TikZ point and width/height', <<'RES';
93 \draw (0cm,0cm) rectangle (3cm,-4cm) ;
94 RES
95
96 $r = eval {
97  Tikz->rectangle((-1+2*i) => { width => 3, height => -4 });
98 };
99 is $@, '', 'creating a rectangle from a constant and width/height doesn\'t croak';
100
101 check $r, 'a rectangle from a constant and width/height', <<'RES';
102 \draw (-1cm,2cm) rectangle (2cm,-2cm) ;
103 RES
104
105 # Circle
106
107 my $c = eval {
108  Tikz->circle($z => 3);
109 };
110 is $@, '', 'creating a circle from a TikZ point and a constant doesn\'t croak';
111
112 check $c, 'a circle from a Tikz point and a constant', <<'RES';
113 \draw (1cm,2cm) circle (3cm) ;
114 RES
115
116 $c = eval {
117  Tikz->circle([-1,2] => 3);
118 };
119 is $@, '', 'creating a circle from an array ref and a constant doesn\'t croak';
120
121 check $c, 'a circle from an array ref and a constant', <<'RES';
122 \draw (-1cm,2cm) circle (3cm) ;
123 RES
124
125 $c = eval {
126  Tikz->circle((4-5*i) => 3);
127 };
128 is $@, '', 'creating a circle from a complex and a constant doesn\'t croak';
129
130 check $c, 'a circle from a complex and a constant', <<'RES';
131 \draw (4cm,-5cm) circle (3cm) ;
132 RES
133
134 eval {
135  Tikz->circle($o => -1);
136 };
137 like $@, qr/isn't a non-negative real number/,
138                               'creating a circle with a negative radius croaks';